69 lines · 4.4 KB · TypeScriptRaw
import { SELF } from "cloudflare:test";import { describe, expect, it } from "vitest";import { originalKeyOf, type PhotoAsset } from "../src/import/photos.ts";import { ADMIN, CLEAR_IMPORT_OPEN, http, OWNER_PHONE, PERSONAL_TABLES, PERSONAL_TOOLS, openTables, PEOPLE_OPEN, PHOTOS_OPEN } from "./personal.ts";/** * The Mac lane's fixtures, written from its Rust tests (crates/cotton-client/fixtures/import): the bodies the Mac * sends, exactly as serde serialises them. Each lands through its route on an object whose database is the * fixtures' own, and a second send changes nothing. */const FIXTURES = import.meta.glob("../../../crates/cotton-client/fixtures/import/*.json", { eager: true, import: "default" }) as Record<string, Record<string, unknown>>;const read = (name: string): Record<string, unknown> => Object.entries(FIXTURES).find(([path]) => path.endsWith(`/${name}.json`))![1];const MESSAGES = read("messages");const CALLS = read("calls");const PHOTOS = read("photos");const DATABASE = MESSAGES.workspaceId as string;describe("the Mac's fixtures", () => { const SLUG = "import-fixtures"; let auth: Record<string, string> = {}; it("are bodies for one database", async () => { expect(DATABASE).toMatch(/^[0-9a-f-]{36}$/); expect((await http(SLUG, "PUT", "/v1/admin/business", { name: "Personal", kind: "personal", owner_phone: OWNER_PHONE, database_id: DATABASE }, ADMIN)).status).toBe(200); expect((await http(SLUG, "PUT", "/v1/admin/definitions", { tools: PERSONAL_TOOLS, tables: PERSONAL_TABLES, flavour: {} }, ADMIN)).status).toBe(200); const code = await http<{ challenge_id: string }>(SLUG, "POST", "/v1/code", { phone: OWNER_PHONE, device_id: "mac" }); const signed = await http<{ credential: string }>(SLUG, "POST", "/v1/sign-in", { challenge_id: code.body.challenge_id, code: "123456", device_id: "mac" }); auth = { authorization: `Bearer ${signed.body.credential}` }; // The old message and call routes are refused on a sealed database (ROUTES-AUDIT 11): their tables are opened. await openTables(SLUG, CLEAR_IMPORT_OPEN.open); }); it("messages.json lands, and lands once", async () => { const first = await http<{ applied: Record<string, number>; threadId: string }>(SLUG, "POST", "/v1/import/messages", MESSAGES, auth); expect(first.status, JSON.stringify(first.body)).toBe(200); expect(first.body.applied.messages).toBe((MESSAGES.messages as unknown[]).length); expect(first.body.applied.threads).toBe(1); const again = await http<{ applied: Record<string, number>; threadId: string }>(SLUG, "POST", "/v1/import/messages", MESSAGES, auth); expect(again.body.applied).toEqual({ messages: 0, threads: 0, people: 0, reactions: 0 }); expect(again.body.threadId).toBe(first.body.threadId); }); it("calls.json lands, and lands once", async () => { const first = await http<{ applied: Record<string, number> }>(SLUG, "POST", "/v1/import/calls", CALLS, auth); expect(first.status, JSON.stringify(first.body)).toBe(200); expect(first.body.applied.calls).toBe((CALLS.calls as unknown[]).length); const again = await http<{ applied: Record<string, number> }>(SLUG, "POST", "/v1/import/calls", CALLS, auth); expect(again.body.applied.calls).toBe(0); }); it("photos.json lands once its originals are in R2, and lands once", async () => { // The route is the older Mac's, over People and Photos open (the Mac decides the library itself now, crates/cotton-mac photos.rs). await openTables(SLUG, [...PEOPLE_OPEN.open, ...PHOTOS_OPEN.open]); for (const asset of PHOTOS.assets as PhotoAsset[]) { const key = originalKeyOf(DATABASE, asset); if (!key) continue; const put = await SELF.fetch(`https://example.test/b/${SLUG}/v1/media/${key}`, { method: "PUT", headers: { ...auth, "content-type": "application/octet-stream" }, body: new Uint8Array([1, 2, 3, 4]) }); expect(put.status, await put.clone().text()).toBeLessThan(300); } const first = await http<{ applied: { photos: number; videos: number } }>(SLUG, "POST", "/v1/import/photos", PHOTOS, auth); expect(first.status, JSON.stringify(first.body)).toBe(200); expect(first.body.applied.photos + first.body.applied.videos).toBeGreaterThan(0); const again = await http<{ applied: { photos: number; videos: number } }>(SLUG, "POST", "/v1/import/photos", PHOTOS, auth); expect(again.status).toBe(200); expect(again.body.applied.photos + again.body.applied.videos).toBe(0); });});