fix: use mock.module for image-downloader tests

Replace the _fsForTest/DI patterns with Bun's native mock.module() API.
This mocks fs/promises at the module loader level, which works reliably
on Linux where jest.fn() call tracking fails cross-module. The source
code is now clean with no testing artifacts.

Also switches from jest.fn() to mock() (Bun's native API) for Octokit
mocks in these tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Vlad MocanuandClaude Opus 4.6 committed 2026-02-26 09:05:39 +02:00
1 parent cff360d00a
commit dab92ef0eb
2 files changed
+138 -136

No files matched your search

+2 -13
View File
@@ -2,16 +2,6 @@ import fs from "fs/promises";
import path from "path";
import type { Octokits } from "../api/client";
export type FsOps = {
mkdir: typeof fs.mkdir;
writeFile: typeof fs.writeFile;
};
const defaultFsOps: FsOps = {
mkdir: fs.mkdir.bind(fs),
writeFile: fs.writeFile.bind(fs),
};
// 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
@@ -73,12 +63,11 @@ export async function downloadCommentImages(
owner: string,
repo: string,
comments: CommentWithImages[],
fsOps: FsOps = defaultFsOps,
): Promise<Map<string, string>> {
const urlToPathMap = new Map<string, string>();
const downloadsDir = "/tmp/github-images";
await fsOps.mkdir(downloadsDir, { recursive: true });
await fs.mkdir(downloadsDir, { recursive: true });
const { imageRegex, htmlImgRegex } = buildImageRegexes();
@@ -228,7 +217,7 @@ export async function downloadCommentImages(
const arrayBuffer = await imageResponse.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
await fsOps.writeFile(localPath, buffer);
await fs.writeFile(localPath, buffer);
console.log(`✓ Saved: ${localPath}`);
urlToPathMap.set(originalUrl, localPath);