// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { assert } from 'chai';

import { findBestMatchingAudioDeviceIndex } from '../../calling/findBestMatchingDevice';

describe('"find best matching device" helpers', () => {
  describe('findBestMatchingAudioDeviceIndex', () => {
    const itReturnsUndefinedIfNoDevicesAreAvailable = () => {
      it('returns undefined if no devices are available', () => {
        [
          undefined,
          { name: 'Big Microphone', index: 1, uniqueId: 'abc123' },
        ].forEach(preferred => {
          assert.isUndefined(
            findBestMatchingAudioDeviceIndex({
              available: [],
              preferred,
            })
          );
        });
      });
    };

    const itReturnsTheFirstAvailableDeviceIfNoneIsPreferred = () => {
      it('returns the first available device if none is preferred', () => {
        assert.strictEqual(
          findBestMatchingAudioDeviceIndex({
            available: [
              { name: 'A', index: 123, uniqueId: 'device-A' },
              { name: 'B', index: 456, uniqueId: 'device-B' },
              { name: 'C', index: 789, uniqueId: 'device-C' },
            ],
            preferred: undefined,
          }),
          0
        );
      });
    };

    const testUniqueIdMatch = () => {
      assert.strictEqual(
        findBestMatchingAudioDeviceIndex({
          available: [
            { name: 'A', index: 123, uniqueId: 'device-A' },
            { name: 'B', index: 456, uniqueId: 'device-B' },
            { name: 'C', index: 789, uniqueId: 'device-C' },
          ],
          preferred: { name: 'Ignored', index: 99, uniqueId: 'device-C' },
        }),
        2
      );
    };

    const testNameMatch = () => {
      assert.strictEqual(
        findBestMatchingAudioDeviceIndex({
          available: [
            { name: 'A', index: 123, uniqueId: 'device-A' },
            { name: 'B', index: 456, uniqueId: 'device-B' },
            { name: 'C', index: 789, uniqueId: 'device-C' },
          ],
          preferred: { name: 'C', index: 99, uniqueId: 'ignored' },
        }),
        2
      );
    };

    const itReturnsTheFirstAvailableDeviceIfThePreferredDeviceIsNotFound =
      () => {
        it('returns the first available device if the preferred device is not found', () => {
          assert.strictEqual(
            findBestMatchingAudioDeviceIndex({
              available: [
                { name: 'A', index: 123, uniqueId: 'device-A' },
                { name: 'B', index: 456, uniqueId: 'device-B' },
                { name: 'C', index: 789, uniqueId: 'device-C' },
              ],
              preferred: { name: 'X', index: 123, uniqueId: 'Y' },
            }),
            0
          );
        });
      };

    describe('find best matching device', () => {
      itReturnsUndefinedIfNoDevicesAreAvailable();

      itReturnsTheFirstAvailableDeviceIfNoneIsPreferred();

      [0, 1].forEach(index => {
        it(`returns ${index} if that was the previous preferred index (and a device is available)`, () => {
          assert.strictEqual(
            findBestMatchingAudioDeviceIndex({
              available: [
                { name: 'A', index: 123, uniqueId: 'device-A' },
                { name: 'B', index: 456, uniqueId: 'device-B' },
                { name: 'C', index: 789, uniqueId: 'device-C' },
              ],
              preferred: { name: 'C', index, uniqueId: 'device-C' },
            }),
            index
          );
        });
      });

      it("returns 0 if the previous preferred index was 1 but there's only 1 audio device", () => {
        assert.strictEqual(
          findBestMatchingAudioDeviceIndex({
            available: [{ name: 'A', index: 123, uniqueId: 'device-A' }],
            preferred: { name: 'C', index: 1, uniqueId: 'device-C' },
          }),
          0
        );
      });

      it('returns a unique ID match if it exists and the preferred index is not 0 or 1', () => {
        testUniqueIdMatch();
      });

      it('returns a name match if it exists and the preferred index is not 0 or 1', () => {
        testNameMatch();
      });

      itReturnsTheFirstAvailableDeviceIfThePreferredDeviceIsNotFound();
    });
  });
});
