Files
2026-11-16 04:21:21 -07:00

119 lines
7.0 KiB
TypeScript

// The legacy format: the native feature finds visible boards, task notes, and the root label file, and offers the
// migration. The migration of the offer is the migration of the plugin, with its backups.
import test from "node:test";
import assert from "node:assert/strict";
import { findLegacy, hasLegacy, mentionsProjectData } from "../../../../src/lib/features/pm/storage/legacy.ts";
import { TaskStore } from "../../../../src/lib/features/pm/model/store.ts";
import { boardMarkdown, catalogPath } from "../../../../src/lib/features/pm/storage/catalog.ts";
import { defaultBoard } from "../../../../src/lib/features/pm/storage/board.ts";
import { cardPath } from "../../../../src/lib/features/pm/storage/cards.ts";
import { deletedCardsPath } from "../../../../src/lib/features/pm/storage/deleted-cards.ts";
import { labelsPath, legacyLabelsPath } from "../../../../src/lib/features/pm/storage/labels.ts";
import { newTask, readTask } from "../../../../src/lib/features/pm/storage/notes.ts";
import { isHiddenCard } from "../../../../src/lib/features/pm/storage/persistence.ts";
import { fields, hiddenWorkspace, memoryWorkspace } from "./_memory.ts";
const labels = JSON.stringify({ schema: 1, labels: [{ id: "bug", name: "Bug", color: "red" }] });
test("only notes that name runestone_pm in their properties count as Project Management data", () => {
assert.equal(mentionsProjectData("---\nrunestone_pm:\n kind: task\n---\n"), true);
assert.equal(mentionsProjectData(String.fromCharCode(0xfeff) + "---\r\ntitle: x\r\nrunestone_pm: {}\r\n---\r\n"), true);
assert.equal(mentionsProjectData("# Plan\nrunestone_pm: in the body\n"), false);
assert.equal(mentionsProjectData("---\ntitle: x\n---\nrunestone_pm: in the body\n"), false);
assert.equal(mentionsProjectData("---\nother_runestone_pm: x\n---\n"), false);
});
test("an empty workspace or a migrated workspace has nothing to offer", async () => {
assert.equal(hasLegacy(await findLegacy(memoryWorkspace({ "Notes/Plain.md": "# Plain\n" }))), false);
const workspace = hiddenWorkspace([{ id: "hidden", title: "Hidden" }]);
assert.deepEqual(await findLegacy(workspace), { boards: [], cards: [], invalid: [], labels: false });
});
test("visible boards, task notes, and the root label file are found without a parse of plain notes", async () => {
const board = defaultBoard("visible-board");
const workspace = memoryWorkspace({
"Boards/Plan.md": boardMarkdown(board),
"Tasks/Write.md": newTask("Write", fields(), "Body", "write").source,
"Notes/Plain.md": "---\ntitle: [broken yaml\n---\nPlain note.\n",
"Notes/Broken.md": "---\nrunestone_pm: [\n---\n",
[legacyLabelsPath]: labels,
});
const content = await findLegacy(workspace);
assert.deepEqual(content, { boards: ["Boards/Plan.md"], cards: ["Tasks/Write.md"], invalid: ["Notes/Broken.md"], labels: true });
assert.equal(hasLegacy(content), true);
});
test("migrated, deleted, and already saved data does not count again", async () => {
const board = defaultBoard("old-board");
const workspace = hiddenWorkspace([{ id: "copied", title: "Copied" }], {
"Old.md": boardMarkdown(board),
"Gone.md": boardMarkdown(defaultBoard("gone-board")),
"Copied.md": newTask("Copied", fields(), "", "copied").source,
"Deleted.md": newTask("Deleted", fields(), "", "deleted").source,
[deletedCardsPath]: JSON.stringify({ schema: 1, ids: ["deleted"] }),
[legacyLabelsPath]: labels,
[labelsPath]: labels,
});
const catalog = JSON.parse(workspace.files.get(catalogPath)!);
catalog.boards.push({ name: "Old", value: board });
catalog.deletedBoardIds = ["gone-board"];
workspace.files.set(catalogPath, JSON.stringify(catalog));
assert.deepEqual(await findLegacy(workspace), { boards: [], cards: [], invalid: [], labels: false });
});
test("the offer migrates every legacy board, task note, and the label file with backups, and changes no original", async () => {
const board = defaultBoard("visible-board");
const originals = {
"Boards/Plan.md": boardMarkdown(board),
"Tasks/Write.md": newTask("Write", fields({ placements: { "visible-board": { listId: "todo" } } }), "Body", "write").source,
"Tasks/Read.md": newTask("Read", fields(), "", "read").source,
[legacyLabelsPath]: labels,
};
const workspace = memoryWorkspace(originals);
// The native view sees nothing before the migration.
const hidden = new TaskStore(workspace, undefined, { legacy: false });
await hidden.reload();
assert.equal(hidden.tasks.length, 0);
assert.equal(hidden.boards.length, 0);
// The offer: a legacy store migrates all boards, the labels, and every visible task note.
const legacy = new TaskStore(workspace, undefined, { legacy: true });
await legacy.reload();
const files = await legacy.migrateStorage(legacy.tasks.filter(task => !isHiddenCard(task.path)).map(task => task.id));
legacy.dispose();
assert.deepEqual(files.map(file => file.path).sort(), ["Boards/Plan.md", "Tasks/Read.md", "Tasks/Write.md", legacyLabelsPath].sort());
for (const file of files) assert.equal(workspace.files.get(file.backup), file.source);
for (const [path, source] of Object.entries(originals)) assert.equal(workspace.files.get(path), source, path);
assert.equal(hasLegacy(await findLegacy(workspace)), false);
await hidden.reload();
assert.deepEqual(hidden.tasks.map(task => task.id).sort(), ["read", "write"]);
assert.deepEqual(hidden.boards.map(item => [item.id, item.name]), [["visible-board", "Plan"]]);
assert.deepEqual(hidden.labels.value.map(label => label.id), ["bug"]);
});
test("Add existing note copies a plain note into a private card and leaves the note unchanged", async () => {
const note = "---\ntags: [keep]\n---\n# Research\nDetails.\n";
const workspace = hiddenWorkspace([], { "Notes/Research.md": note });
const store = new TaskStore(workspace, { id: "board", path: "pm-board:board" }, { legacy: false });
await store.reload();
const card = await store.adopt("Notes/Research.md");
assert.equal(workspace.files.get("Notes/Research.md"), note);
assert.equal(card.path, cardPath(card.id));
assert.equal(card.title, "Research");
assert.equal(card.body, "# Research\nDetails.\n");
assert.equal(card.placements?.board.listId, "todo");
assert.match(workspace.files.get(card.path)!, /^tags: \[ keep \]$/m);
assert.deepEqual(store.tasks.map(task => task.id), [card.id]);
// A legacy task note migrates with a backup and keeps its ID.
const legacyNote = newTask("Legacy", fields(), "Old body", "legacy-id").source;
workspace.files.set("Notes/Legacy.md", legacyNote);
const migrated = await store.adopt("Notes/Legacy.md");
assert.equal(migrated.id, "legacy-id");
assert.equal(migrated.path, cardPath("legacy-id"));
assert.equal(workspace.files.get("Notes/Legacy.md"), legacyNote);
assert.equal(workspace.files.get(".runestone/pm/backups/Notes/Legacy.md"), legacyNote);
assert.equal(readTask(migrated.path, workspace.files.get(migrated.path)!)!.placements?.board.listId, "todo");
// A second add of the same note finds the same card.
assert.equal((await store.adopt("Notes/Legacy.md")).path, migrated.path);
assert.equal(store.tasks.length, 2);
});