src/index.js
// AgentRove native agent: test-report-analyst (MIT).
// Reads test results (JUnit XML, TAP, Jest / Vitest JSON) and lcov coverage from files inside one
// workspace folder (or from text passed in) and summarises them: per-suite pass/fail/skip counts,
// slowest tests, failures with the first lines of their messages, a comparison of two runs (new
// failures, fixed tests, flaky candidates) and per-file coverage. Read-only, offline, runs nothing.
// The only write is write_report: one new file, exclusive create, after confirm: true.
import { readFileSync } from "node:fs";
import { defineAgent, makeContext, handleMcpMessage, runCli, runMcpStdio, validateManifest } from "../sdk/index.js";
import { ToolError } from "./errors.js";
import { workspaceRoot, normalizeRel } from "./fsguard.js";
import { createContext, loadRun, summarize, compare, coverage, summaryMarkdown, compareMarkdown, coverageMarkdown } from "./analyze.js";
import { writeReport } from "./write.js";
export { makeContext, handleMcpMessage, runCli, runMcpStdio, validateManifest, normalizeRel, workspaceRoot };
export { parseXml } from "./xml.js";
export { parseJunit, parseTap, parseJestJson, parseLcov, detectFormat } from "./parsers.js";
export const manifest = JSON.parse(readFileSync(new URL("../agent.json", import.meta.url), "utf8"));
/** The workspace folder: AGENTROVE_TEST_REPORTS_ROOT, or the folder the CLI / MCP server was started in. */
export function currentWorkspace(env = process.env) {
// An explicitly set folder is used as given (except roots, the home folder itself and system folders);
// the start folder is used only when it looks like a project (see PROJECT_MARKERS in fsguard.js).
const explicit = !!env.AGENTROVE_TEST_REPORTS_ROOT;
return workspaceRoot(explicit ? env.AGENTROVE_TEST_REPORTS_ROOT : process.cwd(), { explicit, envName: "AGENTROVE_TEST_REPORTS_ROOT" });
}
/** opts.root / opts.deadlineMs are for tests and embedding. */
export function analyzeContext(input, opts = {}) {
return createContext(opts.root ?? currentWorkspace(), { max_file_bytes: input.max_file_bytes, deadlineMs: opts.deadlineMs });
}
const runOf = (input) => ({ paths: input.paths, results_text: input.results_text, format: input.format, label: input.label });
export function summarizeResults(input, opts = {}) {
const ctx = analyzeContext(input, opts);
const run = loadRun(ctx, runOf(input), "tests", "summarize_results");
const s = summarize(run, input);
if (!s.totals.tests) ctx.warn("No test results were found in the given files.");
return { workspace: ctx.root, ...s, warnings: ctx.warnings, report: summaryMarkdown(s, input.title ?? "Test results") };
}
export function compareRuns(input, opts = {}) {
const ctx = analyzeContext(input, opts);
const base = loadRun(ctx, { ...input.base, label: input.base?.label ?? "base" }, "tests", "base");
const head = loadRun(ctx, { ...input.head, label: input.head?.label ?? "head" }, "tests", "head");
if (!base.cases.length) throw new ToolError("invalid_input", "base: no test results could be read, so the runs cannot be compared.");
if (!head.cases.length) throw new ToolError("invalid_input", "head: no test results could be read, so the runs cannot be compared.");
const c = compare(base, head, input);
if (!c.changed_files_known) ctx.warn("changed_files was not given, so status changes are reported as new failures or fixes only. Pass changed_files (an empty list when no files changed between the runs) to flag flaky candidates.");
return { workspace: ctx.root, ...c, base_files: base.files, head_files: head.files, warnings: ctx.warnings, report: compareMarkdown(c) };
}
export function coverageReport(input, opts = {}) {
const ctx = analyzeContext(input, opts);
const run = loadRun(ctx, runOf(input), "coverage", "coverage_report");
const c = coverage(run, input);
return { workspace: ctx.root, ...c, warnings: ctx.warnings, report: coverageMarkdown(c) };
}
async function summarize_results(input) {
return summarizeResults(input);
}
async function compare_runs(input) {
return compareRuns(input);
}
async function coverage_report(input) {
return coverageReport(input);
}
async function write_report(input) {
return { workspace: currentWorkspace(), ...writeReport(currentWorkspace(), input) };
}
const agent = defineAgent(manifest, { summarize_results, compare_runs, coverage_report, write_report });
export default agent;