fix: add PUT /tasks/mirror (array full-replace of desktop Idle backlog)

The desktop pushes its full Idle backlog as a JSON array to /tasks/mirror, not per-task.
Previously /tasks/mirror matched tasks/[id].put.ts (id=mirror) and rejected the array with
400. New static route validates per-element, accepts empty arrays, upserts each as
consumed=true (desktop-owned), deletes consumed=true rows not in the array, and leaves
web-created consumed=false rows untouched. Mirrors PUT /lists.
This commit is contained in:
2026-06-10 09:35:33 +00:00
parent 812048a0b5
commit 65543cb6ee
4 changed files with 146 additions and 6 deletions

View File

@@ -93,3 +93,66 @@ describe("tasks", () => {
expect(await repo.getTasksForList(sql, "L")).toEqual([]);
});
});
describe("mirrorDesktopTasks (desktop Idle full-replace)", () => {
beforeEach(async () => {
await repo.replaceLists(sql, [
{ id: "L", name: "List" },
{ id: "L2", name: "List 2" },
]);
});
it("inserts items as desktop-owned (consumed=true, source=desktop)", async () => {
await repo.mirrorDesktopTasks(sql, [
{ id: "m1", listId: "L", title: "one", description: "d1" },
{ id: "m2", listId: "L2", title: "two" },
]);
const t = await repo.getTasksForList(sql, "L");
expect(t).toHaveLength(1);
expect(t[0]).toMatchObject({ id: "m1", title: "one", description: "d1", source: "desktop", consumed: true });
expect(await repo.getTasksForList(sql, "L2")).toHaveLength(1);
});
it("upserts existing items by id (title/listId/description)", async () => {
await repo.mirrorDesktopTasks(sql, [{ id: "m1", listId: "L", title: "one" }]);
await repo.mirrorDesktopTasks(sql, [{ id: "m1", listId: "L2", title: "renamed", description: "x" }]);
const onL = await repo.getTasksForList(sql, "L");
const onL2 = await repo.getTasksForList(sql, "L2");
expect(onL).toHaveLength(0);
expect(onL2).toHaveLength(1);
expect(onL2[0]).toMatchObject({ title: "renamed", description: "x" });
});
it("deletes desktop tasks whose id is not in the array", async () => {
await repo.mirrorDesktopTasks(sql, [
{ id: "m1", listId: "L", title: "one" },
{ id: "m2", listId: "L", title: "two" },
]);
await repo.mirrorDesktopTasks(sql, [{ id: "m1", listId: "L", title: "one" }]); // m2 dropped
const t = await repo.getTasksForList(sql, "L");
expect(t.map((x) => x.id)).toEqual(["m1"]);
});
it("empty array deletes all desktop tasks", async () => {
await repo.mirrorDesktopTasks(sql, [{ id: "m1", listId: "L", title: "one" }]);
await repo.mirrorDesktopTasks(sql, []);
expect(await repo.getTasksForList(sql, "L")).toEqual([]);
});
it("does NOT touch web-created tasks awaiting pull (consumed=false)", async () => {
const web = await repo.createWebTask(sql, { listId: "L", title: "web", description: null });
await repo.mirrorDesktopTasks(sql, [{ id: "m1", listId: "L", title: "desk" }]);
// web task survives the mirror (and is still unconsumed)
const unconsumed = await repo.getUnconsumed(sql);
expect(unconsumed.map((x) => x.id)).toEqual([web.id]);
expect((await repo.getTasksForList(sql, "L")).map((x) => x.id).sort()).toEqual([web.id, "m1"].sort());
});
it("claims a web task when its id is mirrored (becomes consumed=true)", async () => {
const web = await repo.createWebTask(sql, { listId: "L", title: "web", description: null });
await repo.mirrorDesktopTasks(sql, [{ id: web.id, listId: "L", title: "web (imported)" }]);
expect(await repo.getUnconsumed(sql)).toEqual([]); // no longer awaiting pull
const t = await repo.getTasksForList(sql, "L");
expect(t[0]).toMatchObject({ id: web.id, title: "web (imported)", consumed: true });
});
});