/** * AUDIT A H2 (28 Nov 2026): SMS pumping. A code goes only to a country on the allow list, a number is sent at most * SMS_PER_NUMBER_PER_DAY codes a day wherever it asks, and every number together stops at the day's ceiling, alerted. * All numbers invented. */import { env, runInDurableObject } from"cloudflare:test";import { describe, expect, it, vi } from"vitest";import { SMS_COUNTRIES, smsCountryAllowed } from"../src/auth.ts";import { SMS_PER_NUMBER_PER_DAY, typeDirectory } from"../src/directory.ts";describe("AUDIT A H2: SMS codes", () => { it("go only to a country on the allow list, the default or the Worker's own", () => { expect(SMS_COUNTRIES).toBe("91,1,44,971"); expect(smsCountryAllowed({}, "+919000000001")).toBe(true); expect(smsCountryAllowed({}, "+15555550100")).toBe(true); expect(smsCountryAllowed({}, "+447700900001")).toBe(true); expect(smsCountryAllowed({}, "+971500000001")).toBe(true); expect(smsCountryAllowed({}, "+882000000001"), "a premium-rate range").toBe(false); expect(smsCountryAllowed({}, "+4915100000001")).toBe(false); expect(smsCountryAllowed({ SMS_COUNTRIES: "49" }, "+4915100000001")).toBe(true); expect(smsCountryAllowed({ SMS_COUNTRIES: "49" }, "+919000000001")).toBe(false); }); it("stop for one number after its day's count, and for everyone at the day's ceiling, with an alert line", async () => {const stub = env.DIRECTORY.get(env.DIRECTORY.idFromName("audit-a-sms"));const got = await runInDurableObject(stub, async (instance: Directory) => {const me = instance as unknown as { env: Record<string, unknown>; smsAllowed(p: string): Promise<{ code: string } | null> }; me.env = { ...me.env, SMS_PER_NUMBER_PER_DAY: String(SMS_PER_NUMBER_PER_DAY) };const one: (string | null)[] = [];for (let i = 0; i <= SMS_PER_NUMBER_PER_DAY; i++) one.push((await me.smsAllowed("+919000000101"))?.code ?? null);const was = me.env; me.env = { ...was, SMS_PER_DAY: String(SMS_PER_NUMBER_PER_DAY + 3) };const alerts = vi.spyOn(console, "error").mockImplementation(() => undefined);try {const others: (string | null)[] = [];for (let i = 0; i < 4; i++) others.push((await me.smsAllowed(`+91900000020${i}`))?.code ?? null);return { one, others, alerted: alerts.mock.calls.map((c) => String(c[0])).filter((l) => l.startsWith("cottondb: ALERT sms")).length }; } finally { alerts.mockRestore(); me.env = was; } }); expect(got.one.slice(0, SMS_PER_NUMBER_PER_DAY).every((c) => c === null)).toBe(true); expect(got.one[SMS_PER_NUMBER_PER_DAY]).toBe("too_many_codes");// 10 sent for the first number; a ceiling of 13 lets three more numbers through and pauses the fourth. expect(got.others).toEqual([null, null, null, "codes_paused"]); expect(got.alerted, "80% and the ceiling").toBe(2); });});