import test from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, readFile, writeFile, mkdir, readdir, rm, } from "node:fs/promises"; import { execFileSync } from "node:child_process"; import { tmpdir } from "node:os"; import { resolve, join, dirname } from "node:path"; 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 { cardOrder } from "../../../../src/lib/features/pm/storage/order.ts"; import { privateRulesPath, cardsMarker, isHiddenCard, } from "../../../../src/lib/features/pm/storage/persistence.ts"; import { catalogPath } from "../../../../src/lib/features/pm/storage/catalog.ts"; import { defaultLabels, labelsPath } from "../../../../src/lib/features/pm/storage/labels.ts"; import type { Workspace, TaskFields } from "../../../../src/lib/features/pm/model/model.ts"; const fields: TaskFields = { status: "todo", tags: ["keep"], labels: ["bug"], archived: false, sessions: [], due: { date: "2026-10-30" }, plan: { start: "2026-10-07", end: "2026-10-10" }, placements: { board: { listId: "todo" } }, }; async function fixture(t: test.TestContext) { const root = await mkdtemp(join(tmpdir(), "runestone-pm-private-")); t.after(async () => { assert.ok( resolve(root).startsWith(resolve(tmpdir(), "runestone-pm-private-")), ); await rm(root, { recursive: true, force: true }); }); const file = (path: string) => { const absolute = resolve(root, path); assert.ok( absolute === root || absolute.startsWith(root + "/") || absolute.startsWith(root + "\\"), ); return absolute; }; const workspace: Workspace = { async readOptional(path) { try { return await readFile(file(path), "utf8"); } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return null; throw error; } }, async read(path) { return readFile(file(path), "utf8"); }, async list(path = "") { return (await readdir(file(path), { withFileTypes: true })) .filter((entry) => ![".git", ".runestone"].includes(entry.name)) .map((entry) => ({ name: entry.name, rel_path: (path ? path + "/" : "") + entry.name, kind: entry.isDirectory() ? "folder" : "file", })); }, async create(path, source) { await mkdir(dirname(file(path)), { recursive: true }); await writeFile(file(path), source, { flag: "wx" }); }, async write(path, source, expected) { assert.equal(await workspace.read(path), expected, "Changed on disk"); await writeFile(file(path), source); }, }; const git = (...args: string[]) => execFileSync("git", args, { cwd: root, encoding: "utf8", windowsHide: true, }).trim(); return { root, file, workspace, git, store: new TaskStore(workspace) }; } test("new cards and other Project Management data stay outside navigation and normal Git staging", async (t) => { const { store, workspace, git } = await fixture(t); git("init", "--quiet"); await workspace.create!("README.md", "Shared project\n"); await store.createBoard("Board"); await store.create("Card: / duplicate?", fields, "Private details"); await store.create("Card: / duplicate?", fields, "Second card"); await store.saveLabels(defaultLabels(), store.labels.source); await store.reload(); assert.equal(store.tasks.length, 2); assert.equal(new Set(store.tasks.map((task) => task.id)).size, 2); assert.ok(store.tasks.every((task) => isHiddenCard(task.path))); assert.deepEqual( (await workspace.list()).map((entry) => entry.name), ["README.md"], ); assert.equal(await workspace.readOptional!(".gitignore"), null); for (const path of [ catalogPath, labelsPath, privateRulesPath, cardsMarker, ...store.tasks.map((task) => task.path), ]) assert.equal(git("check-ignore", "--", path), path); git("add", "."); assert.equal(git("ls-files"), "README.md"); }); test("Git protection is installed before data writes and failures preserve privacy", async (t) => { const { store, workspace } = await fixture(t); const create = workspace.create!; const writes: string[] = []; workspace.create = async (path, source) => { writes.push(path); if (path === privateRulesPath) throw new Error("Cannot create Git exclusion"); await create(path, source); }; await assert.rejects(store.create("Private", fields, ""), /Git exclusion/); assert.deepEqual(writes, [privateRulesPath]); workspace.create = async (path, source) => { writes.push(path); await create(path, source); }; writes.length = 0; await store.create("Private", fields, ""); assert.deepEqual(writes, [ privateRulesPath, cardsMarker, store.tasks[0].path, ]); }); test("selected legacy cards migrate with exact backups, stable order, and no changes to linked notes", async (t) => { const { store, workspace, file, git } = await fixture(t); git("init", "--quiet"); const original = newTask("Existing", fields, "Preserve **body**.\n"); const source = original.source.replace("---\n", "---\nowner: Ada # retain\n"); await workspace.create!(original.path, source); const linked = newTask("Shared", fields, "Keep as a note"); await workspace.create!("Docs/Shared.md", linked.source); git("add", "."); await store.reload(); const before = store.tasks.find((task) => task.title === "Existing")!; const stale = new TaskStore(workspace); await stale.reload(); const report = await store.migrateStorage([before.id]); assert.equal(report.length, 1); const current = store.tasks.find((task) => task.id === before.id)!; assert.equal(current.path, cardPath(before.id)); assert.equal(current.title, before.title); assert.equal(current.body, before.body); for (const key of [ "status", "tags", "labels", "due", "plan", "sessions", "archived", ] as const) assert.deepEqual(current[key], before[key]); assert.equal(cardOrder(current, "board"), cardOrder(before, "board")); assert.equal(cardOrder(current), cardOrder(before)); assert.equal(await workspace.read(original.path), source); assert.equal(await workspace.read(report[0].backup), source); assert.equal(await workspace.read("Docs/Shared.md"), linked.source); assert.match(current.source, /owner: Ada # retain/); await assert.rejects(stale.save(before, { status: "done" }), /changed/); await store.save(current, { title: "Private: edited", status: "doing" }); await store.migrateStorage([before.id]); assert.equal( store.tasks.find((task) => task.id === before.id)!.title, "Private: edited", ); await rm(file(original.path)); await store.reload(); assert.equal(store.tasks.length, 2); assert.equal( store.tasks.find((task) => task.id === before.id)!.status, "doing", ); git("add", "-A"); assert.equal(git("ls-files"), "Docs/Shared.md"); }); test("partial card migration resumes without replacing newer hidden edits", async (t) => { const { workspace, store } = await fixture(t); for (const title of ["A", "B"]) { const note = newTask(title, fields); await workspace.create!(note.path, note.source); } await store.reload(); const ids = store.tasks.map((task) => task.id); const blocked = cardPath(ids[1]); const create = workspace.create!; workspace.create = async (path, source) => { if (path === blocked) throw new Error("Disk full"); await create(path, source); }; await assert.rejects(store.migrateStorage(ids), /Disk full/); await store.reload(); assert.equal(store.tasks.length, 2); const first = store.tasks.find((task) => task.id === ids[0])!; await store.save(first, { body: "Later edit" }); workspace.create = create; await store.migrateStorage(ids); assert.ok(store.tasks.every((task) => isHiddenCard(task.path))); assert.equal( store.tasks.find((task) => task.id === ids[0])!.body, "Later edit", ); }); test("changed originals block further migration and stale saves without losing either version", async (t) => { const { workspace, store } = await fixture(t); const note = newTask("A", fields); await workspace.create!(note.path, note.source); await store.reload(); const id = store.tasks[0].id; await store.migrateStorage([id]); const hidden = store.tasks[0]; await workspace.write( note.path, editTask(readTask(note.path, note.source)!, { body: "Outside edit" }), note.source, ); await assert.rejects(store.save(hidden, { status: "done" }), /changed/); await assert.rejects(store.migrateStorage(), /issues/); assert.equal(await workspace.read(hidden.path), hidden.source); }); test("migration retains long filename titles and an existing Project Management ignore file", async (t) => { const { workspace, store } = await fixture(t); const note = newTask("A", fields); const path = "Legacy/" + "a".repeat(130) + ".md"; await workspace.create!(path, note.source); await workspace.create!( privateRulesPath, "# Personal rules\ncache/\n!keep.json\n", ); await store.reload(); await store.migrateStorage([store.tasks[0].id]); assert.equal(store.tasks[0].title, "a".repeat(130)); assert.equal( await workspace.read(privateRulesPath), "# Personal rules\ncache/\n!keep.json\n# RuneStone Project Management local data\n*\n", ); }); test("an invalid hidden card cannot fall back to editing its old visible note", async (t) => { const { workspace, store } = await fixture(t); const note = newTask("A", fields); await workspace.create!(note.path, note.source); await store.reload(); const legacy = store.tasks[0]; await store.migrateStorage([legacy.id]); const hidden = store.tasks[0]; await workspace.write(hidden.path, "broken data", hidden.source); await store.reload(); assert.equal(store.tasks.length, 0); assert.match(store.issues.join(" "), /hidden card ID/); await assert.rejects(store.save(legacy, { status: "done" }), /changed/); assert.equal(await workspace.read(note.path), note.source); });