77 lines · 4.6 KB · TypeScriptRaw
/** * `POST /v1/import/message_changes`: the edits, deletes for everyone and * reactions a linked device sees after a message arrived, on the row the * message's own id names — through the front door, with the other import * routes' caller rules (the engine itself is tested in import-engine.test.ts). */import { describe, expect, it } from "vitest";import { CLEAR_IMPORT_OPEN, DATABASE_ID, http, mint, personalObject } from "./personal.ts";type Row = { table: string; id: string; data: Record<string, unknown>; deleted?: boolean };describe("message changes through the front door", () => { const SLUG = "import-changes"; let credential = ""; let owner = ""; const auth = () => ({ authorization: `Bearer ${credential}` }); const at = "2026-10-02T10:00:00.000Z"; const dm = { workspaceId: DATABASE_ID, channel: "whatsapp", dm: { label: "A Friend", phone: "919800000001" }, messages: [ { direction: "in", body: "hello there", sentAt: "2026-10-01T10:00:00.000Z", externalId: "wa-1" }, { direction: "out", body: "hi", sentAt: "2026-10-01T10:01:00.000Z", externalId: "wa-2" }, ], }; const changes = { workspaceId: DATABASE_ID, channel: "whatsapp", changes: [ { kind: "edit", externalId: "wa-1", body: "hello again", at }, { kind: "reaction", externalId: "wa-1", actor: "owner", emoji: "❤️", at }, { kind: "reaction", externalId: "wa-2", actor: "919800000001", emoji: "👍", at }, { kind: "revoke", externalId: "wa-2", at }, { kind: "edit", externalId: "never-read", body: "x", at }, ], }; const live = async (table: string): Promise<Row[]> => { const res = await http<{ rows: Row[] }>(SLUG, "GET", "/v1/sync?since=0", undefined, auth()); return res.body.rows.filter((r) => r.table === table && !r.deleted); }; it("refuses anyone but the owner, and a batch for another database or of the wrong shape", async () => { ({ credential, owner } = await personalObject(SLUG, CLEAR_IMPORT_OPEN)); expect((await http(SLUG, "POST", "/v1/import/message_changes", changes)).status).toBe(403); const reader = await mint(SLUG, credential, "reader", ["read"]); expect((await http(SLUG, "POST", "/v1/import/message_changes", changes, { authorization: `Bearer ${reader}` })).status).toBe(403); const other = await http(SLUG, "POST", "/v1/import/message_changes", { ...changes, workspaceId: "99999999-9999-4999-8999-999999999999" }, auth()); expect(other).toMatchObject({ status: 400, body: { code: "wrong_database" } }); const bad = await http(SLUG, "POST", "/v1/import/message_changes", { ...changes, changes: [{ kind: "pin", externalId: "wa-1", at }] }, auth()); expect(bad).toMatchObject({ status: 400, body: { code: "bad_batch", message: "changes[0].kind is edit, revoke or reaction" } }); const noEmoji = await http(SLUG, "POST", "/v1/import/message_changes", { ...changes, changes: [{ kind: "reaction", externalId: "wa-1", actor: "owner", at }] }, auth()); expect(noEmoji).toMatchObject({ status: 400, body: { code: "bad_batch" } }); }); it("lands each change on its message, counts the ones not held, and changes nothing the second time", async () => { expect((await http(SLUG, "POST", "/v1/import/messages", dm, auth())).status).toBe(200); const res = await http<{ applied: Record<string, number>; version: number }>(SLUG, "POST", "/v1/import/message_changes", changes, { authorization: `Bearer ${owner}` }); expect(res.status, JSON.stringify(res.body)).toBe(200); expect(res.body.applied).toEqual({ edited: 1, revoked: 1, reacted: 2, unreacted: 0, voted: 0, missing: 1 }); const messages = await live("message"); const byExternal = (id: string) => messages.find((m) => m.data.external_id === id)!; expect(byExternal("wa-1").data).toMatchObject({ body: "hello again", edited_at: at }); expect(byExternal("wa-2").data).toMatchObject({ body: "", deleted_at: at }); expect(messages).toHaveLength(2); expect((await live("message_reaction")).map((r) => r.data.emoji).sort()).toEqual(["❤️", "👍"].sort()); const again = await http<{ version: number }>(SLUG, "POST", "/v1/import/message_changes", changes, auth()); expect(again.body.version, "the same changes again move no version").toBe(res.body.version); const off = await http<{ applied: Record<string, number> }>(SLUG, "POST", "/v1/import/message_changes", { ...changes, changes: [{ kind: "reaction", externalId: "wa-1", actor: "owner", emoji: "", at }] }, auth()); expect(off.body.applied.unreacted).toBe(1); expect((await live("message_reaction")).map((r) => r.data.emoji)).toEqual(["👍"]); });});