'use strict';

const { mergeConfig } = require('../src/configService');

function base() {
  return {
    cameras: [
      { id: 'cam0', device: '/dev/video0', name: 'Kamera 0', position: 'front', stream: true, hires: true, note: 'x', liveSize: '640x480', hiresSize: '1280x960' },
      { id: 'cam1', device: '/dev/video2', name: 'Kamera 1', position: 'left', stream: true, hires: true, note: 'y', hiresSize: '1280x960' },
    ],
  };
}

describe('mergeConfig', () => {
  test('patcht liveSize, erhält ALLE übrigen Felder', () => {
    const out = mergeConfig(base(), [{ id: 'cam0', liveSize: '320x240', stream: true }]);
    const c0 = out.cameras.find((c) => c.id === 'cam0');
    expect(c0.liveSize).toBe('320x240');
    expect(c0.device).toBe('/dev/video0');
    expect(c0.name).toBe('Kamera 0');
    expect(c0.position).toBe('front');
    expect(c0.hiresSize).toBe('1280x960');
    expect(c0.note).toBe('x');
  });

  test('lässt nicht genannte Kameras unverändert', () => {
    const b = base();
    const out = mergeConfig(b, [{ id: 'cam0', liveSize: '320x240', stream: true }]);
    expect(out.cameras.find((c) => c.id === 'cam1')).toEqual(b.cameras[1]);
  });

  test('setzt stream:false', () => {
    const out = mergeConfig(base(), [{ id: 'cam1', stream: false }]);
    expect(out.cameras.find((c) => c.id === 'cam1').stream).toBe(false);
  });

  test('mutiert das Original nicht', () => {
    const b = base();
    const snapshot = JSON.stringify(b);
    mergeConfig(b, [{ id: 'cam0', liveSize: '160x120', stream: true }]);
    expect(JSON.stringify(b)).toBe(snapshot);
  });

  test('erhält Top-Level-Felder neben "cameras"', () => {
    const b = { version: 2, cameras: base().cameras };
    const out = mergeConfig(b, [{ id: 'cam0', liveSize: '800x600', stream: true }]);
    expect(out.version).toBe(2);
  });
});
