From 1be4df37c589f0fe4929d721a31f98813b48648e Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 15 Jun 2026 12:35:07 -0700 Subject: [PATCH 1/2] test(rebuild): cover rebuild flow outcomes --- src/lib/actions/sandbox/rebuild-flow.test.ts | 266 +++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 src/lib/actions/sandbox/rebuild-flow.test.ts diff --git a/src/lib/actions/sandbox/rebuild-flow.test.ts b/src/lib/actions/sandbox/rebuild-flow.test.ts new file mode 100644 index 00000000000..7b34e17e3fe --- /dev/null +++ b/src/lib/actions/sandbox/rebuild-flow.test.ts @@ -0,0 +1,266 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { createRequire } from "node:module"; + +import { afterEach, beforeEach, describe, expect, it, type MockInstance, vi } from "vitest"; + +type RebuildSandbox = + typeof import("../../../../dist/lib/actions/sandbox/rebuild")["rebuildSandbox"]; + +const requireDist = createRequire(import.meta.url); +const rebuildModulePath = "../../../../dist/lib/actions/sandbox/rebuild.js"; + +type RebuildFlowOverrides = { + applyPreset?: (name: string) => boolean; + executeSandboxCommand?: () => { status: number; stdout: string; stderr: string } | null; + repairMutableConfigPerms?: () => + | { applied: false; skipReason: "agent" | "locked" | "unreadable"; reason: string } + | { applied: true; verified: boolean; errors: string[] }; + restoreSandboxState?: () => { + success: boolean; + restoredDirs: string[]; + restoredFiles: string[]; + failedDirs: string[]; + failedFiles: string[]; + }; +}; + +type RebuildFlowHarness = { + rebuildSandbox: RebuildSandbox; + applyPresetSpy: MockInstance; + backupSandboxStateSpy: MockInstance; + executeSandboxCommandSpy: MockInstance; + logSpy: MockInstance; + onboardSpy: MockInstance; + registryUpdateSpy: MockInstance; + relockSpy: MockInstance; + restoreSandboxStateSpy: MockInstance; + runOpenshellSpy: MockInstance; +}; + +const originalSandboxName = process.env.NEMOCLAW_SANDBOX_NAME; + +function createRebuildFlowHarness(overrides: RebuildFlowOverrides = {}): RebuildFlowHarness { + delete require.cache[requireDist.resolve(rebuildModulePath)]; + + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined); + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => undefined); + + const gatewayDrift = requireDist("../../../../dist/lib/adapters/openshell/gateway-drift.js"); + const openshellRuntime = requireDist("../../../../dist/lib/adapters/openshell/runtime.js"); + const sandboxList = requireDist("../../../../dist/lib/openshell-sandbox-list.js"); + const resolve = requireDist("../../../../dist/lib/adapters/openshell/resolve.js"); + const agentDefs = requireDist("../../../../dist/lib/agent/defs.js"); + const agentRuntime = requireDist("../../../../dist/lib/agent/runtime.js"); + const onboardMod = requireDist("../../../../dist/lib/onboard.js"); + const onboardSession = requireDist("../../../../dist/lib/state/onboard-session.js"); + const registry = requireDist("../../../../dist/lib/state/registry.js"); + const sandboxState = requireDist("../../../../dist/lib/state/sandbox.js"); + const sandboxSession = requireDist("../../../../dist/lib/state/sandbox-session.js"); + const sandboxVersion = requireDist("../../../../dist/lib/sandbox/version.js"); + const destroy = requireDist("../../../../dist/lib/actions/sandbox/destroy.js"); + const rebuildShields = requireDist("../../../../dist/lib/actions/sandbox/rebuild-shields.js"); + const nim = requireDist("../../../../dist/lib/inference/nim.js"); + const policies = requireDist("../../../../dist/lib/policy/index.js"); + const processRecovery = requireDist("../../../../dist/lib/actions/sandbox/process-recovery.js"); + const shields = requireDist("../../../../dist/lib/shields/index.js"); + + const session = { + sandboxName: "alpha", + provider: "ollama-local", + model: "nvidia/nemotron", + credentialEnv: null, + metadata: {}, + hermesToolGateways: [], + }; + const rebuildShieldsWindow = { relocked: false, wasLocked: false }; + const agentDef = { name: "openclaw", expectedVersion: "0.2.0", messagingPlatforms: [] }; + + vi.spyOn(gatewayDrift, "detectOpenShellStateRpcPreflightIssue").mockReturnValue(null); + vi.spyOn(gatewayDrift, "detectOpenShellStateRpcResultIssue").mockReturnValue(null); + vi.spyOn(sandboxList, "captureSandboxListWithGatewayRecovery").mockResolvedValue({ + result: { status: 0, output: "alpha Ready" }, + }); + vi.spyOn(resolve, "resolveOpenshell").mockReturnValue(null); + vi.spyOn(agentDefs, "loadAgent").mockReturnValue(agentDef); + vi.spyOn(agentRuntime, "getSessionAgent").mockReturnValue({ name: "openclaw" }); + vi.spyOn(agentRuntime, "getAgentDisplayName").mockReturnValue("OpenClaw"); + vi.spyOn(onboardSession, "loadSession").mockReturnValue(session); + vi.spyOn(onboardSession, "updateSession").mockImplementation((mutator: unknown) => { + if (typeof mutator !== "function") { + throw new TypeError("updateSession expected a mutator function"); + } + (mutator as (value: typeof session) => typeof session | void)(session); + return session; + }); + vi.spyOn(registry, "getSandbox").mockReturnValue({ + name: "alpha", + provider: "ollama-local", + model: "nvidia/nemotron", + policies: ["npm"], + agent: null, + nimContainer: null, + }); + vi.spyOn(registry, "listSandboxes").mockReturnValue({ sandboxes: [] }); + const registryUpdateSpy = vi.spyOn(registry, "updateSandbox").mockImplementation(() => undefined); + vi.spyOn(sandboxSession, "getActiveSandboxSessions").mockReturnValue({ + detected: false, + sessions: [], + }); + vi.spyOn(sandboxVersion, "checkAgentVersion").mockReturnValue({ + expectedVersion: "0.2.0", + sandboxVersion: "0.1.0", + }); + vi.spyOn(rebuildShields, "openRebuildShieldsWindow").mockReturnValue(rebuildShieldsWindow); + const relockSpy = vi + .spyOn(rebuildShields, "relockRebuildShieldsWindow") + .mockImplementation((...args: unknown[]) => { + const window = args[1] as typeof rebuildShieldsWindow; + window.relocked = true; + return true; + }); + const backupSandboxStateSpy = vi.spyOn(sandboxState, "backupSandboxState").mockReturnValue({ + success: true, + backedUpDirs: ["workspace"], + backedUpFiles: ["user.md"], + failedDirs: [], + failedFiles: [], + manifest: { + backupPath: "/tmp/nemoclaw-rebuild-backup", + timestamp: "2026-06-01T00:00:00.000Z", + policyPresets: ["npm", "bad", "throw"], + }, + }); + const restoreSandboxStateSpy = vi.spyOn(sandboxState, "restoreSandboxState").mockImplementation( + overrides.restoreSandboxState ?? + (() => ({ + success: true, + restoredDirs: ["workspace"], + restoredFiles: ["user.md"], + failedDirs: [], + failedFiles: [], + })), + ); + const runOpenshellSpy = vi + .spyOn(openshellRuntime, "runOpenshell") + .mockReturnValue({ status: 0, output: "" }); + vi.spyOn(destroy, "removeSandboxRegistryEntry").mockImplementation(() => undefined); + vi.spyOn(nim, "stopNimContainer").mockImplementation(() => undefined); + vi.spyOn(nim, "stopNimContainerByName").mockImplementation(() => undefined); + const onboardSpy = vi.spyOn(onboardMod, "onboard").mockResolvedValue(undefined); + const applyPresetSpy = vi.spyOn(policies, "applyPreset").mockImplementation((name: unknown) => { + if (overrides.applyPreset) return overrides.applyPreset(String(name)); + if (name === "throw") throw new Error("preset boom"); + return name === "npm"; + }); + const executeSandboxCommandSpy = vi + .spyOn(processRecovery, "executeSandboxCommand") + .mockImplementation( + overrides.executeSandboxCommand ?? (() => ({ status: 0, stdout: "doctor ok", stderr: "" })), + ); + vi.spyOn(shields, "repairMutableConfigPerms").mockImplementation( + overrides.repairMutableConfigPerms ?? (() => ({ applied: true, verified: true, errors: [] })), + ); + + errorSpy.mockClear(); + logSpy.mockClear(); + warnSpy.mockClear(); + + return { + rebuildSandbox: requireDist(rebuildModulePath).rebuildSandbox, + applyPresetSpy, + backupSandboxStateSpy, + executeSandboxCommandSpy, + logSpy, + onboardSpy, + registryUpdateSpy, + relockSpy, + restoreSandboxStateSpy, + runOpenshellSpy, + }; +} + +describe("rebuildSandbox flow", () => { + beforeEach(() => { + delete process.env.NEMOCLAW_SANDBOX_NAME; + }); + + afterEach(() => { + vi.restoreAllMocks(); + delete require.cache[requireDist.resolve(rebuildModulePath)]; + if (originalSandboxName === undefined) { + delete process.env.NEMOCLAW_SANDBOX_NAME; + } else { + process.env.NEMOCLAW_SANDBOX_NAME = originalSandboxName; + } + }); + + it("backs up, recreates, restores, reapplies policy, and relocks on a successful OpenClaw rebuild", async () => { + const harness = createRebuildFlowHarness({ + applyPreset: () => true, + }); + + await expect( + harness.rebuildSandbox("alpha", ["--yes"], { throwOnError: true }), + ).resolves.toBeUndefined(); + + expect(harness.backupSandboxStateSpy).toHaveBeenCalledWith("alpha"); + expect(harness.runOpenshellSpy).toHaveBeenCalledWith( + ["sandbox", "delete", "alpha"], + expect.objectContaining({ ignoreError: true }), + ); + expect(harness.onboardSpy).toHaveBeenCalledWith( + expect.objectContaining({ + resume: true, + nonInteractive: true, + recreateSandbox: true, + autoYes: true, + }), + ); + expect(harness.restoreSandboxStateSpy).toHaveBeenCalledWith( + "alpha", + "/tmp/nemoclaw-rebuild-backup", + ); + expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "npm"); + expect(harness.registryUpdateSpy).toHaveBeenCalledWith("alpha", { agentVersion: "0.2.0" }); + expect(harness.executeSandboxCommandSpy).toHaveBeenCalledWith("alpha", "openclaw doctor --fix"); + expect(harness.relockSpy).toHaveBeenCalledWith("alpha", expect.any(Object), true, "nemoclaw"); + expect(process.env.NEMOCLAW_SANDBOX_NAME).toBe("alpha"); + expect(harness.logSpy.mock.calls.map((call) => String(call[0])).join("\n")).toContain( + "rebuilt successfully", + ); + }); + + it("finishes the rebuild while surfacing incomplete post-restore work", async () => { + const harness = createRebuildFlowHarness({ + executeSandboxCommand: () => ({ status: 1, stdout: "", stderr: "hash refresh failed" }), + repairMutableConfigPerms: () => ({ + applied: false, + skipReason: "unreadable", + reason: "cannot stat mutable config", + }), + restoreSandboxState: () => ({ + success: false, + restoredDirs: ["workspace"], + restoredFiles: [], + failedDirs: ["config"], + failedFiles: ["user.md"], + }), + }); + + await expect( + harness.rebuildSandbox("alpha", ["--yes"], { throwOnError: true }), + ).resolves.toBeUndefined(); + + const output = harness.logSpy.mock.calls.map((call) => String(call[0])).join("\n"); + expect(output).toContain("rebuilt but some post-restore steps were incomplete"); + expect(output).toContain("State restore was incomplete"); + expect(output).toContain("Mutable config permissions were not verified"); + expect(output).toContain("Mutable OpenClaw config hash was not refreshed"); + expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "bad"); + expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "throw"); + expect(harness.relockSpy).toHaveBeenCalledWith("alpha", expect.any(Object), true, "nemoclaw"); + }); +}); From 6c955943e564c8147f40e65b41505278a4837e0e Mon Sep 17 00:00:00 2001 From: Carlos Villela Date: Mon, 15 Jun 2026 12:47:31 -0700 Subject: [PATCH 2/2] test(rebuild): assert preset reapply outcomes --- src/lib/actions/sandbox/rebuild-flow.test.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/lib/actions/sandbox/rebuild-flow.test.ts b/src/lib/actions/sandbox/rebuild-flow.test.ts index 7b34e17e3fe..27957ca4482 100644 --- a/src/lib/actions/sandbox/rebuild-flow.test.ts +++ b/src/lib/actions/sandbox/rebuild-flow.test.ts @@ -12,7 +12,7 @@ const requireDist = createRequire(import.meta.url); const rebuildModulePath = "../../../../dist/lib/actions/sandbox/rebuild.js"; type RebuildFlowOverrides = { - applyPreset?: (name: string) => boolean; + applyPreset?: (presetName: string) => boolean; executeSandboxCommand?: () => { status: number; stdout: string; stderr: string } | null; repairMutableConfigPerms?: () => | { applied: false; skipReason: "agent" | "locked" | "unreadable"; reason: string } @@ -30,6 +30,7 @@ type RebuildFlowHarness = { rebuildSandbox: RebuildSandbox; applyPresetSpy: MockInstance; backupSandboxStateSpy: MockInstance; + errorSpy: MockInstance; executeSandboxCommandSpy: MockInstance; logSpy: MockInstance; onboardSpy: MockInstance; @@ -150,11 +151,14 @@ function createRebuildFlowHarness(overrides: RebuildFlowOverrides = {}): Rebuild vi.spyOn(nim, "stopNimContainer").mockImplementation(() => undefined); vi.spyOn(nim, "stopNimContainerByName").mockImplementation(() => undefined); const onboardSpy = vi.spyOn(onboardMod, "onboard").mockResolvedValue(undefined); - const applyPresetSpy = vi.spyOn(policies, "applyPreset").mockImplementation((name: unknown) => { - if (overrides.applyPreset) return overrides.applyPreset(String(name)); - if (name === "throw") throw new Error("preset boom"); - return name === "npm"; - }); + const applyPresetSpy = vi + .spyOn(policies, "applyPreset") + .mockImplementation((_sandboxName: unknown, presetName: unknown) => { + const normalizedPresetName = String(presetName); + if (overrides.applyPreset) return overrides.applyPreset(normalizedPresetName); + if (normalizedPresetName === "throw") throw new Error("preset boom"); + return normalizedPresetName === "npm"; + }); const executeSandboxCommandSpy = vi .spyOn(processRecovery, "executeSandboxCommand") .mockImplementation( @@ -172,6 +176,7 @@ function createRebuildFlowHarness(overrides: RebuildFlowOverrides = {}): Rebuild rebuildSandbox: requireDist(rebuildModulePath).rebuildSandbox, applyPresetSpy, backupSandboxStateSpy, + errorSpy, executeSandboxCommandSpy, logSpy, onboardSpy, @@ -224,6 +229,8 @@ describe("rebuildSandbox flow", () => { "/tmp/nemoclaw-rebuild-backup", ); expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "npm"); + expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "bad"); + expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "throw"); expect(harness.registryUpdateSpy).toHaveBeenCalledWith("alpha", { agentVersion: "0.2.0" }); expect(harness.executeSandboxCommandSpy).toHaveBeenCalledWith("alpha", "openclaw doctor --fix"); expect(harness.relockSpy).toHaveBeenCalledWith("alpha", expect.any(Object), true, "nemoclaw"); @@ -261,6 +268,7 @@ describe("rebuildSandbox flow", () => { expect(output).toContain("Mutable OpenClaw config hash was not refreshed"); expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "bad"); expect(harness.applyPresetSpy).toHaveBeenCalledWith("alpha", "throw"); + expect(harness.errorSpy).toHaveBeenCalledWith(expect.stringContaining("bad, throw")); expect(harness.relockSpy).toHaveBeenCalledWith("alpha", expect.any(Object), true, "nemoclaw"); }); });