Skip to content

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

44.7 KB
Raw Blame
import { env } from "cloudflare:workers";
import { applyD1Migrations, createExecutionContext, waitOnExecutionContext } from "cloudflare:test";
import { describe, expect, it, vi } from "vitest";

import { createAuth } from "../src/auth";
import { readConfig } from "../src/config";
import { buildOTPEmail, sendOTPEmail } from "../src/email";
import worker from "../src/index";
import { sha256 } from "../src/installations";

const BASE_URL = "https://auth.openmausbot.test";

interface CallOptions {
  method?: string;
  token?: string;
  body?: unknown;
  rawBody?: string;
  bodyChunks?: string[];
  headers?: Record<string, string>;
  origin?: string;
}

async function call(path: string, options: CallOptions = {}) {
  const headers = new Headers(options.headers);
  if (options.token) headers.set("authorization", `Bearer ${options.token}`);
  if (options.origin) headers.set("origin", options.origin);
  let body: BodyInit | undefined;
  if (options.bodyChunks) {
    const encoder = new TextEncoder();
    body = new ReadableStream({
      start(controller) {
        for (const chunk of options.bodyChunks ?? []) controller.enqueue(encoder.encode(chunk));
        controller.close();
      },
    });
  } else if (options.rawBody !== undefined) body = options.rawBody;
  else if (options.body !== undefined) body = JSON.stringify(options.body);
  if (body !== undefined && !headers.has("content-type")) headers.set("content-type", "application/json");
  const request = new Request(`${BASE_URL}${path}`, {
    method: options.method ?? "GET",
    headers,
    body,
  });
  const ctx = createExecutionContext();
  const response = await worker.fetch(request, env, ctx);
  await waitOnExecutionContext(ctx);
  return response;
}

// Counts D1 round trips the way production pays for them: one per statement
// call and one per batch. Each call is recorded a macrotask late, so a query
// queued with ctx.waitUntil as the response returns is counted after it.
function tracedDB(queries: string[], failOn?: RegExp): D1Database {
  const statements = new WeakMap<object, { statement: D1PreparedStatement; sql: string }>();
  async function roundTrip<T>(sql: string, run: () => Promise<T>): Promise<T> {
    await new Promise((resolve) => setTimeout(resolve, 0));
    queries.push(sql.replace(/\s+/g, " ").trim());
    if (failOn?.test(sql)) throw new Error("D1_ERROR: injected failure");
    return run();
  }
  function wrap(statement: D1PreparedStatement, sql: string): D1PreparedStatement {
    const traced = {
      bind: (...values: unknown[]) => wrap(statement.bind(...values), sql),
      all: () => roundTrip(sql, () => statement.all()),
      run: () => roundTrip(sql, () => statement.run()),
      first: (column?: string) => roundTrip(sql, () => (
        column === undefined ? statement.first() : statement.first(column)
      )),
    } as unknown as D1PreparedStatement;
    statements.set(traced, { statement, sql });
    return traced;
  }
  return {
    prepare: (sql: string) => wrap(env.DB.prepare(sql), sql),
    batch: (batch: D1PreparedStatement[]) => {
      const entries = batch.map((traced) => {
        const entry = statements.get(traced);
        if (!entry) throw new Error("batch received an untraced statement");
        return entry;
      });
      return roundTrip(
        `batch: ${entries.map((entry) => entry.sql).join("; ")}`,
        () => env.DB.batch(entries.map((entry) => entry.statement)),
      );
    },
    // Better Auth only checks that this exists to pick its D1 dialect.
    exec: (sql: string) => env.DB.exec(sql),
  } as unknown as D1Database;
}

async function tracedCall(
  path: string,
  body: unknown,
  options: { ip?: string; failOn?: RegExp } = {},
) {
  const queries: string[] = [];
  const request = new Request(`${BASE_URL}${path}`, {
    method: "POST",
    headers: { "content-type": "application/json", "cf-connecting-ip": options.ip ?? "198.51.100.200" },
    body: JSON.stringify(body),
  });
  const ctx = createExecutionContext();
  const response = await worker.fetch(request, { ...env, DB: tracedDB(queries, options.failOn) }, ctx);
  const beforeResponse = [...queries];
  await waitOnExecutionContext(ctx);
  return { response, beforeResponse, afterResponse: queries.slice(beforeResponse.length) };
}

async function mintOTP(email: string) {
  const ctx = createExecutionContext();
  const auth = createAuth(env, ctx, readConfig(env), crypto.randomUUID());
  const otp = await auth.api.createVerificationOTP({ body: { email, type: "sign-in" } });
  await waitOnExecutionContext(ctx);
  return otp;
}

async function signIn(email: string) {
  const otp = await mintOTP(email);
  const response = await call("/api/auth/sign-in/email-otp", {
    method: "POST",
    body: { email, otp, name: email.split("@", 1)[0] },
  });
  expect(response.status).toBe(200);
  const result = await response.json<{ token: string; user: { id: string; email: string } }>();
  const token = response.headers.get("set-auth-token");
  expect(token).toBeTruthy();
  if (!token) throw new Error("Better Auth did not return a signed bearer token");
  expect(result.user.email).toBe(email);
  return { token, rawToken: result.token, userId: result.user.id };
}

async function createInstall(
  token: string,
  clientInstanceId: string = crypto.randomUUID(),
  name = "Milind's Mac",
  platform: "darwin" | "windows" | "linux" = "darwin",
  appVersion: string | undefined = "0.1.0",
) {
  const response = await call("/v1/installations", {
    method: "POST",
    token,
    body: { clientInstanceId, name, platform, appVersion },
  });
  expect(response.status).toBe(201);
  return response.json<{
    installation: {
      id: string;
      clientInstanceId: string;
      name: string;
      platform: "darwin" | "windows" | "linux";
      appVersion: string | null;
      lastSeenAt: number | null;
    };
    credential: string;
    credentialExpiresAt: number;
  }>();
}

describe("control-plane migrations and health", () => {
  it("applies the pinned Better Auth and installation schemas in workerd", async () => {
    const rows = await env.DB.prepare(
      "SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name",
    ).all<{ name: string }>();
    expect(rows.results.map((row) => row.name)).toEqual(expect.arrayContaining([
      "account",
      "control_action_rate_limits",
      "installation_credentials",
      "installation_action_rate_limits",
      "installation_endpoints",
      "installations",
      "otp_recipient_rate_limits",
      "rateLimit",
      "session",
      "user",
      "verification",
    ]));

    const trigger = await env.DB.prepare(
      "SELECT name FROM sqlite_master WHERE type = 'trigger' AND name = ?",
    ).bind("installations_active_limit_before_insert").first<{ name: string }>();
    expect(trigger?.name).toBe("installations_active_limit_before_insert");

    const rotationTriggers = await env.DB.prepare(
      "SELECT name FROM sqlite_master WHERE type = 'trigger' AND name LIKE ? ORDER BY name",
    ).bind("%rotation%").all<{ name: string }>();
    expect(rotationTriggers.results.map((row) => row.name)).toEqual([
      "installation_credentials_rotation_guard_before_insert",
      "installations_rotation_cooldown_before_update",
    ]);

    const fk = await env.DB.prepare("PRAGMA foreign_key_list(installation_credentials)").all<{ table: string }>();
    expect(fk.results.some((row) => row.table === "installations")).toBe(true);

    const endpointColumns = await env.DB.prepare("PRAGMA table_info(installation_endpoints)")
      .all<{ name: string }>();
    expect(endpointColumns.results.map((column) => column.name)).toEqual(expect.arrayContaining([
      "cleanup_attempts",
      "last_cleanup_attempt_at",
      "provider_account",
      "reclaim_requested_at",
    ]));
    // The one-row table from 0006 stays, since an older Worker still writes it.
    const capacityRows = await env.DB.prepare("SELECT id, scan_page FROM managed_endpoint_capacity")
      .all<{ id: number; scan_page: number }>();
    expect(capacityRows.results).toEqual([{ id: 1, scan_page: 1 }]);
    const accountIndex = await env.DB.prepare(
      "SELECT name FROM sqlite_master WHERE type = 'index' AND tbl_name = 'installation_endpoints' AND name = ?",
    ).bind("installation_endpoints_account_status_idx").first<{ name: string }>();
    expect(accountIndex?.name).toBe("installation_endpoints_account_status_idx");
  });

  it("lets Better Auth write an account row on the migrated schema", async () => {
    const { userId } = await signIn("[email protected]");
    const ctx = createExecutionContext();
    const context = await createAuth(env, ctx, readConfig(env), crypto.randomUUID()).$context;
    // Better Auth 1.7.3 and later never write account.issuer, which 0001 created NOT NULL.
    await context.internalAdapter.createAccount({ userId, providerId: "credential", accountId: userId });
    await waitOnExecutionContext(ctx);
    const rows = await env.DB.prepare('SELECT "providerId", "accountId" FROM "account" WHERE "userId" = ?')
      .bind(userId).all();
    expect(rows.results).toEqual([{ providerId: "credential", accountId: userId }]);
  });

  it("ties every existing endpoint to the original account and carries its capacity state over", async () => {
    const db = env.MIGRATION_DB;
    const accounts = env.TEST_MIGRATIONS.findIndex((migration) => migration.name.startsWith("0007_"));
    expect(accounts).toBeGreaterThan(0);
    await applyD1Migrations(db, env.TEST_MIGRATIONS.slice(0, accounts));
    // What a Worker from before 0007 left behind.
    await db.batch([
      db.prepare(
        `INSERT INTO installation_endpoints
          (installation_id, hostname, tunnel_name, tunnel_id, dns_record_id, status,
           last_reconciled_at, created_at, updated_at)
         VALUES ('before-0007', ?, ?, '10000000-0000-4000-8000-000000000001', 'dns-1', 'ready', 5, 1, 5)`,
      ).bind(`c-${"a".repeat(32)}.openmausbot.com`, `omb-c-${"a".repeat(32)}`),
      db.prepare(
        `UPDATE managed_endpoint_capacity
            SET scan_page = 7, tunnel_count = 1000, dns_record_count = 990, reclaim_pending = 3,
                checked_at = 123, capacity_rejected_at = 456, capacity_rejected_code = 'cf_api_1045',
                updated_at = 789
          WHERE id = 1`,
      ),
    ]);

    await applyD1Migrations(db, env.TEST_MIGRATIONS);

    expect(await db.prepare(
      "SELECT provider_account, hostname, status FROM installation_endpoints WHERE installation_id = 'before-0007'",
    ).first()).toEqual({
      provider_account: env.CLOUDFLARE_ACCOUNT_ID,
      hostname: `c-${"a".repeat(32)}.openmausbot.com`,
      status: "ready",
    });
    // A Worker that predates the column keeps inserting into the same account.
    await db.prepare(
      `INSERT INTO installation_endpoints (installation_id, hostname, tunnel_name, status, created_at, updated_at)
       VALUES ('older-worker', ?, ?, 'pending', 1, 1)`,
    ).bind(`c-${"b".repeat(32)}.openmausbot.com`, `omb-c-${"b".repeat(32)}`).run();
    expect(await db.prepare(
      "SELECT provider_account FROM installation_endpoints WHERE installation_id = 'older-worker'",
    ).first()).toEqual({ provider_account: env.CLOUDFLARE_ACCOUNT_ID });
    await expect(db.prepare(
      `INSERT INTO installation_endpoints
        (installation_id, provider_account, hostname, tunnel_name, status, created_at, updated_at)
       VALUES ('bad-account', 'short', 'c-x.example', 'omb-c-x', 'pending', 1, 1)`,
    ).run()).rejects.toThrow(/CHECK/);
    expect(await db.prepare("SELECT * FROM managed_endpoint_account_capacity").all().then((rows) => rows.results))
      .toEqual([{
        provider_account: env.CLOUDFLARE_ACCOUNT_ID,
        scan_page: 7,
        tunnel_count: 1000,
        dns_record_count: 990,
        reclaim_pending: 3,
        dormant_endpoints: 0,
        checked_at: 123,
        capacity_rejected_at: 456,
        capacity_rejected_code: "cf_api_1045",
        updated_at: 789,
      }]);
  });

  it("serves a no-store health response without CORS wildcards", async () => {
    const response = await call("/healthz");
    expect(response.status).toBe(200);
    await expect(response.json()).resolves.toEqual({
      ok: true,
      service: "openmausbot-control-plane",
      capacity: {
        status: "unknown",
        checkedAt: null,
        tunnels: { used: null, limit: 1000 },
        dnsRecords: { used: null, limit: 3500 },
        providerRejectedAt: null,
        reclaim: { mode: "on", pending: 0 },
      },
      relayAdoption: { e2eDevices: 0, legacyDevices: 0, installs: 0 },
      tunnelRelease: {
        relayProven: { percent: 0, days: 3, eligible: 0 },
        legacyPhoneCutoff: { percent: 0, eligible: 0 },
        minAppVersion: { on: false, eligible: 0 },
      },
    });
    expect(response.headers.get("cache-control")).toBe("no-store");
    expect(response.headers.get("access-control-allow-origin")).toBeNull();
  });

  it("answers repeated health checks from one capacity read per data center", async () => {
    const prepare = vi.spyOn(env.DB, "prepare");
    const first = await call("/healthz");
    const second = await call("/healthz");
    await expect(second.json()).resolves.toEqual(await first.json());
    expect(prepare.mock.calls.filter(([sql]) => !sql.includes("relay_adoption"))).toHaveLength(1);
    expect(prepare.mock.calls.filter(([sql]) => sql.includes("relay_adoption")).length).toBeLessThanOrEqual(1);
    // The public response stays uncached; only the internal copy expires on
    // its own, which bounds how far the report can trail D1.
    expect(second.headers.get("cache-control")).toBe("no-store");
    const cache = await caches.open("healthz-capacity");
    const copy = await cache.match(new URL("/__internal/healthz-capacity-row/v2", env.BETTER_AUTH_URL));
    expect(copy?.headers.get("cache-control")).toBe("max-age=120");
    prepare.mockRestore();
  });

  it("reads capacity from D1 when the data-center cache is unavailable", async () => {
    await env.DB.prepare(
      `INSERT INTO managed_endpoint_account_capacity (provider_account, tunnel_count, checked_at, updated_at)
       VALUES (?, 950, ?, 0)`,
    ).bind(env.CLOUDFLARE_ACCOUNT_ID, Date.now()).run();
    const unavailable = () => Promise.reject(new Error("cache unavailable"));
    const brokenCache = { delete: unavailable, match: unavailable, put: unavailable } as unknown as Cache;
    const open = vi.spyOn(caches, "open")
      .mockImplementationOnce(unavailable)
      .mockResolvedValueOnce(brokenCache);
    for (let attempt = 0; attempt < 2; attempt += 1) {
      const response = await call("/healthz");
      expect(response.status).toBe(200);
      await expect(response.json()).resolves.toMatchObject({
        ok: true,
        capacity: { status: "high", tunnels: { used: 950, limit: 1000 } },
      });
    }
    expect(open).toHaveBeenCalledTimes(2);
    open.mockRestore();
  });

  it("validates capacity tuning variables and keeps safe defaults when they are absent", () => {
    const base = { ...env } as Record<string, unknown>;
    for (const name of [
      "OMB_TUNNEL_LIMIT",
      "OMB_DNS_RECORD_LIMIT",
      "OMB_TUNNEL_RECLAIM",
      "OMB_TUNNEL_OFFLINE_RECLAIM_DAYS",
      "OMB_CLEANUP_SWEEP_LIMIT",
    ]) {
      Reflect.deleteProperty(base, name);
    }
    expect(readConfig(base as unknown as Env).capacity).toEqual({
      cleanupSweepLimit: 20,
      dnsRecordLimit: 1000,
      offlineReclaimMs: 21 * 24 * 60 * 60 * 1_000,
      reclaimMode: "observe",
      reclaimPreviewMs: null,
      tunnelLimit: 1000,
    });
    const withVars = (vars: Record<string, string>) => ({ ...base, ...vars }) as unknown as Env;
    expect(readConfig(withVars({
      OMB_TUNNEL_LIMIT: "2500",
      OMB_TUNNEL_RECLAIM: "observe",
      OMB_TUNNEL_OFFLINE_RECLAIM_DAYS: "30",
      OMB_CLEANUP_SWEEP_LIMIT: "4",
    })).capacity).toMatchObject({
      cleanupSweepLimit: 4,
      offlineReclaimMs: 30 * 24 * 60 * 60 * 1_000,
      reclaimMode: "observe",
      tunnelLimit: 2500,
    });
    expect(readConfig(withVars({ OMB_TUNNEL_OFFLINE_RECLAIM_DAYS: "0" })).capacity.offlineReclaimMs).toBe(21 * 24 * 60 * 60 * 1_000);
    expect(readConfig(withVars({ OMB_TUNNEL_OFFLINE_RECLAIM_DAYS: "1" })).capacity.offlineReclaimMs).toBe(24 * 60 * 60 * 1_000);
    expect(readConfig(withVars({ OMB_TUNNEL_RECLAIM_PREVIEW_DAYS: "1" })).capacity.reclaimPreviewMs).toBe(24 * 60 * 60 * 1_000);
    for (const invalid of ["", "0", "366", "soon"]) {
      expect(readConfig(withVars({ OMB_TUNNEL_RECLAIM_PREVIEW_DAYS: invalid })).capacity.reclaimPreviewMs).toBeNull();
    }
    expect(readConfig(withVars({ OMB_TUNNEL_OFFLINE_RECLAIM_DAYS: "3" })).capacity.offlineReclaimMs).toBe(3 * 24 * 60 * 60 * 1_000);
    expect(readConfig(withVars({ OMB_CLEANUP_SWEEP_LIMIT: "51" })).capacity.cleanupSweepLimit).toBe(20);
    expect(readConfig(withVars({ OMB_TUNNEL_LIMIT: "1e3" })).capacity.tunnelLimit).toBe(1000);
    expect(readConfig(withVars({ OMB_TUNNEL_RECLAIM: "yes" })).capacity.reclaimMode).toBe("observe");
    expect(readConfig(withVars({ OMB_TUNNEL_RECLAIM: "on" })).capacity.reclaimMode).toBe("on");
  });

  it("reports an unhealthy deployment without exposing invalid configuration", async () => {
    const misconfiguredEnv: Env = {
      DB: env.DB,
      EMAIL: env.EMAIL,
      BETTER_AUTH_URL: env.BETTER_AUTH_URL,
      EMAIL_FROM: env.EMAIL_FROM,
      ALLOWED_ORIGINS: env.ALLOWED_ORIGINS,
      CLOUDFLARE_ACCOUNT_ID: env.CLOUDFLARE_ACCOUNT_ID,
      CLOUDFLARE_ZONE_ID: env.CLOUDFLARE_ZONE_ID,
      COMPANION_HOST_SUFFIX: env.COMPANION_HOST_SUFFIX,
      CLOUDFLARE_API_TOKEN: env.CLOUDFLARE_API_TOKEN,
      CLOUDFLARE_ENDPOINT_ACCOUNTS: env.CLOUDFLARE_ENDPOINT_ACCOUNTS,
      CLOUDFLARE_API_TOKEN_MAUSBOT_SI: env.CLOUDFLARE_API_TOKEN_MAUSBOT_SI,
      OMB_TUNNEL_LIMIT: env.OMB_TUNNEL_LIMIT,
      OMB_DNS_RECORD_LIMIT: env.OMB_DNS_RECORD_LIMIT,
      OMB_TUNNEL_RECLAIM: env.OMB_TUNNEL_RECLAIM,
      OMB_TUNNEL_OFFLINE_RECLAIM_DAYS: env.OMB_TUNNEL_OFFLINE_RECLAIM_DAYS,
      OMB_CLEANUP_SWEEP_LIMIT: env.OMB_CLEANUP_SWEEP_LIMIT,
      OMB_TUNNEL_RECLAIM_PREVIEW_DAYS: env.OMB_TUNNEL_RECLAIM_PREVIEW_DAYS,
      RELAY_TUNNEL_RELEASE_PERCENT: env.RELAY_TUNNEL_RELEASE_PERCENT,
      RELAY_TUNNEL_RELEASE_DAYS: env.RELAY_TUNNEL_RELEASE_DAYS,
      TUNNEL_LEGACY_PHONE_CUTOFF_PERCENT: env.TUNNEL_LEGACY_PHONE_CUTOFF_PERCENT,
      TUNNEL_MIN_APP_VERSION: env.TUNNEL_MIN_APP_VERSION,
      TUNNEL_REASON_MIN_VERSION: env.TUNNEL_REASON_MIN_VERSION,
      RELAY_NODES: env.RELAY_NODES,
      RELAY_NODES_AMERICAS: env.RELAY_NODES_AMERICAS,
      RELAY_TOKEN_KID: env.RELAY_TOKEN_KID,
      RELAY_ROLLOUT_PERCENT: env.RELAY_ROLLOUT_PERCENT,
      RELAY_ALLOW_INSTALLATIONS: env.RELAY_ALLOW_INSTALLATIONS,
      RELAY_DISABLED: env.RELAY_DISABLED,
      RELAY_CAPABLE_MIN_VERSION: env.RELAY_CAPABLE_MIN_VERSION,
      RELAY_TOKEN_SIGNING_KEY: "",
      BETTER_AUTH_SECRET: "too-short",
    };
    const request = new Request(`${BASE_URL}/healthz`);
    const ctx = createExecutionContext();
    const response = await worker.fetch(request, misconfiguredEnv, ctx);
    await waitOnExecutionContext(ctx);
    expect(response.status).toBe(503);
    const body = await response.text();
    expect(body).toBe('{"error":"misconfigured"}');
    expect(body).not.toContain("too-short");
  });

  it("reports a missing companion hostname suffix with a stable configuration error", () => {
    const missingSuffixEnv = { ...env };
    Reflect.deleteProperty(missingSuffixEnv, "COMPANION_HOST_SUFFIX");
    expect(() => readConfig(missingSuffixEnv)).toThrow(
      "COMPANION_HOST_SUFFIX must be a lowercase DNS suffix",
    );
  });
});

describe("Better Auth email OTP and bearer boundary", () => {
  it("sends enumeration-safe OTP responses and stores only a hash", async () => {
    const response = await call("/api/auth/email-otp/send-verification-otp", {
      method: "POST",
      body: { email: "[email protected]", type: "sign-in" },
    });
    expect(response.status).toBe(200);
    await expect(response.json()).resolves.toEqual({ success: true });
    expect(response.headers.get("cache-control")).toBe("no-store");

    const verification = await env.DB.prepare(
      'SELECT value FROM "verification" WHERE identifier = ?',
    ).bind("[email protected]").first<{ value: string }>();
    expect(verification?.value).toMatch(/^[A-Za-z0-9_-]{43}:0$/);
    expect(verification?.value).not.toMatch(/^\d{8}$/);
    const rateLimits = await env.DB.prepare('SELECT COUNT(*) AS count FROM "rateLimit"').first<{ count: number }>();
    expect(rateLimits?.count).toBeGreaterThan(0);

    await signIn("[email protected]");
    const knownResponse = await call("/api/auth/email-otp/send-verification-otp", {
      method: "POST",
      body: { email: "[email protected]", type: "sign-in" },
    });
    expect(knownResponse.status).toBe(response.status);
    await expect(knownResponse.json()).resolves.toEqual({ success: true });
  });

  it("limits OTP sends per recipient even when callers change addresses", async () => {
    const email = "[email protected]";
    for (let index = 0; index < 3; index += 1) {
      const response = await call("/api/auth/email-otp/send-verification-otp", {
        method: "POST",
        headers: { "cf-connecting-ip": `198.51.100.${index + 1}` },
        body: { email, type: "sign-in" },
      });
      expect(response.status).toBe(200);
      await expect(response.json()).resolves.toEqual({ success: true });
    }

    const before = await env.DB.prepare(
      'SELECT value FROM "verification" WHERE identifier = ?',
    ).bind(`sign-in-otp-${email}`).first<{ value: string }>();
    expect(before?.value).toBeTruthy();

    const limited = await call("/api/auth/email-otp/send-verification-otp", {
      method: "POST",
      headers: { "cf-connecting-ip": "203.0.113.44" },
      body: { email, type: "sign-in" },
    });
    expect(limited.status).toBe(200);
    await expect(limited.json()).resolves.toEqual({ success: true });
    const after = await env.DB.prepare(
      'SELECT value FROM "verification" WHERE identifier = ?',
    ).bind(`sign-in-otp-${email}`).first<{ value: string }>();
    expect(after?.value).toBe(before?.value);

    const rateLimit = await env.DB.prepare(
      "SELECT attempts FROM otp_recipient_rate_limits",
    ).first<{ attempts: number }>();
    expect(rateLimit?.attempts).toBe(3);
  });

  it("canonicalizes Better Auth's message-only rate limit response", async () => {
    const responses = [];
    for (let index = 0; index < 6; index += 1) {
      responses.push(await call("/api/auth/email-otp/send-verification-otp", {
        method: "POST",
        headers: { "cf-connecting-ip": "198.51.100.42" },
        body: { email: `better-auth-limit-${index}@example.com`, type: "sign-in" },
      }));
    }

    expect(responses.slice(0, 5).map((response) => response.status)).toEqual([
      200,
      200,
      200,
      200,
      200,
    ]);
    const limited = responses[5];
    expect(limited.status).toBe(429);
    await expect(limited.json()).resolves.toEqual({ error: "rate_limited" });
    expect(limited.headers.get("x-request-id")).toMatch(/^[0-9a-f-]{36}$/i);
  });

  it("completes email OTP registration once and authenticates a bearer", async () => {
    const account = await signIn("[email protected]");
    const me = await call("/v1/me", { token: account.token });
    expect(me.status).toBe(200);
    await expect(me.json()).resolves.toMatchObject({
      user: { id: account.userId, email: "[email protected]", emailVerified: true },
    });
    expect((await call("/v1/me", { token: account.rawToken })).status).toBe(401);

    const accountAgain = await signIn("[email protected]");
    expect(accountAgain.userId).toBe(account.userId);
    const count = await env.DB.prepare('SELECT COUNT(*) AS count FROM "user" WHERE email = ?')
      .bind("[email protected]").first<{ count: number }>();
    expect(count?.count).toBe(1);
  });

  it("rejects invalid, expired, and replayed OTPs and invalidates signed-out bearers", async () => {
    const invalidEmail = "[email protected]";
    const invalidContext = createExecutionContext();
    const invalidAuth = createAuth(env, invalidContext, readConfig(env), crypto.randomUUID());
    const validOTP = await invalidAuth.api.createVerificationOTP({
      body: { email: invalidEmail, type: "sign-in" },
    });
    await waitOnExecutionContext(invalidContext);

    const invalid = await call("/api/auth/sign-in/email-otp", {
      method: "POST",
      body: { email: invalidEmail, otp: "00000000", name: "Invalid" },
    });
    expect(invalid.status).toBe(400);
    await expect(invalid.json()).resolves.toEqual({ error: "invalid_otp" });

    const accepted = await call("/api/auth/sign-in/email-otp", {
      method: "POST",
      body: { email: invalidEmail, otp: validOTP, name: "Valid" },
    });
    expect(accepted.status).toBe(200);
    const acceptedBody = await accepted.json<{ token: string }>();

    const replayed = await call("/api/auth/sign-in/email-otp", {
      method: "POST",
      body: { email: invalidEmail, otp: validOTP, name: "Replay" },
    });
    expect(replayed.status).toBe(400);
    await expect(replayed.json()).resolves.toEqual({ error: "invalid_otp" });

    const expiredEmail = "[email protected]";
    const expiredContext = createExecutionContext();
    const expiredAuth = createAuth(env, expiredContext, readConfig(env), crypto.randomUUID());
    const expiredOTP = await expiredAuth.api.createVerificationOTP({
      body: { email: expiredEmail, type: "sign-in" },
    });
    await waitOnExecutionContext(expiredContext);
    await env.DB.prepare(
      'UPDATE "verification" SET "expiresAt" = ? WHERE "identifier" = ?',
    ).bind(Date.now() - 1, `sign-in-otp-${expiredEmail}`).run();
    const expired = await call("/api/auth/sign-in/email-otp", {
      method: "POST",
      body: { email: expiredEmail, otp: expiredOTP, name: "Expired" },
    });
    expect(expired.status).toBe(400);
    await expect(expired.json()).resolves.toEqual({ error: "otp_expired" });

    const signedOut = await call("/api/auth/sign-out", {
      method: "POST",
      token: acceptedBody.token,
    });
    expect(signedOut.status).toBe(200);
    expect((await call("/v1/me", { token: acceptedBody.token })).status).toBe(401);
  });

  it("builds both plain-text and HTML OTP mail and redacts send failures", async () => {
    const message = buildOTPEmail("[email protected]", {
      email: "[email protected]",
      otp: "12345678",
      type: "sign-in",
    });
    expect(message.text).toContain("12345678");
    expect(message.html).toContain("12345678");

    const error = vi.spyOn(console, "error").mockImplementation(() => undefined);
    await sendOTPEmail({ send: async () => { throw new Error("[email protected] 12345678"); } },
      "[email protected]",
      { email: "[email protected]", otp: "12345678", type: "sign-in" },
      "request-safe");
    const logged = error.mock.calls.flat().join(" ");
    expect(logged).toContain("request-safe");
    expect(logged).not.toContain("[email protected]");
    expect(logged).not.toContain("12345678");
    error.mockRestore();
  });

  it("requires account bearers and never confuses installation credentials", async () => {
    expect((await call("/v1/me")).status).toBe(401);
    expect((await call("/v1/me", { token: "not-a-signed-session" })).status).toBe(401);
    expect((await call("/v1/me", { token: "omb_install_AAAAAAAAAAAAAAAAAAAAAA.AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" })).status).toBe(401);

    const account = await signIn("[email protected]");
    expect((await call("/v1/installations/self", { token: account.token })).status).toBe(401);
  });
});

// Production pays a cross-region round trip to the D1 primary for every query
// on these paths, so the budgets below are latency budgets.
describe("email OTP D1 round trips", () => {
  const SEND_PATH = "/api/auth/email-otp/send-verification-otp";
  const SIGN_IN_PATH = "/api/auth/sign-in/email-otp";
  const EXPIRED_SWEEP = /delete from "verification" where "verification"\."expiresAt" </i;

  async function verificationIdentifiers() {
    const rows = await env.DB.prepare('SELECT identifier FROM "verification" ORDER BY identifier')
      .all<{ identifier: string }>();
    return rows.results.map((row) => row.identifier);
  }

  it("sends a code in at most five round trips with one for the recipient limit", async () => {
    const { response, beforeResponse } = await tracedCall(SEND_PATH, {
      email: "[email protected]",
      type: "sign-in",
    });
    expect(response.status).toBe(200);
    expect(beforeResponse.filter((sql) => sql.includes("otp_recipient_rate_limits"))).toHaveLength(1);
    expect(beforeResponse.length).toBeLessThanOrEqual(5);
  });

  it("keeps the 24-hour recipient retention sweep", async () => {
    const stale = Date.now() - 25 * 60 * 60 * 1_000;
    await env.DB.prepare(
      `INSERT INTO otp_recipient_rate_limits (recipient_key, window_started_at, attempts, updated_at)
       VALUES (?, ?, 3, ?)`,
    ).bind("0".repeat(64), stale, stale).run();

    const response = await call(SEND_PATH, {
      method: "POST",
      body: { email: "[email protected]", type: "sign-in" },
    });
    expect(response.status).toBe(200);
    const rows = await env.DB.prepare("SELECT recipient_key, attempts FROM otp_recipient_rate_limits")
      .all<{ recipient_key: string; attempts: number }>();
    expect(rows.results).toHaveLength(1);
    expect(rows.results[0]?.recipient_key).not.toBe("0".repeat(64));
    expect(rows.results[0]?.attempts).toBe(1);
  });

  it("signs in within eight round trips and sweeps expired codes after responding", async () => {
    const email = "[email protected]";
    await signIn(email);
    const otp = await mintOTP(email);
    await mintOTP("[email protected]");
    await env.DB.prepare('UPDATE "verification" SET "expiresAt" = ? WHERE "identifier" = ?')
      .bind(new Date(Date.now() - 60_000).toISOString(), "[email protected]").run();
    await mintOTP("[email protected]");

    const { response, beforeResponse, afterResponse } = await tracedCall(SIGN_IN_PATH, {
      email,
      otp,
      name: "Known",
    });
    expect(response.status).toBe(200);
    expect(beforeResponse.some((sql) => EXPIRED_SWEEP.test(sql))).toBe(false);
    expect(beforeResponse.length).toBeLessThanOrEqual(8);
    expect(afterResponse.filter((sql) => EXPIRED_SWEEP.test(sql))).toHaveLength(1);
    // Only the expired row goes; a live code for another address survives.
    expect(await verificationIdentifiers()).toEqual(["[email protected]"]);
  });

  it("keeps sign-in working when the expired-code sweep fails and logs no credentials", async () => {
    const email = "[email protected]";
    const otp = await mintOTP(email);
    const error = vi.spyOn(console, "error").mockImplementation(() => undefined);
    const { response, afterResponse } = await tracedCall(
      SIGN_IN_PATH,
      { email, otp, name: "Sweep" },
      { failOn: EXPIRED_SWEEP },
    );
    const logged = error.mock.calls.flat().join(" ");
    error.mockRestore();

    expect(response.status).toBe(200);
    expect(afterResponse.filter((sql) => EXPIRED_SWEEP.test(sql))).toHaveLength(1);
    expect(logged).toContain("expired verification sweep failed");
    expect(logged).toContain(response.headers.get("x-request-id"));
    expect(logged).not.toContain(email);
    expect(logged).not.toContain(otp);
    expect(logged).not.toContain("injected failure");
  });

  it("does not sweep for rate-limited sign-ins", async () => {
    const ip = "198.51.100.201";
    const body = { email: "[email protected]", otp: "00000000", name: "Limited" };
    for (let index = 0; index < 10; index += 1) {
      const response = await call(SIGN_IN_PATH, {
        method: "POST",
        headers: { "cf-connecting-ip": ip },
        body,
      });
      expect(response.status).toBe(400);
    }

    const { response, beforeResponse, afterResponse } = await tracedCall(SIGN_IN_PATH, body, { ip });
    expect(response.status).toBe(429);
    expect(beforeResponse.some((sql) => EXPIRED_SWEEP.test(sql))).toBe(false);
    expect(afterResponse).toEqual([]);
  });
});

describe("installation lifecycle", () => {
  it("registers once, stores no raw credential, and serves installation self", async () => {
    const account = await signIn("[email protected]");
    const created = await createInstall(account.token, "mac-stable-1");
    expect(created.credential).toMatch(/^omb_install_[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}$/);
    expect(created.credentialExpiresAt).toBeGreaterThan(Date.now() + 89 * 24 * 60 * 60 * 1_000);

    const stored = await env.DB.prepare(
      "SELECT lookup_id, secret_hash FROM installation_credentials WHERE installation_id = ?",
    ).bind(created.installation.id).first<{ lookup_id: string; secret_hash: string }>();
    expect(stored?.secret_hash).toBe(await sha256(created.credential));
    expect(JSON.stringify(stored)).not.toContain(created.credential);
    const entireRow = await env.DB.prepare(
      "SELECT * FROM installation_credentials WHERE installation_id = ?",
    ).bind(created.installation.id).first<{
      id: string;
      installation_id: string;
      lookup_id: string;
      secret_hash: string;
      created_at: number;
      expires_at: number;
      last_used_at: number | null;
      revoked_at: number | null;
    }>();
    expect(JSON.stringify(entireRow)).not.toContain(created.credential);

    const self = await call("/v1/installations/self", { token: created.credential });
    expect(self.status).toBe(200);
    const selfPayload = await self.json<{
      installation: { id: string; clientInstanceId: string; platform: string; appVersion: string; lastSeenAt: number };
      credentialExpiresAt: number;
    }>();
    expect(selfPayload).toMatchObject({
      installation: {
        id: created.installation.id,
        clientInstanceId: "mac-stable-1",
        platform: "darwin",
        appVersion: "0.1.0",
        lastSeenAt: expect.any(Number),
      },
    });
    expect(selfPayload.credentialExpiresAt).toBe(created.credentialExpiresAt);
    const used = await env.DB.prepare(
      "SELECT last_used_at FROM installation_credentials WHERE installation_id = ?",
    ).bind(created.installation.id).first<{ last_used_at: number | null }>();
    expect(used?.last_used_at).toEqual(expect.any(Number));
    const seen = await env.DB.prepare(
      "SELECT last_seen_at FROM installations WHERE id = ?",
    ).bind(created.installation.id).first<{ last_seen_at: number | null }>();
    expect(seen?.last_seen_at).toEqual(expect.any(Number));
  });

  it("enforces active client uniqueness per owner and permits re-registration after revocation", async () => {
    const first = await signIn("[email protected]");
    const second = await signIn("[email protected]");
    await createInstall(first.token, "stable-client");

    const duplicate = await call("/v1/installations", {
      method: "POST",
      token: first.token,
      body: { clientInstanceId: "stable-client", name: "Again", platform: "darwin" },
    });
    expect(duplicate.status).toBe(409);
    await expect(duplicate.json()).resolves.toEqual({ error: "installation_exists" });

    const crossAccount = await call("/v1/installations", {
      method: "POST",
      token: second.token,
      body: { clientInstanceId: "stable-client", name: "Independent", platform: "linux" },
    });
    expect(crossAccount.status).toBe(201);
    await expect(crossAccount.json()).resolves.toMatchObject({
      installation: { clientInstanceId: "stable-client", platform: "linux", appVersion: null },
    });

    const firstList = await call("/v1/installations", { token: first.token });
    const firstInstallation = (await firstList.json<{
      installations: Array<{ id: string; clientInstanceId: string }>;
    }>()).installations[0];
    expect((await call(`/v1/installations/${firstInstallation.id}`, {
      method: "DELETE",
      token: first.token,
    })).status).toBe(204);
    const registeredAgain = await createInstall(first.token, "stable-client", "Replacement Mac");
    expect(registeredAgain.installation.id).not.toBe(firstInstallation.id);
  });

  it("rejects expired installation credentials", async () => {
    const owner = await signIn("[email protected]");
    const created = await createInstall(owner.token);
    await env.DB.prepare(
      "UPDATE installation_credentials SET expires_at = ? WHERE installation_id = ?",
    ).bind(Date.now() - 1, created.installation.id).run();
    expect((await call("/v1/installations/self", { token: created.credential })).status).toBe(401);
  });

  it("caps active installations while allowing a slot to be reused after revocation", async () => {
    const owner = await signIn("[email protected]");
    const first = await createInstall(owner.token, "cap-client-0");
    const now = Date.now();
    await env.DB.batch(Array.from({ length: 99 }, (_, index) => env.DB.prepare(
      `INSERT INTO installations
        (id, owner_user_id, client_instance_id, display_name, platform, created_at, updated_at)
       VALUES (?, ?, ?, ?, 'darwin', ?, ?)`,
    ).bind(
      crypto.randomUUID(),
      owner.userId,
      `cap-client-${index + 1}`,
      `Cap Mac ${index + 1}`,
      now,
      now,
    )));

    const limited = await call("/v1/installations", {
      method: "POST",
      token: owner.token,
      body: { clientInstanceId: "cap-client-overflow", name: "Overflow", platform: "darwin" },
    });
    expect(limited.status).toBe(409);
    await expect(limited.json()).resolves.toEqual({ error: "installation_limit_reached" });

    expect((await call(`/v1/installations/${first.installation.id}`, {
      method: "DELETE",
      token: owner.token,
    })).status).toBe(204);
    expect((await call("/v1/installations", {
      method: "POST",
      token: owner.token,
      body: { clientInstanceId: "cap-client-replacement", name: "Replacement", platform: "darwin" },
    })).status).toBe(201);
  });

  it("rate-limits installation row creation for authenticated accounts", async () => {
    const owner = await signIn("[email protected]");
    const now = Date.now();
    await env.DB.prepare(
      `INSERT INTO control_action_rate_limits
        (user_id, action, window_started_at, attempts, updated_at)
       VALUES (?, 'create_installation', ?, 100, ?)`,
    ).bind(owner.userId, now, now).run();

    const limited = await call("/v1/installations", {
      method: "POST",
      token: owner.token,
      body: { clientInstanceId: "rate-limited", name: "Limited", platform: "darwin" },
    });
    expect(limited.status).toBe(429);
    await expect(limited.json()).resolves.toEqual({ error: "rate_limited" });
    const count = await env.DB.prepare(
      "SELECT COUNT(*) AS count FROM installations WHERE owner_user_id = ?",
    ).bind(owner.userId).first<{ count: number }>();
    expect(count?.count).toBe(0);
  });

  it("isolates every owner-scoped lookup", async () => {
    const owner = await signIn("[email protected]");
    const other = await signIn("[email protected]");
    const created = await createInstall(owner.token);

    const list = await call("/v1/installations", { token: other.token });
    await expect(list.json()).resolves.toEqual({ installations: [] });
    expect((await call(`/v1/installations/${created.installation.id}/credentials/rotate`, {
      method: "POST",
      token: other.token,
    })).status).toBe(404);
    expect((await call(`/v1/installations/${created.installation.id}`, {
      method: "DELETE",
      token: other.token,
    })).status).toBe(404);
    expect((await call("/v1/installations/self", { token: created.credential })).status).toBe(200);
  });

  it("rotates then revokes credentials", async () => {
    const owner = await signIn("[email protected]");
    const created = await createInstall(owner.token);
    const rotated = await call(`/v1/installations/${created.installation.id}/credentials/rotate`, {
      method: "POST",
      token: owner.token,
    });
    expect(rotated.status).toBe(201);
    const next = await rotated.json<{ credential: string; credentialExpiresAt: number }>();
    expect(next.credential).not.toBe(created.credential);
    expect(next.credentialExpiresAt).toBeGreaterThan(Date.now() + 89 * 24 * 60 * 60 * 1_000);
    expect((await call("/v1/installations/self", { token: created.credential })).status).toBe(401);
    expect((await call("/v1/installations/self", { token: next.credential })).status).toBe(200);

    const revoked = await call(`/v1/installations/${created.installation.id}`, {
      method: "DELETE",
      token: owner.token,
    });
    expect(revoked.status).toBe(204);
    expect((await call("/v1/installations/self", { token: next.credential })).status).toBe(401);
    expect((await call(`/v1/installations/${created.installation.id}`, {
      method: "DELETE",
      token: owner.token,
    })).status).toBe(204);
  });

  it("serializes concurrent credential rotations", async () => {
    const owner = await signIn("[email protected]");
    const created = await createInstall(owner.token);
    const rotatePath = `/v1/installations/${created.installation.id}/credentials/rotate`;
    const responses = await Promise.all([
      call(rotatePath, { method: "POST", token: owner.token }),
      call(rotatePath, { method: "POST", token: owner.token }),
    ]);
    expect(responses.map((response) => response.status).sort()).toEqual([201, 429]);

    const successful = responses.find((response) => response.status === 201);
    if (!successful) throw new Error("one credential rotation must succeed");
    const payload = await successful.json<{ credential: string }>();
    expect((await call("/v1/installations/self", { token: payload.credential })).status).toBe(200);
  });
});

describe("HTTP boundary hardening", () => {
  it("rejects malformed, extra, unsupported, and oversized bodies", async () => {
    const owner = await signIn("[email protected]");
    expect((await call("/v1/installations", {
      method: "POST",
      token: owner.token,
      rawBody: "not-json",
    })).status).toBe(400);
    expect((await call("/v1/installations", {
      method: "POST",
      token: owner.token,
      rawBody: "{}",
      headers: { "content-type": "text/plain" },
    })).status).toBe(415);
    expect((await call("/v1/installations", {
      method: "POST",
      token: owner.token,
      body: { clientInstanceId: "valid", name: "Mac", platform: "darwin", unexpected: true },
    })).status).toBe(400);
    expect((await call("/v1/installations", {
      method: "POST",
      token: owner.token,
      body: { clientInstanceId: "valid", name: "bad\nname", platform: "darwin" },
    })).status).toBe(400);
    expect((await call("/v1/installations", {
      method: "POST",
      token: owner.token,
      body: { clientInstanceId: "valid", name: "Mac", platform: "ios" },
    })).status).toBe(400);
    expect((await call("/v1/installations", {
      method: "POST",
      token: owner.token,
      body: { clientInstanceId: "valid", name: "Mac", platform: "darwin", appVersion: "x".repeat(65) },
    })).status).toBe(400);
    const oversized = await call("/v1/installations", {
      method: "POST",
      token: owner.token,
      rawBody: JSON.stringify({ clientInstanceId: "valid", name: "x".repeat(17 * 1024) }),
    });
    expect(oversized.status).toBe(413);
    await expect(oversized.json()).resolves.toEqual({ error: "request_too_large" });

    const chunkedAuthBody = [
      JSON.stringify({ email: "[email protected]", type: "sign-in", padding: "" }).slice(0, -2),
      "x".repeat(17 * 1024),
      '"}',
    ];
    const oversizedAuth = await call("/api/auth/email-otp/send-verification-otp", {
      method: "POST",
      bodyChunks: chunkedAuthBody,
    });
    expect(oversizedAuth.status).toBe(413);
    await expect(oversizedAuth.json()).resolves.toEqual({ error: "request_too_large" });
    const oversizedVerification = await env.DB.prepare(
      'SELECT COUNT(*) AS count FROM "verification" WHERE identifier LIKE ?',
    ).bind("%[email protected]%").first<{ count: number }>();
    expect(oversizedVerification?.count).toBe(0);
  });

  it("defaults CORS to deny and never emits a wildcard", async () => {
    const blocked = await call("/v1/me", { origin: "https://attacker.example" });
    expect(blocked.status).toBe(403);
    expect(blocked.headers.get("access-control-allow-origin")).toBeNull();
    let serializedHeaders = "";
    blocked.headers.forEach((value, name) => { serializedHeaders += `${name}: ${value}\n`; });
    expect(serializedHeaders).not.toContain("*");

    const allowed = await call("/v1/me", { origin: "https://app.openmausbot.test" });
    expect(allowed.status).toBe(401);
    expect(allowed.headers.get("access-control-allow-origin")).toBe("https://app.openmausbot.test");
    expect(allowed.headers.get("cache-control")).toBe("no-store");

    const deniedPreflight = await call("/v1/installations", {
      method: "OPTIONS",
      origin: "https://app.openmausbot.test",
      headers: {
        "access-control-request-method": "POST",
        "access-control-request-headers": "authorization, x-unexpected",
      },
    });
    expect(deniedPreflight.status).toBe(403);
    expect(deniedPreflight.headers.get("access-control-allow-origin")).toBe("https://app.openmausbot.test");
  });
});