import test from "node:test"; import assert from "node:assert/strict"; import { TaskStore } from "../../../../src/lib/features/pm/model/store.ts"; import { newBoardSource, parseBoard, BoardStore } from "../../../../src/lib/features/pm/storage/board.ts"; import { catalogPath, createBoard, readCatalog, boardKey, parseCatalog, } from "../../../../src/lib/features/pm/storage/catalog.ts"; import { migrateStorage } from "../../../../src/lib/features/pm/storage/migration.ts"; import { labelsPath, legacyLabelsPath } from "../../../../src/lib/features/pm/storage/labels.ts"; import { newTask } from "../../../../src/lib/features/pm/storage/notes.ts"; import type { Workspace } from "../../../../src/lib/features/pm/model/model.ts"; function fixture() { const source = newBoardSource() + "Keep the board description.\n"; const id = parseBoard(source).id; const task = newTask( "Keep task", { status: "doing", tags: ["tag"], labels: ["bug"], archived: false, sessions: [], placements: { [id]: { listId: "doing", order: "a" } }, }, "Keep task body.", ); const files = new Map([ ["Project/Launch.md", source], [task.path, task.source], [ legacyLabelsPath, JSON.stringify({ schema: 1, extra: "keep", labels: [{ id: "bug", name: "Defect", color: "red" }], }), ], ]); let fail = ""; const workspace: Workspace = { async readOptional(path) { return files.get(path) ?? null; }, async read(path) { if (!files.has(path)) throw new Error("Missing file"); return files.get(path)!; }, async list(parent = "") { const entries = new Map(); for (const path of files.keys()) { const prefix = parent ? parent + "/" : ""; if (!path.startsWith(prefix) || path.startsWith(".")) 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 create(path, source) { if (path === fail) throw new Error("Disk failure"); if (files.has(path)) throw new Error("Exists"); files.set(path, source); }, async write(path, source, expected) { if (path === fail) throw new Error("Disk failure"); if (files.get(path) !== expected) throw new Error("Conflict"); files.set(path, source); }, }; return { files, workspace, id, task, source, setFailure(path: string) { fail = path; }, }; } test("migration backs up exact originals, preserves cards and survives removal of visible metadata", async () => { const { files, workspace, id, task, source } = fixture(); const originalLabels = files.get(legacyLabelsPath)!; const store = new TaskStore(workspace, { id, path: "Project/Launch.md" }); const report = await store.migrateStorage(); assert.equal(report.length, 2); for (const file of report) assert.equal(files.get(file.backup), files.get(file.path)); assert.equal(files.get("Project/Launch.md"), source); assert.equal(files.get(task.path), task.source); assert.equal(store.board.reference?.path, boardKey(id)); assert.equal(store.scopedTasks.length, 1); assert.equal(store.scopedTasks[0].placements?.[id].order, "a"); assert.equal(store.labels.value[0].name, "Defect"); await store.board.edit("doing", "Building", "doing"); await store.reload(); await store.saveLabels( [{ id: "bug", name: "Issue", color: "blue" }], store.labels.source, ); await store.migrateStorage(); assert.equal(store.board.value.lists[1].name, "Building"); assert.equal(store.labels.value[0].name, "Issue"); assert.equal(files.get(legacyLabelsPath), originalLabels); files.delete("Project/Launch.md"); files.delete(legacyLabelsPath); const reopened = new TaskStore(workspace, { id, path: boardKey(id) }); await reopened.reload(); assert.equal(reopened.scopedTasks[0].id, store.tasks[0].id); assert.equal(reopened.labels.value[0].name, "Issue"); assert.equal(reopened.board.value.lists[1].name, "Building"); assert.equal(reopened.issues.length, 0); }); test("a label write failure can resume after boards commit without duplicating data", async () => { const { files, workspace, setFailure } = fixture(); const store = new TaskStore(workspace); setFailure(labelsPath); await assert.rejects(store.migrateStorage(), /Disk failure/); assert.equal((await readCatalog(workspace)).value.boards.length, 1); assert.ok(files.has(legacyLabelsPath)); setFailure(""); await store.migrateStorage(); assert.equal((await readCatalog(workspace)).value.boards.length, 1); assert.ok(files.has(labelsPath)); }); test("backup failure prevents migration publication", async () => { const { files, workspace, setFailure } = fixture(); setFailure(".runestone/pm/backups/Project/Launch.md"); await assert.rejects( new TaskStore(workspace).migrateStorage(), /Disk failure/, ); assert.equal(files.has(catalogPath), false); assert.equal(files.has(labelsPath), false); }); test("a legacy editor cannot write its original board after another view migrates it", async () => { const { files, workspace, id, source } = fixture(); const stale = new BoardStore(workspace, { id, path: "Project/Launch.md" }); await stale.reload(); await new TaskStore(workspace).migrateStorage(); await assert.rejects(stale.add("Old edit", null), /hidden storage/); assert.equal(files.get("Project/Launch.md"), source); await stale.reload(); assert.equal(stale.reference?.path, boardKey(id)); await stale.add("New edit", null); assert.equal( (await readCatalog(workspace)).value.boards[0].value.lists.at(-1)?.name, "New edit", ); }); test("invalid or duplicate originals and conflicting labels block migration without writes", async () => { for (const kind of [ "duplicate", "invalid-labels", "hidden-labels", "future", ]) { const { files, workspace, source } = fixture(); if (kind === "duplicate") files.set("Duplicate.md", source); if (kind === "invalid-labels") files.set(legacyLabelsPath, "broken"); if (kind === "hidden-labels") files.set(labelsPath, files.get(legacyLabelsPath)!); if (kind === "future") files.set(catalogPath, '{"schema":99,"boards":[]}'); const before = new Map(files); await assert.rejects(new TaskStore(workspace).migrateStorage()); assert.deepEqual(files, before); } }); test("changed legacy files or damaged backups never receive a successful migration check", async () => { for (const path of [ "Project/Launch.md", ".runestone/pm/backups/Project/Launch.md", legacyLabelsPath, ]) { const { files, workspace } = fixture(); const store = new TaskStore(workspace); await store.migrateStorage(); files.set(path, files.get(path)! + "\n"); const before = new Map(files); await assert.rejects(store.migrateStorage()); assert.deepEqual(files, before); } }); test("hidden boards rename, archive, restore, and reject stale writes without changing task notes", async () => { const { files, workspace, task } = fixture(); const reference = await createBoard(workspace, "New board"); const store = new TaskStore(workspace, reference); await store.reload(); await store.renameBoard("Release"); const stale = new BoardStore(workspace, reference); await stale.reload(); await store.board.manage({ archived: true }); await assert.rejects(stale.add("Review", null), /changed/); await store.reload(); assert.equal(store.board.editable, false); await assert.rejects( store.create( "No card", { status: "todo", tags: [], sessions: [], archived: false }, "", ), /Restore/, ); await store.board.manage({ archived: false }); await store.reload(); assert.equal(store.board.name, "Release"); assert.equal(store.board.editable, true); assert.equal(files.get(task.path), task.source); await assert.rejects(createBoard(workspace, "release"), /already exists/); }); test("hidden data is isolated by workspace and unknown catalog fields survive edits", async () => { const a = fixture(), b = fixture(); const reference = await createBoard(a.workspace, "Private"); const value = JSON.parse(a.files.get(catalogPath)!); value.futureMapping = { externalId: "remote-1" }; a.files.set(catalogPath, JSON.stringify(value)); const board = new BoardStore(a.workspace, reference); await board.reload(); await board.add("Review", null); assert.deepEqual( JSON.parse(a.files.get(catalogPath)!).futureMapping, value.futureMapping, ); assert.equal((await readCatalog(b.workspace)).value.boards.length, 0); }); test("deleting a board preserves cards, other boards, and unknown catalog fields", async () => { for (const archived of [false, true]) { const { files, workspace, task, id } = fixture(); const reference = await createBoard(workspace, "Accidental board"); const initial = JSON.parse(files.get(catalogPath)!); initial.futureMapping = { keep: "value" }; initial.boards[0].archived = archived; files.set(catalogPath, JSON.stringify(initial)); const store = new TaskStore(workspace, reference); await store.reload(); await store.changeBoard(() => store.board.deleteBoard()); assert.equal(store.board.available, false); assert.equal(store.board.editable, false); assert.equal(files.get(task.path), task.source); const all = new TaskStore(workspace); await all.reload(); assert.deepEqual(all.boards.map((board) => board.id), [id]); assert.equal(all.tasks[0].placements?.[id].listId, "doing"); const catalog = (await readCatalog(workspace)).value; assert.deepEqual(catalog.boards, []); assert.deepEqual(catalog.deletedBoardIds, [reference.id]); assert.deepEqual(catalog.futureMapping, initial.futureMapping); await assert.rejects(store.board.deleteBoard(), /available/); } }); test("deleted legacy and migrated boards stay deleted after reload and migration", async () => { for (const migrated of [false, true]) { const { files, workspace, id, source, task } = fixture(); const store = new TaskStore(workspace, { id, path: "Project/Launch.md" }); await store.reload(); const original = store.boards[0]; const stale = new BoardStore(workspace, { id, path: "Project/Launch.md" }); await stale.reload(); if (migrated) await store.migrateStorage(); await store.changeBoard(() => store.board.deleteBoard()); assert.equal(files.get("Project/Launch.md"), source); assert.equal(files.get(task.path), task.source); if (migrated) assert.equal(files.get(".runestone/pm/backups/Project/Launch.md"), source); await assert.rejects(stale.add("Stale list", null), /deleted/); await assert.rejects(stale.reload(), /deleted/); assert.equal(stale.available, false); await assert.rejects(migrateStorage(workspace, [original]), /deleted/); const all = new TaskStore(workspace); await all.reload(); assert.equal(all.boards.length, 0); assert.equal(all.legacyBoards.length, 0); assert.equal(all.tasks.length, 1); await all.migrateStorage(); await all.reload(); assert.equal(all.boards.length, 0); assert.equal(all.issues.length, 0); } }); test("changed boards and failed catalog writes block deletion", async () => { for (const managed of [false, true]) { const { files, workspace, id, setFailure } = fixture(); const reference = managed ? await createBoard(workspace, "New") : { id, path: "Project/Launch.md" }; const board = new BoardStore(workspace, reference); await board.reload(); const path = managed ? catalogPath : reference.path; files.set(path, files.get(path)! + "\n"); const before = new Map(files); await assert.rejects(board.deleteBoard(), /changed/); assert.deepEqual(files, before); await board.reload(); setFailure(catalogPath); await assert.rejects(board.deleteBoard(), /Disk failure/); assert.equal(board.available, true); assert.equal((await readCatalog(workspace)).value.deletedBoardIds, undefined); } }); test("a legacy board cannot be deleted from a stale view after migration", async () => { const { workspace, id } = fixture(); const stale = new BoardStore(workspace, { id, path: "Project/Launch.md" }); await stale.reload(); await new TaskStore(workspace).migrateStorage(); await assert.rejects(stale.deleteBoard(), /hidden storage/); assert.equal((await readCatalog(workspace)).value.boards.length, 1); }); test("invalid deletion records fail closed", () => { for (const deletedBoardIds of ["id", [null], [""], ["id", "id"]]) assert.throws(() => parseCatalog(JSON.stringify({ schema: 1, boards: [], deletedBoardIds })), /deleted board/); });