// The hidden mode of the store (`legacy: false`): it reads only `.runestone/pm/`, and a save, a delete, a move, // or a watcher event reads only the changed files again. import test from "node:test"; import assert from "node:assert/strict"; import { TaskStore } from "../../../../src/lib/features/pm/model/store.ts"; import { cardPath } from "../../../../src/lib/features/pm/storage/cards.ts"; import { catalogPath } from "../../../../src/lib/features/pm/storage/catalog.ts"; import { deletedCardsPath } from "../../../../src/lib/features/pm/storage/deleted-cards.ts"; import { labelsPath } from "../../../../src/lib/features/pm/storage/labels.ts"; import { editTask, newTask } from "../../../../src/lib/features/pm/storage/notes.ts"; import { newBoardSource } from "../../../../src/lib/features/pm/storage/board.ts"; import { privateRulesPath } from "../../../../src/lib/features/pm/storage/persistence.ts"; import { fields, hiddenWorkspace } from "./_memory.ts"; const cards = [{ id: "a", title: "Card A" }, { id: "b", title: "Card B" }, { id: "c", title: "Card C" }]; test("the hidden mode reads only .runestone/pm and never a visible Markdown note", async () => { const workspace = hiddenWorkspace(cards, { "Notes/Task.md": newTask("Task", fields(), "", "visible").source, "Board.md": newBoardSource(), }); const store = new TaskStore(workspace, { id: "board", path: "pm-board:board" }, { legacy: false }); await store.reload(); assert.equal(store.legacy, false); assert.deepEqual(store.issues, []); assert.deepEqual(store.tasks.map(task => task.id), ["a", "b", "c"]); assert.deepEqual(store.boards.map(board => board.id), ["board"]); assert.equal(workspace.markdownLists, 0); assert.ok(workspace.reads.every(path => path.startsWith(".runestone/pm/") || path === "runestone-pm-labels.json"), workspace.reads.join(", ")); assert.equal(workspace.reads.some(path => path.endsWith(".md") && !path.startsWith(".runestone/")), false); // The legacy mode, for the migration and the skill helper, still reads the visible notes. const legacy = new TaskStore(workspace); await legacy.reload(); assert.equal(legacy.legacy, true); assert.ok(legacy.tasks.some(task => task.id === "visible")); }); test("a save, a move, and a delete read only the changed card again", async () => { const workspace = hiddenWorkspace(cards); const store = new TaskStore(workspace, { id: "board", path: "pm-board:board" }, { legacy: false }); await store.reload(); const card = (id: string) => store.tasks.find(task => task.id === id)!; // Each write also checks the Git exclusion file of `.runestone/pm/`. It is not a reload. const reads = () => workspace.reads.filter(path => path !== privateRulesPath); workspace.reads.length = 0; await store.save(card("a"), { body: "Changed" }); assert.deepEqual(reads(), [cardPath("a")]); assert.equal(card("a").body, "Changed"); workspace.reads.length = 0; await store.moveTask(card("b"), "doing"); assert.deepEqual(reads(), [cardPath("b")]); assert.equal(card("b").placements!.board.listId, "doing"); assert.equal(card("b").status, "doing"); workspace.reads.length = 0; await store.deleteCard(card("c")); assert.deepEqual(reads().filter(path => path !== deletedCardsPath), [cardPath("c")]); assert.deepEqual(store.tasks.map(task => task.id), ["a", "b"]); assert.match(workspace.files.get(deletedCardsPath)!, /"c"/); }); test("a save still stops when another window changed the card", async () => { const workspace = hiddenWorkspace(cards); const store = new TaskStore(workspace, undefined, { legacy: false }); await store.reload(); const card = store.tasks.find(task => task.id === "a")!; workspace.files.set(cardPath("a"), editTask(card, { body: "Other window" })); await assert.rejects(store.save(card, { body: "This window" }), /changed/); assert.equal(store.tasks.find(task => task.id === "a")!.body, "Other window"); }); test("watcher paths read only the changed files of .runestone/pm", async () => { const workspace = hiddenWorkspace(cards); const store = new TaskStore(workspace, { id: "board", path: "pm-board:board" }, { legacy: false }); await store.reload(); // A change outside `.runestone/pm/`, or of images and drafts, reads nothing. workspace.reads.length = 0; workspace.lists.length = 0; await store.reloadPaths(["Notes/Plan.md", ".runestone/pm/images/x.json", ".runestone/pm/drafts/61.json", ".runestone/tabs.json"]); assert.deepEqual(workspace.reads, []); assert.deepEqual(workspace.lists, []); // A new card from another window. workspace.files.set(cardPath("d"), newTask("Card D", fields({ title: "Card D", placements: { board: { listId: "done" } } }), "", "d").source); workspace.reads.length = 0; await store.reloadPaths([cardPath("d")]); assert.deepEqual(workspace.reads, [cardPath("d")]); assert.deepEqual(store.tasks.map(task => task.id), ["a", "b", "c", "d"]); // A deleted card file. workspace.files.delete(cardPath("b")); workspace.reads.length = 0; await store.reloadPaths([cardPath("b")]); assert.deepEqual(workspace.reads, [cardPath("b"), cardPath("b")]); assert.deepEqual(store.tasks.map(task => task.id), ["a", "c", "d"]); // A renamed board reads only the catalog. const catalog = JSON.parse(workspace.files.get(catalogPath)!); catalog.boards[0].name = "Renamed"; workspace.files.set(catalogPath, JSON.stringify(catalog)); workspace.reads.length = 0; await store.reloadPaths([catalogPath]); assert.deepEqual(workspace.reads, [catalogPath]); assert.equal(store.board.name, "Renamed"); // Labels and deleted cards each read their own file. workspace.files.set(labelsPath, JSON.stringify({ schema: 1, labels: [{ id: "x", name: "X", color: "red" }] })); workspace.files.set(deletedCardsPath, JSON.stringify({ schema: 1, ids: ["d"] })); workspace.reads.length = 0; await store.reloadPaths([labelsPath, deletedCardsPath]); assert.deepEqual(workspace.reads.sort(), [deletedCardsPath, labelsPath].sort()); assert.deepEqual(store.labels.value.map(label => label.id), ["x"]); assert.deepEqual(store.tasks.map(task => task.id), ["a", "c"]); }); test("an invalid card from a watcher event hides all cards until it is valid again", async () => { const workspace = hiddenWorkspace(cards); const store = new TaskStore(workspace, undefined, { legacy: false }); await store.reload(); const valid = workspace.files.get(cardPath("a"))!; workspace.files.set(cardPath("a"), "---\nrunestone_pm: [\n---\n"); await store.reloadPaths([cardPath("a")]); assert.deepEqual(store.tasks, []); assert.match(store.issues.join(" "), /\.runestone\/pm\/cards\/61\.md/); workspace.files.set(cardPath("a"), valid); await store.reloadPaths([cardPath("a")]); assert.deepEqual(store.issues, []); assert.equal(store.tasks.length, 3); }); test("a folder change, a marker change, or an unknown change reads everything again", async () => { const workspace = hiddenWorkspace(cards); const store = new TaskStore(workspace, undefined, { legacy: false }); await store.reload(); for (const paths of [[".runestone/pm/cards"], [".runestone/pm/cards/.gitignore"], [".runestone"], undefined]) { workspace.lists.length = 0; await store.reloadPaths(paths); assert.deepEqual(workspace.lists, [".runestone/pm/cards"], String(paths)); } });