joplin-plugin-gtd-calendar/src/tests/Gtd/matrixMutation.test.ts

113 lines
4.4 KiB
TypeScript

import { moveMatrixItem } from "../../Gtd/matrixMutation";
import { MatrixDestination, MutationAdapter, RawNote, RawTag } from "../../Gtd/types";
function note(isTodo: boolean): RawNote {
return {
id: "card", title: "Card", parent_id: "folder", is_todo: isTodo ? 1 : 0,
todo_due: 123, todo_completed: 0, updated_time: 456, body: "```gtd\n```",
};
}
function fakeAdapter(current: RawNote | null, attached: RawTag[], all?: RawTag[]) {
const calls: string[] = [];
const adapter: MutationAdapter = {
getNote: async () => current,
getNoteTags: async () => attached.slice(),
getAllTags: async () => (all || [
{ id: "u", title: "urgent" }, { id: "i", title: "important" },
]).slice(),
createTag: async (title) => { calls.push(`create:${title}`); return { id: title, title }; },
attachTag: async (tagId) => { calls.push(`attach:${tagId}`); },
detachTag: async (tagId) => { calls.push(`detach:${tagId}`); },
setTodoCompleted: async () => { throw new Error("matrix must not patch completion"); },
};
return { adapter, calls };
}
const states: Array<{
destination: MatrixDestination;
urgent: boolean;
important: boolean;
}> = [
{ destination: "topLeft", urgent: true, important: true },
{ destination: "topRight", urgent: false, important: true },
{ destination: "bottomLeft", urgent: true, important: false },
{ destination: "bottomRight", urgent: false, important: false },
];
const config = { urgentTag: "urgent", importantTag: "important" };
describe("moveMatrixItem", () => {
for (const isTodo of [false, true]) {
for (const source of states) {
for (const target of states) {
test(`${isTodo ? "to-do" : "note"}: ${source.destination} -> ${target.destination}`, async () => {
const tags: RawTag[] = [];
if (source.urgent) tags.push({ id: "u", title: "urgent" });
if (source.important) tags.push({ id: "i", title: "important" });
const { adapter, calls } = fakeAdapter(note(isTodo), tags);
const result = await moveMatrixItem(adapter, "card", target.destination, config);
const expected: string[] = [];
if (source.urgent !== target.urgent) expected.push(`${target.urgent ? "attach" : "detach"}:u`);
if (source.important !== target.important) expected.push(`${target.important ? "attach" : "detach"}:i`);
expect(calls).toEqual(expected);
expect(result.changed).toBe(source.destination !== target.destination);
expect(result.note).toMatchObject({
is_todo: isTodo ? 1 : 0, todo_due: 123, todo_completed: 0,
});
});
}
}
}
test("uses custom tag names, existing case variants, and deterministic duplicates", async () => {
const { adapter, calls } = fakeAdapter(note(false), [
{ id: "z", title: "FIRE" }, { id: "a", title: "fire" },
{ id: "keep", title: "keep-me" },
], [{ id: "m", title: "MATTERS" }]);
await moveMatrixItem(adapter, "card", "topRight", {
urgentTag: "fire", importantTag: "matters",
});
expect(calls).toEqual(["detach:a", "detach:z", "attach:m"]);
});
test("creates only a missing required tag", async () => {
const { adapter, calls } = fakeAdapter(note(true), [], []);
await moveMatrixItem(adapter, "card", "bottomLeft", config);
expect(calls).toEqual(["create:urgent", "attach:urgent"]);
});
test("fresh validation can reject completed or newly filtered cards before writes", async () => {
for (const current of [note(true), note(false)]) {
current.todo_completed = current.is_todo ? 100 : 0;
const { adapter, calls } = fakeAdapter(current, []);
await expect(moveMatrixItem(adapter, "card", "topLeft", config, () => {
throw new Error("card is no longer eligible");
})).rejects.toThrow("no longer eligible");
expect(calls).toEqual([]);
}
});
test("surfaces partial failures without compensating writes", async () => {
const { adapter, calls } = fakeAdapter(note(false), [], [
{ id: "u", title: "urgent" }, { id: "i", title: "important" },
]);
adapter.attachTag = async (tagId) => {
calls.push(`attach:${tagId}`);
if (tagId === "i") throw new Error("important attach failed");
};
await expect(moveMatrixItem(adapter, "card", "topLeft", config)).rejects.toThrow(
"important attach failed"
);
expect(calls).toEqual(["attach:u", "attach:i"]);
});
test("fails before writes when the card disappeared", async () => {
const { adapter, calls } = fakeAdapter(null, []);
await expect(moveMatrixItem(adapter, "card", "topLeft", config)).rejects.toThrow(
"no longer exists"
);
expect(calls).toEqual([]);
});
});