test(background-agent): isolate logger mocks

This commit is contained in:
YeonGyu-Kim
2026-05-30 15:34:40 +09:00
parent 381b75a60a
commit 85eb9e6e0e
4 changed files with 34 additions and 15 deletions
@@ -1,15 +1,12 @@
import { afterAll, beforeEach, describe, expect, mock, test } from "bun:test" import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test"
const logMock = mock(() => {}) const logMock = mock(() => {})
mock.module("../../shared/logger", () => ({
log: logMock,
}))
import type { OpencodeClient } from "./opencode-client" import type { OpencodeClient } from "./opencode-client"
import type { abortWithTimeout as abortWithTimeoutFunction } from "./abort-with-timeout"
const { abortWithTimeout } = await import("./abort-with-timeout") let abortWithTimeout: typeof abortWithTimeoutFunction
mock.restore() let importCounter = 0
function createClient(abort: (...args: Array<unknown>) => Promise<unknown>): OpencodeClient { function createClient(abort: (...args: Array<unknown>) => Promise<unknown>): OpencodeClient {
return { return {
@@ -20,11 +17,16 @@ function createClient(abort: (...args: Array<unknown>) => Promise<unknown>): Ope
} }
describe("abortWithTimeout", () => { describe("abortWithTimeout", () => {
beforeEach(() => { beforeEach(async () => {
logMock.mockClear() logMock.mockClear()
mock.module("../../shared/logger", () => ({
log: logMock,
}))
;({ abortWithTimeout } = await import(`./abort-with-timeout?test=${importCounter}`))
importCounter += 1
}) })
afterAll(() => { afterEach(() => {
mock.restore() mock.restore()
}) })
@@ -24,6 +24,7 @@ type CleanupManager = {
// This prevents bun test from exiting with non-zero code if any test // This prevents bun test from exiting with non-zero code if any test
// called scheduleForcedExit() with exitCode=1 // called scheduleForcedExit() with exitCode=1
afterAll(() => { afterAll(() => {
mock.restore()
process.exitCode = 0 process.exitCode = 0
}) })
@@ -1,12 +1,24 @@
import { describe, test, expect, mock, afterAll } from "bun:test" import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test"
import type {
isActiveSessionStatus as isActiveSessionStatusFunction,
isTerminalSessionStatus as isTerminalSessionStatusFunction,
} from "./session-status-classifier"
const mockLog = mock() const mockLog = mock()
mock.module("../../shared/logger", () => ({ log: mockLog })) let isActiveSessionStatus: typeof isActiveSessionStatusFunction
let isTerminalSessionStatus: typeof isTerminalSessionStatusFunction
let importCounter = 0
afterAll(() => { mock.restore() }) beforeEach(async () => {
mockLog.mockClear()
mock.module("../../shared/logger", () => ({ log: mockLog }))
;({ isActiveSessionStatus, isTerminalSessionStatus } = await import(`./session-status-classifier?test=${importCounter}`))
importCounter += 1
})
const { isActiveSessionStatus, isTerminalSessionStatus } = await import("./session-status-classifier") afterEach(() => {
mock.restore() mock.restore()
})
describe("isActiveSessionStatus", () => { describe("isActiveSessionStatus", () => {
describe("#given a known active session status", () => { describe("#given a known active session status", () => {
+5 -1
View File
@@ -5,7 +5,7 @@
// re-exports this logger) cannot leak a no-op `log` into our imports. See // re-exports this logger) cannot leak a no-op `log` into our imports. See
// script/run-ci-tests.ts — the `mock.module(` substring routes the file out of // script/run-ci-tests.ts — the `mock.module(` substring routes the file out of
// the shared batch. // the shared batch.
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test" import { afterAll, afterEach, beforeEach, describe, expect, mock, test } from "bun:test"
mock.module("./logger-test-isolation", () => ({})) mock.module("./logger-test-isolation", () => ({}))
import * as fs from "fs" import * as fs from "fs"
@@ -16,6 +16,10 @@ type LoggerModule = typeof import("./logger")
const TEST_PREFIX = "oh-my-opencode-logger-test" const TEST_PREFIX = "oh-my-opencode-logger-test"
afterAll(() => {
mock.restore()
})
function makeTempDir(): string { function makeTempDir(): string {
return fs.mkdtempSync(path.join(os.tmpdir(), `${TEST_PREFIX}-`)) return fs.mkdtempSync(path.join(os.tmpdir(), `${TEST_PREFIX}-`))
} }