import { describe, it, expect } from 'vitest';
import { parseLighthouseOutput } from '../lighthouse.js';
import { assertAllowedBinary } from '../../../lib/sandbox.js';

describe('lighthouse_audit helpers', () => {
  it('allows lighthouse and npx binaries', () => {
    expect(() => assertAllowedBinary('lighthouse')).not.toThrow();
    expect(() => assertAllowedBinary('npx')).not.toThrow();
  });

  it('parses category scores and opportunities from Lighthouse JSON', () => {
    const raw = JSON.stringify({
      categories: {
        performance: {
          title: 'Performance',
          description: 'How fast',
          score: 0.82,
          auditRefs: [{ id: 'unused-javascript', weight: 10 }],
        },
        accessibility: { title: 'Accessibility', score: 0.91 },
      },
      audits: {
        'unused-javascript': {
          title: 'Reduce unused JavaScript',
          score: 0.3,
          displayValue: 'Est savings of 200 KiB',
        },
        'good-audit': {
          title: 'Already good',
          score: 1,
        },
      },
    });

    const parsed = parseLighthouseOutput(`noise\n${raw}\nmore noise`);
    expect(parsed.scores.performance).toBe(82);
    expect(parsed.scores.accessibility).toBe(91);
    expect(parsed.opportunities[0]?.title).toBe('Reduce unused JavaScript');
    expect(parsed.categories.find((c) => c.id === 'performance')?.title).toBe('Performance');
    expect(parsed.failingByCategory.performance?.[0]?.id).toBe('unused-javascript');
  });
});
