Fix mock isolation in system.test.ts by moving mocks to importFresh

- Move mock.module() calls from top-level into importFreshSystemModule()
- Restore real modules in afterAll to prevent cross-test contamination
- Reset mocks before each test

🤖 GENERATED WITH ASSISTANCE OF OhMyOpenCode
This commit is contained in:
YeonGyu-Kim
2026-04-04 18:56:29 +09:00
parent 89fd302db4
commit 92ba31272d
+26 -19
View File
@@ -6,10 +6,6 @@ import type { PluginInfo } from "./system-plugin"
type SystemModule = typeof import("./system")
async function importFreshSystemModule(): Promise<SystemModule> {
return import(`./system?test=${Date.now()}-${Math.random()}`)
}
const mockFindOpenCodeBinary = mock(async () => ({ path: "/usr/local/bin/opencode" }))
const mockGetOpenCodeVersion = mock(async () => "1.0.200")
const mockCompareVersions = mock((_leftVersion?: string, _rightVersion?: string) => true)
@@ -31,26 +27,37 @@ const mockGetLoadedPluginVersion = mock(() => ({
const mockGetLatestPluginVersion = mock(async (_currentVersion: string | null) => null as string | null)
const mockGetSuggestedInstallTag = mock(() => "latest")
mock.module("./system-binary", () => ({
findOpenCodeBinary: mockFindOpenCodeBinary,
getOpenCodeVersion: mockGetOpenCodeVersion,
compareVersions: mockCompareVersions,
}))
mock.module("./system-plugin", () => ({
getPluginInfo: mockGetPluginInfo,
}))
mock.module("./system-loaded-version", () => ({
getLoadedPluginVersion: mockGetLoadedPluginVersion,
getLatestPluginVersion: mockGetLatestPluginVersion,
getSuggestedInstallTag: mockGetSuggestedInstallTag,
}))
const realSystemBinary = require("./system-binary")
const realSystemPlugin = require("./system-plugin")
const realSystemLoadedVersion = require("./system-loaded-version")
afterAll(() => {
mock.module("./system-binary", () => realSystemBinary)
mock.module("./system-plugin", () => realSystemPlugin)
mock.module("./system-loaded-version", () => realSystemLoadedVersion)
mock.restore()
})
async function importFreshSystemModule(): Promise<SystemModule> {
mock.module("./system-binary", () => ({
findOpenCodeBinary: mockFindOpenCodeBinary,
getOpenCodeVersion: mockGetOpenCodeVersion,
compareVersions: mockCompareVersions,
}))
mock.module("./system-plugin", () => ({
getPluginInfo: mockGetPluginInfo,
}))
mock.module("./system-loaded-version", () => ({
getLoadedPluginVersion: mockGetLoadedPluginVersion,
getLatestPluginVersion: mockGetLatestPluginVersion,
getSuggestedInstallTag: mockGetSuggestedInstallTag,
}))
return import(`./system?test=${Date.now()}-${Math.random()}`)
}
describe("system check", () => {
beforeEach(() => {
mockFindOpenCodeBinary.mockReset()