fix(test): make runner env setup test deterministic

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-04-04 17:17:11 +09:00
parent 179ad05f5f
commit 8c06c8e2fe
+23 -9
View File
@@ -1,14 +1,23 @@
/// <reference types="bun-types" />
import { describe, it, expect, beforeEach, afterEach, vi } from "bun:test"
import type { OhMyOpenCodeConfig } from "../../config"
import { resolveRunAgent, waitForEventProcessorShutdown } from "./runner"
import { afterEach, beforeEach, describe, expect, it, spyOn } from "bun:test"
import { OhMyOpenCodeConfigSchema, type OhMyOpenCodeConfig } from "../../config"
import { resolveRunAgent } from "./agent-resolver"
const createConfig = (overrides: Partial<OhMyOpenCodeConfig> = {}): OhMyOpenCodeConfig => ({
...overrides,
})
const createConfig = (overrides: Partial<OhMyOpenCodeConfig> = {}): OhMyOpenCodeConfig =>
OhMyOpenCodeConfigSchema.parse(overrides)
describe("resolveRunAgent", () => {
let consoleLogSpy: ReturnType<typeof spyOn>
beforeEach(() => {
consoleLogSpy = spyOn(console, "log").mockImplementation(() => {})
})
afterEach(() => {
consoleLogSpy.mockRestore()
})
it("uses CLI agent over env and config", () => {
// given
const config = createConfig({ default_run_agent: "prometheus" })
@@ -85,6 +94,7 @@ describe("resolveRunAgent", () => {
describe("waitForEventProcessorShutdown", () => {
it("returns quickly when event processor completes", async () => {
//#given
const { waitForEventProcessorShutdown } = await import("./runner")
const eventProcessor = new Promise<void>((resolve) => {
setTimeout(() => {
resolve()
@@ -102,6 +112,7 @@ describe("waitForEventProcessorShutdown", () => {
it("times out and continues when event processor does not complete", async () => {
//#given
const { waitForEventProcessorShutdown } = await import("./runner")
const eventProcessor = new Promise<void>(() => {})
const timeoutMs = 200
const start = performance.now()
@@ -118,10 +129,12 @@ describe("waitForEventProcessorShutdown", () => {
describe("run environment setup", () => {
let originalClient: string | undefined
let originalRunMode: string | undefined
let consoleErrorSpy: ReturnType<typeof spyOn>
beforeEach(() => {
originalClient = process.env.OPENCODE_CLIENT
originalRunMode = process.env.OPENCODE_CLI_RUN_MODE
consoleErrorSpy = spyOn(console, "error").mockImplementation(() => {})
})
afterEach(() => {
@@ -135,15 +148,16 @@ describe("run environment setup", () => {
} else {
process.env.OPENCODE_CLI_RUN_MODE = originalRunMode
}
consoleErrorSpy.mockRestore()
})
it("sets OPENCODE_CLIENT to 'run' to exclude question tool from registry", async () => {
//#given
delete process.env.OPENCODE_CLIENT
//#when - run() sets env vars synchronously before any async work
const { run } = await import(`./runner?env-setup-${Date.now()}`)
run({ message: "test" }).catch(() => {})
//#when
const { run } = await import("./runner")
await run({ message: "test", model: "invalid" })
//#then
expect(String(process.env.OPENCODE_CLIENT)).toBe("run")