102 lines · 4.7 KB
import { describe, expect, mock, test } from "bun:test";
import { Database } from "bun:sqlite";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import type { Viewer } from "~/lib/types";

mock.module("cloudflare:workers", () => ({
  env: { PUBLIC_URL: "https://forge.example.com" },
  waitUntil: () => undefined,
  WorkflowEntrypoint: class {},
  DurableObject: class {},
  WorkerEntrypoint: class {},
}));
mock.module("@tanstack/react-start/server", () => ({
  getRequestHeader: () => undefined,
  getRequest: () => undefined,
  deleteCookie: () => undefined,
  getCookie: () => undefined,
  setCookie: () => undefined,
  setResponseHeader: () => undefined,
  setResponseStatus: () => undefined,
}));

const db = new Database(":memory:");
const migrations = join(import.meta.dir, "../../migrations");
for (const file of readdirSync(migrations).sort()) db.exec(readFileSync(join(migrations, file), "utf8"));

type Statement = { sql: string; params: unknown[] };
const exec = (sql: string, params: unknown[]) => db.query(sql).run(...(params as never[]));
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: exec(sql, params).changes } }),
  batch: async (statements: Statement[]) => statements.map((statement) => exec(statement.sql, statement.params)),
  stmt: (sql: string, ...params: unknown[]): Statement => ({ sql, params }),
  placeholders: (count: number) => Array(count).fill("?").join(", "),
  chunks: <T>(items: T[]) => [items],
  MAX_IN_LIST: 90,
}));
const actualBridge = await import("./git/pull-bridge");