|
| 1 | +// @ts-nocheck |
| 2 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import { describe, it, expect } from "vitest"; |
| 6 | +import fs from "node:fs"; |
| 7 | +import os from "node:os"; |
| 8 | +import path from "node:path"; |
| 9 | +import { spawnSync } from "node:child_process"; |
| 10 | + |
| 11 | +const SCRIPT = path.join(import.meta.dirname, "..", "scripts", "install-openshell.sh"); |
| 12 | + |
| 13 | +function writeExecutable(target: string, contents: string) { |
| 14 | + fs.writeFileSync(target, contents, { mode: 0o755 }); |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Run install-openshell.sh with a fake `openshell` binary that reports the |
| 19 | + * given version. The download/install code path is never reached because we |
| 20 | + * either exit early (version ok / too high) or hit the upgrade warn and then |
| 21 | + * the script tries to download — so we stub curl and gh to fail fast. |
| 22 | + */ |
| 23 | +function runWithInstalledVersion(version: string) { |
| 24 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-openshell-ver-")); |
| 25 | + try { |
| 26 | + const fakeBin = path.join(tmp, "bin"); |
| 27 | + fs.mkdirSync(fakeBin); |
| 28 | + |
| 29 | + // Fake openshell that reports the given version |
| 30 | + writeExecutable( |
| 31 | + path.join(fakeBin, "openshell"), |
| 32 | + `#!/usr/bin/env bash |
| 33 | +if [ "\${1:-}" = "--version" ]; then echo "openshell ${version}"; exit 0; fi |
| 34 | +exit 99`, |
| 35 | + ); |
| 36 | + |
| 37 | + // Stub curl to fail so the install path exits without doing real network I/O |
| 38 | + writeExecutable( |
| 39 | + path.join(fakeBin, "curl"), |
| 40 | + `#!/usr/bin/env bash |
| 41 | +echo "curl stub: not available in test" >&2 |
| 42 | +exit 1`, |
| 43 | + ); |
| 44 | + |
| 45 | + // Stub gh CLI similarly |
| 46 | + writeExecutable( |
| 47 | + path.join(fakeBin, "gh"), |
| 48 | + `#!/usr/bin/env bash |
| 49 | +exit 1`, |
| 50 | + ); |
| 51 | + |
| 52 | + return spawnSync("bash", [SCRIPT], { |
| 53 | + env: { ...process.env, PATH: `${fakeBin}:/usr/bin:/bin` }, |
| 54 | + encoding: "utf8", |
| 55 | + }); |
| 56 | + } finally { |
| 57 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +describe("install-openshell.sh version check", () => { |
| 62 | + it("exits cleanly when openshell 0.0.29 is already installed", () => { |
| 63 | + const result = runWithInstalledVersion("0.0.29"); |
| 64 | + expect(result.status).toBe(0); |
| 65 | + expect(result.stdout).toMatch(/already installed.*0\.0\.29/); |
| 66 | + }); |
| 67 | + |
| 68 | + it("triggers upgrade when openshell 0.0.28 is installed (below MIN_VERSION)", () => { |
| 69 | + const result = runWithInstalledVersion("0.0.28"); |
| 70 | + // Script should warn about upgrade then fail at the download step (curl stub fails) |
| 71 | + expect(result.status).not.toBe(0); |
| 72 | + expect(result.stdout).toMatch(/below minimum.*upgrading/); |
| 73 | + }); |
| 74 | + |
| 75 | + it("triggers upgrade when openshell 0.0.26 is installed (Landlock-vulnerable version)", () => { |
| 76 | + const result = runWithInstalledVersion("0.0.26"); |
| 77 | + expect(result.status).not.toBe(0); |
| 78 | + expect(result.stdout).toMatch(/below minimum.*upgrading/); |
| 79 | + }); |
| 80 | + |
| 81 | + it("triggers upgrade when openshell 0.0.24 is installed (old minimum)", () => { |
| 82 | + const result = runWithInstalledVersion("0.0.24"); |
| 83 | + expect(result.status).not.toBe(0); |
| 84 | + expect(result.stdout).toMatch(/below minimum.*upgrading/); |
| 85 | + }); |
| 86 | + |
| 87 | + it("fails with a clear error when openshell is above MAX_VERSION", () => { |
| 88 | + const result = runWithInstalledVersion("0.0.30"); |
| 89 | + expect(result.status).toBe(1); |
| 90 | + expect(result.stdout).toMatch(/above the maximum/); |
| 91 | + }); |
| 92 | + |
| 93 | + it("fails with a clear error when openshell is at a much newer version", () => { |
| 94 | + const result = runWithInstalledVersion("0.1.0"); |
| 95 | + expect(result.status).toBe(1); |
| 96 | + expect(result.stdout).toMatch(/above the maximum/); |
| 97 | + }); |
| 98 | + |
| 99 | + it("exits cleanly when openshell reports m-dev but sidecar records 0.0.29", () => { |
| 100 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-openshell-mdev-")); |
| 101 | + try { |
| 102 | + const fakeBin = path.join(tmp, "bin"); |
| 103 | + fs.mkdirSync(fakeBin); |
| 104 | + |
| 105 | + // Fake openshell that reports "m-dev" (as openshell 0.0.29 does in practice) |
| 106 | + writeExecutable( |
| 107 | + path.join(fakeBin, "openshell"), |
| 108 | + `#!/usr/bin/env bash |
| 109 | +if [ "\${1:-}" = "--version" ]; then echo "openshell m-dev"; exit 0; fi |
| 110 | +exit 99`, |
| 111 | + ); |
| 112 | + |
| 113 | + // Sidecar file written by a previous install |
| 114 | + fs.writeFileSync(path.join(fakeBin, ".openshell-installed-version"), "0.0.29\n"); |
| 115 | + |
| 116 | + writeExecutable(path.join(fakeBin, "curl"), `#!/usr/bin/env bash\nexit 1`); |
| 117 | + writeExecutable(path.join(fakeBin, "gh"), `#!/usr/bin/env bash\nexit 1`); |
| 118 | + |
| 119 | + const result = spawnSync("bash", [SCRIPT], { |
| 120 | + env: { ...process.env, PATH: `${fakeBin}:/usr/bin:/bin` }, |
| 121 | + encoding: "utf8", |
| 122 | + }); |
| 123 | + expect(result.status).toBe(0); |
| 124 | + expect(result.stdout).toMatch(/already installed.*0\.0\.29/); |
| 125 | + } finally { |
| 126 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + it("triggers upgrade when openshell reports m-dev and sidecar records 0.0.26", () => { |
| 131 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-openshell-mdev-old-")); |
| 132 | + try { |
| 133 | + const fakeBin = path.join(tmp, "bin"); |
| 134 | + fs.mkdirSync(fakeBin); |
| 135 | + |
| 136 | + writeExecutable( |
| 137 | + path.join(fakeBin, "openshell"), |
| 138 | + `#!/usr/bin/env bash |
| 139 | +if [ "\${1:-}" = "--version" ]; then echo "openshell m-dev"; exit 0; fi |
| 140 | +exit 99`, |
| 141 | + ); |
| 142 | + |
| 143 | + // Sidecar from an older install that pre-dates the Landlock fix |
| 144 | + fs.writeFileSync(path.join(fakeBin, ".openshell-installed-version"), "0.0.26\n"); |
| 145 | + |
| 146 | + writeExecutable( |
| 147 | + path.join(fakeBin, "curl"), |
| 148 | + `#!/usr/bin/env bash\necho "curl stub" >&2\nexit 1`, |
| 149 | + ); |
| 150 | + writeExecutable(path.join(fakeBin, "gh"), `#!/usr/bin/env bash\nexit 1`); |
| 151 | + |
| 152 | + const result = spawnSync("bash", [SCRIPT], { |
| 153 | + env: { ...process.env, PATH: `${fakeBin}:/usr/bin:/bin` }, |
| 154 | + encoding: "utf8", |
| 155 | + }); |
| 156 | + expect(result.status).not.toBe(0); |
| 157 | + expect(result.stdout).toMatch(/below minimum.*upgrading/); |
| 158 | + } finally { |
| 159 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + it("proceeds to install when openshell is not present", () => { |
| 164 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-openshell-noop-")); |
| 165 | + try { |
| 166 | + const fakeBin = path.join(tmp, "bin"); |
| 167 | + fs.mkdirSync(fakeBin); |
| 168 | + |
| 169 | + // No openshell binary — just stub curl/gh to fail fast |
| 170 | + writeExecutable( |
| 171 | + path.join(fakeBin, "curl"), |
| 172 | + `#!/usr/bin/env bash |
| 173 | +echo "curl stub: not available in test" >&2 |
| 174 | +exit 1`, |
| 175 | + ); |
| 176 | + writeExecutable( |
| 177 | + path.join(fakeBin, "gh"), |
| 178 | + `#!/usr/bin/env bash |
| 179 | +exit 1`, |
| 180 | + ); |
| 181 | + |
| 182 | + const result = spawnSync("bash", [SCRIPT], { |
| 183 | + env: { ...process.env, PATH: `${fakeBin}:/usr/bin:/bin` }, |
| 184 | + encoding: "utf8", |
| 185 | + }); |
| 186 | + |
| 187 | + // Should attempt install (not exit 0 early) and fail at the download step |
| 188 | + expect(result.stdout).toMatch(/Installing openshell CLI/); |
| 189 | + expect(result.status).not.toBe(0); |
| 190 | + } finally { |
| 191 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 192 | + } |
| 193 | + }); |
| 194 | +}); |
0 commit comments