Cotton code
72 lines · 3.2 KB · TypeScriptRaw
/** * THE CIRCLE'S SERVER HALF (PEOPLE-DESIGN b.4, c; src/chat/chat.ts `standing`, `stillTheirs`, `goneIn`, `tellGone`): * a person is bound to the account they were accepted as the first time they add back; an account that is erased tells * its people before the directory lets the number go ("left Cotton"); a number that later leads to another account is * "changed", and nothing sealed to a person goes to it. Every number is invented. */import { env } from "cloudflare:test";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { bearer, fakes, open } from "./calling.ts";import { chatPhone, type ChatPhone } from "./chat.ts";import { postSharingKey } from "./send-phone.ts";vi.setConfig({ testTimeout: 180_000 });let n = 0;const fresh = (): string => `+91900004${String(1000 + ++n).padStart(4, "0")}`;beforeEach(() => {  fakes();});afterEach(() => {  vi.restoreAllMocks();});type Body = Record<string, unknown>;interface P {  phone: ChatPhone;  number: string;  slug: string;  db: string;  ask<T = Body>(method: string, path: string, body?: unknown): Promise<{ status: number; body: T }>;}const dir = () => env.DIRECTORY.get(env.DIRECTORY.idFromName("directory"));async function person(number = fresh()): Promise<P> {  const phone = await chatPhone(number, `dev-circle-${++n}`);  const slug = (await dir().lookup(number))!;  const db = (await open<{ business: { database_id: string } }>("GET", "/v1/me", undefined, bearer(phone.credential))).body.business.database_id;  return { phone, number, slug, db, ask: <T = Body>(method: string, path: string, body?: unknown) => open<T>(method, path, body, bearer(phone.credential)) };}async function standing(p: P, number: string): Promise<Record<string, unknown> | undefined> {  return (await p.ask<{ people: Record<string, unknown>[] }>("GET", "/v1/chat/people")).body.people.find((x) => x.number === number);}describe("the Circle's server half", () => {  it("binds a person when they add back, says left Cotton once their account goes, and changed when the number is someone else's", async () => {    const a = await person();    const b = await person();    await a.phone.say("accept", b.number);    await b.phone.say("accept", a.number);    expect(await standing(a, b.number)).toEqual({ number: b.number, name: null, waiting: false });    // b's account is erased: its object tells a while the number is still b's, then the directory lets it go.    await env.BUSINESS.get(env.BUSINESS.idFromName(a.slug)).chatGone(b.number);    await dir().forget(b.db, b.slug);    expect(await standing(a, b.number)).toMatchObject({ left: true });    // Someone new signs up with b's old number, accepts a and posts a key: a still sees "changed", and a's phone is    // answered a made-up key, never the new account's.    const c = await person(b.number);    expect(c.slug).not.toBe(b.slug);    await c.phone.say("accept", a.number);    const key = await postSharingKey(c);    expect(await standing(a, b.number)).toMatchObject({ changed: true });    const asked = await a.ask<{ key: string }>("GET", `/v1/send/key?number=${encodeURIComponent(b.number)}`);    expect(asked.status).toBe(200);    expect(asked.body.key).not.toBe(key);  });});