407 lines · 17.9 KB
import { beforeEach, describe, expect, mock, test } from "bun:test";
import { Database } from "bun:sqlite";
import { applyCombinedSchema } from "./test-schema.test-helper";
import type { ReporterKind } from "./abuse";

const published: string[] = [];
mock.module("cloudflare:workers", () => ({
  waitUntil: () => undefined,
  env: {
    PUBLIC_URL: "https://forge.example.com",
    SITES_DOMAIN: "sites.example.net",
    SECRETS_KEY: "test-secret",
    ABUSE_FORWARD_TO: "",
    KV: { put: async (key: string) => void published.push(key) },
  },
}));
mock.module("./sites/directory", () => ({ forgetSiteDirectory: async () => undefined }));

const db = new Database(":memory:");
applyCombinedSchema(db);

mock.module("./db", () => ({
  first: async (sql: string, ...params: unknown[]) => db.query(sql).get(...(params as never[])) ?? null,
  all: async (sql: string, ...params: unknown[]) => db.query(sql).all(...(params as never[])),
  run: async (sql: string, ...params: unknown[]) => ({
    meta: { changes: db.query(sql).run(...(params as never[])).changes },
  }),
  stmt: (sql: string, ...params: unknown[]) => ({ sql, params }),
}));
// Every repo and account object shares the one test database (it still has every table), so seeds stay in one place.
const scoped = {
  first: async (sql: string, ...params: unknown[]) => db.query(sql).get(...(params as never[])) ?? null,
  all: async (sql: string, ...params: unknown[]) => db.query(sql).all(...(params as never[])),
  run: async (sql: string, ...params: unknown[]) => ({
    meta: {
      changes: db.query(sql).run(...(params as never[])).changes,
      last_row_id: 0,
    },
  }),
  batch: async (statements: Array<{ sql: string; params: unknown[] }>) =>