Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

9.4 KB
Raw Blame
import { describe, expect, it, vi } from "vitest";

import { extractCode, senderDomain, sentBySite, waitForEmailCode, type CodeMailbox, type MailSummary } from "./email-code.ts";

const ASKED = 1_700_000_000_000;

function clocked() {
  let clock = ASKED;
  return { now: () => clock, sleep: async (ms: number) => { clock += ms; }, advance: (ms: number) => { clock += ms; } };
}

function mailbox(messages: Array<MailSummary & { text?: string | null }>): CodeMailbox & { read: string[] } {
  const read: string[] = [];
  return {
    read,
    list: async () => messages,
    body: async (id) => { read.push(id); return messages.find((mail) => mail.id === id)?.text ?? null; },
  };
}

const mail = (over: Partial<MailSummary & { text: string | null }> = {}) => ({ id: "m1", at: ASKED + 2_000, from: "Acme <[email protected]>", subject: "Your sign-in code", text: "Your verification code is 483920. It expires in 10 minutes.", ...over });

describe("extractCode", () => {
  it.each([
    ["Your code", "Your verification code is 483920.", "483920"],
    ["483920 is your code", "", "483920"],
    ["Sign in", "<html><head><style>.a{}</style></head><body><p>Your one-time code:</p><div>48 39</div><h1>728194</h1></body></html>", "728194"],
    ["Code", "Enter this code: 123-456", "123456"],
    ["Verify", "Your security code: A1B2C3", "A1B2C3"],
    ["Código", "Seu código de verificação é 553211", "553211"],
    ["登录验证码", "您的验证码为:483920,5 分钟内有效。", "483920"],
    ["登入驗證碼", "您的驗證碼是 483920,有效時間為 5 分鐘。", "483920"],
    ["登录通知", "您的验证码是483920,请勿分享。", "483920"],
    ["登入通知", "您的驗證碼為A1B2C3,請勿分享。", "A1B2C3"],
    ["", "<p>您的验证码是 <b>483920</b>,5 分钟内有效</p>", "483920"],
    ["483920是您的验证码", "", "483920"],
    ["Code", "Your code is 900100. Again: 900100.", "900100"],
  ])("takes the one code named as one in %s", (subject, body, code) => {
    expect(extractCode(subject, body)).toEqual({ kind: "code", code });
  });

  it.each([
    ["no number", "Your sign-in code", "Click the button to continue."],
    ["a number nothing calls a code", "Order shipped", "Order 483920 is on its way."],
    ["a zip code", "Hello", "Delivered to zip code 94103."],
    ["a code inside a link", "Welcome", "Your code: https://acme.test/v?c=482913 was generated."],
    ["a year in the footer", "Your code", "Use the app. © 2026 Acme. Your code is below."],
    ["a Chinese postal address", "送货通知", "邮政编码为 100000。"],
    ["a Chinese label beside a link", "验证码", "请访问 https://acme.test/v?c=483920 获取验证码。"],
    ["a number inside a longer Latin identifier", "验证码", "abc483920 和 483920abc 都不是验证码。"],
  ])("finds none in %s", (_name, subject, body) => {
    expect(extractCode(subject, body).kind).toBe("none");
  });

  it("calls two different codes ambiguous rather than guessing", () => {
    expect(extractCode("Code", "Your code is 111222 or use code 333444")).toEqual({ kind: "ambiguous" });
    expect(extractCode("验证码", "验证码为111222,或使用验证码333444。")).toEqual({ kind: "ambiguous" });
  });

  it("keeps only codes that fit the boxes on the page", () => {
    expect(extractCode("Code", "Your code is 483920 (reference 1234567)", 6)).toEqual({ kind: "code", code: "483920" });
    expect(extractCode("Code", "Your code is 483920", 4)).toEqual({ kind: "none" });
  });
});

describe("senderDomain", () => {
  it.each([
    ["[email protected]", "acme.test"],
    ["Acme <[email protected]>", "mail.acme.test"],
    ['"Acme, Inc" <[email protected]>', "acme.test"],
  ])("reads %s", (from, domain) => expect(senderDomain(from)).toBe(domain));

  it.each([
    "evil <[email protected]> <[email protected]>",
    "[email protected] <[email protected]>",
    "[email protected], [email protected]",
    "Acme <[email protected]> trailing",
    "not an address",
    "hello [email protected]",
  ])("reads no sender from %s", (from) => expect(senderDomain(from)).toBeNull());

  it("matches by registrable domain and never by a look-alike", () => {
    expect(sentBySite("[email protected]", "acme.test")).toBe(true);
    expect(sentBySite("[email protected]", "acme.test")).toBe(false);
    expect(sentBySite("[email protected]", "acme.test")).toBe(false);
  });
});

describe("waitForEmailCode", () => {
  const options = (time: ReturnType<typeof clocked>) => ({ site: "acme.test", askedAt: ASKED, isUsed: () => false, now: time.now, sleep: time.sleep });

  it("takes a matching new code", async () => {
    expect(await waitForEmailCode([mailbox([mail()])], options(clocked()))).toEqual({ kind: "code", code: "483920" });
  });

  it("reads no body of a message from another sender", async () => {
    const box = mailbox([mail({ from: "[email protected]", subject: "Lunch?" })]);
    await waitForEmailCode([box], options(clocked()));
    expect(box.read).toEqual([]);
  });

  it("reports the wrong sender and types nothing from it", async () => {
    expect(await waitForEmailCode([mailbox([mail({ from: "[email protected]" })])], options(clocked()))).toEqual({ kind: "miss", why: "wrong-sender" });
  });

  it("does not read a Chinese verification email from another site", async () => {
    const box = mailbox([mail({ from: "[email protected]", subject: "登录验证码", text: "您的验证码为:483920。" })]);
    expect(await waitForEmailCode([box], options(clocked()))).toEqual({ kind: "miss", why: "wrong-sender" });
    expect(box.read).toEqual([]);
  });

  it("ignores a message from before the code was asked for, past the clock skew", async () => {
    expect(await waitForEmailCode([mailbox([mail({ at: ASKED - 31_000 })])], options(clocked()))).toEqual({ kind: "miss", why: "not-found" });
    expect(await waitForEmailCode([mailbox([mail({ at: ASKED - 29_000 })])], options(clocked()))).toEqual({ kind: "code", code: "483920" });
  });

  it("falls back on two different codes, across mails", async () => {
    const box = mailbox([mail(), mail({ id: "m2", text: "Your code is 111222" })]);
    expect(await waitForEmailCode([box], options(clocked()))).toEqual({ kind: "miss", why: "ambiguous" });
  });

  it("waits for a code that arrives late, and stops when one does", async () => {
    const time = clocked();
    const messages: ReturnType<typeof mail>[] = [];
    const late: CodeMailbox = { list: async () => { if (time.now() >= ASKED + 20_000) messages[0] = mail({ at: ASKED + 20_000 }); return messages; }, body: async () => messages[0]!.text };
    expect(await waitForEmailCode([late], options(time))).toEqual({ kind: "code", code: "483920" });
    expect(time.now()).toBeLessThan(ASKED + 30_000);
  });

  it("gives up after about a minute, saying nothing arrived", async () => {
    const time = clocked();
    expect(await waitForEmailCode([mailbox([])], options(time))).toEqual({ kind: "miss", why: "not-found" });
    expect(time.now() - ASKED).toBeGreaterThanOrEqual(60_000);
    expect(time.now() - ASKED).toBeLessThan(70_000);
  });

  it("falls back on a code already used", async () => {
    const box = mailbox([mail()]);
    expect(await waitForEmailCode([box], { ...options(clocked()), isUsed: (code) => code === "483920" })).toEqual({ kind: "miss", why: "used" });
  });

  it("stops looking once the turn has ended", async () => {
    const time = clocked();
    await waitForEmailCode([mailbox([])], { ...options(time), active: () => false });
    expect(time.now()).toBe(ASKED);
  });

  it("does not read a mailbox when the turn has already ended", async () => {
    const box = { list: vi.fn(async () => [mail()]), body: vi.fn(async () => mail().text) };
    expect(await waitForEmailCode([box], { ...options(clocked()), active: () => false })).toEqual({ kind: "miss", why: "not-found" });
    expect(box.list).not.toHaveBeenCalled();
    expect(box.body).not.toHaveBeenCalled();
  });

  it("does not read message bodies when the turn ends during the mailbox listing", async () => {
    let active = true;
    const box = {
      list: async () => { active = false; return [mail()]; },
      body: vi.fn(async () => mail().text),
    };
    expect(await waitForEmailCode([box], { ...options(clocked()), active: () => active })).toEqual({ kind: "miss", why: "not-found" });
    expect(box.body).not.toHaveBeenCalled();
  });

  it("discards a late code and reads no further messages after the turn ends", async () => {
    let active = true;
    const box = {
      list: async () => [mail(), mail({ id: "m2" })],
      body: vi.fn(async () => { active = false; return mail().text; }),
    };
    expect(await waitForEmailCode([box], { ...options(clocked()), active: () => active })).toEqual({ kind: "miss", why: "not-found" });
    expect(box.body).toHaveBeenCalledExactlyOnceWith("m1");
  });

  it("does not start another poll after the turn ends while waiting", async () => {
    let active = true;
    const box = { list: vi.fn(async () => []), body: vi.fn(async () => null) };
    expect(await waitForEmailCode([box], {
      ...options(clocked()), active: () => active, sleep: async () => { active = false; },
    })).toEqual({ kind: "miss", why: "not-found" });
    expect(box.list).toHaveBeenCalledTimes(1);
  });

  it("says the mailbox couldn't be read when every look fails", async () => {
    const broken: CodeMailbox = { list: async () => { throw new Error("signed out"); }, body: async () => null };
    expect(await waitForEmailCode([broken], options(clocked()))).toEqual({ kind: "miss", why: "unreadable" });
  });
});