Skip to content
File

Blob: tests/setup.ts

typescript50 lines
1import { applyD1Migrations } from "cloudflare:test";
2import { env } from "cloudflare:workers";
3import { beforeAll, vi } from "vitest";
4 
5// Global Turnstile bypass for the test pool. Production has no host- or
6// secret-based bypass — verifyTurnstileToken always calls Cloudflare
7// siteverify. Tests mock the verifier outright; the dedicated
8// tests/worker/turnstile.test.ts opts back into the real module via
9// vi.importActual to exercise the verifier itself.
10vi.mock("@/worker/services/turnstile", async () => {
11 const actual = await vi.importActual<typeof import("@/worker/services/turnstile")>("@/worker/services/turnstile");
12 return {
13 ...actual,
14 verifyTurnstileToken: async (
15 _env: Env,
16 options: { token: string },
17 ): Promise<{ ok: true } | { ok: false; status: 400; reason: string; message: string; errorCodes: string[] }> => {
18 if (!options.token?.trim()) {
19 return {
20 ok: false,
21 status: 400,
22 reason: "missing_token",
23 message: actual.TURNSTILE_REQUIRED_MESSAGE,
24 errorCodes: [],
25 };
26 }
27 return { ok: true };
28 },
29 };
30});
31 
32// Glob-import every SQL file in drizzle/d1. The Cloudflare vitest pool
33// recognizes `?raw` and the migrations bag shape it produces.
34const migrationModules = import.meta.glob("../drizzle/d1/*.sql", {
35 query: "?raw",
36 eager: true,
37 import: "default",
38});
39 
40const migrations = Object.entries(migrationModules)
41 .sort(([a], [b]) => a.localeCompare(b))
42 .map(([path, queries]) => ({
43 name: path.split("/").pop() ?? path,
44 queries: typeof queries === "string" ? queries.split(/--> statement-breakpoint\s*/g) : [],
45 }));
46 
47beforeAll(async () => {
48 await applyD1Migrations(env.DB, migrations);
49});