152 lines · 9.6 KB · TypeScriptRaw
/** * Published databases (src/published.ts): reference rows any signed-in device * reads and only their publisher writes. Synthetic rows and numbers only. */import { describe, expect, it } from "vitest";import { keysOf, searchKey } from "../src/published.ts";import { bearer, open, signUp } from "./calling.ts";type Body = Record<string, unknown>;const COTTON = bearer("test-publisher-token-cotton-0123456789abcdef");const SOMEONE = bearer("test-publisher-token-someone-0123456789abcdef");let numbers = 0;const reader = (): Promise<string> => signUp(`+9198000${String(20000 + (numbers += 1)).padStart(5, "0")}`, `published-phone-${numbers}`);const PLACES = [ { id: "geonames:1", row: { name: "Hyderabad", country: "IN", zone: "Asia/Kolkata" }, names: ["Hyderabad"], lat: 17.38, lng: 78.47, rank: 6_800_000, group: "IN" }, { id: "geonames:2", row: { name: "Hyderabad", country: "PK", zone: "Asia/Karachi" }, names: ["Hyderabad"], lat: 25.39, lng: 68.37, rank: 1_700_000, group: "PK" }, { id: "geonames:3", row: { name: "São Paulo", country: "BR", zone: "America/Sao_Paulo" }, names: ["São Paulo", "Sampa"], lat: -23.55, lng: -46.63, rank: 12_000_000, group: "BR" }, { id: "geonames:4", row: { name: "Secunderabad", country: "IN", zone: "Asia/Kolkata" }, names: ["Secunderabad"], lat: 17.5, lng: 78.5, rank: 200_000, group: "IN" }, { id: "geonames:5", row: { name: "Suva", country: "FJ", zone: "Pacific/Fiji" }, names: ["Suva"], lat: -18.14, lng: 178.44, rank: 90_000, group: "FJ" }, { id: "geonames:7", row: { name: "Gandhisagar", country: "IN", zone: "Asia/Kolkata" }, names: ["Gandhisagar", "Hyderabad Colony"], lat: 17.3, lng: 78.4, rank: 9_000_000, group: "IN" }, { id: "geonames:6", row: { name: "Apia", country: "WS", zone: "Pacific/Apia" }, names: ["Apia"], lat: -13.83, lng: -171.76, rank: 40_000, group: "WS" },];async function publish(name: string, version: string, rows: Body[], token = COTTON, table = "places"): Promise<void> { const base = `/v1/published/cotton/${name}/versions/${version}`; const begun = await open("PUT", base, undefined, token); expect(begun.status, JSON.stringify(begun.body)).toBe(200); const loaded = await open("POST", `${base}/rows`, { table, rows }, token); expect(loaded.status, JSON.stringify(loaded.body)).toBe(200); const committed = await open("POST", `${base}/commit`, undefined, token); expect(committed.status, JSON.stringify(committed.body)).toBe(200);}const search = (credential: string, name: string, body: Body, table = "places") => open<Body & { rows: Body[] }>("POST", `/v1/published/cotton/${name}/${table}/search`, body, bearer(credential));describe("search keys", () => { it("drop case and accents, split on anything that is not a letter or digit, and find a name from each word on", () => { expect(searchKey(" São-Paulo!! ")).toBe("sao paulo"); expect(keysOf(["New York City"])).toEqual(["new york city", "york city", "city"]); });});describe("reading a published database", () => { it("answers a prefix search to a signed-in device, accents ignored, the preferred group first, then by rank", async () => { await publish("cities-a", "2026-11-14", PLACES); const credential = await reader(); const hy = await search(credential, "cities-a", { q: "hyd", prefer: "PK" }); expect(hy.status, JSON.stringify(hy.body)).toBe(200); expect(hy.body).toMatchObject({ publisher: "cotton", name: "cities-a", table: "places", version: "2026-11-14" }); // A row found by its own name comes before one found by another name, however large. expect(hy.body.rows.map((r) => r.id)).toEqual(["geonames:2", "geonames:1", "geonames:7"]); expect(hy.body.rows[1]).toEqual({ id: "geonames:1", name: "Hyderabad", country: "IN", zone: "Asia/Kolkata", lat: 17.38, lng: 78.47 }); const sao = await search(credential, "cities-a", { q: "SAO P" }); expect(sao.body.rows.map((r) => r.id)).toEqual(["geonames:3"]); const alt = await search(credential, "cities-a", { q: "paulo" }); expect(alt.body.rows.map((r) => r.id)).toEqual(["geonames:3"]); const bad = await search(credential, "cities-a", { q: "h" }); expect(bad.status).toBe(400); expect(bad.body.code).toBe("short_query"); }); it("answers the nearest rows to a point, across the date line", async () => { await publish("cities-b", "1", PLACES); const credential = await reader(); const near = await search(credential, "cities-b", { near: { lat: 17.45, lng: 78.49 }, limit: 2 }); expect(near.body.rows.map((r) => r.id)).toEqual(["geonames:4", "geonames:1"]); expect((await search(credential, "cities-b", { near: { lat: 17.31, lng: 78.4 }, limit: 1 })).body.rows.map((r) => r.id)).toEqual(["geonames:7"]); const pacific = await search(credential, "cities-b", { near: { lat: -16, lng: 179.9 }, limit: 1 }); expect(pacific.body.rows.map((r) => r.id)).toEqual(["geonames:5"]); const across = await search(credential, "cities-b", { near: { lat: -14, lng: -179.9 }, limit: 1 }); expect(across.body.rows.map((r) => r.id)).toEqual(["geonames:5"]); }); it("answers rows by id, and an earlier version while it is still held", async () => { await publish("cities-c", "v1", PLACES); await publish("cities-c", "v2", PLACES.map((p) => ({ ...p, row: { ...p.row, name: `${p.row.name} (v2)` } }))); const credential = await reader(); const now = await search(credential, "cities-c", { ids: ["geonames:3", "geonames:1"] }); expect(now.body.version).toBe("v2"); expect(now.body.rows.map((r) => r.name)).toEqual(["Hyderabad (v2)", "São Paulo (v2)"]); const then = await search(credential, "cities-c", { ids: ["geonames:1"], version: "v1" }); expect(then.body).toMatchObject({ version: "v1", rows: [{ id: "geonames:1", name: "Hyderabad" }] }); await publish("cities-c", "v3", PLACES); const gone = await search(credential, "cities-c", { ids: ["geonames:1"], version: "v1" }); expect(gone.status).toBe(410); const about = await open("GET", "/v1/published/cotton/cities-c", undefined, bearer(credential)); expect(about.body).toEqual({ publisher: "cotton", name: "cities-c", version: "v3", tables: { places: PLACES.length } }); }); it("answers the most ids a read allows (100) in one go", async () => { await publish("cities-ids", "1", PLACES); const credential = await reader(); const ids = ["geonames:1", ...Array.from({ length: 99 }, (_, i) => `geonames:none-${i}`)]; const many = await search(credential, "cities-ids", { ids }); expect(many.status).toBe(200); expect(many.body.rows.map((r) => r.id)).toEqual(["geonames:1"]); }); it("refuses a read with no sign-in or a credential no object vouches for, and a search sent as a GET", async () => { await publish("cities-d", "1", PLACES); const none = await open("POST", "/v1/published/cotton/cities-d/places/search", { q: "hyd" }); expect(none.status).toBe(401); const forged = await search("ctd_00000000-0000-4000-8000-000000000000_forged", "cities-d", { q: "hyd" }); expect(forged.status).toBe(401); const credential = await reader(); const got = await open("GET", "/v1/published/cotton/cities-d/places/search?q=hyd", undefined, bearer(credential)); expect(got.status).toBe(404); const missing = await search(credential, "nothing-here", { q: "hyd" }); expect(missing.status).toBe(404); expect(missing.body.code).toBe("not_published"); });});describe("writing a published database", () => { it("refuses every token but the publisher's own", async () => { const credential = await reader(); const base = "/v1/published/cotton/cities-e/versions/1"; for (const headers of [{}, bearer(credential), bearer("test-admin-token"), SOMEONE, bearer("test-publisher-token-cotton-0123456789abcdeX")]) { for (const [method, path, body] of [["PUT", base, undefined], ["POST", `${base}/rows`, { table: "places", rows: PLACES }], ["POST", `${base}/commit`, undefined]] as const) { const res = await open(method, path, body, headers); expect(res.status, `${method} ${path} ${JSON.stringify(headers)}`).toBe(403); expect(res.body.code).toBe("publisher_only"); } } // Another publisher writes its own name and never Cotton's. const theirs = await open("PUT", "/v1/published/someone/their-set/versions/1", undefined, SOMEONE); expect(theirs.status).toBe(200); const ours = await open("PUT", "/v1/published/someone/their-set/versions/2", undefined, COTTON); expect(ours.status).toBe(403); const about = await open("GET", "/v1/published/cotton/cities-e", undefined, bearer(credential)); expect(about.status).toBe(404); }); it("keeps a version loading until its commit, and a committed version is never written again", async () => { const credential = await reader(); const base = "/v1/published/cotton/cities-f/versions/1"; expect((await open("PUT", base, undefined, COTTON)).status).toBe(200); expect((await open("POST", `${base}/rows`, { table: "places", rows: PLACES }, COTTON)).status).toBe(200); expect((await search(credential, "cities-f", { q: "hyd" })).status).toBe(404); expect((await open("POST", `${base}/commit`, undefined, COTTON)).status).toBe(200); expect((await search(credential, "cities-f", { q: "hyd" })).body.rows).toHaveLength(3); const again = await open("PUT", base, undefined, COTTON); expect(again.status).toBe(409); const late = await open("POST", `${base}/rows`, { table: "places", rows: PLACES }, COTTON); expect(late.status).toBe(409); const bad = await open("POST", "/v1/published/cotton/cities-f/versions/2/rows", { table: "places", rows: [{ id: "x", row: {}, lat: 1 }] }, COTTON); expect(bad.status).toBe(400); });});