Cotton code
92 lines · 5.3 KB · TypeScriptRaw
/** * THE CIRCLE'S INVITATION LINK (src/chat/circle_invite.ts; PEOPLE-DESIGN c "Invite"): his phone makes a link whose * secret never reaches the server; the person invited, once on Cotton, redeems it from their own object; both are then * each other's people (the Circle). A used, stopped, wrong or made-up link answers the same. Every number is invented. */import { env, runInDurableObject } from "cloudflare:test";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { sha256Text } from "../src/chat/circle_invite.ts";import { bearer, fakes, open } from "./calling.ts";import { chatPhone, type ChatPhone } from "./chat.ts";vi.setConfig({ testTimeout: 180_000 });let n = 0;const number = (): string => `+91900003${String(1000 + ++n).padStart(4, "0")}`;beforeEach(() => {  fakes();});afterEach(() => {  vi.restoreAllMocks();});type Body = Record<string, unknown>;async function person(): Promise<{ phone: ChatPhone; ask<T = Body>(method: string, path: string, body?: unknown): Promise<{ status: number; body: T }> }> {  const phone = await chatPhone(number(), `dev-invite-${n}`);  return { phone, ask: <T = Body>(method: string, path: string, body?: unknown) => open<T>(method, path, body, bearer(phone.credential)) };}async function peopleOf(p: Awaited<ReturnType<typeof person>>): Promise<{ number: string; name: string | null; waiting: boolean }[]> {  return (await p.ask<{ people: { number: string; name: string | null; waiting: boolean }[] }>("GET", "/v1/chat/people")).body.people;}describe("the Circle's invitation link", () => {  it("is redeemed once by the person invited, and both are in each other's Circle", async () => {    const asha = await person();    const meera = await person();    const secret = "s3cret-of-the-link-0123456789";    const made = await asha.ask<{ id: string; code: string; ends_at: string }>("POST", "/v1/chat/circle/invite", { secret_hash: await sha256Text(secret) });    expect(made.status, JSON.stringify(made.body)).toBe(200);    expect(made.body.code).toMatch(/^[a-z0-9]{12}$/);    expect((await asha.ask<{ invites: unknown[] }>("GET", "/v1/chat/circle/invites")).body.invites).toHaveLength(1);    // A wrong secret is refused in the same words as a made-up code.    const wrong = await meera.ask<{ code: string; message: string }>("POST", "/v1/chat/circle/redeem", { code: made.body.code, secret: "not-the-secret-0123456789" });    const madeUp = await meera.ask<{ code: string; message: string }>("POST", "/v1/chat/circle/redeem", { code: "abcdefghijkm", secret });    expect([wrong.status, madeUp.status]).toEqual([404, 404]);    expect(wrong.body.message).toBe(madeUp.body.message);    const redeemed = await meera.ask<{ number: string }>("POST", "/v1/chat/circle/redeem", { code: made.body.code, secret });    expect(redeemed.status, JSON.stringify(redeemed.body)).toBe(200);    expect(redeemed.body.number).toBe(asha.phone.number);    // Each holds the other, not waiting: both are in.    await vi.waitFor(async () => {      expect((await peopleOf(meera)).find((p) => p.number === asha.phone.number)?.waiting).toBe(false);      expect((await peopleOf(asha)).find((p) => p.number === meera.phone.number)?.waiting).toBe(false);    });    // Used: it leads nowhere again, and no longer stands on his list.    expect((await meera.ask("POST", "/v1/chat/circle/redeem", { code: made.body.code, secret })).status).toBe(404);    expect((await asha.ask<{ invites: unknown[] }>("GET", "/v1/chat/circle/invites")).body.invites).toHaveLength(0);  });  it("AUDIT D: a link forwarded to two people, redeemed by both at once, lets only one in", async () => {    const asha = await person();    const first = await person();    const second = await person();    const secret = "forwarded-link-secret-0123456789";    const made = await asha.ask<{ code: string }>("POST", "/v1/chat/circle/invite", { secret_hash: await sha256Text(secret) });    const both = await Promise.all([first, second].map((p) => p.ask("POST", "/v1/chat/circle/redeem", { code: made.body.code, secret })));    expect(both.map((r) => r.status).sort()).toEqual([200, 404]);    await vi.waitFor(async () => expect((await peopleOf(asha)).map((p) => p.number)).toHaveLength(1));  });  it("AUDIT D: a new link's sweep of ended codes reads through the index, not every account's codes", async () => {    const asha = await person();    expect((await asha.ask("POST", "/v1/chat/circle/invite", { secret_hash: await sha256Text("indexed-link-secret-0123456789") })).status).toBe(200);    const plan = await runInDurableObject(env.DIRECTORY.get(env.DIRECTORY.idFromName("directory")), (_i, s) => JSON.stringify(s.storage.sql.exec("EXPLAIN QUERY PLAN DELETE FROM circle_invite_code WHERE ends_at < ?", Date.now()).toArray()));    expect(plan).toContain("circle_invite_code_ends");  });  it("leads nowhere once he stops it", async () => {    const asha = await person();    const ravi = await person();    const secret = "another-link-secret-0123456789";    const made = await asha.ask<{ id: string; code: string }>("POST", "/v1/chat/circle/invite", { secret_hash: await sha256Text(secret) });    expect((await asha.ask("POST", "/v1/chat/circle/invite/stop", { id: made.body.id })).status).toBe(200);    expect((await ravi.ask("POST", "/v1/chat/circle/redeem", { code: made.body.code, secret })).status).toBe(404);    expect(await peopleOf(asha)).toEqual([]);  });});