import test from "node:test"; import assert from "node:assert/strict"; import { newTask, readTask, editTask, adoptNote } from "../../../../src/lib/features/pm/storage/notes.ts"; import { DescriptionDraft } from "../../../../src/lib/features/pm/storage/description-draft.ts"; import { defaultBoardPath } from "../../../../src/lib/features/pm/model/workspace-default.ts"; import { defaultBoard } from "../../../../src/lib/features/pm/storage/board.ts"; import { catalogPath, boardMarkdown } from "../../../../src/lib/features/pm/storage/catalog.ts"; import type { Workspace } from "../../../../src/lib/features/pm/model/model.ts"; function fixture() { const files = new Map(); const workspace: Workspace = { async list() { throw new Error("No task scan expected"); }, async read(path) { if (!files.has(path)) throw new Error("Missing"); return files.get(path)!; }, async readOptional(path) { return files.get(path) ?? null; }, async create(path, source) { if (files.has(path)) throw new Error("Exists"); files.set(path, source); }, async write(path, source, expected) { if (files.get(path) !== expected) throw new Error("Conflict"); files.set(path, source); }, }; return { files, workspace }; } const fields = { status: "todo" as const, tags: [], sessions: [], archived: false }; test("new captures record immutable timestamps and existing legacy cards keep an unknown date", () => { const before = Date.now(); const note = newTask("Timestamp", fields); const task = readTask(note.path, note.source)!; assert.ok(Date.parse(task.createdAt!) >= before && Date.parse(task.createdAt!) <= Date.now()); const edited = readTask(task.path, editTask(task, { body: "Updated", status: "done", placements: { next: { listId: "done" } } }))!; assert.equal(edited.createdAt, task.createdAt); assert.throws(() => editTask(task, { createdAt: undefined }), /cannot change/); assert.throws(() => editTask(task, { createdAt: "2026-01-01T00:00:00Z" }), /cannot change/); assert.ok(Date.parse(readTask("Old.md", adoptNote("Old.md", "Original note"))!.createdAt!) >= before); const legacy = readTask(note.path, note.source.replace(/^ createdAt:.*\n/m, ""))!; assert.equal(readTask(legacy.path, editTask(legacy, { title: "Legacy edit" }))!.createdAt, undefined); assert.equal(adoptNote(legacy.path, legacy.source), legacy.source); }); test("description drafts persist separately, cancel clears them, and stale writers cannot overwrite drafts", async () => { const { files, workspace } = fixture(); const draft = new DescriptionDraft(workspace, "task"); assert.equal(await draft.load(), null); await draft.save("Uncommitted text"); const other = new DescriptionDraft(workspace, "task"); assert.equal(await other.load(), "Uncommitted text"); await draft.save("Newer draft"); await assert.rejects(other.save("Stale draft"), /Conflict/); await draft.save(null); assert.equal(await new DescriptionDraft(workspace, "task").load(), null); assert.ok([...files.keys()].every(path => !path.includes("/cards/"))); }); test("default board resolves without scanning cards and keeps unavailable selections fixed", async () => { const { files, workspace } = fixture(); const board = defaultBoard("chosen"); const catalog = { schema: 1, boards: [{ name: "Chosen", value: board }], defaultBoardId: "chosen", unknown: "keep" }; files.set(catalogPath, JSON.stringify(catalog)); assert.equal(await defaultBoardPath(workspace), "pm-board:chosen"); files.set(catalogPath, JSON.stringify({ ...catalog, boards: [] })); assert.equal(await defaultBoardPath(workspace), undefined); assert.equal(JSON.parse(files.get(catalogPath)!).defaultBoardId, "chosen"); files.set("Legacy.md", boardMarkdown(board)); files.set(catalogPath, JSON.stringify({ ...catalog, boards: [], defaultBoardPath: "Legacy.md" })); assert.equal(await defaultBoardPath(workspace), "Legacy.md"); files.set(catalogPath, JSON.stringify({ ...catalog, boards: [], defaultBoardPath: "Legacy.md", deletedBoardIds: ["chosen"] })); assert.equal(await defaultBoardPath(workspace), undefined); });