ktx/packages/cli/test/io/mode.test.ts

61 lines
2.6 KiB
TypeScript
Raw Permalink Normal View History

2026-05-10 23:12:26 +02:00
import { describe, expect, it } from 'vitest';
2026-05-25 13:17:46 +02:00
import type { KtxCliIo } from '../../src/cli-runtime.js';
import { resolveOutputMode } from '../../src/io/mode.js';
2026-05-10 23:12:26 +02:00
2026-05-10 23:51:24 +02:00
function ioWith(isTTY: boolean | undefined): KtxCliIo {
2026-05-10 23:12:26 +02:00
return {
stdout: { isTTY, write: () => {} },
stderr: { write: () => {} },
};
}
describe('resolveOutputMode', () => {
it('uses explicit value when provided', () => {
expect(resolveOutputMode({ explicit: 'pretty', io: ioWith(false), env: {} })).toBe('pretty');
expect(resolveOutputMode({ explicit: 'plain', io: ioWith(true), env: {} })).toBe('plain');
expect(resolveOutputMode({ explicit: 'json', io: ioWith(true), env: {} })).toBe('json');
});
it('json:true takes precedence over explicit value', () => {
expect(resolveOutputMode({ explicit: 'pretty', json: true, io: ioWith(true), env: {} })).toBe('json');
});
it('throws on unknown explicit value', () => {
expect(() => resolveOutputMode({ explicit: 'fancy', io: ioWith(true), env: {} })).toThrow(/Invalid --output/);
});
2026-05-10 23:51:24 +02:00
it('honors KTX_OUTPUT env var when no explicit value', () => {
expect(resolveOutputMode({ io: ioWith(true), env: { KTX_OUTPUT: 'plain' } })).toBe('plain');
expect(resolveOutputMode({ io: ioWith(false), env: { KTX_OUTPUT: 'pretty' } })).toBe('pretty');
expect(resolveOutputMode({ io: ioWith(false), env: { KTX_OUTPUT: 'json' } })).toBe('json');
2026-05-10 23:12:26 +02:00
});
2026-05-10 23:51:24 +02:00
it('throws on unknown KTX_OUTPUT', () => {
expect(() => resolveOutputMode({ io: ioWith(true), env: { KTX_OUTPUT: 'fancy' } })).toThrow(/Invalid KTX_OUTPUT/);
2026-05-10 23:12:26 +02:00
});
it('returns plain when CI is set to a truthy value', () => {
expect(resolveOutputMode({ io: ioWith(true), env: { CI: 'true' } })).toBe('plain');
expect(resolveOutputMode({ io: ioWith(true), env: { CI: '1' } })).toBe('plain');
});
it('ignores CI when set to a falsy value', () => {
expect(resolveOutputMode({ io: ioWith(true), env: { CI: '' } })).toBe('pretty');
expect(resolveOutputMode({ io: ioWith(true), env: { CI: '0' } })).toBe('pretty');
expect(resolveOutputMode({ io: ioWith(true), env: { CI: 'false' } })).toBe('pretty');
});
it('returns pretty when stdout is a TTY and CI is not set', () => {
expect(resolveOutputMode({ io: ioWith(true), env: {} })).toBe('pretty');
});
it('returns plain when stdout is not a TTY', () => {
expect(resolveOutputMode({ io: ioWith(false), env: {} })).toBe('plain');
expect(resolveOutputMode({ io: ioWith(undefined), env: {} })).toBe('plain');
});
2026-05-10 23:51:24 +02:00
it('explicit value beats KTX_OUTPUT env var', () => {
expect(resolveOutputMode({ explicit: 'json', io: ioWith(true), env: { KTX_OUTPUT: 'plain' } })).toBe('json');
2026-05-10 23:12:26 +02:00
});
});