import assert from "node:assert/strict"; import { randomUUID } from "node:crypto"; import { rm } from "node:fs/promises"; import { join } from "node:path"; import test from "node:test"; import { assertCanonicalRunDirectory, assertRunFile, createRun, createRunId, pathsForRun, requestIndexDirectory, runsRoot, } from "./run-paths.mjs"; test("run IDs and paths always resolve below the project run root", () => { const runId = createRunId(new Date("2026-07-23T12:34:56.000Z"), "abcdef"); assert.equal(runId, "run-20260723203456-abcdef"); const paths = pathsForRun(runId); assert.equal(paths.run_directory, join(runsRoot, runId)); assert.equal(paths.runs_root, join(paths.project_root, "生成结果", "电商七图")); assert.equal(paths.attachments_directory, join(runsRoot, runId, "商品附件")); assert.equal(paths.images_directory, join(runsRoot, runId, "成品图")); }); test("run IDs use Asia/Shanghai calendar dates across the UTC midnight boundary", () => { const runId = createRunId(new Date("2026-07-23T16:55:04.000Z"), "123abc"); assert.equal(runId, "run-20260724005504-123abc"); }); test("run path checks reject skill-source and nested output directories", () => { assert.throws( () => assertCanonicalRunDirectory(join(runsRoot, "run-ok", "nested")), /RUN_PATH_INVALID/, ); const runDirectory = join(runsRoot, "run-20260723123456-abcdef"); assert.throws( () => assertRunFile(join(runDirectory, "nested", "jimeng-result.json"), runDirectory), /RUN_PATH_INVALID/, ); }); test("Windows run path checks ignore drive-letter casing", { skip: process.platform !== "win32" }, () => { const runDirectory = join(runsRoot, "run-20260723123456-abcdef"); const lowerCaseDrive = `${runDirectory[0].toLowerCase()}${runDirectory.slice(1)}`; assert.equal(assertCanonicalRunDirectory(lowerCaseDrive).toLowerCase(), runDirectory.toLowerCase()); assert.equal( assertRunFile(join(lowerCaseDrive, "vision-result.json"), lowerCaseDrive, "vision-result.json").toLowerCase(), join(runDirectory, "vision-result.json").toLowerCase(), ); }); test("the same Claude request key always reuses one run directory", async () => { const requestKey = `test-request-${randomUUID()}`; const indexDirectory = requestIndexDirectory(requestKey); let first; try { first = await createRun({ requestKey }); const second = await createRun({ requestKey }); assert.equal(first.reused_existing_run, false); assert.equal(second.reused_existing_run, true); assert.equal(second.run_id, first.run_id); assert.equal(second.run_directory, first.run_directory); } finally { if (first) await rm(first.run_directory, { recursive: true, force: true }); await rm(indexDirectory, { recursive: true, force: true }); } });