76 lines
4.1 KiB
TypeScript
76 lines
4.1 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { newTask, readTask, editTask } from "../../../../src/lib/features/pm/storage/notes.ts";
|
|
import { priorities, type TaskFields } from "../../../../src/lib/features/pm/model/model.ts";
|
|
import { renderMarkdown } from "../../../../src/lib/features/pm/model/markdown.ts";
|
|
|
|
const fields: TaskFields = {
|
|
status: "todo",
|
|
tags: [],
|
|
labels: ["high-priority"],
|
|
sessions: [],
|
|
archived: false,
|
|
};
|
|
test("priority round-trips independently of labels and survives unrelated changes", () => {
|
|
for (const priority of [...priorities, null]) {
|
|
const note = newTask("Priority card", { ...fields, priority });
|
|
const task = readTask(note.path, note.source)!;
|
|
assert.equal(task.priority, priority);
|
|
const changed = readTask(
|
|
note.path,
|
|
editTask(task, { status: "doing", listId: "review", body: "Edited" }),
|
|
)!;
|
|
assert.equal(changed.priority, priority);
|
|
assert.deepEqual(changed.labels, fields.labels);
|
|
}
|
|
});
|
|
test("unrelated edits preserve absent priority and explicit removal writes null", () => {
|
|
const note = newTask("Legacy", fields);
|
|
const task = readTask(note.path, note.source)!;
|
|
assert.doesNotMatch(editTask(task, { body: "Edit" }), /priority:/);
|
|
const cleared = editTask(task, { priority: null });
|
|
assert.match(cleared, /priority: null/);
|
|
assert.deepEqual(readTask(note.path, cleared)!.labels, fields.labels);
|
|
assert.throws(
|
|
() => newTask("Invalid", { ...fields, priority: "urgent" as never }),
|
|
/priority/,
|
|
);
|
|
});
|
|
test("Markdown preview escapes HTML and rejects active links and external images", () => {
|
|
const result = renderMarkdown(
|
|
"# Heading\n\n**Bold**\n\n<script>alert(1)</script>\n\n[x](javascript:alert(1))\n\n\n\n\n\n[safe](https://example.com)",
|
|
);
|
|
assert.match(result, /<h1>Heading<\/h1>/);
|
|
assert.match(result, /<strong>Bold<\/strong>/);
|
|
assert.doesNotMatch(result, /<script|src=|href="javascript:/);
|
|
assert.match(result, /data-pm-image="12345678-1234-1234-1234-123456789abc"/);
|
|
assert.match(result, /rel="noopener noreferrer"/);
|
|
});
|
|
|
|
test("covers, subtasks, and card links round-trip without changing other fields", () => {
|
|
const coverImage = "12345678-1234-1234-1234-123456789abc";
|
|
const subtasks = [{ id: "subtask", title: "Check the change", done: true }];
|
|
const links = [{ taskId: "dependency", relation: "blocked-by" as const }];
|
|
const note = newTask("Relationships", { ...fields, coverImage, subtasks, links });
|
|
const task = readTask(note.path, note.source)!;
|
|
assert.deepEqual(task.subtasks, subtasks); assert.deepEqual(task.links, links); assert.equal(task.coverImage, coverImage);
|
|
const edited = readTask(note.path, editTask(task, { body: "Revised", priority: "lowest" }))!;
|
|
assert.deepEqual(edited.subtasks, subtasks); assert.deepEqual(edited.links, links); assert.equal(edited.coverImage, coverImage);
|
|
assert.throws(() => editTask(task, { coverImage: "../outside.png" }), /image/);
|
|
assert.throws(() => editTask(task, { subtasks: [...subtasks, ...subtasks] }), /unique ID/);
|
|
assert.throws(() => editTask(task, { links: [...links, ...links] }), /once/);
|
|
});
|
|
|
|
test("attachments round-trip independently of description edits and reject duplicate or invalid IDs", () => {
|
|
const attachments = [{ id: "12345678-1234-1234-1234-123456789abc", name: "Screenshot.png" }];
|
|
const note = newTask("Attached image", { ...fields, attachments }, "Description");
|
|
const task = readTask(note.path, note.source)!;
|
|
assert.deepEqual(task.attachments, attachments);
|
|
const changed = readTask(note.path, editTask(task, { body: "", status: "doing" }))!;
|
|
assert.deepEqual(changed.attachments, attachments);
|
|
assert.equal(changed.body, "");
|
|
assert.deepEqual(readTask(note.path, editTask(changed, { attachments: [] }))!.attachments, []);
|
|
assert.throws(() => editTask(task, { attachments: [...attachments, ...attachments] }), /unique image ID/);
|
|
assert.throws(() => editTask(task, { attachments: [{ id: "../outside", name: "Image" }] }), /unique image ID/);
|
|
});
|