Files
RuneStone/tests/unit/features/pm/core.spec.ts
T
2026-11-16 04:21:21 -07:00

199 lines
7.3 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { readTask, editTask, newTask, adoptNote } from "../../../../src/lib/features/pm/storage/notes.ts";
import {
addDays,
dayDistance,
validDate,
type Workspace,
type TaskFields,
} from "../../../../src/lib/features/pm/model/model.ts";
import { TaskStore } from "../../../../src/lib/features/pm/model/store.ts";
import { isHiddenCard } from "../../../../src/lib/features/pm/storage/persistence.ts";
const fields: TaskFields = {
status: "todo",
tags: [],
sessions: [],
archived: false,
};
function memory(initial: Record<string, string> = {}) {
const files = new Map(Object.entries(initial));
const workspace: Workspace = {
async readOptional(path) {
return files.get(path) ?? null;
},
async list(parent = "") {
const entries = new Map();
for (const path of files.keys()) {
const prefix = parent ? `${parent}/` : "";
if (!path.startsWith(prefix)) continue;
const parts = path.slice(prefix.length).split("/");
entries.set(parts[0], {
name: parts[0],
rel_path: `${prefix}${parts[0]}`,
kind: parts.length > 1 ? "folder" : "file",
});
}
return [...entries.values()];
},
async read(path) {
const text = files.get(path);
if (text === undefined) throw Error("Missing file");
return text;
},
async write(path, text, expected) {
assert.equal(files.get(path), expected, "Changed on disk");
files.set(path, text);
},
async create(path, text) {
if (files.has(path)) throw Error("Exists");
files.set(path, text);
},
};
return { workspace, files };
}
test("tasks round trip with a stable ID, description, deadline, plan, and sessions", () => {
const note = newTask(
"Launch",
{
...fields,
due: { date: "2026-10-30" },
plan: { start: "2026-10-05", end: "2026-10-31" },
sessions: [
{
id: "session-1",
start: "2026-10-08T16:00:00Z",
end: "2026-10-08T17:00:00Z",
timeZone: "America/Vancouver",
},
],
},
"Keep **Markdown**.\n",
);
const task = readTask(note.path, note.source)!;
const moved = readTask(note.path, editTask(task, { status: "doing" }))!;
assert.equal(moved.id, task.id);
assert.equal(moved.body, task.body);
assert.deepEqual(moved.plan, task.plan);
assert.deepEqual(moved.sessions, task.sessions);
assert.deepEqual(moved.due, task.due);
assert.equal(moved.status, "doing");
});
test("metadata edits preserve unknown properties, comments, and the exact body", () => {
const source =
'\uFEFF---\r\nowner: Ada # keep this\r\nrunestone_pm:\r\n schema: 1\r\n id: stable\r\n kind: task\r\n status: todo # workflow\r\n future: {value: 12}\r\n plan:\r\n start: "2026-10-04"\r\n end: "2026-10-08"\r\n custom: untouched\r\n---\r\n# Body\r\n\r\n- [ ] Keep spacing \r\n';
const original = readTask("Note.md", source)!;
const next = editTask(original, {
status: "doing",
plan: { start: "2026-10-05", end: "2026-10-09" },
});
assert.equal(readTask("Note.md", next)!.body, original.body);
assert.match(next, /owner: Ada # keep this/);
assert.match(next, /# workflow/);
assert.match(next, /future:/);
assert.match(next, /custom: untouched/);
assert.ok(next.startsWith("\uFEFF"));
assert.ok(!/(?<!\r)\n/.test(next));
});
test("adding an existing note preserves its content and does not duplicate its identity", () => {
const source = "---\ncategory: reference\n---\nOriginal body\n";
const adopted = adoptNote("Existing.md", source),
task = readTask("Existing.md", adopted)!;
assert.equal(task.body, "Original body\n");
assert.match(adopted, /category: reference/);
assert.equal(adoptNote("Existing.md", adopted), adopted);
});
test("unsupported metadata and invalid dates are rejected without rewriting a note", () => {
const note = newTask("Task", fields);
assert.throws(
() => readTask(note.path, note.source.replace("schema: 1", "schema: 2")),
/unsupported/,
);
assert.throws(
() =>
readTask(
note.path,
note.source.replace("status: todo", "status: unknown"),
),
/status/,
);
assert.throws(
() =>
editTask(readTask(note.path, note.source)!, {
due: { date: "2026-02-30" },
}),
/deadline/,
);
assert.throws(
() => adoptNote("Bad.md", "---\nkey: 1\nkey: 2\n---\nBody"),
/YAML/,
);
assert.throws(() => newTask("../escape", fields), /name/);
assert.throws(() => newTask("CON", fields), /name/);
assert.throws(
() =>
newTask("Task", {
...fields,
plan: { start: "2026-10-10", end: "2026-10-09" },
}),
/end/,
);
});
test("date arithmetic preserves whole dates over leap years and daylight-saving boundaries", () => {
assert.equal(addDays("2024-02-28", 1), "2024-02-29");
assert.equal(addDays("2026-12-31", 1), "2027-01-01");
assert.equal(dayDistance("2026-03-07", "2026-03-09"), 2);
assert.equal(dayDistance("2026-10-31", "2026-11-02"), 2);
assert.equal(validDate("2026-02-29"), false);
});
test("save and reload use one note, and archive retains its file", async () => {
const { files, workspace } = memory();
const store = new TaskStore(workspace);
await store.create("Launch", fields, "Description");
assert.equal([...files.keys()].filter(isHiddenCard).length, 1);
const original = store.tasks[0];
await store.save(original, { status: "done", archived: true });
const reopened = new TaskStore(workspace);
await reopened.reload();
assert.equal(reopened.tasks[0].id, original.id);
assert.equal(reopened.tasks[0].status, "done");
assert.equal(reopened.tasks[0].archived, true);
assert.equal([...files.keys()].filter(isHiddenCard).length, 1);
});
test("stale edits cannot overwrite external changes", async () => {
const { workspace, files } = memory();
const store = new TaskStore(workspace);
await store.create("Task", fields, "Original");
const original = store.tasks[0];
const external = editTask(original, { body: "External change" });
files.set(original.path, external);
await assert.rejects(store.save(original, { status: "done" }), /changed/);
assert.equal(files.get(original.path), external);
});
test("duplicate identities are excluded and blocked even when introduced after reload", async () => {
const note = newTask("Task", fields);
const { workspace, files } = memory({ [note.path]: note.source });
const store = new TaskStore(workspace);
await store.reload();
const original = store.tasks[0];
files.set("Copy.md", note.source);
await assert.rejects(store.save(original, { status: "done" }), /duplicate/);
assert.equal(store.tasks.length, 0);
assert.match(store.issues[0], /Duplicate/);
assert.equal(files.get(note.path), note.source);
});
test("duplicate card titles preserve both cards and disposal blocks new writes", async () => {
const { workspace, files } = memory();
const store = new TaskStore(workspace);
await store.create("Task", fields, "Original");
const first = store.tasks[0];
await store.create("Task", fields, "Second card");
assert.equal(store.tasks.length, 2);
assert.equal(files.get(first.path), first.source);
assert.equal(new Set(store.tasks.map((task) => task.id)).size, 2);
store.dispose();
await assert.rejects(store.create("Later", fields, ""), /closed/);
});