import {type ExtensionAPI, isBashToolResult} from "@earendil-works/pi-coding-agent"; import {isTestCommand} from "./parsing/typescript-command-output"; // noinspection JSUnusedGlobalSymbols export default function testStatusExtension(pi: ExtensionAPI) { // After every tool execution, check if tests just ran pi.on("tool_result", async (event, ctx) => { // Only care about bash commands if (!isBashToolResult(event)) return; if (!isTestCommand(event.input.command as string) ) return; const textContent = event.content.find((c): c is { type: "text"; text: string } => c.type === "text"); const output = textContent?.text ?? ""; const theme = ctx.ui.theme; // Detect pass/fail from vitest summary output const testsPassed = /Tests\s+.+passed/.test(output) && !/Tests\s+.+failed/.test(output); const testsFailed = /Tests\s+.+failed/.test(output) || event.details?.exitCode !== 0; if (testsPassed) { // Extract summary line for inline detail (e.g., "1 passed (242ms)") const summaryLine = output .split("\n") .find((line: string) => /Test Files\s+.+passed/.test(line)); const summary = summaryLine?.trim() ?? "All tests passed"; ctx.ui.setWidget("test-status", [ `${theme.fg("success", "●")} ${theme.fg("text", summary)}`, ]); } else if (testsFailed) { const summaryLine = output .split("\n") .find((line: string) => /Tests\s+.+failed/.test(line)); const summary = summaryLine?.trim() ?? "Tests failed"; ctx.ui.setWidget("test-status", [ `${theme.fg("error", "●")} ${theme.fg("text", summary)}`, ]); } }); }