diff --git a/src/shared/tmux/tmux-utils.test.ts b/src/shared/tmux/tmux-utils.test.ts index 3e9f2c0d9..c5c4ea243 100644 --- a/src/shared/tmux/tmux-utils.test.ts +++ b/src/shared/tmux/tmux-utils.test.ts @@ -3,6 +3,7 @@ import { isInsideTmux, isServerRunning, resetServerCheck, + markServerRunningInProcess, spawnTmuxPane, closeTmuxPane, applyLayout, @@ -165,6 +166,46 @@ describe("resetServerCheck", () => { }) }) +describe("markServerRunningInProcess", () => { + const originalFetch = globalThis.fetch + const SERVER_RUNNING_KEY = Symbol.for("oh-my-opencode:server-running-in-process") + + beforeEach(() => { + resetServerCheck() + delete (globalThis as Record)[SERVER_RUNNING_KEY] + }) + + afterEach(() => { + globalThis.fetch = originalFetch + delete (globalThis as Record)[SERVER_RUNNING_KEY] + }) + + test("skips HTTP fetch when marked as running in-process", async () => { + // given + const fetchMock = mock(async () => ({ ok: true })) as any + globalThis.fetch = fetchMock + markServerRunningInProcess() + + // when + const result = await isServerRunning("http://localhost:4096") + + // then + expect(result).toBe(true) + expect(fetchMock.mock.calls.length).toBe(0) + }) + + test("uses globalThis so flag survives across module instances", () => { + // given + markServerRunningInProcess() + + // when + const flag = (globalThis as Record)[SERVER_RUNNING_KEY] + + // then + expect(flag).toBe(true) + }) +}) + describe("tmux pane functions", () => { test("spawnTmuxPane is exported as function", async () => { // given, #when diff --git a/src/shared/tmux/tmux-utils.ts b/src/shared/tmux/tmux-utils.ts index df4d417d6..fe978eadb 100644 --- a/src/shared/tmux/tmux-utils.ts +++ b/src/shared/tmux/tmux-utils.ts @@ -1,7 +1,7 @@ export { isInsideTmux, getCurrentPaneId } from "./tmux-utils/environment" export type { SplitDirection } from "./tmux-utils/environment" -export { isServerRunning, resetServerCheck } from "./tmux-utils/server-health" +export { isServerRunning, resetServerCheck, markServerRunningInProcess } from "./tmux-utils/server-health" export { getPaneDimensions } from "./tmux-utils/pane-dimensions" export type { PaneDimensions } from "./tmux-utils/pane-dimensions" diff --git a/src/shared/tmux/tmux-utils/server-health.ts b/src/shared/tmux/tmux-utils/server-health.ts index a3627100c..043c9048d 100644 --- a/src/shared/tmux/tmux-utils/server-health.ts +++ b/src/shared/tmux/tmux-utils/server-health.ts @@ -1,17 +1,22 @@ let serverAvailable: boolean | null = null let serverCheckUrl: string | null = null -let inProcessServerRunning = false + +const SERVER_RUNNING_KEY = Symbol.for("oh-my-opencode:server-running-in-process") function delay(milliseconds: number): Promise { return new Promise((resolve) => setTimeout(resolve, milliseconds)) } export function markServerRunningInProcess(): void { - inProcessServerRunning = true + ;(globalThis as Record)[SERVER_RUNNING_KEY] = true +} + +function isMarkedRunningInProcess(): boolean { + return (globalThis as Record)[SERVER_RUNNING_KEY] === true } export async function isServerRunning(serverUrl: string): Promise { - if (inProcessServerRunning) { + if (isMarkedRunningInProcess()) { return true }