|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { describe, it, expect, afterAll, beforeEach, vi } from 'vitest'; |
| 5 | +import supertest from 'supertest'; |
| 6 | + |
| 7 | +vi.mock('child_process', () => ({ |
| 8 | + execFile: vi.fn((cmd, args, opts, cb) => { |
| 9 | + if (typeof opts === 'function') { cb = opts; opts = {}; } |
| 10 | + cb(null, '', ''); |
| 11 | + }), |
| 12 | + spawn: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +import { execFile, spawn } from 'child_process'; |
| 16 | +import serverModule from '../server.js'; |
| 17 | +const { server, _resetForTesting, _setMocksForTesting } = serverModule; |
| 18 | +import setupModule from './setup.js'; |
| 19 | +const { cleanTempFiles, FIXTURES } = setupModule; |
| 20 | +const request = supertest; |
| 21 | + |
| 22 | +// === TC-CI01 through TC-CI10: Cluster inference === |
| 23 | + |
| 24 | +describe("GET /api/cluster-inference", () => { |
| 25 | + beforeEach(() => { |
| 26 | + _resetForTesting(); |
| 27 | + _setMocksForTesting({ execFile, spawn }); |
| 28 | + cleanTempFiles(); |
| 29 | + execFile.mockClear(); |
| 30 | + }); |
| 31 | + |
| 32 | + afterAll(() => { server.close(); }); |
| 33 | + |
| 34 | + it("TC-CI01: returns parsed providerName, modelId, version on success", async () => { |
| 35 | + execFile.mockImplementation((cmd, args, opts, cb) => { |
| 36 | + if (typeof opts === "function") { cb = opts; opts = {}; } |
| 37 | + cb(null, FIXTURES.clusterInferenceOutput, ""); |
| 38 | + }); |
| 39 | + |
| 40 | + const res = await request(server).get("/api/cluster-inference"); |
| 41 | + expect(res.status).toBe(200); |
| 42 | + expect(res.body.ok).toBe(true); |
| 43 | + expect(res.body.providerName).toBe("nvidia-inference"); |
| 44 | + expect(res.body.modelId).toBe("meta/llama-3.1-70b-instruct"); |
| 45 | + expect(res.body.version).toBe(2); |
| 46 | + }); |
| 47 | + |
| 48 | + it("TC-CI02: returns nulls when 'not configured' in stderr", async () => { |
| 49 | + execFile.mockImplementation((cmd, args, opts, cb) => { |
| 50 | + if (typeof opts === "function") { cb = opts; opts = {}; } |
| 51 | + const err = new Error("fail"); |
| 52 | + err.code = 1; |
| 53 | + cb(err, "", "cluster inference not configured"); |
| 54 | + }); |
| 55 | + |
| 56 | + const res = await request(server).get("/api/cluster-inference"); |
| 57 | + expect(res.status).toBe(200); |
| 58 | + expect(res.body.ok).toBe(true); |
| 59 | + expect(res.body.providerName).toBeNull(); |
| 60 | + expect(res.body.modelId).toBe(""); |
| 61 | + expect(res.body.version).toBe(0); |
| 62 | + }); |
| 63 | + |
| 64 | + it("TC-CI03: returns nulls when 'not found' in stderr", async () => { |
| 65 | + execFile.mockImplementation((cmd, args, opts, cb) => { |
| 66 | + if (typeof opts === "function") { cb = opts; opts = {}; } |
| 67 | + const err = new Error("fail"); |
| 68 | + err.code = 1; |
| 69 | + cb(err, "", "inference config not found"); |
| 70 | + }); |
| 71 | + |
| 72 | + const res = await request(server).get("/api/cluster-inference"); |
| 73 | + expect(res.status).toBe(200); |
| 74 | + expect(res.body.providerName).toBeNull(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("TC-CI04: returns 400 on other CLI errors", async () => { |
| 78 | + execFile.mockImplementation((cmd, args, opts, cb) => { |
| 79 | + if (typeof opts === "function") { cb = opts; opts = {}; } |
| 80 | + const err = new Error("fail"); |
| 81 | + err.code = 1; |
| 82 | + cb(err, "", "unexpected error occurred"); |
| 83 | + }); |
| 84 | + |
| 85 | + const res = await request(server).get("/api/cluster-inference"); |
| 86 | + expect(res.status).toBe(400); |
| 87 | + expect(res.body.ok).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it("TC-CI05: ANSI codes in output are stripped before parsing", async () => { |
| 91 | + execFile.mockImplementation((cmd, args, opts, cb) => { |
| 92 | + if (typeof opts === "function") { cb = opts; opts = {}; } |
| 93 | + cb(null, FIXTURES.clusterInferenceAnsi, ""); |
| 94 | + }); |
| 95 | + |
| 96 | + const res = await request(server).get("/api/cluster-inference"); |
| 97 | + expect(res.status).toBe(200); |
| 98 | + expect(res.body.providerName).toBe("nvidia-inference"); |
| 99 | + expect(res.body.modelId).toBe("meta/llama-3.1-70b-instruct"); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe("POST /api/cluster-inference", () => { |
| 104 | + beforeEach(() => { |
| 105 | + _resetForTesting(); |
| 106 | + _setMocksForTesting({ execFile, spawn }); |
| 107 | + cleanTempFiles(); |
| 108 | + execFile.mockClear(); |
| 109 | + }); |
| 110 | + |
| 111 | + it("TC-CI06: returns 200 with parsed output on success", async () => { |
| 112 | + execFile.mockImplementation((cmd, args, opts, cb) => { |
| 113 | + if (typeof opts === "function") { cb = opts; opts = {}; } |
| 114 | + cb(null, "Provider: my-prov\nModel: llama\nVersion: 1\n", ""); |
| 115 | + }); |
| 116 | + |
| 117 | + const res = await request(server) |
| 118 | + .post("/api/cluster-inference") |
| 119 | + .send({ providerName: "my-prov", modelId: "llama" }); |
| 120 | + expect(res.status).toBe(200); |
| 121 | + expect(res.body.ok).toBe(true); |
| 122 | + }); |
| 123 | + |
| 124 | + it("TC-CI07: returns 400 when providerName missing", async () => { |
| 125 | + const res = await request(server) |
| 126 | + .post("/api/cluster-inference") |
| 127 | + .send({ modelId: "llama" }); |
| 128 | + expect(res.status).toBe(400); |
| 129 | + expect(res.body.error).toContain("providerName"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("TC-CI08: returns 400 when modelId missing", async () => { |
| 133 | + const res = await request(server) |
| 134 | + .post("/api/cluster-inference") |
| 135 | + .send({ providerName: "prov" }); |
| 136 | + expect(res.status).toBe(400); |
| 137 | + expect(res.body.error).toContain("modelId"); |
| 138 | + }); |
| 139 | + |
| 140 | + it("TC-CI09: returns 400 on CLI failure", async () => { |
| 141 | + execFile.mockImplementation((cmd, args, opts, cb) => { |
| 142 | + if (typeof opts === "function") { cb = opts; opts = {}; } |
| 143 | + const err = new Error("fail"); |
| 144 | + err.code = 1; |
| 145 | + cb(err, "", "set failed"); |
| 146 | + }); |
| 147 | + |
| 148 | + const res = await request(server) |
| 149 | + .post("/api/cluster-inference") |
| 150 | + .send({ providerName: "p", modelId: "m" }); |
| 151 | + expect(res.status).toBe(400); |
| 152 | + }); |
| 153 | + |
| 154 | + it("TC-CI10: calls nemoclaw cluster inference set with --provider and --model", async () => { |
| 155 | + execFile.mockImplementation((cmd, args, opts, cb) => { |
| 156 | + if (typeof opts === "function") { cb = opts; opts = {}; } |
| 157 | + cb(null, "", ""); |
| 158 | + }); |
| 159 | + |
| 160 | + await request(server) |
| 161 | + .post("/api/cluster-inference") |
| 162 | + .send({ providerName: "test-prov", modelId: "test-model" }); |
| 163 | + |
| 164 | + const setCall = execFile.mock.calls.find( |
| 165 | + (c) => c[0] === "nemoclaw" && c[1]?.includes("inference") && c[1]?.includes("set") |
| 166 | + ); |
| 167 | + expect(setCall).toBeDefined(); |
| 168 | + const args = setCall[1]; |
| 169 | + expect(args).toContain("--provider"); |
| 170 | + expect(args).toContain("test-prov"); |
| 171 | + expect(args).toContain("--model"); |
| 172 | + expect(args).toContain("test-model"); |
| 173 | + }); |
| 174 | +}); |
0 commit comments