|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import type fs from "node:fs"; |
| 6 | + |
| 7 | +interface FsEntry { |
| 8 | + type: "file" | "dir"; |
| 9 | + content?: string; |
| 10 | +} |
| 11 | + |
| 12 | +const store = new Map<string, FsEntry>(); |
| 13 | +const mockExeca = vi.fn(); |
| 14 | + |
| 15 | +vi.mock("node:os", () => ({ |
| 16 | + homedir: () => "/fakehome", |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("node:crypto", () => ({ |
| 20 | + randomUUID: () => "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock("node:fs", async (importOriginal) => { |
| 24 | + const original = await importOriginal<typeof fs>(); |
| 25 | + return { |
| 26 | + ...original, |
| 27 | + existsSync: (p: string) => store.has(p), |
| 28 | + mkdirSync: vi.fn((p: string) => { |
| 29 | + store.set(p, { type: "dir" }); |
| 30 | + }), |
| 31 | + readFileSync: (p: string) => { |
| 32 | + const entry = store.get(p); |
| 33 | + if (entry?.type !== "file") { |
| 34 | + throw new Error(`ENOENT: ${p}`); |
| 35 | + } |
| 36 | + return entry.content ?? ""; |
| 37 | + }, |
| 38 | + writeFileSync: vi.fn((p: string, data: string) => { |
| 39 | + store.set(p, { type: "file", content: data }); |
| 40 | + }), |
| 41 | + readdirSync: (p: string) => { |
| 42 | + const prefix = p.endsWith("/") ? p : `${p}/`; |
| 43 | + const entries = new Set<string>(); |
| 44 | + for (const key of store.keys()) { |
| 45 | + if (key.startsWith(prefix)) { |
| 46 | + const [first] = key.slice(prefix.length).split("/"); |
| 47 | + if (first) { |
| 48 | + entries.add(first); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + if (entries.size === 0 && !store.has(p)) { |
| 53 | + throw new Error(`ENOENT: ${p}`); |
| 54 | + } |
| 55 | + return [...entries].sort(); |
| 56 | + }, |
| 57 | + }; |
| 58 | +}); |
| 59 | + |
| 60 | +vi.mock("execa", () => ({ |
| 61 | + execa: (...args: unknown[]) => mockExeca(...args), |
| 62 | +})); |
| 63 | + |
| 64 | +vi.mock("./ssrf.js", () => ({ |
| 65 | + validateEndpointUrl: vi.fn(async (url: string) => ({ url, pinnedUrl: url })), |
| 66 | +})); |
| 67 | + |
| 68 | +const { validateEndpointUrl } = await import("./ssrf.js"); |
| 69 | +const mockedValidateEndpoint = vi.mocked(validateEndpointUrl); |
| 70 | +const { metrics } = await import("../observability/metrics.js"); |
| 71 | +const { actionApply, actionPlan } = await import("./runner.js"); |
| 72 | + |
| 73 | +const stdoutChunks: string[] = []; |
| 74 | + |
| 75 | +function captureStdout(): void { |
| 76 | + vi.spyOn(process.stdout, "write").mockImplementation((chunk: string | Uint8Array) => { |
| 77 | + stdoutChunks.push(String(chunk)); |
| 78 | + return true; |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +function minimalBlueprint(): Record<string, unknown> { |
| 83 | + return { |
| 84 | + version: "1.0", |
| 85 | + components: { |
| 86 | + inference: { |
| 87 | + profiles: { |
| 88 | + default: { |
| 89 | + provider_type: "openai", |
| 90 | + provider_name: "my-provider", |
| 91 | + endpoint: "https://api.example.com/v1", |
| 92 | + model: "gpt-4", |
| 93 | + credential_env: "MY_API_KEY", |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + sandbox: { |
| 98 | + image: "openclaw", |
| 99 | + name: "test-sandbox", |
| 100 | + forward_ports: [18789], |
| 101 | + }, |
| 102 | + policy: { additions: {} }, |
| 103 | + }, |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +describe("runner metrics", () => { |
| 108 | + beforeEach(() => { |
| 109 | + store.clear(); |
| 110 | + stdoutChunks.length = 0; |
| 111 | + vi.clearAllMocks(); |
| 112 | + process.env.NEMOCLAW_METRICS_ENABLED = "true"; |
| 113 | + metrics.reset(); |
| 114 | + }); |
| 115 | + |
| 116 | + afterEach(() => { |
| 117 | + vi.restoreAllMocks(); |
| 118 | + metrics.reset(); |
| 119 | + delete process.env.NEMOCLAW_METRICS_ENABLED; |
| 120 | + }); |
| 121 | + |
| 122 | + it("records blueprint and endpoint validation metrics for successful plans", async () => { |
| 123 | + captureStdout(); |
| 124 | + mockExeca.mockResolvedValue({ exitCode: 0 }); |
| 125 | + |
| 126 | + await actionPlan("default", minimalBlueprint()); |
| 127 | + |
| 128 | + const output = metrics.renderPrometheus(); |
| 129 | + expect(output).toContain('blueprint_execution_total{action="plan",status="success"} 1'); |
| 130 | + expect(output).toContain( |
| 131 | + 'blueprint_execution_duration_seconds_count{action="plan",status="success"} 1', |
| 132 | + ); |
| 133 | + expect(output).toContain( |
| 134 | + 'api_validation_total{kind="endpoint_url",source="blueprint",status="success"} 1', |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + it("records blueprint and endpoint validation metrics for failed plans", async () => { |
| 139 | + captureStdout(); |
| 140 | + mockExeca.mockResolvedValue({ exitCode: 0 }); |
| 141 | + mockedValidateEndpoint.mockRejectedValueOnce(new Error("SSRF blocked")); |
| 142 | + |
| 143 | + await expect(actionPlan("default", minimalBlueprint())).rejects.toThrow("SSRF blocked"); |
| 144 | + |
| 145 | + const output = metrics.renderPrometheus(); |
| 146 | + expect(output).toContain('blueprint_execution_total{action="plan",status="error"} 1'); |
| 147 | + expect(output).toContain( |
| 148 | + 'api_validation_total{kind="endpoint_url",source="blueprint",status="error"} 1', |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + it("records sandbox lifecycle metrics during apply", async () => { |
| 153 | + captureStdout(); |
| 154 | + mockExeca.mockResolvedValue({ exitCode: 0, stdout: "", stderr: "" }); |
| 155 | + |
| 156 | + await actionApply("default", minimalBlueprint()); |
| 157 | + |
| 158 | + const output = metrics.renderPrometheus(); |
| 159 | + expect(output).toContain('blueprint_execution_total{action="apply",status="success"} 1'); |
| 160 | + expect(output).toContain('sandbox_lifecycle_total{operation="create",status="success"} 1'); |
| 161 | + expect(output).toContain( |
| 162 | + 'sandbox_lifecycle_duration_seconds_count{operation="create",status="success"} 1', |
| 163 | + ); |
| 164 | + }); |
| 165 | +}); |
0 commit comments