import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { buildQualityPayload } from './callQuality';

describe('buildQualityPayload', () => {
  it("sends null for every 'auto' field so a prior cap is reset", () => {
    const payload = buildQualityPayload({
      callAudioBitrate: 'auto',
      screenshareBitrate: 'auto',
      screenshareFramerate: 'auto',
    });
    assert.deepEqual(payload, {
      audioMaxBitrate: null,
      screenshareMaxBitrate: null,
      screenshareMaxFramerate: null,
    });
  });

  it('converts kbps user settings to bits/sec and passes fps through', () => {
    const payload = buildQualityPayload({
      callAudioBitrate: '64',
      screenshareBitrate: '1500',
      screenshareFramerate: '30',
    });
    assert.equal(payload.audioMaxBitrate, 64_000);
    assert.equal(payload.screenshareMaxBitrate, 1_500_000);
    assert.equal(payload.screenshareMaxFramerate, 30);
  });

  it('clamps the user setting down to the room cap (lower wins)', () => {
    const payload = buildQualityPayload(
      { callAudioBitrate: '256', screenshareBitrate: '8000', screenshareFramerate: '60' },
      { audio_max_kbps: 64, screenshare_max_kbps: 1500, screenshare_max_fps: 30 },
    );
    assert.equal(payload.audioMaxBitrate, 64_000);
    assert.equal(payload.screenshareMaxBitrate, 1_500_000);
    assert.equal(payload.screenshareMaxFramerate, 30);
  });

  it('does not raise a user setting that is already below the room cap', () => {
    const payload = buildQualityPayload(
      { callAudioBitrate: '32', screenshareBitrate: 'auto', screenshareFramerate: '15' },
      { audio_max_kbps: 128, screenshare_max_kbps: 3000, screenshare_max_fps: 60 },
    );
    assert.equal(payload.audioMaxBitrate, 32_000);
    // user 'auto' but room caps screenshare bitrate -> room cap applies
    assert.equal(payload.screenshareMaxBitrate, 3_000_000);
    assert.equal(payload.screenshareMaxFramerate, 15);
  });

  it('applies a room cap even when the user left the field on auto', () => {
    const payload = buildQualityPayload(
      { callAudioBitrate: 'auto', screenshareBitrate: 'auto', screenshareFramerate: 'auto' },
      { audio_max_kbps: 96 },
    );
    assert.equal(payload.audioMaxBitrate, 96_000);
    assert.equal(payload.screenshareMaxBitrate, null);
    assert.equal(payload.screenshareMaxFramerate, null);
  });
});
