136 lines · 5.6 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 { PullRow } from "./pulls";

mock.module("cloudflare:workers", () => ({ env: {}, waitUntil: () => 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"));

const bind = (params: unknown[]) => params as never[];
mock.module("./db", () => ({
  first: async (sql: string, ...params: unknown[]) => db.query(sql).get(...bind(params)) ?? null,
  all: async (sql: string, ...params: unknown[]) => db.query(sql).all(...bind(params)),
  run: async (sql: string, ...params: unknown[]) => db.query(sql).run(...bind(params)),
  batch: async () => [],
  stmt: (sql: string, ...params: unknown[]) => ({ sql, params }),
  placeholders: (count: number) => Array(count).fill("?").join(", "),
  chunks: <T>(items: T[]) => [items],
  MAX_IN_LIST: 90,
}));

// The test database holds both the directory and the repository tables, so the repo scope reads it too.
mock.module("./scoped-db", () => ({
  repoDb: (scopeId: string) => ({
    scopeId,
    first: async (sql: string, ...params: unknown[]) => db.query(sql).get(...bind(params)) ?? null,
    all: async (sql: string, ...params: unknown[]) => db.query(sql).all(...bind(params)),
    run: async (sql: string, ...params: unknown[]) => db.query(sql).run(...bind(params)),
    batch: async () => [],
  }),
  accountDb: () => {
    throw new Error("not used");
  },
  stmt: (sql: string, ...params: unknown[]) => ({ sql, params }),
}));

const { loadStack, mergePath, stackChildren, stackParent } = await import("./stacks");