test: localize mock.module setup to fresh imports

This commit is contained in:
YeonGyu-Kim
2026-04-04 19:49:25 +09:00
parent 8b8559f39d
commit a4db240d47
9 changed files with 204 additions and 158 deletions
+12 -12
View File
@@ -18,18 +18,6 @@ const mockAutoMigrate = mock((): MigrationResult => ({
const mockShowToast = mock((_arg: any) => Promise.resolve())
const mockLog = mock(() => {})
mock.module("../../shared/legacy-plugin-warning", () => ({
checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
}))
mock.module("../../shared/logger", () => ({
log: mockLog,
}))
mock.module("./auto-migrate-runner", () => ({
autoMigrateLegacyPluginEntry: mockAutoMigrate,
}))
afterAll(() => {
mock.restore()
})
@@ -53,6 +41,18 @@ function createEvent(type: string, parentID?: string) {
}
async function importFreshModule() {
mock.module("../../shared/legacy-plugin-warning", () => ({
checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
}))
mock.module("../../shared/logger", () => ({
log: mockLog,
}))
mock.module("./auto-migrate-runner", () => ({
autoMigrateLegacyPluginEntry: mockAutoMigrate,
}))
const module = await import(`./hook?t=${Date.now()}-${Math.random()}`)
mock.restore()
return module
+20 -15
View File
@@ -40,30 +40,35 @@ const transformModelForProviderMock = mock((provider: string, model: string) =>
return model
})
mock.module("../../shared/connected-providers-cache", () => ({
readConnectedProvidersCache: readConnectedProvidersCacheMock,
readProviderModelsCache: readProviderModelsCacheMock,
}))
mock.module("../../shared/provider-model-id-transform", () => ({
transformModelForProvider: transformModelForProviderMock,
}))
mock.module("../../shared/model-error-classifier", () => ({
selectFallbackProvider: selectFallbackProviderMock,
}))
afterAll(() => {
mock.restore()
})
async function importFreshModelFallbackHookModule() {
mock.module("../../shared/connected-providers-cache", () => ({
readConnectedProvidersCache: readConnectedProvidersCacheMock,
readProviderModelsCache: readProviderModelsCacheMock,
}))
mock.module("../../shared/provider-model-id-transform", () => ({
transformModelForProvider: transformModelForProviderMock,
}))
mock.module("../../shared/model-error-classifier", () => ({
selectFallbackProvider: selectFallbackProviderMock,
}))
const module = await import(`./hook?test=${Date.now()}-${Math.random()}`)
mock.restore()
return module
}
const {
clearPendingModelFallback,
createModelFallbackHook,
setSessionFallbackChain,
setPendingModelFallback,
} = await import("./hook")
mock.restore()
} = await importFreshModelFallbackHookModule()
describe("model fallback hook", () => {
beforeEach(() => {
+42 -45
View File
@@ -1,4 +1,4 @@
import { afterAll, afterEach, beforeEach, describe, expect, it, mock } from "bun:test";
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test";
import * as fs from "node:fs";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import * as os from "node:os";
@@ -17,45 +17,6 @@ const originalReadFileSync = fs.readFileSync.bind(fs);
const originalStatSync = fs.statSync.bind(fs);
const originalHomedir = os.homedir.bind(os);
mock.module("node:fs", () => ({
...fs,
readFileSync: (filePath: string, encoding?: string) => {
if (filePath === trackedRulePath) {
trackedReadFileCount += 1;
}
return originalReadFileSync(filePath, encoding as never);
},
statSync: (filePath: string) => {
if (filePath === trackedRulePath) {
const next = statSnapshots.shift();
if (next instanceof Error) {
throw next;
}
if (next) {
return {
mtimeMs: next.mtimeMs,
size: next.size,
isFile: () => true,
} as ReturnType<typeof originalStatSync>;
}
}
return originalStatSync(filePath);
},
}));
mock.module("node:os", () => ({
...os,
homedir: () => mockedHomeDir || originalHomedir(),
}));
mock.module("./matcher", () => ({
shouldApplyRule: () => ({ applies: true, reason: "matched" }),
isDuplicateByRealPath: (realPath: string, cache: Set<string>) =>
cache.has(realPath),
createContentHash: (content: string) => `hash:${content}`,
isDuplicateByContentHash: (hash: string, cache: Set<string>) => cache.has(hash),
}));
function createOutput(): { title: string; output: string; metadata: unknown } {
return { title: "tool", output: "", metadata: {} };
}
@@ -67,7 +28,47 @@ async function createProcessor(projectRoot: string): Promise<{
output: { title: string; output: string; metadata: unknown }
) => Promise<void>;
}> {
const { createRuleInjectionProcessor } = await import("./injector");
mock.module("node:fs", () => ({
...fs,
readFileSync: (filePath: string, encoding?: string) => {
if (filePath === trackedRulePath) {
trackedReadFileCount += 1;
}
return originalReadFileSync(filePath, encoding as never);
},
statSync: (filePath: string) => {
if (filePath === trackedRulePath) {
const next = statSnapshots.shift();
if (next instanceof Error) {
throw next;
}
if (next) {
return {
mtimeMs: next.mtimeMs,
size: next.size,
isFile: () => true,
} as ReturnType<typeof originalStatSync>;
}
}
return originalStatSync(filePath);
},
}));
mock.module("node:os", () => ({
...os,
homedir: () => mockedHomeDir || originalHomedir(),
}));
mock.module("./matcher", () => ({
shouldApplyRule: () => ({ applies: true, reason: "matched" }),
isDuplicateByRealPath: (realPath: string, cache: Set<string>) =>
cache.has(realPath),
createContentHash: (content: string) => `hash:${content}`,
isDuplicateByContentHash: (hash: string, cache: Set<string>) => cache.has(hash),
}));
const { createRuleInjectionProcessor } = await import(`./injector?test=${Date.now()}-${Math.random()}`);
mock.restore();
const sessionCaches = new Map<
string,
{ contentHashes: Set<string>; realPaths: Set<string> }
@@ -102,10 +103,6 @@ function getInjectedRulesPath(sessionID: string): string {
}
describe("createRuleInjectionProcessor", () => {
afterAll(() => {
mock.restore();
});
let testRoot: string;
let projectRoot: string;
let homeRoot: string;