Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(agent): a green npm test reads green, an unknown summary reports its exit code (CA)

purpleed7e80bf96fb
diff --git a/src/api/agents/__tests__/a-green-npm-test-reads-green.test.ts b/src/api/agents/__tests__/a-green-npm-test-reads-green.test.ts
new file mode 100644
index 00000000..eab9697e
--- /dev/null
+++ b/src/api/agents/__tests__/a-green-npm-test-reads-green.test.ts
@@ -0,0 +1,62 @@
+import { describe, it, expect } from 'vitest'
+import { parseForRunner, renderResult, sniffRunner } from '../test-runner'
+
+// CA bench CO10 (14.11.): `npm test` ran `node --test`, six tests green, and
+// the step was headed "NO TESTS / UNPARSEABLE (unknown): 0/0 passed".
+const SPEC_GREEN = `
+> test
+> node --test
+
+✔ get returns what was set (0.30075ms)
+✔ missing key is undefined (0.0545ms)
+✔ evicts the least recently used (0.08ms)
+✔ set on an existing key refreshes it (0.05ms)
+✔ size counts entries (0.04ms)
+✔ delete removes (0.04ms)
+ℹ tests 6
+ℹ suites 0
+ℹ pass 6
+ℹ fail 0
+ℹ cancelled 0
+ℹ skipped 0
+ℹ todo 0
+ℹ duration_ms 32.5`
+
+const TAP_RED = `TAP version 13
+ok 1 - get returns what was set
+not ok 2 - evicts the least recently used
+1..2
+# tests 2
+# pass 1
+# fail 1`
+
+describe('a test run behind npm test', () => {
+ it('reads a green node --test run as passed', () => {
+ const r = parseForRunner('unknown', SPEC_GREEN)
+ expect(r.runner).toBe('node')
+ expect(r.passed).toBe(true)
+ expect(r.passedCount).toBe(6)
+ expect(renderResult(r, 0)).toMatch(/^PASSED \(node\): 6\/6 passed, 0 failed\./)
+ })
+
+ it('names the failing test in TAP output', () => {
+ const r = parseForRunner('unknown', TAP_RED)
+ expect(r.passed).toBe(false)
+ expect(r.failedCount).toBe(1)
+ expect(r.failedTests).toEqual(['evicts the least recently used'])
+ })
+
+ it('falls back to the exit code, not "NO TESTS", when no summary is known', () => {
+ expect(sniffRunner('all good\n')).toBe('unknown')
+ const r = parseForRunner('unknown', 'all good\n')
+ expect(renderResult(r, 0)).toMatch(/^RAN \(exit 0\)/)
+ expect(renderResult(r, 2)).toMatch(/^FAILED \(exit 2\)/)
+ expect(renderResult(r)).toMatch(/^NO TESTS/)
+ })
+
+ it('still reads the runners it knew', () => {
+ expect(sniffRunner(' Tests 2 failed | 10 passed (12)')).toBe('vitest')
+ expect(sniffRunner('test result: ok. 3 passed; 0 failed; 0 ignored')).toBe('cargo')
+ expect(sniffRunner('===== 4 passed in 0.1s =====')).toBe('pytest')
+ })
+})
diff --git a/src/api/agents/test-runner.ts b/src/api/agents/test-runner.ts
index e571a194..a83e6bbe 100644
--- a/src/api/agents/test-runner.ts
+++ b/src/api/agents/test-runner.ts
@@ -7,7 +7,7 @@
* tail of the runner output — instead of an opaque stdout dump.
*/
-export type Runner = 'vitest' | 'cargo' | 'pytest' | 'jest' | 'unknown'
+export type Runner = 'vitest' | 'cargo' | 'pytest' | 'jest' | 'node' | 'unknown'
export interface ParsedTestRun {
runner: Runner
@@ -170,8 +170,56 @@ export function parsePytest(out: string): ParsedTestRun {
}
}
+/**
+ * Node's built-in runner (`node --test`, often behind `npm test`):
+ * ℹ tests 6 (spec reporter) # tests 6 (TAP reporter)
+ * ℹ pass 6 # pass 6
+ * ℹ fail 0 # fail 0
+ * ✖ evicts the least recently used (1.2ms) not ok 3 - evicts …
+ */
+export function parseNodeTest(out: string): ParsedTestRun {
+ const count = (name: string) => {
+ const m = out.match(new RegExp(`^\\s*[ℹ#]\\s*${name}\\s+(\\d+)\\s*$`, 'm'))
+ return m ? parseInt(m[1], 10) : 0
+ }
+ const passedCount = count('pass')
+ const failedCount = count('fail')
+ const total = Math.max(count('tests'), passedCount + failedCount)
+ const failedTests: string[] = []
+ for (const line of out.split('\n')) {
+ const m = line.match(/^\s*(?:✖\s+(.+?)(?:\s+\([\d.]+ms\))?|not ok \d+ - (.+))$/)
+ const name = m && (m[1] ?? m[2])
+ if (name && !/^failing tests:?$/i.test(name.trim()) && !failedTests.includes(name.trim())) failedTests.push(name.trim())
+ }
+ return {
+ runner: 'node',
+ passed: failedCount === 0 && total > 0,
+ total,
+ passedCount,
+ failedCount,
+ failedTests,
+ outputTail: tailLines(out, 40),
+ }
+}
+
+/**
+ * The runner read off the output, for a command that does not name one
+ * (`npm test`, `make test`). A green `node --test` behind `npm test` was
+ * headed "NO TESTS / UNPARSEABLE (unknown): 0/0" (CA bench CO10, 14.11.).
+ */
+export function sniffRunner(out: string): Runner {
+ if (/^\s*[ℹ#]\s*(?:tests|pass)\s+\d+\s*$/m.test(out)) return 'node'
+ if (/test result:\s+\w+\.\s+\d+\s+passed/.test(out)) return 'cargo'
+ if (/^\s*Tests\s+(?:\d+\s+failed\s*\|\s*)?\d+\s+passed/m.test(out)) return 'vitest'
+ if (/^=+ .*\d+ (?:passed|failed).* =+$/m.test(out)) return 'pytest'
+ return 'unknown'
+}
+
export function parseForRunner(runner: Runner, out: string): ParsedTestRun {
+ if (runner === 'unknown') runner = sniffRunner(out)
switch (runner) {
+ case 'node':
+ return parseNodeTest(out)
case 'vitest':
case 'jest':
return { ...parseVitest(out), runner }
@@ -193,9 +241,15 @@ export function parseForRunner(runner: Runner, out: string): ParsedTestRun {
}
/** Renders a ParsedTestRun as a string for the model. Compact + greppable. */
-export function renderResult(r: ParsedTestRun): string {
- const status = r.passed ? 'PASSED' : r.total === 0 ? 'NO TESTS / UNPARSEABLE' : 'FAILED'
- const head = `${status} (${r.runner}): ${r.passedCount}/${r.total} passed, ${r.failedCount} failed.`
+export function renderResult(r: ParsedTestRun, exitCode?: number | null): string {
+ // No summary found: the exit code is the only honest verdict left. "NO
+ // TESTS" over a run that printed six green ticks sent models back to run
+ // the tests again.
+ const head = r.total === 0 && typeof exitCode === 'number'
+ ? exitCode === 0
+ ? 'RAN (exit 0): the test summary was not recognised, read the output below.'
+ : `FAILED (exit ${exitCode}): the test summary was not recognised, read the output below.`
+ : `${r.passed ? 'PASSED' : r.total === 0 ? 'NO TESTS / UNPARSEABLE' : 'FAILED'} (${r.runner}): ${r.passedCount}/${r.total} passed, ${r.failedCount} failed.`
const failed = r.failedTests.length
? `\nFailing tests:\n${r.failedTests.map((t) => `- ${t}`).join('\n')}`
: ''
diff --git a/src/api/mcp/builtin-tools.ts b/src/api/mcp/builtin-tools.ts
index 0db8d3fd..9fffa237 100644
--- a/src/api/mcp/builtin-tools.ts
+++ b/src/api/mcp/builtin-tools.ts
@@ -1164,7 +1164,7 @@ async function executeShellExecute(
: 'unknown'
const combined = `${rawOut}\n${rawErr}`.trim()
const tail = combined.length > 4000 ? `…${combined.slice(-4000)}` : combined
- return `${renderResult(parseForRunner(runner, combined))}\n---\n${tail}`
+ return `${renderResult(parseForRunner(runner, combined), data.exitCode)}\n---\n${tail}`
}
if (data.exitCode && data.exitCode !== 0) return `Error (${data.exitCode}):\n${err || output}`
if (kind === 'git-status' && /--porcelain=2/.test(command)) {
@@ -1586,7 +1586,7 @@ async function executeRunTests(args: ToolArgs, run?: AgentRunContext, signal?: A
}
const combined = `${data.stdout || ''}\n${data.stderr || ''}`.trim()
const parsed = parseForRunner(runner ?? 'unknown', combined)
- return renderResult(parsed)
+ return renderResult(parsed, data.exitCode)
}
/**
* `runner` as the model may send it. A value the runner list does not know