import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
describe('CLI Formatters', () => {
describe('formatOutput', () => {
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
let processExitSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
processExitSpy = vi.spyOn(process, 'exit').mockImplementation(() => undefined as never);
});
afterEach(() => {
consoleLogSpy.mockRestore();
processExitSpy.mockRestore();
});
// Since formatOutput calls process.exit, we'll test the logic separately
it('should format result as JSON', () => {
const result = {
success: true,
message: 'Test message',
data: [{ id: 1 }],
};
const formatted = JSON.stringify(result, null, 2);
expect(formatted).toContain('"success": true');
expect(formatted).toContain('"message": "Test message"');
expect(formatted).toContain('"data"');
});
it('should format error result as JSON', () => {
const result = {
success: false,
message: 'Error occurred',
error: 'TEST_ERROR',
};
const formatted = JSON.stringify(result, null, 2);
expect(formatted).toContain('"success": false');
expect(formatted).toContain('"error": "TEST_ERROR"');
});
it('should handle nested objects', () => {
const result = {
success: true,
message: 'Success',
data: {
user: { id: 1, name: 'Test' },
orders: [{ id: 1 }, { id: 2 }],
},
};
const formatted = JSON.stringify(result, null, 2);
expect(formatted).toContain('"user"');
expect(formatted).toContain('"orders"');
});
it('should determine correct exit code for success', () => {
const result = { success: true, message: 'Success' };
const exitCode = result.success ? 0 : 1;
expect(exitCode).toBe(0);
});
it('should determine correct exit code for failure', () => {
const result = { success: false, message: 'Failure' };
const exitCode = result.success ? 0 : 1;
expect(exitCode).toBe(1);
});
});
});