import test from "node:test"; import assert from "node:assert/strict"; import { TaskStore } from "../../../../src/lib/features/pm/model/store.ts"; import { newTask, readTask, editTask } from "../../../../src/lib/features/pm/storage/notes.ts"; import { cardPath } from "../../../../src/lib/features/pm/storage/cards.ts"; import { cardsMarker, privateRulesPath } from "../../../../src/lib/features/pm/storage/persistence.ts"; import type { Workspace } from "../../../../src/lib/features/pm/model/model.ts"; function fixture(count = 3) { const files = new Map([[cardsMarker, "*\n"], [privateRulesPath, "*\n"]]); const ids = Array.from({ length: count }, () => crypto.randomUUID()); for (const [index, id] of ids.entries()) files.set(cardPath(id), newTask(`Card ${index}`, { title: `Card ${index}`, status: "todo", tags: [], sessions: [], archived: false, ...(index === 0 ? { links: [{ taskId: ids[1], relation: "blocks" }, { taskId: ids[2], relation: "relates" }] as const } : {}), }, "Keep body", id).source); let batches = 0, singleReads = 0; const workspace: Workspace = { async listMarkdown() { return []; }, async list() { return [...files.keys()].filter(path => path.endsWith(".md")).map(path => ({ name: path.split("/").at(-1)!, rel_path: path, kind: "file" })); }, async read(path) { singleReads++; return files.get(path)!; }, async readOptional(path) { return files.get(path) ?? null; }, async readMany(paths) { assert.ok(paths.length <= 64); batches++; return paths.map(path => ({ path, source: files.get(path) })); }, async write(path, source, expected) { assert.equal(files.get(path), expected); files.set(path, source); }, async create(path, source) { assert.equal(files.has(path), false); files.set(path, source); }, }; return { files, ids, workspace, counts: () => ({ batches, singleReads }) }; } test("1000 cards use 16 bounded reads and share parsed cards between board views", async t => { const f = fixture(1000), store = new TaskStore(f.workspace); let started = performance.now(); await store.reload(); t.diagnostic(`Initial 1000-card parse: ${Math.round(performance.now() - started)} ms`); assert.deepEqual(f.counts(), { batches: 16, singleReads: 0 }); const first = store.tasks[0]; const next = new TaskStore(f.workspace); started = performance.now(); await next.reload(); t.diagnostic(`Switch with unchanged sources: ${Math.round(performance.now() - started)} ms`); assert.equal(next.tasks[0], first); started = performance.now(); await next.save(first, { body: "Changed body" }); t.diagnostic(`Save with full conflict scan: ${Math.round(performance.now() - started)} ms`); assert.deepEqual(f.counts(), { batches: 48, singleReads: 0 }); const stale = next.tasks.find(task => task.id === first.id)!; f.files.set(stale.path, editTask(stale, { body: "External body" })); await assert.rejects(next.save(stale, { body: "Overwrite" }), /changed/); assert.equal(readTask(stale.path, f.files.get(stale.path)!)!.body, "External body"); }); test("incoming unlink preserves unrelated links and rejects stale source cards", async () => { const f = fixture(), store = new TaskStore(f.workspace); await store.reload(); const source = store.tasks.find(task => task.id === f.ids[0])!; await store.unlinkIncoming(f.ids[1], [source]); const saved = readTask(source.path, f.files.get(source.path)!)!; assert.deepEqual(saved.links, [{ taskId: f.ids[2], relation: "relates" }]); assert.equal(saved.body, "Keep body"); await store.unlinkIncoming(f.ids[1], [source]); // A retry is harmless. f.files.set(saved.path, editTask(saved, { body: "External change" })); await assert.rejects(store.unlinkIncoming(f.ids[2], [saved]), /linked card changed/); assert.deepEqual(readTask(saved.path, f.files.get(saved.path)!)!.links, saved.links); });