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.
159 lines
6.5 KiB
TypeScript
159 lines
6.5 KiB
TypeScript
import { afterAll, beforeEach, describe, expect, it } from "vitest";
|
|
import postgres from "postgres";
|
|
import * as repo from "../server/utils/repo";
|
|
|
|
const sql = postgres(process.env.DATABASE_URL!, { max: 1, onnotice: () => {} });
|
|
|
|
beforeEach(async () => {
|
|
await sql`truncate lists cascade`;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await sql.end();
|
|
});
|
|
|
|
describe("lists", () => {
|
|
it("full-replace upserts supplied and deletes missing", async () => {
|
|
await repo.replaceLists(sql, [
|
|
{ id: "a", name: "A" },
|
|
{ id: "b", name: "B" },
|
|
]);
|
|
await repo.replaceLists(sql, [{ id: "a", name: "A2" }]); // b removed, a renamed
|
|
const got = await repo.getLists(sql);
|
|
expect(got).toEqual([{ id: "a", name: "A2" }]);
|
|
});
|
|
|
|
it("empty payload clears all lists", async () => {
|
|
await repo.replaceLists(sql, [{ id: "a", name: "A" }]);
|
|
await repo.replaceLists(sql, []);
|
|
expect(await repo.getLists(sql)).toEqual([]);
|
|
});
|
|
|
|
it("deleting a list cascades its tasks", async () => {
|
|
await repo.replaceLists(sql, [{ id: "a", name: "A" }]);
|
|
await repo.upsertDesktopTask(sql, "t1", { listId: "a", title: "x" });
|
|
await repo.replaceLists(sql, []); // removes a + cascades t1
|
|
expect(await repo.getUnconsumed(sql)).toEqual([]);
|
|
});
|
|
|
|
it("listExists validates listId", async () => {
|
|
await repo.replaceLists(sql, [{ id: "L", name: "List" }]);
|
|
expect(await repo.listExists(sql, "L")).toBe(true);
|
|
expect(await repo.listExists(sql, "ghost")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("tasks", () => {
|
|
beforeEach(async () => {
|
|
await repo.replaceLists(sql, [{ id: "L", name: "List" }]);
|
|
});
|
|
|
|
it("web create returns generated GUID, source=web, consumed=false", async () => {
|
|
const t = await repo.createWebTask(sql, { listId: "L", title: "buy milk", description: null });
|
|
expect(t.id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
|
|
expect(t.source).toBe("web");
|
|
expect(t.consumed).toBe(false);
|
|
expect(t.title).toBe("buy milk");
|
|
});
|
|
|
|
it("desktop upsert is idempotent by id; source=desktop on insert", async () => {
|
|
const a = await repo.upsertDesktopTask(sql, "fixed-id", { listId: "L", title: "one" });
|
|
expect(a.created).toBe(true);
|
|
const b = await repo.upsertDesktopTask(sql, "fixed-id", { listId: "L", title: "two", description: "d" });
|
|
expect(b.created).toBe(false);
|
|
const tasks = await repo.getTasksForList(sql, "L");
|
|
expect(tasks).toHaveLength(1);
|
|
expect(tasks[0].title).toBe("two");
|
|
expect(tasks[0].description).toBe("d");
|
|
expect(tasks[0].source).toBe("desktop");
|
|
});
|
|
|
|
it("getUnconsumed returns only web tasks with consumed=false", async () => {
|
|
await repo.createWebTask(sql, { listId: "L", title: "web1", description: null });
|
|
await repo.upsertDesktopTask(sql, "d1", { listId: "L", title: "desk1" });
|
|
const u = await repo.getUnconsumed(sql);
|
|
expect(u).toHaveLength(1);
|
|
expect(u[0].title).toBe("web1");
|
|
});
|
|
|
|
it("consume sets consumed=true and is reflected in getUnconsumed", async () => {
|
|
const t = await repo.createWebTask(sql, { listId: "L", title: "c", description: null });
|
|
expect(await repo.consume(sql, t.id)).toBe(true);
|
|
expect(await repo.getUnconsumed(sql)).toEqual([]);
|
|
});
|
|
|
|
it("consume on unknown id returns false", async () => {
|
|
expect(await repo.consume(sql, "nope")).toBe(false);
|
|
});
|
|
|
|
it("deleteTask is idempotent", async () => {
|
|
const t = await repo.createWebTask(sql, { listId: "L", title: "c", description: null });
|
|
await repo.deleteTask(sql, t.id);
|
|
await repo.deleteTask(sql, t.id); // no throw on absent
|
|
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 });
|
|
});
|
|
});
|