461 lines · 26.8 KB · TypeScriptRaw
/** * Cotton Calling's group calls and Android rings (12 Nov 2026): several * people rung at once, a third added to a live one-to-one (which moves it to * the SFU), members leaving, mute and speaking, the opaque e2ee relay, and * FCM beside APNs. Every number is synthetic; the SFU, APNs and FCM are fakes. */import { runInDurableObject } from "cloudflare:test";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { forgetAccessToken } from "../src/calling/fcm.ts";import { acquaint, bearer, call, deadline, fakes, join, kept, open, register, registerAndroid, room, rung, signUp, socket, type Fakes, type Phone } from "./calling.ts";vi.setConfig({ testTimeout: 30_000 });let world: Fakes;let n = 0;const number = (): string => `+91900000${String(9500 + ++n).padStart(4, "0")}`;const token = (): string => (++n).toString(16).padStart(64, "c");const fcmToken = (): string => `synthetic-fcm:${String(++n).padStart(40, "f")}`;beforeEach(() => { world = fakes(); forgetAccessToken();});afterEach(() => { vi.restoreAllMocks();});interface Person { number: string; credential: string; token: string;}/** Someone with Cotton and one iPhone registered for rings; `knows`: a caller they accepted (only they ring them, src/chat/gate.ts). */async function person(device = "phone", knows?: Person): Promise<Person> { const phone = number(); const credential = await signUp(phone, device); const t = token(); await register(credential, t); if (knows) await acquaint(credential, knows.number); return { number: phone, credential, token: t };}/** A rung member's phone, joined and accepted: its socket. */async function answer(who: Person, roomId: string): Promise<Phone> { const joined = await join(who.credential, roomId); expect(joined.status, JSON.stringify(joined.body)).toBe(200); const phone = await socket(joined.body.credential); await phone.next({ t: "state", state: "ringing_in" }); phone.send({ t: "accept" }); return phone;}/** Frames of a kind about a member. */const about = (phone: Phone, member: string): unknown[] => phone.frames.filter((f) => f.t === "member" && (f.member as { id: string }).id === member).map((f) => (f.member as { state: string }).state);describe("a call to three people at once", () => { it("goes through the SFU: one declines, one never answers, one joins, and the call goes on", async () => { const a = await person(); const [b, c, d] = [await person("phone", a), await person("phone", a), await person("phone", a)]; const started = await call(a.credential, [b.number, c.number, d.number], true); expect(started.members).toEqual([ { id: "m1", number: b.number }, { id: "m2", number: c.number }, { id: "m3", number: d.number }, ]); const caller = await socket(started.credential); await caller.next({ t: "mode", mode: "sfu" }); const roster = await caller.next({ t: "roster" }); expect((roster.members as { id: string; state: string }[]).map((m) => [m.id, m.state])).toEqual([["caller", "joined"], ["m1", "invited"], ["m2", "invited"], ["m3", "invited"]]); await rung(world, started.room, 3); expect(world.ringsFor(started.room).map((p) => p.token).sort()).toEqual([b.token, c.token, d.token].sort()); // B declines from its lock screen without joining. expect((await open("POST", `/v1/calls/${started.room}/decline`, {}, bearer(b.credential))).status).toBe(200); await vi.waitFor(() => expect(about(caller, "m1")).toEqual(["declined"])); // D joins: the caller's call is live, D is told the mode and who is here. const dPhone = await answer(d, started.room); await caller.next({ t: "state", state: "live" }); await vi.waitFor(() => expect(about(caller, "m3")).toEqual(["joined"])); await dPhone.next({ t: "mode", mode: "sfu" }); const seen = await dPhone.next({ t: "roster" }); expect((seen.members as { id: string; state: string }[]).map((m) => [m.id, m.state])).toEqual([["caller", "joined"], ["m1", "declined"], ["m2", "invited"], ["m3", "joined"]]); // C never answers: no answer at 45 s, a missed stop push, and the call goes on. await deadline(started.room); await vi.waitFor(() => expect(about(caller, "m2")).toEqual(["no_answer"])); await vi.waitFor(() => expect(world.stopsFor(started.room).map((p) => [p.token, p.body.stop])).toEqual([[c.token, "missed"]])); expect(caller.closed).toBe(false); // The last two leave: the call ends. dPhone.send({ t: "leave" }); expect(await dPhone.ended()).toBe("hung_up"); expect(await caller.ended()).toBe("remote_hung_up"); expect(await kept(started.room)).toBe(0); }); it("ends with no answer for the caller when nobody joins, and cancels the rings when the caller leaves", async () => { const a = await person(); const [b, c] = [await person("phone", a), await person("phone", a)]; const started = await call(a.credential, [b.number, c.number]); const caller = await socket(started.credential); await rung(world, started.room, 2); caller.send({ t: "leave" }); expect(await caller.ended()).toBe("cancelled"); await vi.waitFor(() => expect(world.stopsFor(started.room).map((p) => p.body.stop)).toEqual(["cancelled", "cancelled"])); const again = await call(a.credential, [b.number, c.number]); const waiting = await socket(again.credential); await rung(world, again.room, 2); await deadline(again.room); expect(await waiting.ended()).toBe("no_answer"); });});describe("a third person added to a live one-to-one", () => { it("moves the call to the SFU, where the members push, pull and renegotiate their tracks", async () => { const a = await person(); const [b, c] = [await person("phone", a), await person("phone", a)]; const started = await call(a.credential, b.number); const caller = await socket(started.credential); await rung(world, started.room); const bPhone = await answer(b, started.room); await caller.next({ t: "state", state: "connecting" }); bPhone.send({ t: "media", state: "connected" }); await caller.next({ t: "state", state: "live" }); expect(caller.frames.some((f) => f.t === "mode")).toBe(false); const invited = await open<{ members: { id: string; number: string }[] }>("POST", `/v1/calls/${started.room}/invite`, { to: c.number }, bearer(a.credential)); expect(invited.status, JSON.stringify(invited.body)).toBe(200); expect(invited.body.members).toEqual([{ id: "m2", number: c.number }]); await caller.next({ t: "mode", mode: "sfu" }); await bPhone.next({ t: "mode", mode: "sfu" }); await vi.waitFor(() => expect(about(bPhone, "m2")).toEqual(["invited"])); const [ring] = await rung(world, started.room, 2); expect(ring).toBeDefined(); // C is shown who invited it. const joined = await join(c.credential, started.room); expect(joined.body).toMatchObject({ member: "m2", from: a.number }); const cPhone = await socket(joined.body.credential); cPhone.send({ t: "accept" }); await cPhone.next({ t: "mode", mode: "sfu" }); await vi.waitFor(() => expect(about(caller, "m2")).toEqual(["invited", "joined"])); // The caller pushes its tracks; the others hear of them. caller.send({ t: "sfu", op: "push", sdp: "v=0 synthetic-push", tracks: [{ mid: "0", name: "caller-audio", kind: "audio" }, { mid: "1", name: "caller-video", kind: "video" }] }); expect(await caller.next({ t: "sfu", op: "answer" })).toEqual({ t: "sfu", op: "answer", sdp: "answer-for-sfu-session-1" }); const tracks = { t: "tracks", member: "caller", tracks: [{ name: "caller-audio", kind: "audio" }, { name: "caller-video", kind: "video" }] }; expect(await bPhone.next({ t: "tracks" })).toEqual(tracks); expect(await cPhone.next({ t: "tracks" })).toEqual(tracks); // C pushes its own, pulls the caller's, and answers the SFU's offer. cPhone.send({ t: "sfu", op: "push", sdp: "v=0 synthetic-push-c", tracks: [{ mid: "0", name: "audio-0", kind: "audio" }] }); expect(await cPhone.next({ t: "sfu", op: "answer" })).toEqual({ t: "sfu", op: "answer", sdp: "answer-for-sfu-session-2" }); cPhone.send({ t: "sfu", op: "pull", tracks: [{ member: "caller", name: "caller-audio" }, { member: "caller", name: "caller-video" }, { member: "caller", name: "not-a-track" }] }); // The offer says whose each track is, by its mid: one person's voice stays one track. expect(await cPhone.next({ t: "sfu", op: "offer" })).toEqual({ t: "sfu", op: "offer", sdp: "offer-for-sfu-session-2", tracks: [{ mid: "100", member: "caller", name: "caller-audio" }, { mid: "101", member: "caller", name: "caller-video" }] }); cPhone.send({ t: "sfu", op: "renegotiate", sdp: "v=0 synthetic-renegotiate" }); cPhone.send({ t: "sfu", op: "close", mids: ["0"] }); await vi.waitFor(() => expect(world.sfuCalls.map((x) => `${x.method} ${x.path}`)).toEqual([ "POST /sessions/new", "POST /sessions/sfu-session-1/tracks/new", "POST /sessions/new", "POST /sessions/sfu-session-2/tracks/new", "POST /sessions/sfu-session-2/tracks/new", "PUT /sessions/sfu-session-2/renegotiate", "PUT /sessions/sfu-session-2/tracks/close", ])); expect(world.sfuCalls.every((x) => x.authorization === "Bearer synthetic-sfu-secret")).toBe(true); expect(world.sfuCalls[1]!.body).toEqual({ sessionDescription: { type: "offer", sdp: "v=0 synthetic-push" }, tracks: [{ location: "local", mid: "0", trackName: "caller-audio" }, { location: "local", mid: "1", trackName: "caller-video" }] }); expect(world.sfuCalls[4]!.body).toEqual({ tracks: [{ location: "remote", sessionId: "sfu-session-1", trackName: "caller-audio" }, { location: "remote", sessionId: "sfu-session-1", trackName: "caller-video" }] }); expect(world.sfuCalls[5]!.body).toEqual({ sessionDescription: { type: "answer", sdp: "v=0 synthetic-renegotiate" } }); expect(world.sfuCalls[6]!.body).toEqual({ tracks: [{ mid: "0" }], force: false }); // B, joining, sends its pull and its push at once, the pull first (as an emulator on dev did): one session is // made for it, the push goes first, and the offer of the caller's track is that session's, after it. const before = world.sfuCalls.length; world.sfuDelayMs = 200; bPhone.send({ t: "sfu", op: "pull", tracks: [{ member: "caller", name: "caller-audio" }] }); bPhone.send({ t: "sfu", op: "push", sdp: "v=0 synthetic-push-b", tracks: [{ mid: "0", name: "audio-0", kind: "audio" }] }); expect(await bPhone.next({ t: "sfu", op: "answer" })).toEqual({ t: "sfu", op: "answer", sdp: "answer-for-sfu-session-3" }); expect(await bPhone.next({ t: "sfu", op: "offer" })).toMatchObject({ t: "sfu", op: "offer", sdp: "offer-for-sfu-session-3" }); expect(world.sfuCalls.slice(before).map((x) => `${x.method} ${x.path}`)).toEqual([ "POST /sessions/new", "POST /sessions/sfu-session-3/tracks/new", "POST /sessions/sfu-session-3/tracks/new", ]); world.sfuDelayMs = 0; // B asks for more before answering that offer: Cloudflare would refuse it (406), so the room holds it until B's // answer goes, and then pulls only what B's session lacks. const asked = world.sfuCalls.length; bPhone.send({ t: "sfu", op: "pull", tracks: [{ member: "caller", name: "caller-audio" }, { member: "caller", name: "caller-video" }, { member: "caller", name: "caller-video" }] }); await new Promise((done) => setTimeout(done, 500)); expect(world.sfuCalls.length).toBe(asked); bPhone.send({ t: "sfu", op: "renegotiate", sdp: "v=0 synthetic-answer-b" }); await bPhone.next({ t: "sfu", op: "offer", tracks: [{ mid: "100", member: "caller", name: "caller-video" }] }); expect(world.sfuCalls.slice(asked).map((x) => `${x.method} ${x.path}`)).toEqual(["PUT /sessions/sfu-session-3/renegotiate", "POST /sessions/sfu-session-3/tracks/new"]); expect(world.sfuCalls[asked + 1]!.body).toEqual({ tracks: [{ location: "remote", sessionId: "sfu-session-1", trackName: "caller-video" }] }); // An SFU operation that fails is said to that phone: which one, and the SFU's words. world.sfuRefuse = "/renegotiate"; bPhone.send({ t: "sfu", op: "renegotiate", sdp: "v=0 synthetic-renegotiate-b" }); const failed = await bPhone.next({ t: "error", code: "sfu_failed" }); expect(failed.op).toBe("renegotiate"); expect(String(failed.detail)).toContain("the SFU answered 400 to PUT /sessions/sfu-session-3/renegotiate"); world.sfuRefuse = null; // A member leaves and the call goes on; it ends when the last two leave. bPhone.send({ t: "leave" }); expect(await bPhone.ended()).toBe("hung_up"); await vi.waitFor(() => expect(about(caller, "m1")).toContain("left")); await vi.waitFor(() => expect(about(cPhone, "m1")).toContain("left")); expect(caller.closed || cPhone.closed).toBe(false); cPhone.send({ t: "end" }); expect(await cPhone.ended()).toBe("hung_up"); expect(await caller.ended()).toBe("remote_hung_up"); }); it("is refused to a phone not in the call", async () => { const a = await person(); const b = await person("phone", a); const started = await call(a.credential, b.number); const stranger = await person(); const res = await open<{ code: string }>("POST", `/v1/calls/${started.room}/invite`, { to: number() }, bearer(stranger.credential)); expect(res.status).toBe(404); expect(res.body.code).toBe("not_in_call"); });});describe("what the members tell each other", () => { async function group(): Promise<{ caller: Phone; bPhone: Phone; cPhone: Phone; roomId: string }> { const a = await person(); const [b, c] = [await person("phone", a), await person("phone", a)]; const started = await call(a.credential, [b.number, c.number]); const caller = await socket(started.credential); await rung(world, started.room, 2); const bPhone = await answer(b, started.room); const cPhone = await answer(c, started.room); await vi.waitFor(() => expect(about(caller, "m2")).toEqual(["joined"])); return { caller, bPhone, cPhone, roomId: started.room }; } it("relays mute to everyone and speaking at most four times a second", async () => { const { caller, bPhone, cPhone } = await group(); bPhone.send({ t: "mute", audio: false, video: false }); const muted = await caller.next({ t: "member", member: { id: "m1", number: (caller.frames.find((f) => f.t === "roster")!.members as { number: string }[])[1]!.number, state: "joined", audio: false, video: false, speaking: false, notes: false, can: [] } }); expect(muted).toBeDefined(); for (let i = 0; i < 6; i += 1) bPhone.send({ t: "speaking", on: i % 2 === 0 }); bPhone.send({ t: "ping" }); await bPhone.next({ t: "pong" }); await vi.waitFor(() => expect(cPhone.frames.filter((f) => f.t === "speaking").length).toBe(4)); expect(caller.frames.filter((f) => f.t === "speaking")).toEqual([ { t: "speaking", member: "m1", on: true }, { t: "speaking", member: "m1", on: false }, { t: "speaking", member: "m1", on: true }, { t: "speaking", member: "m1", on: false }, ]); expect(bPhone.frames.some((f) => f.t === "speaking")).toBe(false); }); it("relays e2ee frames, to one member or to all, and keeps none of them", async () => { const { caller, bPhone, cPhone, roomId } = await group(); bPhone.send({ t: "hello", e2ee: ["sframe-v1"] }); expect(await caller.next({ t: "hello" })).toEqual({ t: "hello", e2ee: ["sframe-v1"], from: "m1" }); bPhone.send({ t: "e2ee", epoch: 1, data: "opaque-synthetic-blob-all" }); expect(await caller.next({ t: "e2ee" })).toEqual({ t: "e2ee", from: "m1", epoch: 1, data: "opaque-synthetic-blob-all" }); expect(await cPhone.next({ t: "e2ee" })).toEqual({ t: "e2ee", from: "m1", epoch: 1, data: "opaque-synthetic-blob-all" }); bPhone.send({ t: "e2ee", to: "caller", epoch: 2, data: "opaque-synthetic-blob-one" }); await caller.next({ t: "e2ee", epoch: 2 }); bPhone.send({ t: "ping" }); await bPhone.next({ t: "pong" }); expect(cPhone.frames.filter((f) => f.t === "e2ee").length).toBe(1); const stored = await runInDurableObject(room(roomId), async (_instance, state) => JSON.stringify([...(await state.storage.list()).values()])); expect(stored).not.toContain("opaque-synthetic-blob"); });});describe("Android phones", () => { it("are rung and stopped by FCM data messages, the service account traded for one access token", async () => { const a = await person(); const bNumber = number(); const b = await signUp(bNumber, "pixel"); await acquaint(b, a.number); const t = fcmToken(); await registerAndroid(b, t); const first = await call(a.credential, bNumber, true); const [ring] = await rung(world, first.room); expect(ring!.host).toBe("fcm.test"); expect(ring!.message).toEqual({ token: t, data: { kind: "ring", room: first.room, video: "1" }, android: { priority: "HIGH", ttl: "45s" } }); expect(ring!.verified).toBe(true); expect(world.fcmTokens).toEqual([{ verified: true, claims: expect.objectContaining({ iss: "calling-test@synthetic-calling.iam.gserviceaccount.test", scope: "https://www.googleapis.com/auth/firebase.messaging", aud: "https://oauth2.googleapis.com/token" }) }]); const caller = await socket(first.credential); caller.send({ t: "end" }); await vi.waitFor(() => expect(world.stopsFor(first.room).map((p) => p.message)).toEqual([{ token: t, data: { kind: "stop", room: first.room, stop: "cancelled" }, android: { priority: "HIGH", ttl: "45s" } }])); // The access token is reused, not traded again. expect(world.fcmTokens.length).toBe(1); }); it("drop a token FCM says is unregistered", async () => { const a = await person(); const bNumber = number(); const b = await signUp(bNumber, "pixel"); await acquaint(b, a.number); const t = fcmToken(); await registerAndroid(b, t); world.refuse.set(t, { status: 404, reason: "UNREGISTERED" }); const first = await call(a.credential, bNumber); await rung(world, first.room); world.refuse.delete(t); const next = await call(a.credential, bNumber); await new Promise((resolve) => setTimeout(resolve, 150)); expect(world.ringsFor(next.room)).toEqual([]); }); it("ring beside an iPhone on the same number, and either may answer", async () => { const a = await person(); const bNumber = number(); const iphone = await signUp(bNumber, "iphone"); await acquaint(iphone, a.number); const pixel = await signUp(bNumber, "pixel"); const t1 = token(); const t2 = fcmToken(); await register(iphone, t1); await registerAndroid(pixel, t2); const started = await call(a.credential, bNumber); const caller = await socket(started.credential); const rings = await rung(world, started.room, 2); expect(rings.map((p) => [p.host, p.token]).sort()).toEqual([["apns.test", t1], ["fcm.test", t2]]); const phone = await answer({ number: bNumber, credential: pixel, token: t2 }, started.room); await caller.next({ t: "state", state: "connecting" }); await phone.next({ t: "state", state: "connecting" }); await vi.waitFor(() => expect(world.stopsFor(started.room).map((p) => [p.host, p.body.stop])).toEqual([["apns.test", "answered_elsewhere"]])); }); it("is refused a token that is not one, or no platform", async () => { const a = await signUp(number(), "pixel"); expect((await open<{ code: string }>("POST", "/v1/calls/device", { platform: "android", token: "short" }, bearer(a))).body.code).toBe("bad_token"); expect((await open<{ code: string }>("POST", "/v1/calls/device", { token: fcmToken() }, bearer(a))).body.code).toBe("bad_platform"); });});describe("a group's call", () => { const GROUP = "0123456789abcdef0123456789abcdef"; it("is the SFU's even with one other member, and its ring and join name the group", async () => { const a = await person(); const b = await person("phone", a); const res = await open<{ mode: string; group: string; room: string; credential: string }>("POST", "/v1/calls", { to: [b.number], group: GROUP }, bearer(a.credential)); expect(res.status, JSON.stringify(res.body)).toBe(200); expect(res.body).toMatchObject({ mode: "sfu", group: GROUP }); const caller = await socket(res.body.credential); await caller.next({ t: "mode", mode: "sfu" }); await rung(world, res.body.room); const joined = await join(b.credential, res.body.room); expect(joined.body).toMatchObject({ member: "m1", group: GROUP }); }); it("refuses a group id that is not one", async () => { const a = await person(); const b = await person("phone", a); const res = await open<{ code: string }>("POST", "/v1/calls", { to: [b.number], group: "not-a-group" }, bearer(a.credential)); expect(res.status).toBe(400); expect(res.body.code).toBe("bad_group"); }); it("lets a member come in late, and back after leaving; nobody else; and says whether it goes on", async () => { const a = await person(); const [b, c] = [await person("phone", a), await person("phone", a)]; const started = await open<{ room: string; credential: string }>("POST", "/v1/calls", { to: [b.number, c.number], group: GROUP }, bearer(a.credential)); const roomId = started.body.room; const caller = await socket(started.body.credential); await rung(world, roomId, 2); const bPhone = await answer(b, roomId); await caller.next({ t: "state", state: "live" }); // C declines its ring, then thinks again: in through enter, accepted on its socket. expect((await open("POST", `/v1/calls/${roomId}/decline`, {}, bearer(c.credential))).status).toBe(200); await vi.waitFor(() => expect(about(caller, "m2")).toEqual(["declined"])); const status = await open<{ live: boolean; joined: string[]; you: string; group: string }>("GET", `/v1/calls/${roomId}/status`, undefined, bearer(c.credential)); expect(status.body).toEqual({ live: true, video: false, group: GROUP, joined: [a.number, b.number], you: "declined" }); const entered = await open<{ member: string; credential: string; mode: string; from: string }>("POST", `/v1/calls/${roomId}/enter`, {}, bearer(c.credential)); expect(entered.status, JSON.stringify(entered.body)).toBe(200); expect(entered.body).toMatchObject({ member: "m2", mode: "sfu", from: a.number }); const cPhone = await socket(entered.body.credential); await cPhone.next({ t: "state", state: "ringing_in" }); cPhone.send({ t: "accept" }); await vi.waitFor(() => expect(about(caller, "m2")).toEqual(["declined", "joined"])); // A stranger is no member: nothing to enter, nothing to read. const stranger = await person(); expect((await open<{ code: string }>("POST", `/v1/calls/${roomId}/enter`, {}, bearer(stranger.credential))).body.code).toBe("no_call"); expect((await open("GET", `/v1/calls/${roomId}/status`, undefined, bearer(stranger.credential))).body).toEqual({ live: false }); // B in the call already: its other phone may not come in beside it. const bTablet = await signUp(b.number, "tablet"); expect((await open<{ code: string }>("POST", `/v1/calls/${roomId}/enter`, {}, bearer(bTablet))).body.code).toBe("in_call_elsewhere"); // B leaves and comes back. bPhone.send({ t: "leave" }); expect(await bPhone.ended()).toBe("hung_up"); await vi.waitFor(() => expect(about(caller, "m1")).toContain("left")); const again = await open<{ credential: string }>("POST", `/v1/calls/${roomId}/enter`, {}, bearer(b.credential)); expect(again.status).toBe(200); const bBack = await socket(again.body.credential); await bBack.next({ t: "state", state: "ringing_in" }); bBack.send({ t: "accept" }); await vi.waitFor(() => expect(about(caller, "m1").slice(-1)).toEqual(["joined"])); // Everyone leaves: over, and enter says so. bBack.send({ t: "leave" }); cPhone.send({ t: "leave" }); expect(await caller.ended()).toBe("remote_hung_up"); expect((await open<{ code: string }>("POST", `/v1/calls/${roomId}/enter`, {}, bearer(b.credential))).body.code).toBe("no_call"); expect((await open("GET", `/v1/calls/${roomId}/status`, undefined, bearer(b.credential))).body).toEqual({ live: false }); }); it("is no way into a one-to-one call", async () => { const a = await person(); const b = await person("phone", a); const started = await call(a.credential, b.number); await rung(world, started.room); expect((await open<{ code: string }>("POST", `/v1/calls/${started.room}/enter`, {}, bearer(b.credential))).body.code).toBe("no_call"); }); it("takes notes only while everyone can show the mark, tells everyone by the member frame, and stops for a joiner who cannot", async () => { const a = await person(); const [b, c] = [await person("phone", a), await person("phone", a)]; const started = await open<{ room: string; credential: string }>("POST", "/v1/calls", { to: [b.number, c.number], group: GROUP, can: ["notes", "nonsense"] }, bearer(a.credential)); const roomId = started.body.room; const caller = await socket(started.body.credential); await rung(world, roomId, 2); // B's phone cannot show the mark: a start is refused, to the caller alone. const bJoined = await open<{ credential: string }>("POST", `/v1/calls/${roomId}/join`, {}, bearer(b.credential)); const bPhone = await socket(bJoined.body.credential); bPhone.send({ t: "accept" }); await caller.next({ t: "state", state: "live" }); caller.send({ t: "notes", on: true }); expect(await caller.next({ t: "notes" })).toEqual({ t: "notes", refused: "cannot_show", members: ["m1"] }); expect(bPhone.frames.some((f) => f.t === "notes")).toBe(false); bPhone.send({ t: "leave" }); await vi.waitFor(() => expect(about(caller, "m1")).toContain("left")); // B back with a phone that can: notes start, everyone told by the member frame. const back = await open<{ credential: string }>("POST", `/v1/calls/${roomId}/enter`, { can: ["notes"] }, bearer(b.credential)); const bBack = await socket(back.body.credential); bBack.send({ t: "accept" }); await vi.waitFor(() => expect(about(caller, "m1").slice(-1)).toEqual(["joined"])); caller.send({ t: "notes", on: true }); const mark = (f: Record<string, unknown>): boolean => f.t === "member" && (f.member as { id: string; notes: boolean }).id === "caller" && (f.member as { notes: boolean }).notes; await vi.waitFor(() => expect(bBack.frames.some(mark)).toBe(true)); await vi.waitFor(() => expect(caller.frames.some(mark)).toBe(true)); // C joins with a phone that cannot show it: notes stop, and the note-taker is told who. const cJoined = await open<{ credential: string }>("POST", `/v1/calls/${roomId}/join`, {}, bearer(c.credential)); const cPhone = await socket(cJoined.body.credential); cPhone.send({ t: "accept" }); expect(await caller.next({ t: "notes", stopped: "cannot_show" })).toEqual({ t: "notes", stopped: "cannot_show", member: "m2" }); await vi.waitFor(() => expect(bBack.frames.filter((f) => f.t === "member" && (f.member as { id: string }).id === "caller").slice(-1).map((f) => (f.member as { notes: boolean }).notes)).toEqual([false])); });});