mirror of
https://github.com/garrytan/gbrain.git
synced 2026-07-31 04:07:52 +00:00
fix(jobs): decouple 'jobs watch' format (--json) from loop (--follow); non-TTY prints one human snapshot (#1784)
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* v0.42.11.0 (#1784) — non-TTY output contract E2E (the `cmd </dev/null` smoke
|
||||
* test the issue asked for).
|
||||
*
|
||||
* `jobs watch` is the command whose PRIMARY output was gated on isTTY: non-TTY
|
||||
* callers (subagent / pipe / cron) got JSON-only with no way to a human view,
|
||||
* and the loop ran forever. The fix decouples FORMAT (--json) from LOOP
|
||||
* (--follow, default=isTTY), so a non-TTY run prints ONE human snapshot and
|
||||
* exits.
|
||||
*
|
||||
* This drives the REAL binary in a non-TTY child (stdin ignored, stdout piped →
|
||||
* `process.stdout.isTTY` is false), so it exercises the exact path subagents and
|
||||
* pipes hit. Hermetic PGLite brain in a temp HOME; no Postgres, no API keys.
|
||||
*
|
||||
* Serial because it spawns subprocesses against a temp brain and PGLite is a
|
||||
* single-writer engine.
|
||||
*/
|
||||
import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
|
||||
import { execFileSync } from 'child_process';
|
||||
import { mkdtempSync, rmSync } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
|
||||
describe('non-TTY output contract: jobs watch (#1784)', () => {
|
||||
let home: string;
|
||||
let env: NodeJS.ProcessEnv;
|
||||
|
||||
beforeAll(() => {
|
||||
home = mkdtempSync(join(tmpdir(), 'gbrain-nontty-e2e-'));
|
||||
env = { ...process.env, GBRAIN_HOME: home };
|
||||
// Fresh local PGLite brain so `jobs watch` has an engine to read.
|
||||
execFileSync('bun', ['run', 'src/cli.ts', 'init', '--pglite', '--no-embedding', '--non-interactive'], {
|
||||
cwd: process.cwd(), env, stdio: 'ignore',
|
||||
});
|
||||
}, 60_000);
|
||||
|
||||
afterAll(() => {
|
||||
try { rmSync(home, { recursive: true, force: true }); } catch { /* best-effort */ }
|
||||
});
|
||||
|
||||
test('non-TTY, no flags → ONE human snapshot, non-empty, not JSON, exit 0', () => {
|
||||
// stdin 'ignore' + stdout 'pipe' makes the child non-TTY on both ends.
|
||||
const out = execFileSync('bun', ['run', 'src/cli.ts', 'jobs', 'watch'], {
|
||||
cwd: process.cwd(), env, stdio: ['ignore', 'pipe', 'ignore'], encoding: 'utf-8', timeout: 30_000,
|
||||
});
|
||||
expect(out.length).toBeGreaterThan(0); // the bug was (no output)
|
||||
expect(out).toContain('gbrain jobs watch'); // human renderer header
|
||||
expect(out).toContain('Queue'); // human panel
|
||||
expect(out.trimStart().startsWith('{')).toBe(false); // NOT JSON by default
|
||||
}, 35_000);
|
||||
|
||||
test('non-TTY, --json → ONE JSON snapshot, parses, exit 0', () => {
|
||||
const out = execFileSync('bun', ['run', 'src/cli.ts', 'jobs', 'watch', '--json'], {
|
||||
cwd: process.cwd(), env, stdio: ['ignore', 'pipe', 'ignore'], encoding: 'utf-8', timeout: 30_000,
|
||||
});
|
||||
const firstLine = out.split('\n').find(l => l.trim().length > 0) ?? '';
|
||||
const parsed = JSON.parse(firstLine);
|
||||
expect(parsed.event).toBe('jobs.watch.snapshot');
|
||||
expect(parsed.queue_health).toBeDefined();
|
||||
}, 35_000);
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* v0.42.15.0 (#1784) — jobs-watch format × loop matrix.
|
||||
*
|
||||
* Pins resolveWatchMode so the TTY-gating contract can't regress. The bug this
|
||||
* guards: TTY + --json used to loop forever (follow defaulted to isTTY); the
|
||||
* documented matrix says --json one-shots unless --follow. Caught by codex in
|
||||
* the pre-landing review; the e2e missed it (non-TTY only).
|
||||
*/
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import { resolveWatchMode } from '../src/commands/jobs-watch.ts';
|
||||
|
||||
describe('resolveWatchMode — format × loop matrix', () => {
|
||||
test('TTY, no flags → live dashboard (json off, follow on, ansi on)', () => {
|
||||
expect(resolveWatchMode({}, true)).toEqual({ json: false, follow: true, useAnsiDashboard: true });
|
||||
});
|
||||
|
||||
test('non-TTY, no flags → one human snapshot (the bug fix: not JSON, one-shot)', () => {
|
||||
expect(resolveWatchMode({}, false)).toEqual({ json: false, follow: false, useAnsiDashboard: false });
|
||||
});
|
||||
|
||||
test('TTY + --json → ONE JSON snapshot, NOT a forever loop (codex finding)', () => {
|
||||
expect(resolveWatchMode({ json: true }, true)).toEqual({ json: true, follow: false, useAnsiDashboard: false });
|
||||
});
|
||||
|
||||
test('non-TTY + --json → one JSON snapshot', () => {
|
||||
expect(resolveWatchMode({ json: true }, false)).toEqual({ json: true, follow: false, useAnsiDashboard: false });
|
||||
});
|
||||
|
||||
test('TTY + --follow → live dashboard loops', () => {
|
||||
expect(resolveWatchMode({ follow: true }, true)).toEqual({ json: false, follow: true, useAnsiDashboard: true });
|
||||
});
|
||||
|
||||
test('non-TTY + --follow → plain human stream (loops, no ansi)', () => {
|
||||
expect(resolveWatchMode({ follow: true }, false)).toEqual({ json: false, follow: true, useAnsiDashboard: false });
|
||||
});
|
||||
|
||||
test('--json --follow → JSONL stream, never the ansi dashboard', () => {
|
||||
expect(resolveWatchMode({ json: true, follow: true }, true)).toEqual({ json: true, follow: true, useAnsiDashboard: false });
|
||||
});
|
||||
|
||||
test('explicit --follow false on a TTY → one-shot (override wins)', () => {
|
||||
expect(resolveWatchMode({ follow: false }, true)).toEqual({ json: false, follow: false, useAnsiDashboard: false });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user