import test from "node:test"; import assert from "node:assert/strict"; import { BoardStore, newBoardSource, parseBoard } from "../../../../src/lib/features/pm/storage/board.ts"; import { TaskStore } from "../../../../src/lib/features/pm/model/store.ts"; import { newTask, readTask } from "../../../../src/lib/features/pm/storage/notes.ts"; import type { Workspace, TaskFields } from "../../../../src/lib/features/pm/model/model.ts"; const fields: TaskFields = { status: "todo", tags: [], sessions: [], archived: false, }; function fixture() { const source = newBoardSource(); const files = new Map([["Board.md", source]]); const reference = { id: parseBoard(source).id, path: "Board.md" }; const workspace: Workspace = { async readOptional(path) { return files.get(path) ?? null; }, async read(path) { if (!files.has(path)) throw Error("Missing"); return files.get(path)!; }, async create(path, text) { if (files.has(path)) throw Error("Exists"); files.set(path, text); }, async write(path, text, expected) { if (files.get(path) !== expected) throw Error("Conflict"); files.set(path, text); }, async list(parent = "") { return [...files.keys()] .filter((path) => !path.startsWith(".")) .map((path) => ({ name: path, rel_path: path, kind: "file" })); }, }; return { files, workspace, reference }; } test("list names, completion mapping, order, and collapse survive reload", async () => { const { workspace, reference } = fixture(); const board = new BoardStore(workspace, reference); await board.reload(); await board.add("Review", null); const review = board.value.lists.at(-1)!; await board.edit(review.id, "Approved", "done"); await board.move(review.id, 0); await board.collapse(review.id); const reopened = new BoardStore(workspace, reference); await reopened.reload(); assert.deepEqual(reopened.value.lists[0], { id: review.id, name: "Approved", status: "done", collapsed: true, }); await assert.rejects(reopened.add("approved", null), /already exists/); }); test("list maximum survives edits and reload, rejects invalid values, and can be removed", async () => { const { workspace, reference } = fixture(); const board = new BoardStore(workspace, reference); await board.reload(); await board.setLimit("todo", 2); await board.edit("todo", "Next", "todo"); await board.collapse("todo"); await board.reload(); assert.equal(board.value.lists[0].maxCards, 2); for (const invalid of [0, -1, 1.5, Infinity, NaN, Number.MAX_SAFE_INTEGER + 1]) await assert.rejects(board.setLimit("todo", invalid), /whole number/); const source = newBoardSource().replace("collapsed: false", "collapsed: false\n maxCards: -2"); assert.throws(() => parseBoard(source), /invalid list/); await board.setLimit("todo"); await board.reload(); assert.equal(board.value.lists[0].maxCards, undefined); }); test("deleting a populated list preserves notes and sends its cards to Unlisted", async () => { const { workspace, files, reference } = fixture(); const note = newTask( "Keep me", { ...fields, placements: { [reference.id]: { listId: "todo" } } }, "Keep the body", ); files.set("Keep me.md", note.source); const store = new TaskStore(workspace, reference); await store.reload(); await store.changeBoard(() => store.board.remove("todo")); assert.equal(store.board.listFor(store.tasks[0]), undefined); assert.equal(files.get("Keep me.md"), note.source); await store.board.add("Replacement", null); assert.equal(store.board.listFor(store.tasks[0]), undefined); assert.equal( store.board.listFor({ status: "todo", listId: undefined }), undefined, ); }); test("moving a card stores its list and completion in one guarded note write", async () => { const { workspace, files, reference } = fixture(); const note = newTask("Task", fields); files.set("Task.md", note.source); const store = new TaskStore(workspace, reference); await store.reload(); await store.moveTask(store.tasks[0], "done"); const saved = readTask("Task.md", files.get("Task.md")!)!; assert.equal(saved.placements?.[reference.id].listId, "done"); assert.equal(saved.status, "done"); assert.equal(saved.id, store.tasks[0].id); await store.moveTask(store.tasks[0], null); assert.equal(store.board.listFor(store.tasks[0]), undefined); assert.equal(store.tasks[0].status, "todo"); }); test("stale list edits do not overwrite another board revision", async () => { const { workspace, files, reference } = fixture(); const first = new BoardStore(workspace, reference), second = new BoardStore(workspace, reference); await first.reload(); await second.reload(); await first.add("Review", null); const saved = files.get("Board.md"); await assert.rejects(second.add("Other", null), /changed/); assert.equal(files.get("Board.md"), saved); }); test("malformed or future board files stay untouched", async () => { const { workspace, files, reference } = fixture(); const text = newBoardSource().replace("schema: 1", "schema: 2"); files.set("Board.md", text); const board = new BoardStore(workspace, reference); await assert.rejects(board.reload(), /unsupported/); await assert.rejects(board.add("Oops", null), /Open an available board/); assert.equal(files.get("Board.md"), text); }); test("deleting all lists remains empty after reload and allows a new list", async () => { const { workspace, reference } = fixture(); const board = new BoardStore(workspace, reference); await board.reload(); for (const id of ["todo", "doing", "done"]) await board.remove(id); const next = new BoardStore(workspace, reference); await next.reload(); assert.equal(next.value.lists.length, 0); await next.add("First", null); assert.equal(next.value.lists[0].name, "First"); }); test("All tasks cannot save board lists", async () => { const { workspace, reference } = fixture(); const board = new BoardStore(workspace); await board.reload(); await assert.rejects(board.add("Review", null), /Open an available board/); assert.equal(board.value.lists.length, 3); }); test("archiving a list scopes by board placement and preserves status, bodies, and other memberships", async () => { const { workspace, files, reference } = fixture(); const sourceBoard = files.get(reference.path); for (const [title, placements] of [ ["Target", { [reference.id]: { listId: "todo" }, other: { listId: "doing" } }], ["Other list", { [reference.id]: { listId: "doing" } }], ["Other board", { other: { listId: "todo" } }], ] as const) { const task = newTask(title, { ...fields, placements }, "Keep body"); files.set(task.path, task.source); } const store = new TaskStore(workspace, reference); await store.reload(); const original = store.tasks.find(task => task.title === "Target")!; await store.archiveList("todo"); await store.reload(); const archived = store.tasks.find(task => task.title === "Target")!; assert.equal(archived.archived, true); assert.equal(archived.status, original.status); assert.deepEqual(archived.placements, original.placements); assert.equal(archived.body, original.body); assert.equal(store.tasks.filter(task => task.archived).length, 1); assert.equal(files.get(reference.path), sourceBoard); await store.archiveList("todo"); await assert.rejects(store.archiveList("missing"), /unavailable/); }); test("board image appearance survives legacy saves and hidden catalog saves independently", async () => { const { workspace, reference } = fixture(); const board = new BoardStore(workspace, reference); await board.reload(); const appearance = { overlay: -0.4, position: "bottom right", size: "contain" } as const; await board.update(value => { value.backgroundImage = "11111111-1111-4111-8111-111111111111"; value.backgroundAppearance = appearance; }); await board.reload(); assert.equal(board.value.backgroundImage, "11111111-1111-4111-8111-111111111111"); assert.deepEqual(board.value.backgroundAppearance, appearance); const store = new TaskStore(workspace); await store.reload(); const hidden = await store.createBoard("Hidden"); const managed = new BoardStore(workspace, hidden); await managed.reload(); await managed.update(value => { value.backgroundImage = "22222222-2222-4222-8222-222222222222"; value.backgroundAppearance = { overlay: 0.7, position: "top", size: "auto" }; }); await managed.reload(); assert.equal(managed.value.backgroundImage, "22222222-2222-4222-8222-222222222222"); assert.equal(managed.value.backgroundAppearance!.overlay, 0.7); await board.reload(); assert.deepEqual(board.value.backgroundAppearance, appearance); }); test("leaving a Done rule reopens a card and neutral moves preserve manual completion", async () => { const { workspace, files, reference } = fixture(); const note = newTask("Task", { ...fields, status: "done", placements: { [reference.id]: { listId: "done" } } }); files.set("Task.md", note.source); const store = new TaskStore(workspace, reference); await store.reload(); await store.changeBoard(() => store.board.add("Review", null)); const review = store.board.value.lists.at(-1)!.id; await store.moveTask(store.tasks[0], review); await store.reload(); assert.equal(store.tasks[0].status, "todo"); assert.equal(store.tasks[0].placements![reference.id].listId, review); await store.save(store.tasks[0], { status: "done" }); await store.moveTask(store.tasks[0], null); await store.reload(); assert.equal(store.tasks[0].status, "done"); await store.changeBoard(() => store.board.edit(review, "Review", "doing")); await store.moveTask(store.tasks[0], review); await store.reload(); assert.equal(store.tasks[0].status, "doing"); await store.moveTask(store.tasks[0], "todo"); await store.changeBoard(() => store.board.edit(review, "Review", null)); await store.save(store.tasks[0], { listId: review }, true); await store.reload(); assert.equal(store.tasks[0].status, "todo"); assert.equal(store.board.value.lists.find(list => list.id === review)!.status, null); }); test("overview creation writes one assigned card and rejects stale destinations without extra cards", async () => { const { workspace, files, reference } = fixture(); const store = new TaskStore(workspace); await store.reload(); await store.create("Assigned card", { ...fields, listId: "doing" }, "Card body", reference.id); const task = store.tasks.find(task => task.title === "Assigned card")!; assert.equal(task.placements?.[reference.id].listId, "doing"); assert.equal(Object.keys(task.placements ?? {}).length, 1); assert.equal(task.body.trim(), "Card body"); const snapshot = new Map(files); await assert.rejects(store.create("Missing board", fields, "", "missing"), /active board/); await assert.rejects(store.create("Missing list", { ...fields, listId: "missing" }, "", reference.id), /list no longer exists/); assert.deepEqual(files, snapshot); files.delete("Board.md"); const afterDelete = new Map(files); await assert.rejects(store.create("Deleted board", fields, "", reference.id), /active board/); assert.deepEqual(files, afterDelete); });