Skip to content

Commit d0b7b6e

Browse files
fix: [#939] inject serve-upgrade-check collaborators instead of mock.module
CI "TypeScript" job failed: `SyntaxError: Export named 'isValidVersion' not found in module cli/upgrade.ts`. Cause was the new test — bun's `mock.module` is process-global, so mocking `../upgrade` (and `../../project/instance`, `../../project/bootstrap`) replaced those modules for the entire test run, stripping `cli/upgrade`'s other exports (`compareVersions`, `isValidVersion`) that `upgrade-decision.test.ts` imports. It passed locally only because the file was run in isolation. Replace module-mocking with dependency injection: `runStartupUpgradeCheck` takes an optional `StartupUpgradeDeps` ({ provide, run }) defaulting to the real `Instance.provide`(no-dispose) + `upgrade`. The test injects fakes directly — no global module state touched. Added a case for `provide()` itself rejecting. Verified in the combined suite (previously-colliding files together, then full test/cli): 512 pass, 0 fail; typecheck clean.
1 parent 49f64ff commit d0b7b6e

2 files changed

Lines changed: 86 additions & 64 deletions

File tree

‎packages/opencode/src/cli/cmd/serve-upgrade-check.ts‎

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,50 @@ const log = Log.create({ service: "serve" })
1010
export const STARTUP_UPGRADE_DELAY_MS = 1000
1111

1212
/**
13-
* Runs a single best-effort upgrade check, mirroring the TUI worker
14-
* (`cli/cmd/tui/worker.ts` → `checkUpgrade`): provide an `Instance` context for
15-
* `upgrade()` but — exactly like the worker — do NOT dispose it.
13+
* Collaborators for {@link runStartupUpgradeCheck}, injectable for tests.
1614
*
17-
* `upgrade()` reads only global config + `Installation`, but `Bus.publish`
18-
* (its update notifications) needs an ambient instance. We use the same
19-
* `process.cwd()` key the server's default-directory requests use
20-
* (`server/server.ts:196`), so we reuse/seed that shared cached instance rather
21-
* than a throwaway one, and Bus notifications still reach default-directory SSE
22-
* subscribers.
23-
*
24-
* Crucially we never dispose: an earlier version wrapped this in `bootstrap()`,
25-
* whose `finally` → `Instance.dispose()` tears down the entire `process.cwd()`
26-
* bucket — including state created by concurrent server requests that defaulted
27-
* to that directory (use-after-dispose / needless churn). The worker avoids
28-
* this by running in a separate thread; in-process we avoid it by not disposing.
29-
*
30-
* Resolves, never rejects: `upgrade()` swallows its own errors via the inner
31-
* catch; the outer guard only fires for an `Instance.provide` failure.
15+
* Injected rather than module-mocked on purpose: bun's `mock.module` is
16+
* process-global, so mocking `../upgrade` / `../../project/instance` here would
17+
* clobber those modules for every other test in the run (e.g. blow away
18+
* `cli/upgrade`'s `compareVersions`/`isValidVersion` exports).
19+
*/
20+
export interface StartupUpgradeDeps {
21+
/**
22+
* Runs `fn` inside an ambient `Instance` for `directory` and, like the TUI
23+
* worker, does NOT dispose it (see note on the default below).
24+
*/
25+
provide: (directory: string, fn: () => Promise<unknown>) => Promise<unknown>
26+
/** The upgrade check itself. */
27+
run: () => Promise<unknown>
28+
}
29+
30+
const defaultDeps: StartupUpgradeDeps = {
31+
// Mirror the TUI worker (cli/cmd/tui/worker.ts → checkUpgrade): provide an
32+
// Instance context for upgrade() — Bus.publish needs one — but never dispose.
33+
//
34+
// We use the same process.cwd() key the server's default-directory requests
35+
// use (server/server.ts:196), so we reuse/seed that shared cached instance and
36+
// Bus notifications still reach default-directory SSE subscribers. Crucially we
37+
// do NOT dispose: an earlier version wrapped this in bootstrap(), whose
38+
// finally → Instance.dispose() tears down the entire process.cwd() bucket —
39+
// including state created by concurrent server requests that defaulted to that
40+
// directory (use-after-dispose / needless churn). The worker avoids this by
41+
// running in a separate thread; in-process we avoid it by not disposing.
42+
provide: (directory, fn) => Instance.provide({ directory, init: InstanceBootstrap, fn }),
43+
run: upgrade,
44+
}
45+
46+
/**
47+
* Runs a single best-effort upgrade check. Resolves, never rejects: `run()`
48+
* (upgrade) swallows its own errors via the inner catch; the outer guard only
49+
* fires for an `Instance.provide` failure. A flaky network/registry therefore
50+
* can't take the server down.
3251
*/
33-
export async function runStartupUpgradeCheck(): Promise<void> {
52+
export async function runStartupUpgradeCheck(deps: StartupUpgradeDeps = defaultDeps): Promise<void> {
3453
try {
35-
await Instance.provide({
36-
directory: process.cwd(),
37-
init: InstanceBootstrap,
38-
fn: () => upgrade().catch((err) => log.error("startup upgrade check failed", { error: String(err) })),
39-
})
54+
await deps.provide(process.cwd(), () =>
55+
deps.run().catch((err) => log.error("startup upgrade check failed", { error: String(err) })),
56+
)
4057
} catch (err) {
4158
log.error("startup upgrade instance failed", { error: String(err) })
4259
}

‎packages/opencode/test/cli/serve-upgrade-check.test.ts‎

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,64 @@
1-
import { beforeEach, describe, expect, mock, test } from "bun:test"
1+
import { beforeEach, describe, expect, test } from "bun:test"
22
import { Log } from "../../src/util/log"
3+
import { runStartupUpgradeCheck, STARTUP_UPGRADE_DELAY_MS, type StartupUpgradeDeps } from "../../src/cli/cmd/serve-upgrade-check"
34

45
Log.init({ print: false })
56

6-
// State the mocks record so each test can assert orchestration.
7-
let upgradeCalls = 0
8-
let upgradeShouldThrow = false
9-
let provideDirectory: string | undefined
10-
let provideCalls = 0
11-
12-
// Mirror the real Instance.provide contract just enough: capture the directory
13-
// key and run the supplied fn (no real instance, no dispose).
14-
mock.module("../../src/project/instance", () => ({
15-
Instance: {
16-
provide: async (input: { directory: string; fn: () => Promise<unknown> }) => {
17-
provideCalls++
18-
provideDirectory = input.directory
19-
return input.fn()
20-
},
21-
},
22-
}))
23-
24-
mock.module("../../src/project/bootstrap", () => ({
25-
InstanceBootstrap: async () => {},
26-
}))
27-
28-
mock.module("../../src/cli/upgrade", () => ({
29-
upgrade: async () => {
30-
upgradeCalls++
31-
if (upgradeShouldThrow) throw new Error("boom")
32-
},
33-
}))
34-
35-
const { runStartupUpgradeCheck, STARTUP_UPGRADE_DELAY_MS } = await import("../../src/cli/cmd/serve-upgrade-check")
36-
7+
// No mock.module here: it is process-global in bun and would clobber
8+
// ../upgrade / ../../project/instance for every other test in the run. The
9+
// collaborators are injected instead.
3710
describe("serve-upgrade-check", () => {
11+
let runCalls: number
12+
let runShouldThrow: boolean
13+
let provideCalls: number
14+
let provideDirectory: string | undefined
15+
16+
function makeDeps(): StartupUpgradeDeps {
17+
return {
18+
provide: async (directory, fn) => {
19+
provideCalls++
20+
provideDirectory = directory
21+
return fn()
22+
},
23+
run: async () => {
24+
runCalls++
25+
if (runShouldThrow) throw new Error("boom")
26+
},
27+
}
28+
}
29+
3830
beforeEach(() => {
39-
upgradeCalls = 0
40-
upgradeShouldThrow = false
41-
provideDirectory = undefined
31+
runCalls = 0
32+
runShouldThrow = false
4233
provideCalls = 0
34+
provideDirectory = undefined
4335
})
4436

4537
test("runs upgrade() once inside the process.cwd() instance", async () => {
46-
await runStartupUpgradeCheck()
47-
expect(upgradeCalls).toBe(1)
38+
await runStartupUpgradeCheck(makeDeps())
39+
expect(runCalls).toBe(1)
4840
expect(provideCalls).toBe(1)
4941
expect(provideDirectory).toBe(process.cwd())
5042
})
5143

5244
test("resolves without throwing when upgrade() rejects", async () => {
53-
upgradeShouldThrow = true
45+
runShouldThrow = true
5446
// Must not reject — a flaky network/registry can't take the server down.
55-
await expect(runStartupUpgradeCheck()).resolves.toBeUndefined()
56-
expect(upgradeCalls).toBe(1)
47+
await expect(runStartupUpgradeCheck(makeDeps())).resolves.toBeUndefined()
48+
expect(runCalls).toBe(1)
49+
})
50+
51+
test("resolves without throwing when provide() itself rejects", async () => {
52+
const deps: StartupUpgradeDeps = {
53+
provide: async () => {
54+
throw new Error("instance boom")
55+
},
56+
run: async () => {
57+
runCalls++
58+
},
59+
}
60+
await expect(runStartupUpgradeCheck(deps)).resolves.toBeUndefined()
61+
expect(runCalls).toBe(0)
5762
})
5863

5964
test("uses a short, sane settle delay", () => {

0 commit comments

Comments
 (0)