import { describe, it, expect } from 'vitest';
import { formatDiagnosticsBlock } from '../feedback.js';
import type { Diagnostic } from '../types.js';

function diag(severity: number, line: number, message: string): Diagnostic {
  return {
    range: { start: { line, character: 0 }, end: { line, character: 1 } },
    severity,
    message,
  };
}

describe('formatDiagnosticsBlock', () => {
  it('returns a clean one-liner for zero diagnostics', () => {
    expect(formatDiagnosticsBlock([])).toBe('LSP: no diagnostics.');
  });

  it('counts errors and warnings and 1-indexes positions', () => {
    const out = formatDiagnosticsBlock([
      diag(1, 0, "Cannot find name 'foo'."),
      diag(2, 4, 'Unused variable.'),
    ]);
    expect(out).toContain('1 error(s), 1 warning(s)');
    // line 0/char 0 surfaces as 1:1
    expect(out).toContain("[error] 1:1 Cannot find name 'foo'.");
    expect(out).toContain('[warning] 5:1 Unused variable.');
  });

  it('caps the list and reports the remainder', () => {
    const many = Array.from({ length: 25 }, (_, i) => diag(1, i, `err ${i}`));
    const out = formatDiagnosticsBlock(many);
    expect(out).toContain('...and 5 more');
    // 20 shown + header + remainder line
    expect(out.split('\n')).toHaveLength(22);
  });
});
