test: localize mock.module setup to fresh imports
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user