test: centralize cli runtime capture helpers

This commit is contained in:
Peter Steinberger
2026-03-23 01:41:10 +00:00
parent 9a07187339
commit 2e6f2b0f07
4 changed files with 131 additions and 106 deletions

View File

@@ -3,6 +3,12 @@ import os from "node:os";
import path from "node:path";
import { Command } from "commander";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import {
firstWrittenJsonArg,
spyRuntimeErrors,
spyRuntimeJson,
spyRuntimeLogs,
} from "./test-runtime-capture.js";
const getMemorySearchManager = vi.hoisted(() => vi.fn());
const loadConfig = vi.hoisted(() => vi.fn(() => ({})));
@@ -58,26 +64,6 @@ afterEach(() => {
});
describe("memory cli", () => {
function spyRuntimeLogs() {
const logSpy = vi.spyOn(defaultRuntime, "log").mockImplementation(() => {});
vi.spyOn(defaultRuntime, "writeJson").mockImplementation((value: unknown, space = 2) => {
logSpy(JSON.stringify(value, null, space > 0 ? space : undefined));
});
return logSpy;
}
function spyRuntimeJson() {
return vi.spyOn(defaultRuntime, "writeJson").mockImplementation(() => {});
}
function spyRuntimeErrors() {
return vi.spyOn(defaultRuntime, "error").mockImplementation(() => {});
}
function firstWrittenJson(writeJson: ReturnType<typeof vi.spyOn>) {
return (writeJson.mock.calls[0]?.[0] ?? null) as Record<string, unknown>;
}
const inactiveMemorySecretDiagnostic = "agents.defaults.memorySearch.remote.apiKey inactive"; // pragma: allowlist secret
function expectCliSync(sync: ReturnType<typeof vi.fn>) {
@@ -175,7 +161,7 @@ describe("memory cli", () => {
});
mockManager({ ...params.manager, close });
const error = spyRuntimeErrors();
const error = spyRuntimeErrors(defaultRuntime);
await runMemoryCli(params.args);
params.beforeExpect?.();
@@ -206,7 +192,7 @@ describe("memory cli", () => {
close,
});
const log = spyRuntimeLogs();
const log = spyRuntimeLogs(defaultRuntime);
await runMemoryCli(["status"]);
expect(log).toHaveBeenCalledWith(expect.stringContaining("Vector: ready"));
@@ -255,7 +241,7 @@ describe("memory cli", () => {
const close = vi.fn(async () => {});
setupMemoryStatusWithInactiveSecretDiagnostics(close);
const log = spyRuntimeLogs();
const log = spyRuntimeLogs(defaultRuntime);
await runMemoryCli(["status"]);
expect(hasLoggedInactiveSecretDiagnostic(log)).toBe(true);
@@ -288,7 +274,7 @@ describe("memory cli", () => {
close,
});
const log = spyRuntimeLogs();
const log = spyRuntimeLogs(defaultRuntime);
await runMemoryCli(["status", "--agent", "main"]);
expect(log).toHaveBeenCalledWith(expect.stringContaining("Vector: unavailable"));
@@ -306,7 +292,7 @@ describe("memory cli", () => {
close,
});
const log = spyRuntimeLogs();
const log = spyRuntimeLogs(defaultRuntime);
await runMemoryCli(["status", "--deep"]);
expect(probeEmbeddingAvailability).toHaveBeenCalled();
@@ -349,7 +335,7 @@ describe("memory cli", () => {
close,
});
spyRuntimeLogs();
spyRuntimeLogs(defaultRuntime);
await runMemoryCli(["status", "--index"]);
expectCliSync(sync);
@@ -362,7 +348,7 @@ describe("memory cli", () => {
const sync = vi.fn(async () => {});
mockManager({ sync, close });
const log = spyRuntimeLogs();
const log = spyRuntimeLogs(defaultRuntime);
await runMemoryCli(["index"]);
expectCliSync(sync);
@@ -376,7 +362,7 @@ describe("memory cli", () => {
await withQmdIndexDb("sqlite-bytes", async (dbPath) => {
mockManager({ sync, status: () => ({ backend: "qmd", dbPath }), close });
const log = spyRuntimeLogs();
const log = spyRuntimeLogs(defaultRuntime);
await runMemoryCli(["index"]);
expectCliSync(sync);
@@ -392,7 +378,7 @@ describe("memory cli", () => {
await withQmdIndexDb("", async (dbPath) => {
mockManager({ sync, status: () => ({ backend: "qmd", dbPath }), close });
const error = spyRuntimeErrors();
const error = spyRuntimeErrors(defaultRuntime);
await runMemoryCli(["index"]);
expectCliSync(sync);
@@ -441,7 +427,7 @@ describe("memory cli", () => {
});
mockManager({ search, close });
const error = spyRuntimeErrors();
const error = spyRuntimeErrors(defaultRuntime);
await runMemoryCli(["search", "oops"]);
expect(search).toHaveBeenCalled();
@@ -458,10 +444,14 @@ describe("memory cli", () => {
close,
});
const writeJson = spyRuntimeJson();
const writeJson = spyRuntimeJson(defaultRuntime);
await runMemoryCli(["status", "--json"]);
const payload = firstWrittenJson(writeJson);
const payload = firstWrittenJsonArg<unknown[]>(writeJson);
expect(payload).not.toBeNull();
if (!payload) {
throw new Error("expected json payload");
}
expect(Array.isArray(payload)).toBe(true);
expect((payload[0] as Record<string, unknown>)?.agentId).toBe("main");
expect(close).toHaveBeenCalled();
@@ -471,11 +461,15 @@ describe("memory cli", () => {
const close = vi.fn(async () => {});
setupMemoryStatusWithInactiveSecretDiagnostics(close);
const writeJson = spyRuntimeJson();
const error = spyRuntimeErrors();
const writeJson = spyRuntimeJson(defaultRuntime);
const error = spyRuntimeErrors(defaultRuntime);
await runMemoryCli(["status", "--json"]);
const payload = firstWrittenJson(writeJson);
const payload = firstWrittenJsonArg<unknown[]>(writeJson);
expect(payload).not.toBeNull();
if (!payload) {
throw new Error("expected json payload");
}
expect(Array.isArray(payload)).toBe(true);
expect(hasLoggedInactiveSecretDiagnostic(error)).toBe(true);
});
@@ -483,7 +477,7 @@ describe("memory cli", () => {
it("logs default message when memory manager is missing", async () => {
getMemorySearchManager.mockResolvedValueOnce({ manager: null });
const log = spyRuntimeLogs();
const log = spyRuntimeLogs(defaultRuntime);
await runMemoryCli(["status"]);
expect(log).toHaveBeenCalledWith("Memory search disabled.");
@@ -496,7 +490,7 @@ describe("memory cli", () => {
close,
});
const log = spyRuntimeLogs();
const log = spyRuntimeLogs(defaultRuntime);
await runMemoryCli(["index"]);
expect(log).toHaveBeenCalledWith("Memory backend does not support manual reindex.");
@@ -508,7 +502,7 @@ describe("memory cli", () => {
const search = vi.fn(async () => []);
mockManager({ search, close });
const log = spyRuntimeLogs();
const log = spyRuntimeLogs(defaultRuntime);
await runMemoryCli(["search", "hello"]);
expect(search).toHaveBeenCalledWith("hello", {
@@ -524,7 +518,7 @@ describe("memory cli", () => {
const search = vi.fn(async () => []);
mockManager({ search, close });
const log = spyRuntimeLogs();
const log = spyRuntimeLogs(defaultRuntime);
await runMemoryCli(["search", "--query", "deployment notes"]);
expect(search).toHaveBeenCalledWith("deployment notes", {
@@ -541,7 +535,7 @@ describe("memory cli", () => {
const search = vi.fn(async () => []);
mockManager({ search, close });
spyRuntimeLogs();
spyRuntimeLogs(defaultRuntime);
await runMemoryCli(["search", "positional", "--query", "flagged"]);
expect(search).toHaveBeenCalledWith("flagged", {
@@ -552,7 +546,7 @@ describe("memory cli", () => {
});
it("fails when neither positional query nor --query is provided", async () => {
const error = spyRuntimeErrors();
const error = spyRuntimeErrors(defaultRuntime);
await runMemoryCli(["search"]);
expect(error).toHaveBeenCalledWith(
@@ -575,12 +569,16 @@ describe("memory cli", () => {
]);
mockManager({ search, close });
const writeJson = spyRuntimeJson();
const writeJson = spyRuntimeJson(defaultRuntime);
await runMemoryCli(["search", "hello", "--json"]);
const payload = firstWrittenJson(writeJson);
const payload = firstWrittenJsonArg<{ results: unknown[] }>(writeJson);
expect(payload).not.toBeNull();
if (!payload) {
throw new Error("expected json payload");
}
expect(Array.isArray(payload.results)).toBe(true);
expect(payload.results as unknown[]).toHaveLength(1);
expect(payload.results).toHaveLength(1);
expect(close).toHaveBeenCalled();
});
});