import { describe, expect, mock, test } from "bun:test";
import { Database } from "bun:sqlite";
import { applyCombinedSchema } from "./test-schema.test-helper";
import type { PullRow } from "./pulls";
mock.module("cloudflare:workers", () => ({ env: {}, waitUntil: () => undefined }));
const db = new Database(":memory:");
applyCombinedSchema(db);
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");
const { PULL_SELECT } = await import("./pulls");