import { beforeEach, describe, expect, mock, test } from "bun:test";
import { Database } from "bun:sqlite";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
mock.module("cloudflare:workers", () => ({ env: { PUBLIC_URL: "https://forge.example.com" } }));
mock.module("@tanstack/react-start/server", () => ({ getRequestHeader: () => 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"));
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[]) => {
const result = db.query(sql).run(...(params as never[]));
return { meta: { changes: result.changes } };
},
batch: async (statements: { sql: string; params: unknown[] }[]) => {
for (const statement of statements) db.query(statement.sql).run(...(statement.params as never[]));
return [];
},
stmt: (sql: string, ...params: unknown[]) => ({ sql, params }),
placeholders: (count: number) => Array(count).fill("?").join(", "),
chunks: <T>(items: T[]) => [items],
MAX_IN_LIST: 90,
}));
const { purgeFollowerIfRevoked, purgeInaccessibleFollowers, purgeOrgFollowers } = await import("./access-revocation");
const { repoPermission } = await import("./repos");
function seed(): void {
db.exec("PRAGMA foreign_keys = ON");
db.exec(`
DELETE FROM stars; DELETE FROM watches; DELETE FROM thread_subscriptions; DELETE FROM issues;
DELETE FROM collaborators; DELETE FROM org_members; DELETE FROM repos; DELETE FROM users; DELETE FROM accounts;
INSERT INTO accounts (id, name, kind, created_at) VALUES ('org', 'acme', 'org', 0), ('owner', 'owner', 'user', 0),
('member', 'member', 'user', 0), ('collab', 'collab', 'user', 0), ('stranger', 'stranger', 'user', 0), ('root', 'root', 'user', 0);
INSERT INTO users (account_id, email, is_admin) VALUES ('owner', 'o@x', 0), ('member', 'm@x', 0), ('collab', 'c@x', 0),