Files
jackwener__opencli/src/output.test.ts
jackwener f2b658feac refactor: remove obsolete config parameters
Remove OUTPUT, DEBUG=opencli, OPENCLI_DAEMON_TIMEOUT,
OPENCLI_HEADLESS, and OPENCLI_BROWSER_EXECUTABLE_PATH environment
variables. These are either superseded by OPENCLI_VERBOSE, never
consumed in runtime code, or unnecessary with current defaults.
2026-04-13 23:22:43 +08:00

49 lines
2.1 KiB
TypeScript

import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
import { render } from './output.js';
describe('output TTY detection', () => {
const originalIsTTY = process.stdout.isTTY;
let logSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
});
afterEach(() => {
Object.defineProperty(process.stdout, 'isTTY', { value: originalIsTTY, writable: true });
logSpy.mockRestore();
});
it('outputs YAML in non-TTY when format is default table', () => {
Object.defineProperty(process.stdout, 'isTTY', { value: false, writable: true });
// commanderAdapter always passes fmt:'table' as default — this must still trigger downgrade
render([{ name: 'alice', score: 10 }], { fmt: 'table', columns: ['name', 'score'] });
const out = logSpy.mock.calls.map((c: unknown[]) => c[0]).join('\n');
expect(out).toContain('name: alice');
expect(out).toContain('score: 10');
});
it('outputs table in TTY when format is default table', () => {
Object.defineProperty(process.stdout, 'isTTY', { value: true, writable: true });
render([{ name: 'alice', score: 10 }], { fmt: 'table', columns: ['name', 'score'] });
const out = logSpy.mock.calls.map((c: unknown[]) => c[0]).join('\n');
expect(out).toContain('alice');
});
it('respects explicit -f json even in non-TTY', () => {
Object.defineProperty(process.stdout, 'isTTY', { value: false, writable: true });
render([{ name: 'alice' }], { fmt: 'json' });
const out = logSpy.mock.calls.map((c: unknown[]) => c[0]).join('\n');
expect(JSON.parse(out)).toEqual([{ name: 'alice' }]);
});
it('explicit -f table overrides non-TTY auto-downgrade', () => {
Object.defineProperty(process.stdout, 'isTTY', { value: false, writable: true });
render([{ name: 'alice' }], { fmt: 'table', fmtExplicit: true, columns: ['name'] });
const out = logSpy.mock.calls.map((c: unknown[]) => c[0]).join('\n');
// Should be table output, not YAML
expect(out).not.toContain('name: alice');
expect(out).toContain('alice');
});
});