import { describe, it, expect } from 'vitest';
import { formatJson, formatYaml } from '../../src/formatters/output.js';

describe('formatJson', () => {
  it('formats object as indented JSON', () => {
    const result = formatJson({ key: 'value', num: 42 });
    expect(JSON.parse(result)).toEqual({ key: 'value', num: 42 });
    expect(result).toContain('\n'); // indented
  });

  it('formats arrays', () => {
    const result = formatJson([1, 2, 3]);
    expect(JSON.parse(result)).toEqual([1, 2, 3]);
  });

  it('handles null and undefined values', () => {
    const result = formatJson({ a: null, b: undefined });
    const parsed = JSON.parse(result) as Record<string, unknown>;
    expect(parsed['a']).toBeNull();
    expect('b' in parsed).toBe(false); // undefined stripped by JSON
  });
});

describe('formatYaml', () => {
  it('formats object as YAML', () => {
    const result = formatYaml({ key: 'value', num: 42 });
    expect(result).toContain('key: value');
    expect(result).toContain('num: 42');
  });

  it('formats arrays', () => {
    const result = formatYaml(['a', 'b']);
    expect(result).toContain('- a');
    expect(result).toContain('- b');
  });

  it('does not end with trailing newline', () => {
    const result = formatYaml({ x: 1 });
    expect(result.endsWith('\n')).toBe(false);
  });
});
