refactor: reuse shared cli runtime test mocks

This commit is contained in:
Peter Steinberger
2026-03-23 01:42:00 +00:00
parent 2e6f2b0f07
commit 54213b587f
16 changed files with 176 additions and 283 deletions

View File

@@ -1,6 +1,9 @@
import { Command } from "commander";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { createCliRuntimeCapture } from "./test-runtime-capture.js";
const { defaultRuntime: runtime, resetRuntimeCapture } = createCliRuntimeCapture();
runtime.exit.mockImplementation(() => {});
const callGateway = vi.fn();
const buildGatewayConnectionDetails = vi.fn(() => ({
url: "ws://127.0.0.1:18789",
@@ -11,18 +14,6 @@ const listDevicePairing = vi.fn();
const approveDevicePairing = vi.fn();
const summarizeDeviceTokens = vi.fn();
const withProgress = vi.fn(async (_opts: unknown, fn: () => Promise<unknown>) => await fn());
const runtime = {
log: vi.fn(),
error: vi.fn(),
writeStdout: vi.fn((value: string) => {
runtime.log(value.endsWith("\n") ? value.slice(0, -1) : value);
}),
writeJson: vi.fn((value: unknown, space = 2) => {
runtime.log(JSON.stringify(value, null, space > 0 ? space : undefined));
}),
exit: vi.fn(),
};
vi.mock("../gateway/call.js", () => ({
callGateway,
buildGatewayConnectionDetails,
@@ -59,6 +50,11 @@ async function runDevicesCommand(argv: string[]) {
await program.parseAsync(["devices", ...argv], { from: "user" });
}
function readRuntimeCallText(call: unknown[] | undefined): string {
const value = call?.[0];
return typeof value === "string" ? value : "";
}
describe("devices cli approve", () => {
it("approves an explicit request id without listing", async () => {
callGateway.mockResolvedValueOnce({ device: { deviceId: "device-1" } });
@@ -312,13 +308,14 @@ describe("devices cli list", () => {
await runDevicesCommand(["list"]);
const output = runtime.log.mock.calls.map((entry) => String(entry[0] ?? "")).join("\n");
const output = runtime.log.mock.calls.map((entry) => readRuntimeCallText(entry)).join("\n");
expect(output).toContain("Scopes");
expect(output).toContain("operator.admin, operator.read");
});
});
afterEach(() => {
resetRuntimeCapture();
callGateway.mockClear();
buildGatewayConnectionDetails.mockClear();
buildGatewayConnectionDetails.mockReturnValue({