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:
@@ -1,14 +1,23 @@
|
|||||||
/// <reference types="bun-types" />
|
/// <reference types="bun-types" />
|
||||||
|
|
||||||
import { describe, it, expect, beforeEach, afterEach, vi } from "bun:test"
|
import { afterEach, beforeEach, describe, expect, it, spyOn } from "bun:test"
|
||||||
import type { OhMyOpenCodeConfig } from "../../config"
|
import { OhMyOpenCodeConfigSchema, type OhMyOpenCodeConfig } from "../../config"
|
||||||
import { resolveRunAgent, waitForEventProcessorShutdown } from "./runner"
|
import { resolveRunAgent } from "./agent-resolver"
|
||||||
|
|
||||||
const createConfig = (overrides: Partial<OhMyOpenCodeConfig> = {}): OhMyOpenCodeConfig => ({
|
const createConfig = (overrides: Partial<OhMyOpenCodeConfig> = {}): OhMyOpenCodeConfig =>
|
||||||
...overrides,
|
OhMyOpenCodeConfigSchema.parse(overrides)
|
||||||
})
|
|
||||||
|
|
||||||
describe("resolveRunAgent", () => {
|
describe("resolveRunAgent", () => {
|
||||||
|
let consoleLogSpy: ReturnType<typeof spyOn>
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
consoleLogSpy = spyOn(console, "log").mockImplementation(() => {})
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
consoleLogSpy.mockRestore()
|
||||||
|
})
|
||||||
|
|
||||||
it("uses CLI agent over env and config", () => {
|
it("uses CLI agent over env and config", () => {
|
||||||
// given
|
// given
|
||||||
const config = createConfig({ default_run_agent: "prometheus" })
|
const config = createConfig({ default_run_agent: "prometheus" })
|
||||||
@@ -85,6 +94,7 @@ describe("resolveRunAgent", () => {
|
|||||||
describe("waitForEventProcessorShutdown", () => {
|
describe("waitForEventProcessorShutdown", () => {
|
||||||
it("returns quickly when event processor completes", async () => {
|
it("returns quickly when event processor completes", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { waitForEventProcessorShutdown } = await import("./runner")
|
||||||
const eventProcessor = new Promise<void>((resolve) => {
|
const eventProcessor = new Promise<void>((resolve) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
resolve()
|
resolve()
|
||||||
@@ -102,6 +112,7 @@ describe("waitForEventProcessorShutdown", () => {
|
|||||||
|
|
||||||
it("times out and continues when event processor does not complete", async () => {
|
it("times out and continues when event processor does not complete", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { waitForEventProcessorShutdown } = await import("./runner")
|
||||||
const eventProcessor = new Promise<void>(() => {})
|
const eventProcessor = new Promise<void>(() => {})
|
||||||
const timeoutMs = 200
|
const timeoutMs = 200
|
||||||
const start = performance.now()
|
const start = performance.now()
|
||||||
@@ -118,10 +129,12 @@ describe("waitForEventProcessorShutdown", () => {
|
|||||||
describe("run environment setup", () => {
|
describe("run environment setup", () => {
|
||||||
let originalClient: string | undefined
|
let originalClient: string | undefined
|
||||||
let originalRunMode: string | undefined
|
let originalRunMode: string | undefined
|
||||||
|
let consoleErrorSpy: ReturnType<typeof spyOn>
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
originalClient = process.env.OPENCODE_CLIENT
|
originalClient = process.env.OPENCODE_CLIENT
|
||||||
originalRunMode = process.env.OPENCODE_CLI_RUN_MODE
|
originalRunMode = process.env.OPENCODE_CLI_RUN_MODE
|
||||||
|
consoleErrorSpy = spyOn(console, "error").mockImplementation(() => {})
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -135,15 +148,16 @@ describe("run environment setup", () => {
|
|||||||
} else {
|
} else {
|
||||||
process.env.OPENCODE_CLI_RUN_MODE = originalRunMode
|
process.env.OPENCODE_CLI_RUN_MODE = originalRunMode
|
||||||
}
|
}
|
||||||
|
consoleErrorSpy.mockRestore()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("sets OPENCODE_CLIENT to 'run' to exclude question tool from registry", async () => {
|
it("sets OPENCODE_CLIENT to 'run' to exclude question tool from registry", async () => {
|
||||||
//#given
|
//#given
|
||||||
delete process.env.OPENCODE_CLIENT
|
delete process.env.OPENCODE_CLIENT
|
||||||
|
|
||||||
//#when - run() sets env vars synchronously before any async work
|
//#when
|
||||||
const { run } = await import(`./runner?env-setup-${Date.now()}`)
|
const { run } = await import("./runner")
|
||||||
run({ message: "test" }).catch(() => {})
|
await run({ message: "test", model: "invalid" })
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(String(process.env.OPENCODE_CLIENT)).toBe("run")
|
expect(String(process.env.OPENCODE_CLIENT)).toBe("run")
|
||||||
|
|||||||
Reference in New Issue
Block a user