import { beforeEach, describe, expect, mock, test } from "bun:test";
import { memoryScopes } from "../scoped-db/testing";
type Repo = { id: string; owner_id: string; fork_of_id: string | null };
let repos: Repo[] = [];
let scopes = memoryScopes();
mock.module("cloudflare:workers", () => ({ env: {}, waitUntil: () => undefined }));
mock.module("../db", () => ({
all: async (sql: string, ownerId: string) =>
sql.includes("owner_id = ?") ? repos.filter((repo) => repo.owner_id === ownerId && repo.fork_of_id) : [],
first: async (_sql: string, id: string) => repos.find((repo) => repo.id === id) ?? null,
run: async () => ({ meta: { changes: 0 } }),
batch: async () => [],
stmt: () => ({}),
placeholders: (count: number) => Array(count).fill("?").join(", "),
chunks: <T>(items: T[]) => [items],
MAX_IN_LIST: 90,
}));
mock.module("../scoped-db", () => ({
repoDb: (id: string) => scopes.repoDb(id),
accountDb: (id: string) => scopes.accountDb(id),
stmt: (sql: string, ...params: unknown[]) => ({ sql, params }),
}));
const quota = await import("./bridge-quota");
const GIB = 1024 * 1024 * 1024;
function pull(baseId: string, issueId: string, number: number, headRepoId: string) {
const db = scopes.raw("repo", baseId);
db.query(
"INSERT INTO issues (id, repo_id, number, author_id, title, state, is_pull, created_at, updated_at) VALUES (?, ?, ?, 'u', 't', 'open', 1, 0, 0)",
).run(issueId, baseId, number);
db.query(
"INSERT INTO pull_requests (issue_id, repo_id, head_repo_id, head_ref, head_sha, base_ref, base_sha) VALUES (?, ?, ?, 'f', 'x', 'main', 'y')",
).run(issueId, baseId, headRepoId);
}
beforeEach(() => {
scopes = memoryScopes();