import test from "node:test"; import assert from "node:assert/strict"; import { TaskStore } from "../../../../src/lib/features/pm/model/store.ts"; import { newTask, readTask } from "../../../../src/lib/features/pm/storage/notes.ts"; import { deletedCardsPath } from "../../../../src/lib/features/pm/storage/deleted-cards.ts"; import { migrateCards } from "../../../../src/lib/features/pm/storage/cards.ts"; import type { Workspace } from "../../../../src/lib/features/pm/model/model.ts"; function fixture() { const note = newTask("Delete me", { status: "todo", tags: [], sessions: [], archived: false, placements: { first: { listId: "todo" }, second: { listId: "doing" } } }, "Keep this source"); const task = readTask("Delete me.md", note.source)!; const parent = newTask("Related", { status: "todo", tags: [], sessions: [], archived: false, links: [{ taskId: task.id, relation: "relates" }], subtasks: [{ id: "sub", taskId: task.id, title: task.title, done: false }] }, ""); const files = new Map([[task.path, note.source], ["Related.md", parent.source]]); let fail = false; const workspace: Workspace = { async list(folder = "") { return [...files.keys()].filter(path => folder ? path.startsWith(`${folder}/`) : !path.startsWith(".")).map(path => ({ name: path.split("/").at(-1)!, rel_path: path, kind: "file" })); }, async read(path) { if (!files.has(path)) throw Error("Missing"); return files.get(path)!; }, async readOptional(path) { return files.get(path) ?? null; }, async create(path, source) { if (fail || files.has(path)) throw Error("Write failed"); files.set(path, source); }, async write(path, source, expected) { if (fail) throw Error("Write failed"); assert.equal(files.get(path), expected); files.set(path, source); }, }; return { task, files, workspace, setFailure() { fail = true; } }; } test("confirmed deletion removes legacy and migrated cards from all scopes and retains source records", async () => { for (const migrate of [false, true]) { const { workspace, files, task } = fixture(); if (migrate) await migrateCards(workspace, [task]); const store = new TaskStore(workspace); await store.reload(); const before = new Map(files); await store.deleteCard(store.tasks.find(item => item.id === task.id)!); assert.deepEqual(store.tasks.map(item => item.title), ["Related"]); for (const [path, source] of before) assert.equal(files.get(path), source); assert.deepEqual(JSON.parse(files.get(deletedCardsPath)!).ids, [task.id]); const fresh = new TaskStore(workspace); await fresh.reload(); assert.equal(fresh.tasks.some(item => item.id === task.id), false); await assert.rejects(fresh.openSubtask(fresh.tasks[0].id, "sub"), /unavailable/); assert.equal(fresh.tasks[0].links![0].taskId, task.id); await assert.rejects(fresh.save(task, { archived: false }), /changed|duplicate/); } }); test("stale cards and failed deletion writes preserve the card", async () => { const stale = fixture(); const store = new TaskStore(stale.workspace); await store.reload(); stale.files.set(stale.task.path, stale.task.source + "External edit\n"); await assert.rejects(store.deleteCard(stale.task), /changed/); assert.equal(stale.files.has(deletedCardsPath), false); const failed = fixture(); const next = new TaskStore(failed.workspace); await next.reload(); failed.setFailure(); await assert.rejects(next.deleteCard(failed.task), /Write failed/); await next.reload(); assert.equal(next.tasks.length, 2); }); test("damaged deletion records block card access instead of restoring deleted cards", async () => { const { workspace, files } = fixture(); for (const source of ['{', '{"schema":2,"ids":[]}', '{"schema":1,"ids":[4]}']) { files.set(deletedCardsPath, source); const store = new TaskStore(workspace); await store.reload(); assert.equal(store.tasks.length, 0); assert.ok(store.issues.some(issue => issue.startsWith("Deleted cards:"))); } });