test: isolate bulk-mirror destination tests in a child process

bun's mock.module and the globalThis.fetch swap are process-wide; on CI
(bun 1.3.13) this file's mocks of @/lib/db and @/lib/gitea-enhanced leaked
into gitea-enhanced.test.ts and stuck-status-recovery tests, failing main.
The file now registers nothing in the shared test process and instead
re-runs itself via bun test in a child process where the mocks are contained.
This commit is contained in:
Arunavo Ray
2026-07-16 21:46:08 +05:30
parent b3aad9d80f
commit bb57b52de3
+35 -8
View File
@@ -3,15 +3,34 @@
* bulk "Mirror Organization" must honor the canonical destination
* precedence (starred mode > repo override > org override > strategy).
*
* NOTE: run standalone (`bun test src/lib/gitea-org-mirror-destination.test.ts`).
* These tests replace @/lib/db, @/lib/gitea-enhanced, @/lib/http-client,
* @/lib/helpers, and @/lib/utils/mirror-source-match with module mocks;
* bun's mock.module is process-wide, so running this file in the full
* suite could pollute other test files (see the comment in
* gitea-mirror-failure-recovery.test.ts).
* NOTE: these tests replace @/lib/db, @/lib/gitea-enhanced, @/lib/http-client,
* @/lib/helpers, and globalThis.fetch with mocks. bun's mock.module (and the
* fetch swap) are process-wide and leak into other test files, so in the
* shared test process this file registers NOTHING: it re-runs itself in an
* isolated child process (`bun test <this file>` with GITEA_ORG_MIRROR_DEST_ISOLATED=1)
* where the mocks are safely contained.
*/
import { describe, test, expect, mock, beforeEach } from "bun:test";
const CHILD_FLAG = "GITEA_ORG_MIRROR_DEST_ISOLATED";
const isChild = !!process.env[CHILD_FLAG];
if (!isChild) {
test("bulk org mirror destination routing (#343) — isolated child suite", () => {
const res = Bun.spawnSync({
cmd: [process.execPath, "test", import.meta.path],
env: { ...process.env, [CHILD_FLAG]: "1" },
stdout: "pipe",
stderr: "pipe",
});
if (res.exitCode !== 0) {
console.error(res.stdout.toString());
console.error(res.stderr.toString());
}
expect(res.exitCode).toBe(0);
}, 60_000);
}
// ---------------------------------------------------------------------------
// Shared mutable state the module mocks read from / write to
// ---------------------------------------------------------------------------
@@ -49,6 +68,10 @@ function promiseWithLimit(rows: any[], limitRows?: any[]) {
return p;
}
// Everything below only exists in the isolated child process — registering
// these mocks in the shared test process poisons other test files.
if (isChild) {
mock.module("@/lib/db", () => {
const mockDb = {
select: (_fields?: any) => ({
@@ -135,7 +158,11 @@ globalThis.fetch = mock(async () =>
new Response("not found", { status: 404 })
) as any;
const { mirrorGitHubOrgToGitea } = await import("./gitea");
} // end if (isChild)
const { mirrorGitHubOrgToGitea } = isChild
? await import("./gitea")
: ({ mirrorGitHubOrgToGitea: undefined as any });
// ---------------------------------------------------------------------------
// Fixtures
@@ -227,7 +254,7 @@ beforeEach(() => {
// Tests
// ---------------------------------------------------------------------------
describe("mirrorGitHubOrgToGitea destination routing (#343)", () => {
describe.skipIf(!isChild)("mirrorGitHubOrgToGitea destination routing (#343)", () => {
test("Scenario 1: preserve strategy honors organization destinationOrg override", async () => {
const config = makeConfig({ githubConfig: { mirrorStrategy: "preserve" } });