import { applyD1Migrations } from "cloudflare:test"; import { env } from "cloudflare:workers"; import { beforeAll, vi } from "vitest"; // Global Turnstile bypass for the test pool. Production has no host- or // secret-based bypass — verifyTurnstileToken always calls Cloudflare // siteverify. Tests mock the verifier outright; the dedicated // tests/worker/turnstile.test.ts opts back into the real module via // vi.importActual to exercise the verifier itself. vi.mock("@/worker/services/turnstile", async () => { const actual = await vi.importActual("@/worker/services/turnstile"); return { ...actual, verifyTurnstileToken: async ( _env: Env, options: { token: string }, ): Promise<{ ok: true } | { ok: false; status: 400; reason: string; message: string; errorCodes: string[] }> => { if (!options.token?.trim()) { return { ok: false, status: 400, reason: "missing_token", message: actual.TURNSTILE_REQUIRED_MESSAGE, errorCodes: [], }; } return { ok: true }; }, }; }); // Glob-import every SQL file in drizzle/d1. The Cloudflare vitest pool // recognizes `?raw` and the migrations bag shape it produces. const migrationModules = import.meta.glob("../drizzle/d1/*.sql", { query: "?raw", eager: true, import: "default", }); const migrations = Object.entries(migrationModules) .sort(([a], [b]) => a.localeCompare(b)) .map(([path, queries]) => ({ name: path.split("/").pop() ?? path, queries: typeof queries === "string" ? queries.split(/--> statement-breakpoint\s*/g) : [], })); beforeAll(async () => { await applyD1Migrations(env.DB, migrations); });