Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ When writing tests:
- Plugin tests use TypeScript and are co-located with their source files
- Import CLI source from ordinary tests. Put genuine compiled-artifact assertions under `test/package-contract/`.
- Keep project globs disjoint; `npm run test:projects:check` derives membership from Vitest and rejects overlap.
- Deterministic projects clear mock calls, restore `vi.spyOn`, and undo `vi.stubEnv` and `vi.stubGlobal` before each test. Create those spies and stubs in `beforeEach` or the test body unless a documented import-time stub must run before module evaluation. Restore direct environment or global mutations yourself, and reset mock implementations explicitly when needed. Live E2E and automatic `mockReset` are intentionally excluded.
- Write behavior-oriented titles, put local issue references in a final `(#1234)` suffix, and use `npm run test:spec` for the hierarchical specification view.
- Mock external dependencies; don't call real NVIDIA APIs in unit tests
- E2E tests run on ephemeral Brev cloud instances
Expand Down
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ npx vitest run --project e2e-support
This project is fast and does not run live targets. Live E2E remains opt-in through
`npm run test:live-e2e` or the applicable GitHub Actions workflow.

### Test State Isolation

The `cli`, `integration`, `installer-integration`, `package-contract`, `plugin`, and `e2e-support`
projects clear mock call history, restore `vi.spyOn` descriptors, and undo `vi.stubEnv` and
`vi.stubGlobal` before each test.
Create those spies and stubs in `beforeEach` or the test body. A documented import-time stub may
remain at module scope when the imported module must capture it during evaluation.
These projects do not enable `mockReset`, and Vitest does not track direct `process.env` or global
assignments, so reset mock implementations and restore raw mutations in the test that owns them.
Live E2E projects do not enable this automatic cleanup because their stateful targets require
explicit, validated teardown.

### Test Titles as Behavioral Documentation

Write `describe` and `it` titles so the Vitest tree reads as behavioral documentation. Start test
Expand Down
3 changes: 1 addition & 2 deletions nemoclaw/src/blueprint/private-networks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ let existsCalls: string[] = [];
let statCalls: string[] = [];
let nowMs = 1_000;

vi.spyOn(Date, "now").mockImplementation(() => nowMs);

vi.mock("node:fs", async (importOriginal) => {
const original = await importOriginal<typeof fs>();
return {
Expand Down Expand Up @@ -82,6 +80,7 @@ function advanceStatInterval(): void {

describe("private-networks loader", () => {
beforeEach(() => {
vi.spyOn(Date, "now").mockImplementation(() => nowMs);
store.clear();
mtimes.clear();
sizes.clear();
Expand Down
8 changes: 8 additions & 0 deletions nemoclaw/vitest.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type PluginVitestProjectOptions = {
alias: Array<{ find: RegExp; replacement: string }>;
env: Record<string, string>;
environment: "node";
clearMocks: true;
restoreMocks: true;
unstubEnvs: true;
unstubGlobals: true;
setupFiles: string[];
include: string[];
};
Expand All @@ -39,6 +43,10 @@ const pluginVitestProjectOptions = {
NEMOCLAW_DISABLE_GATEWAY_DRIFT_PREFLIGHT: "1",
},
environment: "node",
clearMocks: true,
restoreMocks: true,
unstubEnvs: true,
unstubGlobals: true,
setupFiles: ["test/helpers/normalize-fixture-umask.ts"],
include: ["nemoclaw/src/**/*.test.ts"],
},
Expand Down
8 changes: 4 additions & 4 deletions src/lib/agent/onboard-web-auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, type MockInstance, vi } from "vitest";
import type { AgentDefinition } from "./defs";
// Import source directly so tests cannot pass against a stale build.
import { printDashboardUi } from "./onboard";
Expand All @@ -28,15 +28,15 @@ function makeBearerAgent(): AgentDefinition {
}

describe("printDashboardUi bearer-token agents", () => {
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
let logSpy: MockInstance<typeof console.log>;
const noteSpy = vi.fn();

beforeEach(() => {
logSpy.mockClear();
logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
noteSpy.mockReset();
});

afterAll(() => {
afterEach(() => {
logSpy.mockRestore();
});

Expand Down
12 changes: 4 additions & 8 deletions src/lib/agent/onboard.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, type MockInstance, vi } from "vitest";
import type { AgentDefinition } from "./defs";
// Import source directly so tests cannot pass against a stale build.
import {
Expand Down Expand Up @@ -100,24 +100,20 @@ const buildUrlsLoopback = (token: string | null, port: number): string[] => {
};

describe("printDashboardUi with port 8642 outside the chat UI (#2078)", () => {
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
let logSpy: MockInstance<typeof console.log>;
const noteSpy = vi.fn();

beforeEach(() => {
logSpy.mockClear();
logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
noteSpy.mockReset();
});

afterEach(() => {
logSpy.mockClear();
logSpy.mockRestore();
delete process.env.NEMOCLAW_HERMES_DASHBOARD;
delete process.env.NEMOCLAW_HERMES_DASHBOARD_PORT;
});

afterAll(() => {
logSpy.mockRestore();
});

it("labels an API-kind agent as the API — not a UI — and does not embed a token in the URL", () => {
printDashboardUi("sandbox-x", "secret-token", apiAgent, {
note: noteSpy,
Expand Down
1 change: 1 addition & 0 deletions src/lib/dashboard-url-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe("dashboard-url command helpers", () => {
{
fetchToken: () => "secret-token",
getSandbox: () => ({ agent: null, dashboardPort: 18789 }),
env: {},
log: sinks.log,
error: sinks.error,
},
Expand Down
59 changes: 35 additions & 24 deletions test/cli/debug-command.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { describe, it, expect } from "vitest";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

import { describe, expect, test as it } from "../helpers/owned-test-resources";

import {
createDebugCommandTestEnv,
run,
Expand All @@ -23,24 +24,28 @@ describe("CLI debug command", () => {
expect(r.out.includes("--output")).toBeTruthy();
});

it("debug --quick exits 0 and produces diagnostic output", testTimeoutOptions(30_000), () => {
const r = runWithEnv(
"debug --quick",
createDebugCommandTestEnv("nemoclaw-cli-debug-quick-"),
30000,
);
expect(r.code).toBe(0);
expect(r.out.includes("Collecting diagnostics")).toBeTruthy();
expect(r.out.includes("System")).toBeTruthy();
expect(r.out.includes("Onboard Session")).toBeTruthy();
expect(r.out.includes("Done")).toBeTruthy();
});
it(
"exits with status 0 and produces diagnostic output for debug --quick",
testTimeoutOptions(30_000),
({ resources }) => {
const r = runWithEnv(
"debug --quick",
createDebugCommandTestEnv(resources, "nemoclaw-cli-debug-quick-"),
30000,
);
expect(r.code).toBe(0);
expect(r.out.includes("Collecting diagnostics")).toBeTruthy();
expect(r.out.includes("System")).toBeTruthy();
expect(r.out.includes("Onboard Session")).toBeTruthy();
expect(r.out.includes("Done")).toBeTruthy();
},
);

it.skipIf(os.platform() !== "linux")(
"debug --quick explains restricted dmesg instead of printing raw stderr on Linux",
testTimeoutOptions(30_000),
() => {
const env = createDebugCommandTestEnv("nemoclaw-cli-debug-dmesg-");
({ resources }) => {
const env = createDebugCommandTestEnv(resources, "nemoclaw-cli-debug-dmesg-");
const localBin = env.PATH?.split(path.delimiter)[0];
if (!localBin) throw new Error("Expected debug test PATH to include a fake bin dir");
fs.writeFileSync(
Expand Down Expand Up @@ -82,15 +87,21 @@ describe("CLI debug command", () => {
expect(r.out.includes("nemoclaw debug")).toBeTruthy();
});

it("debug --sandbox NAME targets the specified sandbox", testTimeoutOptions(30_000), () => {
const r = runWithEnv(
"debug --quick --sandbox mybox",
createDebugCommandTestEnv("nemoclaw-cli-debug-sandbox-", { extraSandboxNames: ["mybox"] }),
30000,
);
expect(r.code).toBe(0);
expect(r.out).toContain("Collecting diagnostics for sandbox 'mybox'");
});
it(
"debug --sandbox NAME targets the specified sandbox",
testTimeoutOptions(30_000),
({ resources }) => {
const r = runWithEnv(
"debug --quick --sandbox mybox",
createDebugCommandTestEnv(resources, "nemoclaw-cli-debug-sandbox-", {
extraSandboxNames: ["mybox"],
}),
30000,
);
expect(r.code).toBe(0);
expect(r.out).toContain("Collecting diagnostics for sandbox 'mybox'");
},
);

it("debug --sandbox NAME rejects an unregistered name and exits non-zero", () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-cli-debug-unknown-"));
Expand Down
Loading
Loading