|
8 | 8 | * Plan: devlog/_fin/260815_gui_codex_restart/010_phase1_backend_endpoint.md |
9 | 9 | */ |
10 | 10 | import { describe, expect, test } from "bun:test"; |
11 | | -import { readFileSync } from "node:fs"; |
| 11 | +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; |
| 12 | +import { tmpdir } from "node:os"; |
| 13 | +import { join } from "node:path"; |
| 14 | +import { startServer } from "../../src/server"; |
12 | 15 | import { helperPath } from "../helpers/repo-root"; |
13 | 16 | import { nativeOwnerRestartService, NATIVE_OWNER_RESTART_REFUSAL } from "../helpers/native-owner-restart-service"; |
14 | 17 | import { handleSystemRoutes } from "../../src/server/management/system-routes"; |
@@ -188,4 +191,70 @@ describe("native-owner fixture process-control boundary", () => { |
188 | 191 | expect(await nativeOwnerRestartService.readState()).toEqual({ state: "unknown", runningCount: 0 }); |
189 | 192 | await expect(nativeOwnerRestartService.performRestart()).rejects.toThrow(NATIVE_OWNER_RESTART_REFUSAL); |
190 | 193 | }); |
| 194 | + |
| 195 | + test("servers on the common startup path serve the injected service over authenticated HTTP", async () => { |
| 196 | + // The child's start() is a thin wrapper over this same startServer call: driving real |
| 197 | + // listeners proves the managementApi -> ctx.deps forwarding cannot silently drop the |
| 198 | + // boundary while the source guard above still passes. Primary and extra fixture |
| 199 | + // servers share one injection path, so both are exercised here. The sentinel restart |
| 200 | + // body is one the live implementation could never emit: it does not fabricate a pid. |
| 201 | + const root = mkdtempSync(join(tmpdir(), "ocx-restart-boundary-")); |
| 202 | + const previousHome = process.env.OPENCODEX_HOME; |
| 203 | + const previousCodexHome = process.env.CODEX_HOME; |
| 204 | + const previousAdminToken = process.env.OPENCODEX_ADMIN_AUTH_TOKEN; |
| 205 | + process.env.OPENCODEX_HOME = root; |
| 206 | + process.env.CODEX_HOME = root; |
| 207 | + process.env.OPENCODEX_ADMIN_AUTH_TOKEN = "restart-boundary-admin"; |
| 208 | + const sentinel: CodexRestartResponse = { |
| 209 | + success: true, |
| 210 | + stateBefore: "unknown", |
| 211 | + synced: false, |
| 212 | + requested: [424242], |
| 213 | + stopped: [424242], |
| 214 | + surviving: [], |
| 215 | + failed: [], |
| 216 | + code: "stopped", |
| 217 | + }; |
| 218 | + const calls: string[] = []; |
| 219 | + const observed = { |
| 220 | + readState: async () => { |
| 221 | + calls.push("readState"); |
| 222 | + return nativeOwnerRestartService.readState(); |
| 223 | + }, |
| 224 | + performRestart: async () => { |
| 225 | + calls.push("performRestart"); |
| 226 | + return sentinel; |
| 227 | + }, |
| 228 | + }; |
| 229 | + const deps = { managementApi: { codexRestartService: observed } }; |
| 230 | + const primary = startServer(0, deps); |
| 231 | + const extra = startServer(0, deps); |
| 232 | + try { |
| 233 | + const headers = { "x-opencodex-api-key": "restart-boundary-admin" }; |
| 234 | + for (const server of [primary, extra]) { |
| 235 | + const state = await fetch(new URL(CODEX_APP_SERVER_STATE_PATH, server.url), { headers }); |
| 236 | + expect(state.status).toBe(200); |
| 237 | + expect(await state.json()).toEqual({ state: "unknown", runningCount: 0 }); |
| 238 | + const restart = await fetch(new URL(CODEX_RESTART_PATH, server.url), { method: "POST", headers }); |
| 239 | + expect(restart.status).toBe(200); |
| 240 | + expect(await restart.json()).toEqual(sentinel); |
| 241 | + } |
| 242 | + expect(calls).toEqual([ |
| 243 | + "readState", "performRestart", |
| 244 | + "readState", "performRestart", |
| 245 | + ]); |
| 246 | + } finally { |
| 247 | + await primary.stop(true); |
| 248 | + await extra.stop(true); |
| 249 | + for (const [name, value] of [ |
| 250 | + ["OPENCODEX_HOME", previousHome], |
| 251 | + ["CODEX_HOME", previousCodexHome], |
| 252 | + ["OPENCODEX_ADMIN_AUTH_TOKEN", previousAdminToken], |
| 253 | + ] as const) { |
| 254 | + if (value === undefined) delete process.env[name]; |
| 255 | + else process.env[name] = value; |
| 256 | + } |
| 257 | + rmSync(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); |
| 258 | + } |
| 259 | + }); |
191 | 260 | }); |
0 commit comments