fix: use dependency injection for image-downloader fs mocks

mock.module("fs/promises") on Linux Bun poisons the entire mock
tracking system — not just fs mocks, but ALL mock/spy assertions
(octokit mocks, spyOn(global, "fetch"), etc.) stop recording calls.

Replace with dependency injection: downloadCommentImages() now accepts
an optional fsDeps parameter. Tests pass mock mkdir/writeFile directly.
Production callers omit it and get real fs. No mock.module anywhere.

Fixes 18 test failures blocking CI on Forgejo Linux runners.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Vlad MocanuandClaude Opus 4.6 committed 2026-02-26 10:45:08 +02:00
1 parent 6113a4ef2d
commit a6b3c94004
3 files changed
+54 -16

No files matched your search

+13 -2
View File
@@ -2,6 +2,15 @@ import fs from "fs/promises";
import path from "path";
import type { Octokits } from "../api/client";
/** Injectable fs operations for cross-platform test compatibility.
* On Linux Bun, mock.module("fs/promises") poisons the entire mock tracking
* system, and spyOn(fs, "mkdir") gives test/source different object refs.
* Dependency injection sidesteps all of this. */
export interface FsDeps {
mkdir: typeof fs.mkdir;
writeFile: typeof fs.writeFile;
}
// Build regexes at call time, reading GITHUB_SERVER_URL from process.env
// directly. The config.ts export freezes the value at module load time,
// which can be wrong when the Forgejo CI runner injects its own
@@ -63,11 +72,13 @@ export async function downloadCommentImages(
owner: string,
repo: string,
comments: CommentWithImages[],
fsDeps?: FsDeps,
): Promise<Map<string, string>> {
const { mkdir, writeFile } = fsDeps ?? fs;
const urlToPathMap = new Map<string, string>();
const downloadsDir = "/tmp/github-images";
await fs.mkdir(downloadsDir, { recursive: true });
await mkdir(downloadsDir, { recursive: true });
const { imageRegex, htmlImgRegex } = buildImageRegexes();
@@ -217,7 +228,7 @@ export async function downloadCommentImages(
const arrayBuffer = await imageResponse.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
await fs.writeFile(localPath, buffer);
await writeFile(localPath, buffer);
console.log(`✓ Saved: ${localPath}`);
urlToPathMap.set(originalUrl, localPath);