|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { |
| 3 | + RuntimeState, |
| 4 | + RuntimeUnitState, |
| 5 | +} from "../../apps/desktop/shared/host"; |
| 6 | +import { |
| 7 | + getDesktopOpenClawUrl, |
| 8 | + isOpenClawSurfaceReady, |
| 9 | +} from "../../apps/desktop/src/lib/openclaw-surface"; |
| 10 | + |
| 11 | +function createUnit(overrides: Partial<RuntimeUnitState>): RuntimeUnitState { |
| 12 | + return { |
| 13 | + id: "openclaw", |
| 14 | + label: "OpenClaw", |
| 15 | + kind: "service", |
| 16 | + launchStrategy: "launchd", |
| 17 | + phase: "starting", |
| 18 | + autoStart: true, |
| 19 | + pid: null, |
| 20 | + port: 18789, |
| 21 | + startedAt: null, |
| 22 | + exitedAt: null, |
| 23 | + exitCode: null, |
| 24 | + lastError: null, |
| 25 | + lastReasonCode: null, |
| 26 | + lastProbeAt: null, |
| 27 | + restartCount: 0, |
| 28 | + commandSummary: null, |
| 29 | + binaryPath: null, |
| 30 | + logFilePath: null, |
| 31 | + logTail: [], |
| 32 | + ...overrides, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +function createRuntimeState(units: RuntimeUnitState[]): RuntimeState { |
| 37 | + return { |
| 38 | + startedAt: new Date(0).toISOString(), |
| 39 | + units, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +describe("openclaw surface gating", () => { |
| 44 | + it("keeps the surface unavailable until the openclaw runtime unit is running", () => { |
| 45 | + const runtimeState = createRuntimeState([ |
| 46 | + createUnit({ phase: "starting" }), |
| 47 | + ]); |
| 48 | + |
| 49 | + expect(isOpenClawSurfaceReady(runtimeState)).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it("builds the gateway URL only after the openclaw runtime unit is running", () => { |
| 53 | + const startingState = createRuntimeState([ |
| 54 | + createUnit({ phase: "starting" }), |
| 55 | + ]); |
| 56 | + const runningState = createRuntimeState([createUnit({ phase: "running" })]); |
| 57 | + |
| 58 | + expect( |
| 59 | + getDesktopOpenClawUrl({ |
| 60 | + runtimeConfig: { |
| 61 | + urls: { openclawBase: "http://127.0.0.1:18789" }, |
| 62 | + tokens: { gateway: "gw-secret-token" }, |
| 63 | + }, |
| 64 | + runtimeState: startingState, |
| 65 | + }), |
| 66 | + ).toBeNull(); |
| 67 | + |
| 68 | + expect( |
| 69 | + getDesktopOpenClawUrl({ |
| 70 | + runtimeConfig: { |
| 71 | + urls: { openclawBase: "http://127.0.0.1:18789" }, |
| 72 | + tokens: { gateway: "gw-secret-token" }, |
| 73 | + }, |
| 74 | + runtimeState: runningState, |
| 75 | + }), |
| 76 | + ).toBe("http://127.0.0.1:18789/#token=gw-secret-token"); |
| 77 | + }); |
| 78 | +}); |
0 commit comments