240 lines · 10.4 KB
import { beforeEach, describe, expect, mock, test } from "bun:test";
import { Database } from "bun:sqlite";
import { applyCombinedSchema } from "./test-schema.test-helper";
import type { Viewer } from "~/lib/types";

const kv = new Map<string, string>();
const destroyed: string[] = [];
class StubEntrypoint {}
mock.module("cloudflare:workers", () => ({
  env: {
    PUBLIC_URL: "https://forge.example.com",
    ACCOUNT_STATE: {
      idFromName: (name: string) => name,
      get: (id: string) => ({ destroy: async () => void destroyed.push(id) }),
    },
    KV: {
      get: async (key: string, type?: string) => {
        const value = kv.get(key) ?? null;
        return type === "json" && value !== null ? JSON.parse(value) : value;
      },
      put: async (key: string, value: string) => {
        kv.set(key, value);
      },
      delete: async (key: string) => {
        kv.delete(key);
      },
    },
  },
  waitUntil: () => undefined,
  WorkflowEntrypoint: StubEntrypoint,
  DurableObject: StubEntrypoint,
  WorkerEntrypoint: StubEntrypoint,
}));
mock.module("@tanstack/react-start/server", () => ({ getRequestHeader: () => 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,