Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
3.3 kB · 99 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import type { AgentMessage } from "@earendil-works/pi-agent-core";import { Container } from "@earendil-works/pi-tui";import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";import { ENV_AGENT_DIR, getDebugLogPath } from "../src/config.js";import { InteractiveMode } from "../src/modes/interactive/interactive-mode.js";import { initTheme } from "../src/modes/interactive/theme/theme.js";
type DebugCommandContext = { ui: { terminal: { columns: number; rows: number }; render: (width: number) => string[]; requestRender: () => void; }; agentConnection: { getMessages: () => Promise<AgentMessage[]>; }; chatContainer: Container; showError: (message: string) => void;};
type InteractiveModePrototype = { handleDebugCommand(this: DebugCommandContext): Promise<void>;};
const interactiveModePrototype = InteractiveMode.prototype as unknown as InteractiveModePrototype;
describe("InteractiveMode /debug", () => { let previousAgentDir: string | undefined; let tempAgentDir: string;
beforeAll(() => { initTheme("dark"); });
beforeEach(() => { previousAgentDir = process.env[ENV_AGENT_DIR]; tempAgentDir = mkdtempSync(join(tmpdir(), "prime-agent-debug-test-")); process.env[ENV_AGENT_DIR] = tempAgentDir; });
afterEach(() => { if (previousAgentDir === undefined) { delete process.env[ENV_AGENT_DIR]; } else { process.env[ENV_AGENT_DIR] = previousAgentDir; } rmSync(tempAgentDir, { recursive: true, force: true }); });
it("writes debug messages from AgentConnection", async () => { const message: AgentMessage = { role: "user", content: "connection-owned message", timestamp: 1 }; const context: DebugCommandContext = { ui: { terminal: { columns: 80, rows: 24 }, render: vi.fn(() => ["rendered line"]), requestRender: vi.fn(), }, agentConnection: { getMessages: vi.fn(async () => [message]) }, chatContainer: new Container(), showError: vi.fn(), };
await interactiveModePrototype.handleDebugCommand.call(context);
const debugLog = readFileSync(getDebugLogPath(), "utf8"); expect(context.agentConnection.getMessages).toHaveBeenCalledWith(); expect(debugLog).toContain("Terminal: 80x24"); expect(debugLog).toContain("rendered line"); expect(debugLog).toContain(JSON.stringify(message)); expect(context.showError).not.toHaveBeenCalled(); expect(context.ui.requestRender).toHaveBeenCalledWith(); });
it("reports connection failures instead of falling back to local UI services", async () => { const context: DebugCommandContext = { ui: { terminal: { columns: 80, rows: 24 }, render: vi.fn(() => ["rendered line"]), requestRender: vi.fn(), }, agentConnection: { getMessages: vi.fn(async () => { throw new Error("connection unavailable"); }), }, chatContainer: new Container(), showError: vi.fn(), };
await interactiveModePrototype.handleDebugCommand.call(context);
expect(context.showError).toHaveBeenCalledWith("Failed to write debug log: connection unavailable"); expect(existsSync(getDebugLogPath())).toBe(false); expect(context.ui.requestRender).not.toHaveBeenCalled(); });});