import test from "node:test"; import assert from "node:assert/strict"; import { newBoardSource, parseBoard } from "../../../../src/lib/features/pm/storage/board.ts"; import { adoptNote, editTask, newTask, readTask } from "../../../../src/lib/features/pm/storage/notes.ts"; import { cardOrder, orderBetween } from "../../../../src/lib/features/pm/storage/order.ts"; import { TaskStore } from "../../../../src/lib/features/pm/model/store.ts"; import { isHiddenCard } from "../../../../src/lib/features/pm/storage/persistence.ts"; import type { TaskFields, Workspace } from "../../../../src/lib/features/pm/model/model.ts"; const fields: TaskFields = { status: "todo", tags: [], sessions: [], archived: false, }; function fixture() { const board = newBoardSource(); const files = new Map([["Board.md", board]]); const reads: string[] = [], lists: string[] = [], writes: string[] = []; const workspace: Workspace = { async readOptional(path) { return files.get(path) ?? null; }, async list(parent = "") { lists.push(parent); const entries = new Map(); for (const path of files.keys()) { const prefix = parent ? parent + "/" : ""; if (!path.startsWith(prefix)) continue; const parts = path.slice(prefix.length).split("/"); entries.set(parts[0], { name: parts[0], rel_path: prefix + parts[0], kind: parts.length > 1 ? "folder" : "file", }); } return [...entries.values()]; }, async read(path) { reads.push(path); if (!files.has(path)) throw new Error("Missing note"); return files.get(path)!; }, async write(path, source, expected) { assert.equal(files.get(path), expected); writes.push(path); files.set(path, source); }, async create(path, source) { if (files.has(path)) throw new Error("Exists"); files.set(path, source); }, }; const reference = { id: parseBoard(board).id, path: "Board.md" }; return { files, reads, lists, writes, workspace, reference, store: new TaskStore(workspace, reference), }; } test("card creation reads one board and does not scan unrelated notes after loading", async () => { const { store, files, lists, reads } = fixture(); for (let index = 0; index < 300; index++) files.set(`Archive/Note ${index}.md`, "Plain note"); await store.reload(); lists.length = reads.length = 0; await store.create("New card", fields, "Details"); assert.deepEqual(lists, []); assert.deepEqual(reads, ["Board.md"]); assert.equal(store.scopedTasks[0].title, "New card"); assert.equal(store.scopedTasks[0].body, "Details"); await store.create("New card", fields, "Second"); assert.equal(store.scopedTasks.length, 2); assert.equal(store.scopedTasks[0].body, "Details"); }); test("creation checks the current list before writing a card", async () => { const { store, files } = fixture(); await store.reload(); await store.board.remove("todo"); const snapshot = new Map(files); await assert.rejects( store.create("Missing list", { ...fields, listId: "todo" }, ""), /list no longer exists/, ); assert.deepEqual(files, snapshot); assert.equal(store.tasks.length, 0); }); test("a background scan cannot replace a newly created card with an older snapshot", async () => { const { store, workspace } = fixture(); await store.reload(); const list = workspace.list; let release!: () => void; const gate = new Promise((resolve) => { release = resolve; }); workspace.list = async (path) => { const snapshot = await list(path); await gate; return snapshot; }; const pending = store.reload(); await store.create("Keep new card", fields, ""); release(); await pending; assert.deepEqual( store.scopedTasks.map((task) => task.title), ["Keep new card"], ); }); test("title edits preserve the filename, identity, metadata, and body", async () => { const { store, files } = fixture(); await store.create("Original", fields, "Keep **Markdown**.\n"); const original = store.tasks[0]; files.set( original.path, original.source.replace("---\n", "---\nowner: Ada # keep\n"), ); await store.reload(); await store.save(store.tasks[0], { title: "Edited: title / shared" }); const updated = store.tasks[0]; assert.equal(updated.title, "Edited: title / shared"); assert.equal(updated.id, original.id); assert.equal(updated.path, original.path); assert.equal(updated.body, original.body); assert.match(updated.source, /owner: Ada # keep/); assert.equal([...files.keys()].filter(isHiddenCard).length, 1); await store.reload(); assert.equal(store.tasks[0].title, updated.title); await assert.rejects(store.save(original, { title: "Stale" }), /changed/); await assert.rejects( store.save(store.tasks[0], { title: " " }), /card title/, ); assert.equal(readTask(original.path, original.source)!.title, "Original"); }); test("legacy notes with long filenames remain editable before a card title is set", () => { const path = "Long existing note ".repeat(8).trim() + ".md"; const source = adoptNote(path, "Existing description"); const task = readTask(path, source)!; assert.equal(task.title, path.slice(0, -3)); const edited = readTask(path, editTask(task, { status: "doing" }))!; assert.equal(edited.title, task.title); assert.equal(edited.status, "doing"); }); test("cards retain insertion order and can move before, between, and after other cards", async () => { const { store, workspace, reference, writes } = fixture(); for (const name of ["Zulu", "Alpha", "Middle"]) await store.create(name, fields, ""); const names = () => store.cardsInList("todo").map((task) => task.title); const task = (title: string) => store.tasks.find((task) => task.title === title)!; assert.deepEqual(names(), ["Zulu", "Alpha", "Middle"]); await store.save(task("Middle"), { status: "done" }); const middle = task("Middle"); writes.length = 0; await store.moveTask(middle, "todo", task("Zulu").id); assert.deepEqual(writes, [middle.path]); assert.equal(task("Middle").status, "done"); assert.deepEqual(names(), ["Middle", "Zulu", "Alpha"]); await store.moveTask(task("Alpha"), "todo", task("Zulu").id); assert.deepEqual(names(), ["Middle", "Alpha", "Zulu"]); await store.moveTask(task("Middle"), "todo", null); assert.deepEqual(names(), ["Alpha", "Zulu", "Middle"]); await store.save(task("Zulu"), { title: "A renamed card", listId: "todo", tags: ["release"], }); const reopened = new TaskStore(workspace, reference); await reopened.reload(); assert.deepEqual( reopened.cardsInList("todo").map((task) => task.title), ["Alpha", "A renamed card", "Middle"], ); }); test("card order stays independent in each board and All tasks", async () => { const { store, files, workspace, reference } = fixture(); const secondSource = newBoardSource(); files.set("Second.md", secondSource); const second = new TaskStore(workspace, { id: parseBoard(secondSource).id, path: "Second.md", }); for (const name of ["Alpha", "Beta", "Gamma"]) { await store.create( name, { ...fields, due: { date: "2026-10-08" } }, "Shared details", ); await second.adopt(store.tasks.find((task) => task.title === name)!.path); } await store.reload(); const gamma = store.tasks.find((task) => task.title === "Gamma")!; const secondPlacement = gamma.placements![second.board.reference!.id]; await store.moveTask( gamma, "todo", store.tasks.find((task) => task.title === "Alpha")!.id, ); await second.reload(); assert.deepEqual( second.cardsInList("todo").map((task) => task.title), ["Alpha", "Beta", "Gamma"], ); assert.deepEqual( second.tasks.find((task) => task.id === gamma.id)!.placements![ second.board.reference!.id ], secondPlacement, ); const all = new TaskStore(workspace); await all.reload(); await all.moveTask(all.tasks[1], "todo", all.tasks[0].id); assert.deepEqual( all.cardsInList("todo").map((task) => task.title), ["Beta", "Alpha", "Gamma"], ); await store.reload(); assert.deepEqual( store.cardsInList("todo").map((task) => task.title), ["Gamma", "Alpha", "Beta"], ); await store.moveTask(store.cardsInList("todo")[0], "done", null); const moved = store.cardsInList("done")[0]; assert.equal(moved.status, "done"); assert.deepEqual(moved.due, { date: "2026-10-08" }); assert.equal(moved.body, "Shared details"); assert.equal(moved.placements![reference.id].listId, "done"); }); test("failed or stale order writes keep card data intact", async () => { const { store, workspace, files } = fixture(); await store.create("Alpha", fields, ""); await store.create("Beta", fields, ""); const [alpha, beta] = store.cardsInList("todo"); const snapshot = new Map(files); workspace.write = async () => { throw new Error("Disk failure"); }; await assert.rejects(store.moveTask(beta, "todo", alpha.id), /Disk failure/); assert.deepEqual(files, snapshot); assert.deepEqual( store.cardsInList("todo").map((task) => task.title), ["Alpha", "Beta"], ); await assert.rejects( store.moveTask(beta, "todo", "missing"), /target card moved/, ); files.set(beta.path, editTask(beta, { body: "External change" })); await assert.rejects(store.moveTask(beta, "todo", alpha.id), /changed/); }); test("ordering handles repeated narrow gaps, legacy notes, and matching saved ranks", () => { let upper = "j"; for (let index = 0; index < 1200; index++) { const rank = orderBetween("i", upper); assert.ok("i" < rank && rank < upper); assert.match(rank, /^[0-9a-z]*[1-9a-z]$/); upper = rank; } const alphaNote = newTask( "Alpha", fields, "", "00000000-0000-4000-8000-000000000001", ); const betaNote = newTask( "Beta", fields, "", "00000000-0000-4000-8000-000000000002", ); const alpha = readTask(alphaNote.path, alphaNote.source)!; const beta = readTask(betaNote.path, betaNote.source)!; assert.ok(cardOrder(alpha) < cardOrder(beta)); assert.equal(cardOrder(alpha), cardOrder({ ...alpha, title: "Zulu" })); alpha.order = beta.order = "i"; const between = orderBetween(cardOrder(alpha), cardOrder(beta)); const inserted = cardOrder({ ...alpha, id: "new", order: between }); assert.ok(cardOrder(alpha) < inserted && inserted < cardOrder(beta)); assert.throws(() => editTask(alpha, { order: "invalid!" }), /order/); });