Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions src/lib/gateway-state.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { describe, it, expect } from "vitest";
import {
isGatewayHealthy,
getGatewayReuseState,
isSandboxReady,
hasStaleGateway,
getReportedGatewayName,
isGatewayConnected,
} from "../../dist/lib/gateway-state";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Importing from dist makes these unit tests unreliable.

Line 12 imports compiled output (../../dist/...) instead of source. That can silently test stale build artifacts and miss regressions in src/lib/gateway-state.ts.

Proposed fix
 } from "vitest";
 import {
   isGatewayHealthy,
   getGatewayReuseState,
   isSandboxReady,
   hasStaleGateway,
   getReportedGatewayName,
   isGatewayConnected,
-} from "../../dist/lib/gateway-state";
+} from "./gateway-state";

As per coding guidelines, "Use TypeScript for CLI code in src/lib/ with type-checking via tsconfig.cli.json".

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} from "../../dist/lib/gateway-state";
} from "vitest";
import {
isGatewayHealthy,
getGatewayReuseState,
isSandboxReady,
hasStaleGateway,
getReportedGatewayName,
isGatewayConnected,
} from "./gateway-state";
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lib/gateway-state.test.ts` at line 12, The test imports the compiled
artifact ("../../dist/lib/gateway-state") which can cause stale builds to be
tested; change the import to point at the TypeScript source module (the
gateway-state source) instead of dist — i.e., replace the
"../../dist/lib/gateway-state" import in src/lib/gateway-state.test.ts with the
corresponding source import (e.g., "../../src/lib/gateway-state" or
"./gateway-state" depending on relative layout) so the tests run against the
live TypeScript code, and ensure your test runner is configured to load TS
sources (ts-node/ts-jest or the tsconfig.cli.json settings).


// Realistic openshell CLI output fixtures
const STATUS_CONNECTED_NEMOCLAW = " Status: Connected\n Gateway: nemoclaw\n";
const GW_INFO_NEMOCLAW = " Gateway: nemoclaw\n Status: Running\n";
const ACTIVE_INFO_NEMOCLAW = " Gateway: nemoclaw\n Gateway endpoint: http://localhost:8080\n";

const STATUS_CONNECTED_OTHER = " Status: Connected\n Gateway: other-gateway\n";
const GW_INFO_NO_METADATA = "No gateway metadata found\n Gateway: nemoclaw\n";
const STATUS_DISCONNECTED = " Status: Disconnected\n Gateway: nemoclaw\n";

describe("isGatewayHealthy", () => {
it("returns true when gateway is running — second onboard should reuse, not restart", () => {
expect(isGatewayHealthy(STATUS_CONNECTED_NEMOCLAW, GW_INFO_NEMOCLAW, ACTIVE_INFO_NEMOCLAW)).toBe(
true,
);
});

it("returns false when not connected", () => {
expect(isGatewayHealthy(STATUS_DISCONNECTED, GW_INFO_NEMOCLAW, ACTIVE_INFO_NEMOCLAW)).toBe(
false,
);
});

it("returns false when gateway metadata is absent", () => {
expect(isGatewayHealthy(STATUS_CONNECTED_NEMOCLAW, "", ACTIVE_INFO_NEMOCLAW)).toBe(false);

Check failure on line 37 in src/lib/gateway-state.test.ts

View workflow job for this annotation

GitHub Actions / checks

[cli] src/lib/gateway-state.test.ts > isGatewayHealthy > returns false when gateway metadata is absent

AssertionError: expected true to be false // Object.is equality - Expected + Received - false + true ❯ src/lib/gateway-state.test.ts:37:83

Check failure on line 37 in src/lib/gateway-state.test.ts

View workflow job for this annotation

GitHub Actions / macos-e2e

[cli] src/lib/gateway-state.test.ts > isGatewayHealthy > returns false when gateway metadata is absent

AssertionError: expected true to be false // Object.is equality - Expected + Received - false + true ❯ src/lib/gateway-state.test.ts:37:83
});

it("returns false when active gateway is a foreign name", () => {
expect(isGatewayHealthy(STATUS_CONNECTED_OTHER, GW_INFO_NEMOCLAW, ACTIVE_INFO_NEMOCLAW)).toBe(
false,
);
});

it("returns false when gwInfo contains no-metadata marker", () => {
expect(
isGatewayHealthy(STATUS_CONNECTED_NEMOCLAW, GW_INFO_NO_METADATA, ACTIVE_INFO_NEMOCLAW),
).toBe(false);

Check failure on line 49 in src/lib/gateway-state.test.ts

View workflow job for this annotation

GitHub Actions / checks

[cli] src/lib/gateway-state.test.ts > isGatewayHealthy > returns false when gwInfo contains no-metadata marker

AssertionError: expected true to be false // Object.is equality - Expected + Received - false + true ❯ src/lib/gateway-state.test.ts:49:7

Check failure on line 49 in src/lib/gateway-state.test.ts

View workflow job for this annotation

GitHub Actions / macos-e2e

[cli] src/lib/gateway-state.test.ts > isGatewayHealthy > returns false when gwInfo contains no-metadata marker

AssertionError: expected true to be false // Object.is equality - Expected + Received - false + true ❯ src/lib/gateway-state.test.ts:49:7
});
});

describe("getGatewayReuseState", () => {
it("returns healthy for a fully running gateway", () => {
expect(getGatewayReuseState(STATUS_CONNECTED_NEMOCLAW, GW_INFO_NEMOCLAW, ACTIVE_INFO_NEMOCLAW)).toBe(
"healthy",
);
});

it("returns foreign-active when a different gateway is active", () => {
expect(getGatewayReuseState(STATUS_CONNECTED_OTHER, GW_INFO_NEMOCLAW, ACTIVE_INFO_NEMOCLAW)).toBe(
"foreign-active",
);
});

it("returns stale when gateway metadata exists but is disconnected", () => {
expect(getGatewayReuseState(STATUS_DISCONNECTED, GW_INFO_NEMOCLAW, "")).toBe("stale");
});

it("returns missing when no state is present", () => {
expect(getGatewayReuseState("", "", "")).toBe("missing");
});
});

describe("hasStaleGateway", () => {
it("returns true for known gateway info", () => {
expect(hasStaleGateway(GW_INFO_NEMOCLAW)).toBe(true);
});

it("returns false when no-metadata marker is present", () => {
expect(hasStaleGateway(GW_INFO_NO_METADATA)).toBe(false);
});

it("returns false for empty string", () => {
expect(hasStaleGateway("")).toBe(false);
});
});

describe("getReportedGatewayName", () => {
it("parses gateway name from status output", () => {
expect(getReportedGatewayName(STATUS_CONNECTED_NEMOCLAW)).toBe("nemoclaw");
});

it("returns null for empty output", () => {
expect(getReportedGatewayName("")).toBeNull();
});
});

describe("isGatewayConnected", () => {
it("returns true when Connected is present", () => {
expect(isGatewayConnected(STATUS_CONNECTED_NEMOCLAW)).toBe(true);
});

it("returns false when Disconnected", () => {
expect(isGatewayConnected(STATUS_DISCONNECTED)).toBe(false);
});
});

describe("isSandboxReady", () => {
it("returns true for a Ready sandbox", () => {
const output = " my-sandbox Ready running\n";
expect(isSandboxReady(output, "my-sandbox")).toBe(true);
});

it("returns false for NotReady", () => {
const output = " my-sandbox NotReady pending\n";
expect(isSandboxReady(output, "my-sandbox")).toBe(false);
});

it("returns false for a different sandbox name", () => {
const output = " other-sandbox Ready running\n";
expect(isSandboxReady(output, "my-sandbox")).toBe(false);
});

it("strips ANSI codes before matching", () => {
const output = "\x1b[32m my-sandbox\x1b[0m Ready running\n";
expect(isSandboxReady(output, "my-sandbox")).toBe(true);
});
});
13 changes: 12 additions & 1 deletion src/lib/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3496,7 +3496,7 @@ async function createSandbox(
console.log(" Waiting for NemoClaw dashboard to become ready...");
for (let i = 0; i < 15; i++) {
const readyMatch = runCaptureOpenshell(
["sandbox", "exec", sandboxName, "curl", "-sf", `http://localhost:${CONTROL_UI_PORT}/`],
["sandbox", "exec", sandboxName, "curl", "-sf", `http://localhost:${effectivePort}/`],
{ ignoreError: true },
);
if (readyMatch) {
Expand Down Expand Up @@ -5558,6 +5558,17 @@ const { resolveDashboardForwardTarget, buildControlUiUrls } = dashboard;
function ensureDashboardForward(sandboxName, chatUiUrl = `http://127.0.0.1:${CONTROL_UI_PORT}`) {
const portToStop = getDashboardForwardPort(chatUiUrl);
const forwardTarget = getDashboardForwardTarget(chatUiUrl);
// Detect port already claimed by a different sandbox and fail fast with an
// actionable message rather than silently stealing that sandbox's forward.
// (Same sandbox is always allowed — covers reconnect and resume paths.)
const existingForwards = runCaptureOpenshell(["forward", "list"], { ignoreError: true });
if (existingForwards?.includes(`:${portToStop}`) && !existingForwards?.includes(sandboxName)) {
throw new Error(
`Port ${portToStop} is already forwarded for another sandbox. ` +
`Set NEMOCLAW_DASHBOARD_PORT to a different port before onboarding ` +
`a second sandbox.`,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
runOpenshell(["forward", "stop", portToStop], { ignoreError: true });
// Use stdio "ignore" to prevent spawnSync from waiting on inherited pipe fds.
// The --background flag forks a child that inherits stdout/stderr; if those are
Expand Down
Loading