import { describe, expect, it } from 'vitest';
import {
  BUG_REPORT_MAX_BYTES,
  validateBugReportBody,
} from '../src/tier1/bugReport';

const valid = {
  prompt: 'build a todo app',
  events: [{ ts: '2026-07-26T00:00:00.000Z', phase: 'encode', featureId: null, message: 'start' }],
  problems: ['F1: 1/3 passing'],
  metrics: { llmCalls: 3, tokensIn: 100, tokensOut: 50 },
  versions: { vault: '0.1.12', corpus: 'V1.11', app: 'V1.11' },
};

describe('validateBugReportBody', () => {
  it('accepts a well-formed payload', () => {
    const r = validateBugReportBody(valid);
    expect(r.ok).toBe(true);
    if (r.ok) {
      expect(r.body.prompt).toBe(valid.prompt);
      expect(r.body.events).toHaveLength(1);
      expect(r.body.versions.app).toBe('V1.11');
    }
  });

  it('accepts optional contact', () => {
    const r = validateBugReportBody({ ...valid, contact: 'a@b.c' });
    expect(r.ok).toBe(true);
    if (r.ok) expect(r.body.contact).toBe('a@b.c');
  });

  it('rejects non-object', () => {
    expect(validateBugReportBody(null).ok).toBe(false);
    expect(validateBugReportBody('x').ok).toBe(false);
  });

  it('rejects missing / wrong-typed fields', () => {
    expect(validateBugReportBody({ ...valid, prompt: 1 }).ok).toBe(false);
    expect(validateBugReportBody({ ...valid, events: {} }).ok).toBe(false);
    expect(validateBugReportBody({ ...valid, problems: 'x' }).ok).toBe(false);
    const { metrics: _m, ...noMetrics } = valid;
    expect(validateBugReportBody(noMetrics).ok).toBe(false);
    expect(validateBugReportBody({ ...valid, versions: { vault: 'a' } }).ok).toBe(false);
    expect(validateBugReportBody({ ...valid, contact: 9 }).ok).toBe(false);
  });

  it('exports the 2 MB cap constant', () => {
    expect(BUG_REPORT_MAX_BYTES).toBe(2 * 1024 * 1024);
  });
});
