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
+14 -7
View File
@@ -6,10 +6,6 @@ import type { PluginInfo } from "./system-plugin"
type SystemModule = typeof import("./system") 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 mockFindOpenCodeBinary = mock(async () => ({ path: "/usr/local/bin/opencode" }))
const mockGetOpenCodeVersion = mock(async () => "1.0.200") const mockGetOpenCodeVersion = mock(async () => "1.0.200")
const mockCompareVersions = mock((_leftVersion?: string, _rightVersion?: string) => true) const mockCompareVersions = mock((_leftVersion?: string, _rightVersion?: string) => true)
@@ -31,6 +27,18 @@ const mockGetLoadedPluginVersion = mock(() => ({
const mockGetLatestPluginVersion = mock(async (_currentVersion: string | null) => null as string | null) const mockGetLatestPluginVersion = mock(async (_currentVersion: string | null) => null as string | null)
const mockGetSuggestedInstallTag = mock(() => "latest") const mockGetSuggestedInstallTag = mock(() => "latest")
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", () => ({ mock.module("./system-binary", () => ({
findOpenCodeBinary: mockFindOpenCodeBinary, findOpenCodeBinary: mockFindOpenCodeBinary,
getOpenCodeVersion: mockGetOpenCodeVersion, getOpenCodeVersion: mockGetOpenCodeVersion,
@@ -47,9 +55,8 @@ mock.module("./system-loaded-version", () => ({
getSuggestedInstallTag: mockGetSuggestedInstallTag, getSuggestedInstallTag: mockGetSuggestedInstallTag,
})) }))
afterAll(() => { return import(`./system?test=${Date.now()}-${Math.random()}`)
mock.restore() }
})
describe("system check", () => { describe("system check", () => {
beforeEach(() => { beforeEach(() => {