@robhowley/pi-structured-return 1.3.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +79 -52
  2. package/extensions/structured-return/src/config/registry.test.ts +171 -0
  3. package/extensions/structured-return/src/config/registry.ts +99 -13
  4. package/extensions/structured-return/src/index.test.ts +54 -0
  5. package/extensions/structured-return/src/index.ts +20 -9
  6. package/extensions/structured-return/src/parsers/ava-text.test.ts +116 -0
  7. package/extensions/structured-return/src/parsers/ava-text.ts +161 -0
  8. package/extensions/structured-return/src/parsers/cargo-build.ts +2 -2
  9. package/extensions/structured-return/src/parsers/cargo-test.ts +2 -2
  10. package/extensions/structured-return/src/parsers/dbt-json.test.ts +172 -0
  11. package/extensions/structured-return/src/parsers/dbt-json.ts +131 -0
  12. package/extensions/structured-return/src/parsers/eslint-json.ts +2 -2
  13. package/extensions/structured-return/src/parsers/go-test-json.test.ts +119 -0
  14. package/extensions/structured-return/src/parsers/go-test-json.ts +137 -0
  15. package/extensions/structured-return/src/parsers/hadolint-json.test.ts +73 -0
  16. package/extensions/structured-return/src/parsers/hadolint-json.ts +55 -0
  17. package/extensions/structured-return/src/parsers/junit-xml.ts +2 -2
  18. package/extensions/structured-return/src/parsers/minitest-text.ts +2 -2
  19. package/extensions/structured-return/src/parsers/mocha-json.test.ts +108 -0
  20. package/extensions/structured-return/src/parsers/mocha-json.ts +77 -0
  21. package/extensions/structured-return/src/parsers/mypy-json.test.ts +101 -0
  22. package/extensions/structured-return/src/parsers/mypy-json.ts +80 -0
  23. package/extensions/structured-return/src/parsers/pylint-json.test.ts +120 -0
  24. package/extensions/structured-return/src/parsers/pylint-json.ts +64 -0
  25. package/extensions/structured-return/src/parsers/rspec-json.ts +2 -2
  26. package/extensions/structured-return/src/parsers/rubocop-json.test.ts +118 -0
  27. package/extensions/structured-return/src/parsers/rubocop-json.ts +65 -0
  28. package/extensions/structured-return/src/parsers/ruff-json.ts +2 -2
  29. package/extensions/structured-return/src/parsers/shellcheck-json.test.ts +110 -0
  30. package/extensions/structured-return/src/parsers/shellcheck-json.ts +61 -0
  31. package/extensions/structured-return/src/parsers/stylelint-json.test.ts +120 -0
  32. package/extensions/structured-return/src/parsers/stylelint-json.ts +68 -0
  33. package/extensions/structured-return/src/parsers/swiftc-text.test.ts +73 -0
  34. package/extensions/structured-return/src/parsers/swiftc-text.ts +47 -0
  35. package/extensions/structured-return/src/parsers/tail-fallback.ts +2 -2
  36. package/extensions/structured-return/src/parsers/tsc-text.test.ts +81 -0
  37. package/extensions/structured-return/src/parsers/tsc-text.ts +47 -0
  38. package/extensions/structured-return/src/parsers/unittest-text.test.ts +126 -0
  39. package/extensions/structured-return/src/parsers/unittest-text.ts +116 -0
  40. package/extensions/structured-return/src/parsers/utils.ts +43 -0
  41. package/extensions/structured-return/src/parsers/vitest-json.ts +2 -2
  42. package/package.json +1 -1
  43. package/skills/structured-return/SKILL.md +44 -0
@@ -0,0 +1,119 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { describe, it, expect } from "vitest";
5
+ import parser from "./go-test-json";
6
+ import type { RunContext } from "../types";
7
+
8
+ function makeCtx(stdout: string, cwd = "/project"): RunContext {
9
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "gotest-test-"));
10
+ const stdoutPath = path.join(dir, "stdout");
11
+ fs.writeFileSync(stdoutPath, stdout);
12
+ return {
13
+ command: "go test -json ./...",
14
+ argv: ["go", "test", "-json", "./..."],
15
+ cwd,
16
+ artifactPaths: [],
17
+ stdoutPath,
18
+ stderrPath: path.join(dir, "stderr"),
19
+ logPath: path.join(dir, "log"),
20
+ };
21
+ }
22
+
23
+ function ndjson(...events: object[]): string {
24
+ return events.map((e) => JSON.stringify(e)).join("\n");
25
+ }
26
+
27
+ describe("go-test-json parser", () => {
28
+ it("assertion failure → file:line and message from t.Errorf output", async () => {
29
+ const stdout = ndjson(
30
+ { Action: "run", Package: "math-test", Test: "TestAdd" },
31
+ { Action: "output", Package: "math-test", Test: "TestAdd", Output: "=== RUN TestAdd\n" },
32
+ { Action: "output", Package: "math-test", Test: "TestAdd", Output: "--- PASS: TestAdd (0.00s)\n" },
33
+ { Action: "pass", Package: "math-test", Test: "TestAdd" },
34
+ { Action: "run", Package: "math-test", Test: "TestMultiply" },
35
+ { Action: "output", Package: "math-test", Test: "TestMultiply", Output: "=== RUN TestMultiply\n" },
36
+ {
37
+ Action: "output",
38
+ Package: "math-test",
39
+ Test: "TestMultiply",
40
+ Output: " math_test.go:14: expected 99, got 12\n",
41
+ },
42
+ { Action: "output", Package: "math-test", Test: "TestMultiply", Output: "--- FAIL: TestMultiply (0.00s)\n" },
43
+ { Action: "fail", Package: "math-test", Test: "TestMultiply" },
44
+ { Action: "fail", Package: "math-test", Elapsed: 0.1 }
45
+ );
46
+ const result = await parser.parse(makeCtx(stdout));
47
+ expect(result.status).toBe("fail");
48
+ expect(result.summary).toBe("1 failed, 1 passed");
49
+ expect(result.failures).toHaveLength(1);
50
+ expect(result.failures![0]).toMatchObject({
51
+ file: "math_test.go",
52
+ line: 14,
53
+ message: "expected 99, got 12",
54
+ });
55
+ });
56
+
57
+ it("panic → message and user-code file:line from stack trace", async () => {
58
+ const stdout = ndjson(
59
+ { Action: "run", Package: "math-test", Test: "TestDoesNotPanic" },
60
+ { Action: "output", Package: "math-test", Test: "TestDoesNotPanic", Output: "=== RUN TestDoesNotPanic\n" },
61
+ {
62
+ Action: "output",
63
+ Package: "math-test",
64
+ Test: "TestDoesNotPanic",
65
+ Output: "--- FAIL: TestDoesNotPanic (0.00s)\n",
66
+ },
67
+ {
68
+ Action: "output",
69
+ Package: "math-test",
70
+ Test: "TestDoesNotPanic",
71
+ Output: "panic: runtime error: invalid memory address or nil pointer dereference [recovered, repanicked]\n",
72
+ },
73
+ { Action: "output", Package: "math-test", Test: "TestDoesNotPanic", Output: "goroutine 23 [running]:\n" },
74
+ {
75
+ Action: "output",
76
+ Package: "math-test",
77
+ Test: "TestDoesNotPanic",
78
+ Output: "testing.tRunner.func1.2({0x1003f2be0, 0x10043cec0})\n",
79
+ },
80
+ {
81
+ Action: "output",
82
+ Package: "math-test",
83
+ Test: "TestDoesNotPanic",
84
+ Output: "\t/usr/local/go/src/testing/testing.go:1974 +0x1a0\n",
85
+ },
86
+ {
87
+ Action: "output",
88
+ Package: "math-test",
89
+ Test: "TestDoesNotPanic",
90
+ Output: "math-test.TestDoesNotPanic(0x123)\n",
91
+ },
92
+ { Action: "output", Package: "math-test", Test: "TestDoesNotPanic", Output: "\t/project/math_test.go:20 +0x4\n" },
93
+ { Action: "fail", Package: "math-test", Test: "TestDoesNotPanic" },
94
+ { Action: "fail", Package: "math-test", Elapsed: 0.1 }
95
+ );
96
+ const result = await parser.parse(makeCtx(stdout, "/project"));
97
+ expect(result.failures).toHaveLength(1);
98
+ expect(result.failures![0].message).toBe("runtime error: invalid memory address or nil pointer dereference");
99
+ expect(result.failures![0].file).toBe("math_test.go");
100
+ expect(result.failures![0].line).toBe(20);
101
+ });
102
+
103
+ it("all passing → status pass", async () => {
104
+ const stdout = ndjson(
105
+ { Action: "run", Package: "math-test", Test: "TestAdd" },
106
+ { Action: "pass", Package: "math-test", Test: "TestAdd" },
107
+ { Action: "pass", Package: "math-test", Elapsed: 0.1 }
108
+ );
109
+ const result = await parser.parse(makeCtx(stdout));
110
+ expect(result.status).toBe("pass");
111
+ expect(result.summary).toBe("1 passed");
112
+ expect(result.failures).toHaveLength(0);
113
+ });
114
+
115
+ it("empty stdout → status error", async () => {
116
+ const result = await parser.parse(makeCtx(""));
117
+ expect(result.status).toBe("error");
118
+ });
119
+ });
@@ -0,0 +1,137 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import type { ParserModule, ParsedFailure } from "../types";
4
+
5
+ interface GoTestEvent {
6
+ Action: string;
7
+ Package?: string;
8
+ Test?: string;
9
+ Output?: string;
10
+ Elapsed?: number;
11
+ }
12
+
13
+ /** Extract file:line and message from a Go t.Error/t.Errorf output line. */
14
+ function parseTestOutput(output: string): { file?: string; line?: number; message?: string } | undefined {
15
+ // Format: " file.go:line: message"
16
+ const m = output.match(/^\s+(\S+\.go):(\d+):\s+(.+)/);
17
+ if (m) return { file: m[1], line: Number(m[2]), message: m[3].trim() };
18
+ return undefined;
19
+ }
20
+
21
+ /** Extract panic message and user-code file:line from a stack trace. */
22
+ function parsePanic(
23
+ outputs: string[],
24
+ testName: string
25
+ ): { message?: string; file?: string; line?: number } | undefined {
26
+ const panicLine = outputs.find((o) => o.trim().startsWith("panic:"));
27
+ if (!panicLine) return undefined;
28
+ const message = panicLine
29
+ .trim()
30
+ .replace(/^panic:\s*/, "")
31
+ .replace(/\s*\[.*\]$/, "")
32
+ .trim();
33
+
34
+ // Find the user code frame: "module.TestName(..." followed by "\tfile.go:line"
35
+ for (let i = 0; i < outputs.length - 1; i++) {
36
+ if (outputs[i].includes(`.${testName}(`)) {
37
+ const m = outputs[i + 1].match(/\t([^\t]+\.go):(\d+)/);
38
+ if (m) return { message, file: m[1], line: Number(m[2]) };
39
+ }
40
+ }
41
+ return { message };
42
+ }
43
+
44
+ const parser: ParserModule = {
45
+ id: "go-test-json",
46
+ async parse(ctx) {
47
+ const stdout = fs.readFileSync(ctx.stdoutPath, "utf8").trim();
48
+ if (!stdout) {
49
+ return { tool: "go test", status: "error", summary: "no output", logPath: ctx.logPath };
50
+ }
51
+
52
+ const events: GoTestEvent[] = [];
53
+ for (const line of stdout.split("\n")) {
54
+ if (!line.trim()) continue;
55
+ try {
56
+ events.push(JSON.parse(line) as GoTestEvent);
57
+ } catch {
58
+ // skip non-JSON lines
59
+ }
60
+ }
61
+
62
+ // Collect outputs per test
63
+ const testOutputs = new Map<string, string[]>();
64
+ let passed = 0;
65
+ let failed = 0;
66
+
67
+ for (const e of events) {
68
+ if (!e.Test) continue;
69
+ if (e.Action === "output") {
70
+ const outputs = testOutputs.get(e.Test) ?? [];
71
+ outputs.push(e.Output ?? "");
72
+ testOutputs.set(e.Test, outputs);
73
+ } else if (e.Action === "pass") {
74
+ passed++;
75
+ } else if (e.Action === "fail") {
76
+ failed++;
77
+ }
78
+ }
79
+
80
+ const failures: ParsedFailure[] = [];
81
+ for (const e of events) {
82
+ if (e.Action !== "fail" || !e.Test) continue;
83
+ const outputs = testOutputs.get(e.Test) ?? [];
84
+
85
+ // Try parsing as a t.Error/t.Errorf output first
86
+ let found = false;
87
+ for (const o of outputs) {
88
+ const parsed = parseTestOutput(o);
89
+ if (parsed) {
90
+ const relFile = parsed.file
91
+ ? path.isAbsolute(parsed.file)
92
+ ? path.relative(ctx.cwd, parsed.file)
93
+ : parsed.file
94
+ : undefined;
95
+ failures.push({
96
+ id: e.Test,
97
+ file: relFile,
98
+ line: parsed.line,
99
+ message: parsed.message,
100
+ });
101
+ found = true;
102
+ break;
103
+ }
104
+ }
105
+
106
+ if (!found) {
107
+ // Try parsing as a panic
108
+ const panic = parsePanic(outputs, e.Test);
109
+ if (panic) {
110
+ const relFile = panic.file
111
+ ? path.isAbsolute(panic.file)
112
+ ? path.relative(ctx.cwd, panic.file)
113
+ : panic.file
114
+ : undefined;
115
+ failures.push({
116
+ id: e.Test,
117
+ file: relFile,
118
+ line: panic.line,
119
+ message: panic.message ?? "panic",
120
+ });
121
+ } else {
122
+ failures.push({ id: e.Test, message: "test failed" });
123
+ }
124
+ }
125
+ }
126
+
127
+ return {
128
+ tool: "go test",
129
+ status: failed > 0 ? "fail" : "pass",
130
+ summary: failed > 0 ? `${failed} failed, ${passed} passed` : `${passed} passed`,
131
+ failures,
132
+ logPath: ctx.logPath,
133
+ };
134
+ },
135
+ };
136
+
137
+ export default parser;
@@ -0,0 +1,73 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { describe, it, expect } from "vitest";
5
+ import parser from "./hadolint-json";
6
+ import type { RunContext } from "../types";
7
+
8
+ function makeCtx(stdout: string, cwd = "/project"): RunContext {
9
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "hadolint-test-"));
10
+ const stdoutPath = path.join(dir, "stdout");
11
+ fs.writeFileSync(stdoutPath, stdout);
12
+ return {
13
+ command: "hadolint Dockerfile --format json",
14
+ argv: ["hadolint", "Dockerfile", "--format", "json"],
15
+ cwd,
16
+ artifactPaths: [],
17
+ stdoutPath,
18
+ stderrPath: path.join(dir, "stderr"),
19
+ logPath: path.join(dir, "log"),
20
+ };
21
+ }
22
+
23
+ describe("hadolint-json parser", () => {
24
+ it("multiple errors → correct file, line, message, rule", async () => {
25
+ const stdout = JSON.stringify([
26
+ {
27
+ code: "DL3007",
28
+ column: 1,
29
+ file: "Dockerfile",
30
+ level: "warning",
31
+ line: 1,
32
+ message: "Using latest is prone to errors.",
33
+ },
34
+ {
35
+ code: "DL3008",
36
+ column: 1,
37
+ file: "Dockerfile",
38
+ level: "warning",
39
+ line: 2,
40
+ message: "Pin versions in apt get install.",
41
+ },
42
+ ]);
43
+ const result = await parser.parse(makeCtx(stdout));
44
+ expect(result.status).toBe("fail");
45
+ expect(result.summary).toBe("2 lint errors");
46
+ expect(result.failures![0]).toMatchObject({
47
+ file: "Dockerfile",
48
+ line: 1,
49
+ message: "Using latest is prone to errors.",
50
+ rule: "DL3007",
51
+ });
52
+ expect(result.failures![1]).toMatchObject({ rule: "DL3008" });
53
+ });
54
+
55
+ it("empty array → status pass", async () => {
56
+ const result = await parser.parse(makeCtx("[]"));
57
+ expect(result.status).toBe("pass");
58
+ expect(result.failures).toHaveLength(0);
59
+ });
60
+
61
+ it("empty stdout → status pass", async () => {
62
+ const result = await parser.parse(makeCtx(""));
63
+ expect(result.status).toBe("pass");
64
+ });
65
+
66
+ it("absolute paths → relativized", async () => {
67
+ const stdout = JSON.stringify([
68
+ { code: "DL3003", column: 1, file: "/project/Dockerfile", level: "warning", line: 3, message: "Use WORKDIR" },
69
+ ]);
70
+ const result = await parser.parse(makeCtx(stdout, "/project"));
71
+ expect(result.failures![0].file).toBe("Dockerfile");
72
+ });
73
+ });
@@ -0,0 +1,55 @@
1
+ import path from "node:path";
2
+ import type { ParserModule, ParsedFailure } from "../types";
3
+ import { safeReadFile } from "./utils";
4
+
5
+ interface HadolintItem {
6
+ code: string;
7
+ column: number;
8
+ file: string;
9
+ level: string;
10
+ line: number;
11
+ message: string;
12
+ }
13
+
14
+ const parser: ParserModule = {
15
+ id: "hadolint-json",
16
+ async parse(ctx) {
17
+ const stdout = safeReadFile(ctx.stdoutPath).trim();
18
+ if (!stdout || stdout === "[]") {
19
+ return { tool: "hadolint", status: "pass", summary: "no lint errors", failures: [], logPath: ctx.logPath };
20
+ }
21
+
22
+ let items: HadolintItem[];
23
+ try {
24
+ items = JSON.parse(stdout) as HadolintItem[];
25
+ } catch {
26
+ return {
27
+ tool: "hadolint",
28
+ status: "error",
29
+ summary: "failed to parse hadolint JSON output",
30
+ logPath: ctx.logPath,
31
+ };
32
+ }
33
+
34
+ const failures: ParsedFailure[] = items.map((item) => {
35
+ const relPath = path.isAbsolute(item.file) ? path.relative(ctx.cwd, item.file) : item.file;
36
+ return {
37
+ id: `${relPath}:${item.line}:${item.code}`,
38
+ file: relPath,
39
+ line: item.line,
40
+ message: item.message,
41
+ rule: item.code,
42
+ };
43
+ });
44
+
45
+ return {
46
+ tool: "hadolint",
47
+ status: failures.length > 0 ? "fail" : "pass",
48
+ summary: failures.length > 0 ? `${failures.length} lint errors` : "no lint errors",
49
+ failures,
50
+ logPath: ctx.logPath,
51
+ };
52
+ },
53
+ };
54
+
55
+ export default parser;
@@ -1,7 +1,7 @@
1
- import fs from "node:fs";
2
1
  import path from "node:path";
3
2
  import { XMLParser } from "fast-xml-parser";
4
3
  import type { ParserModule, ParsedFailure } from "../types";
4
+ import { safeReadFile } from "./utils";
5
5
 
6
6
  interface JUnitFailureOrError {
7
7
  message?: string;
@@ -148,7 +148,7 @@ const parser: ParserModule = {
148
148
  id: "junit-xml",
149
149
  async parse(ctx) {
150
150
  const artifactPath = ctx.artifactPaths[0] ?? ctx.stdoutPath;
151
- const xml = fs.readFileSync(artifactPath, "utf8");
151
+ const xml = safeReadFile(artifactPath);
152
152
  const doc = xmlParser.parse(xml) as JUnitDocument;
153
153
 
154
154
  const suites: JUnitTestSuite[] = doc.testsuites?.testsuite ?? doc.testsuite ?? [];
@@ -1,6 +1,6 @@
1
- import fs from "node:fs";
2
1
  import path from "node:path";
3
2
  import type { ParserModule, ParsedFailure } from "../types";
3
+ import { safeReadFile } from "./utils";
4
4
 
5
5
  /**
6
6
  * Parses minitest's default text output. No flags or reporters required —
@@ -9,7 +9,7 @@ import type { ParserModule, ParsedFailure } from "../types";
9
9
  const parser: ParserModule = {
10
10
  id: "minitest-text",
11
11
  async parse(ctx) {
12
- const output = fs.readFileSync(ctx.stdoutPath, "utf8");
12
+ const output = safeReadFile(ctx.stdoutPath);
13
13
 
14
14
  // "3 runs, 2 assertions, 1 failures, 1 errors, 0 skips"
15
15
  const summaryMatch = output.match(/(\d+) runs?, \d+ assertions?, (\d+) failures?, (\d+) errors?, (\d+) skips?/);
@@ -0,0 +1,108 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { describe, it, expect } from "vitest";
5
+ import parser from "./mocha-json";
6
+ import type { RunContext } from "../types";
7
+
8
+ function makeCtx(stdout: string, cwd = "/project"): RunContext {
9
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "mocha-test-"));
10
+ const stdoutPath = path.join(dir, "stdout");
11
+ fs.writeFileSync(stdoutPath, stdout);
12
+ return {
13
+ command: "mocha test_math.js --reporter json",
14
+ argv: ["mocha", "test_math.js", "--reporter", "json"],
15
+ cwd,
16
+ artifactPaths: [],
17
+ stdoutPath,
18
+ stderrPath: path.join(dir, "stderr"),
19
+ logPath: path.join(dir, "log"),
20
+ };
21
+ }
22
+
23
+ describe("mocha-json parser", () => {
24
+ it("assertion failure → expected/actual in message, file:line from stack", async () => {
25
+ const stdout = JSON.stringify({
26
+ stats: { passes: 1, failures: 1 },
27
+ failures: [
28
+ {
29
+ title: "multiplies two numbers correctly",
30
+ fullTitle: "basic math multiplies two numbers correctly",
31
+ file: "/project/test_math.js",
32
+ err: {
33
+ stack:
34
+ "AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:\n\n12 !== 99\n\n at Context.<anonymous> (test_math.js:9:12)\n at process.processImmediate (node:internal/timers:504:21)",
35
+ message: "Expected values to be strictly equal:\n\n12 !== 99\n",
36
+ actual: "12",
37
+ expected: "99",
38
+ operator: "strictEqual",
39
+ code: "ERR_ASSERTION",
40
+ name: "AssertionError",
41
+ },
42
+ },
43
+ ],
44
+ });
45
+ const result = await parser.parse(makeCtx(stdout));
46
+ expect(result.status).toBe("fail");
47
+ expect(result.summary).toBe("1 failed, 1 passed");
48
+ expect(result.failures).toHaveLength(1);
49
+ expect(result.failures![0].message).toBe("expected 99, got 12");
50
+ expect(result.failures![0].file).toBe("test_math.js");
51
+ expect(result.failures![0].line).toBe(9);
52
+ expect(result.failures![0].rule).toBe("ERR_ASSERTION");
53
+ });
54
+
55
+ it("runtime error → message from err.message, file:line from stack", async () => {
56
+ const stdout = JSON.stringify({
57
+ stats: { passes: 1, failures: 1 },
58
+ failures: [
59
+ {
60
+ title: "does not divide by zero",
61
+ fullTitle: "basic math does not divide by zero",
62
+ file: "/project/test_math.js",
63
+ err: {
64
+ stack:
65
+ "TypeError: Cannot read properties of null (reading 'value')\n at Context.<anonymous> (test_math.js:13:29)\n at process.processImmediate (node:internal/timers:504:21)",
66
+ message: "Cannot read properties of null (reading 'value')",
67
+ },
68
+ },
69
+ ],
70
+ });
71
+ const result = await parser.parse(makeCtx(stdout));
72
+ expect(result.failures![0].message).toBe("Cannot read properties of null (reading 'value')");
73
+ expect(result.failures![0].file).toBe("test_math.js");
74
+ expect(result.failures![0].line).toBe(13);
75
+ });
76
+
77
+ it("all passing → status pass", async () => {
78
+ const stdout = JSON.stringify({ stats: { passes: 3, failures: 0 }, failures: [] });
79
+ const result = await parser.parse(makeCtx(stdout));
80
+ expect(result.status).toBe("pass");
81
+ expect(result.summary).toBe("3 passed");
82
+ expect(result.failures).toHaveLength(0);
83
+ });
84
+
85
+ it("empty stdout → status error", async () => {
86
+ const result = await parser.parse(makeCtx(""));
87
+ expect(result.status).toBe("error");
88
+ });
89
+
90
+ it("file paths relativized to cwd", async () => {
91
+ const stdout = JSON.stringify({
92
+ stats: { passes: 0, failures: 1 },
93
+ failures: [
94
+ {
95
+ title: "fails",
96
+ fullTitle: "suite fails",
97
+ file: "/project/test/foo.js",
98
+ err: {
99
+ stack: "Error: oops\n at Context.<anonymous> (/project/test/foo.js:5:10)",
100
+ message: "oops",
101
+ },
102
+ },
103
+ ],
104
+ });
105
+ const result = await parser.parse(makeCtx(stdout, "/project"));
106
+ expect(result.failures![0].file).toBe("test/foo.js");
107
+ });
108
+ });
@@ -0,0 +1,77 @@
1
+ import path from "node:path";
2
+ import type { ParserModule, ParsedFailure } from "../types";
3
+ import { extractJsStackLocation, safeReadFile } from "./utils";
4
+
5
+ interface MochaErr {
6
+ message?: string;
7
+ stack?: string;
8
+ actual?: string;
9
+ expected?: string;
10
+ operator?: string;
11
+ code?: string;
12
+ name?: string;
13
+ }
14
+
15
+ interface MochaTest {
16
+ title: string;
17
+ fullTitle: string;
18
+ file: string;
19
+ err: MochaErr;
20
+ }
21
+
22
+ interface MochaReport {
23
+ stats: { passes: number; failures: number };
24
+ failures: MochaTest[];
25
+ }
26
+
27
+ /** Build a compact failure message. For assertions, include expected/actual. */
28
+ function buildMessage(err: MochaErr): string {
29
+ if (err.actual !== undefined && err.expected !== undefined) {
30
+ return `expected ${err.expected}, got ${err.actual}`;
31
+ }
32
+ // First line of message, stripped of newlines
33
+ const firstLine = (err.message ?? "test failed").split("\n")[0].trim();
34
+ return firstLine;
35
+ }
36
+
37
+ const parser: ParserModule = {
38
+ id: "mocha-json",
39
+ async parse(ctx) {
40
+ const stdout = safeReadFile(ctx.stdoutPath).trim();
41
+ if (!stdout) {
42
+ return { tool: "mocha", status: "error", summary: "no output", logPath: ctx.logPath };
43
+ }
44
+
45
+ let report: MochaReport;
46
+ try {
47
+ report = JSON.parse(stdout) as MochaReport;
48
+ } catch {
49
+ return { tool: "mocha", status: "error", summary: "failed to parse mocha JSON output", logPath: ctx.logPath };
50
+ }
51
+
52
+ const failures: ParsedFailure[] = report.failures.map((test) => {
53
+ const loc = extractJsStackLocation(test.err.stack);
54
+ const relFile = loc.file ? path.relative(ctx.cwd, path.resolve(ctx.cwd, loc.file)) : undefined;
55
+ return {
56
+ id: test.fullTitle,
57
+ file: relFile,
58
+ line: loc.line,
59
+ message: buildMessage(test.err),
60
+ rule: test.err.code ?? test.err.name,
61
+ };
62
+ });
63
+
64
+ const failed = report.stats.failures;
65
+ const passed = report.stats.passes;
66
+
67
+ return {
68
+ tool: "mocha",
69
+ status: failed > 0 ? "fail" : "pass",
70
+ summary: failed > 0 ? `${failed} failed, ${passed} passed` : `${passed} passed`,
71
+ failures,
72
+ logPath: ctx.logPath,
73
+ };
74
+ },
75
+ };
76
+
77
+ export default parser;