/** * Cotton Meetings in the object (src/meetings.ts, src/meetings_service.ts): * the queue's shared truth on a recording's clear columns, one transcriber at * a time, and an outside service that ships disabled. Every recording is * invented; nothing is sent anywhere. */ import { env, runInDurableObject } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import { mayDo } from "../src/meetings.ts"; import { assemblyAi, fromAssemblyAi, serviceOf } from "../src/meetings_service.ts"; import { Store } from "../src/store.ts"; import { DATABASE_ID, http, personalObject } from "./personal.ts"; type Doc = Record; const stub = (slug: string) => env.BUSINESS.get(env.BUSINESS.idFromName(slug)); async function act(slug: string, credential: string, name: string, args: Record = {}) { return http>(slug, "POST", "/v1/act", { act: name, args }, { authorization: `Bearer ${credential}` }); } async function row(slug: string, id: string): Promise { const data = await runInDurableObject(stub(slug), (_i, state) => new Store(state.storage.sql, () => new Date()).get("meetings_meeting", id)?.data ?? null); expect(data, `meeting ${id}`).not.toBeNull(); return data!; } /** RULE (voice plan, 17 Nov 2026): a recording asked of any transcriber is the Mac's or the phone's, never an outside service's; one asked of a transcriber is that one's alone. */ describe("who may turn a recording into text", () => { it("lets the Mac and the phone take any, and the service only what was asked of it", () => { expect([mayDo("any", "mac"), mayDo("any", "phone"), mayDo("any", "service")]).toEqual([true, true, false]); expect([mayDo("mac", "phone"), mayDo("phone", "phone"), mayDo("service", "service"), mayDo("", "mac")]).toEqual([false, true, true, true]); }); }); /** RULE (voice plan, 17 Nov 2026): the object holds the queue's one truth, so the Mac and a phone never both take a recording, and a failure says why and keeps the audio. */ describe("the meetings queue", () => { const SLUG = "meetings-queue"; let credential = ""; let owner = ""; const audio = (id: string) => `${DATABASE_ID}/meetings/${id}/full.wav`; it("lists what waits for each transcriber, oldest asked first, for the owner only", async () => { ({ credential, owner } = await personalObject(SLUG, { open: ["meetings_meeting"] })); const made: string[] = []; for (const [take, on, asked] of [["m-late", "any", "2026-11-17T03:00:00.000Z"], ["m-early", "mac", "2026-11-17T01:00:00.000Z"], ["m-phone", "phone", "2026-11-17T00:00:00.000Z"]] as const) { const created = await act(SLUG, credential, "create", { table: "meetings_meeting", data: { take, kind: "meeting", status: "waiting", started_at: asked, transcribe_on: on, asked_at: asked, audio: audio(take), duration_ms: 60_000 } }); expect(created.status, JSON.stringify(created.body)).toBe(200); made.push((created.body.made as { id: string }).id); } expect(made).toEqual(["m-late", "m-early", "m-phone"]); const forMac = await http<{ rows: Doc[] }>(SLUG, "GET", "/v1/meetings/waiting?for=mac", undefined, { authorization: `Bearer ${owner}` }); expect(forMac.status, JSON.stringify(forMac.body)).toBe(200); expect(forMac.body.rows.map((r) => r.id)).toEqual(["m-early", "m-late"]); expect(forMac.body.rows[0]).toEqual({ id: "m-early", kind: "meeting", audio: audio("m-early"), duration_ms: 60_000, asked_at: "2026-11-17T01:00:00.000Z", attempts: 0 }); const forPhone = await http<{ rows: Doc[] }>(SLUG, "GET", "/v1/meetings/waiting?for=phone", undefined, { authorization: `Bearer ${owner}` }); expect(forPhone.body.rows.map((r) => r.id)).toEqual(["m-phone", "m-late"]); expect((await http<{ rows: Doc[] }>(SLUG, "GET", "/v1/meetings/waiting?for=service", undefined, { authorization: `Bearer ${owner}` })).body.rows).toEqual([]); expect((await http(SLUG, "GET", "/v1/meetings/waiting")).status, "the queue is the owner's").toBe(403); }); it("lets one transcriber claim a recording and refuses the next", async () => { const claim = await act(SLUG, owner, "meeting_claim", { id: "m-late", by: "mac" }); expect(claim.status, JSON.stringify(claim.body)).toBe(200); expect(claim.body).toMatchObject({ meeting: "m-late", status: "transcribing", attempts: 1 }); const again = await act(SLUG, owner, "meeting_claim", { id: "m-late", by: "phone" }); expect([again.status, again.body.code]).toEqual([409, "not_waiting"]); const theirs = await act(SLUG, owner, "meeting_claim", { id: "m-phone", by: "mac" }); expect([theirs.status, theirs.body.code]).toEqual([409, "not_yours"]); expect(await row(SLUG, "m-late")).toMatchObject({ status: "transcribing", attempts: 1 }); }); it("takes a failure with its reason, keeping the audio", async () => { expect((await act(SLUG, owner, "meeting_failed", { id: "m-late", why: "it broke" })).body.code).toBe("bad_args"); const failed = await act(SLUG, owner, "meeting_failed", { id: "m-late", why: "engine_failed" }); expect(failed.status, JSON.stringify(failed.body)).toBe(200); expect(await row(SLUG, "m-late")).toMatchObject({ status: "failed", status_why: "engine_failed", audio: audio("m-late") }); expect((await act(SLUG, owner, "meeting_failed", { id: "m-early", why: "engine_failed" })).body.code, "only one being transcribed").toBe("not_transcribing"); }); }); /** RULE (voice plan, 17 Nov 2026): an outside transcription service ships disabled: no setting or no key is no service, and nothing is sent anywhere. */ describe("the outside service", () => { const never = (): Promise => { throw new Error("nothing is sent anywhere"); }; it("is none without the owner's setting and a key", () => { expect(serviceOf(null, { ASSEMBLYAI_API_KEY: "k" }, never)).toBeNull(); expect(serviceOf({ provider: "assemblyai", enabled: false }, { ASSEMBLYAI_API_KEY: "k" }, never)).toBeNull(); expect(serviceOf({ provider: "assemblyai", enabled: true }, {}, never)).toBeNull(); expect(serviceOf({ provider: "elsewhere", enabled: true }, { ASSEMBLYAI_API_KEY: "k" }, never)).toBeNull(); expect(serviceOf({ provider: "assemblyai", enabled: true }, { ASSEMBLYAI_API_KEY: "k" }, never)?.id).toBe("assemblyai"); }); it("maps a provider's utterances into the one transcript, speakers by first speech", () => { const t = fromAssemblyAi({ id: "x", status: "completed", language_code: "en_us", utterances: [ { speaker: "B", start: 2100, end: 4000, text: "Yes, let us." }, { speaker: "A", start: 0, end: 2000, text: "Shall we start? " }, { speaker: "B", start: 5000, end: 5000, text: " " }, ] }); expect(t).toEqual({ by: "service:assemblyai", languages: ["en"], speakers: [{ label: "S1" }, { label: "S2" }], lines: [ { speaker: "S1", start_ms: 0, end_ms: 2000, text: "Shall we start?", language: "en" }, { speaker: "S2", start_ms: 2100, end_ms: 4000, text: "Yes, let us.", language: "en" }, ] }); }); it("runs its adaptor only against a canned provider here", async () => { const asked: string[] = []; const canned = async (url: string): Promise => { asked.push(url.replace("https://api.assemblyai.com/v2", "")); if (url.endsWith("/upload")) return Response.json({ upload_url: "https://canned.invalid/a" }); if (url.endsWith("/transcript")) return Response.json({ id: "j1", status: "processing" }); return Response.json({ id: "j1", status: "completed", language_code: "hi", utterances: [{ speaker: "A", start: 0, end: 900, text: "नमस्ते" }] }); }; const t = await assemblyAi("k", canned, async () => undefined).transcribe(new Uint8Array([1, 2, 3]), "audio/wav", ["hi"]); expect(asked).toEqual(["/upload", "/transcript", "/transcript/j1"]); expect(t.lines).toEqual([{ speaker: "S1", start_ms: 0, end_ms: 900, text: "नमस्ते", language: "hi" }]); }); });