'use strict';

const { resolveHwenc, mseCodecString, h264LiveArgs } = require('../src/hwencode');

describe('resolveHwenc', () => {
  test('intel → vaapi am Default-Renderknoten', () => {
    expect(resolveHwenc({ vendor: 'intel' })).toMatchObject({ encoder: 'vaapi', device: '/dev/dri/renderD128' });
  });
  test('amd → vaapi (gleicher Codepfad)', () => {
    expect(resolveHwenc({ vendor: 'amd' }).encoder).toBe('vaapi');
  });
  test('none → libx264 (Software-Fallback)', () => {
    expect(resolveHwenc({ vendor: 'none' }).encoder).toBe('libx264');
  });
  test('HWENC überschreibt Vendor', () => {
    expect(resolveHwenc({ vendor: 'amd', encoder: 'qsv' }).encoder).toBe('qsv');
  });
  test('eigener Device-Pfad', () => {
    expect(resolveHwenc({ vendor: 'intel', device: '/dev/dri/renderD129' }).device).toBe('/dev/dri/renderD129');
  });
  test('unbekannter Encoder wirft', () => {
    expect(() => resolveHwenc({ encoder: 'nvenc' })).toThrow();
  });
});

describe('mseCodecString', () => {
  test('main/3.1', () => expect(mseCodecString('main', '1F')).toBe('avc1.4D401F'));
  test('high', () => expect(mseCodecString('high', '1F')).toBe('avc1.64001F'));
  test('constrained_baseline', () => expect(mseCodecString('constrained_baseline', '1F')).toBe('avc1.42E01F'));
  test('unbekannt → main', () => expect(mseCodecString('xyz', '1F')).toBe('avc1.4D401F'));
});

describe('h264LiveArgs', () => {
  const base = { device: '/dev/video0', size: '640x480', fps: 30 };

  test('VAAPI: hwupload + h264_vaapi + fragmentiertes mp4 auf pipe:1', () => {
    const { args, useFd3 } = h264LiveArgs({ ...base, hwenc: resolveHwenc({ vendor: 'intel' }), jpegSnapshots: false });
    const s = args.join(' ');
    expect(s).toContain('-vaapi_device /dev/dri/renderD128');
    expect(s).toContain('format=nv12,hwupload');
    expect(s).toContain('-c:v h264_vaapi');
    expect(s).toContain('-movflags +frag_keyframe+empty_moov+default_base_moof');
    expect(args[args.length - 1]).toBe('pipe:1');
    expect(useFd3).toBe(false);
  });

  test('mit Snapshot-Nebenausgang: split + pipe:3 + useFd3', () => {
    const { args, useFd3 } = h264LiveArgs({ ...base, hwenc: resolveHwenc({ vendor: 'amd' }), jpegSnapshots: true });
    const s = args.join(' ');
    expect(s).toContain('-filter_complex');
    expect(s).toContain('split=2');
    expect(s).toContain('-c:v mjpeg');
    expect(args[args.length - 1]).toBe('pipe:3');
    expect(useFd3).toBe(true);
  });

  test('libx264-Software-Fallback', () => {
    const { args } = h264LiveArgs({ ...base, hwenc: resolveHwenc({ vendor: 'none' }), jpegSnapshots: false });
    const s = args.join(' ');
    expect(s).toContain('-c:v libx264');
    expect(s).toContain('-tune zerolatency');
    expect(s).not.toContain('hwupload');
  });

  test('GOP default ~2×fps, überschreibbar', () => {
    const def = h264LiveArgs({ ...base, hwenc: resolveHwenc({}), jpegSnapshots: false });
    expect(def.args[def.args.indexOf('-g') + 1]).toBe('60');
    const custom = h264LiveArgs({ ...base, gop: 15, hwenc: resolveHwenc({}), jpegSnapshots: false });
    expect(custom.args[custom.args.indexOf('-g') + 1]).toBe('15');
  });
});
