text: import {assert} from '@webex/test-helper-chai';
  import sinon from '@webex/test-helper-sinon';
  import MockSpark from '@webex/test-helper-mock-webex';
  import Phone from '@webex/plugin-phone';
  import AmpState from 'ampersand-state';
  import Locus from '@webex/internal-plugin-locus';
  import Mercury from '@webex/internal-plugin-mercury';
  import Device from '@webex/internal-plugin-wdm';
  
  import {makeLocus} from '../../lib/locus';
  import {joinedOnThisDevice, participantsToCallMemberships} from '../../../src/state-parsers';
  
  describe('plugin-phone', () => {
    describe('state-parsers', () => {
      let spark;
  
      beforeEach(() => {
        spark = new MockSpark({
          children: {
            device: Device,
            locus: Locus,
            mercury: Mercury,
            phone: Phone,
            people: AmpState.extend({
              inferPersonIdFromUuid: (uuid) => uuid
            })
          }
        });
  
        sinon.stub(spark.internal.mercury, 'connect').returns(Promise.resolve());
  
        spark.internal.device.url = 'https://example.com/devices/1';
        spark.config.phone = {
          audioBandwidthLimit: 64000,
          videoBandwidthLimit: 1000000
        };
      });
  
      describe('#joinedOnThisDevice', () => {
        it('returns true when the local device is in state JOINED', () => {
          const locus = makeLocus({});
  
          locus.self.state = 'JOINED';
          locus.self.devices[0].state = 'JOINED';
          assert.isTrue(joinedOnThisDevice(spark, locus));
        });
  
        it('returns false when the local device is not in state JOINED', () => {
          const locus = makeLocus({});
  
          locus.self.state = 'JOINED';
          // Set current device to LEFT state
          locus.self.devices[0].state = 'LEFT';
          assert.isFalse(joinedOnThisDevice(spark, locus));
        });
  
        it('returns false when the local device is not in state JOINED and other devices are JOINED', () => {
          const locus = makeLocus({});
  
          locus.self.state = 'JOINED';
          // Create second device
          locus.self.devices.push(locus.self.devices[0]);
          locus.self.devices[1].state = 'JOINED';
          locus.self.devices[1].url = 'https://example.com/devices/2';
          // Set current device to LEFT state
          locus.self.devices[0].state = 'LEFT';
          assert.isFalse(joinedOnThisDevice(spark, locus));
        });
      });
  
      describe('#participantsToCallMemberships', () => {
        it('returns an array of membership objects', () => {
          const locus = makeLocus({});
          const memberships = participantsToCallMemberships(spark, locus);
  
          assert.isTrue(Array.isArray(memberships));
          assert.equal(memberships.length, 2);
        });
  
        it('associates devices to user memberships', () => {
          const locus = makeLocus({});
  
          // Add a device to locus participant object
          locus.participants[0].devices = [{
            id: 'a',
            deviceType: 'IPHONE',
            url: 'https://example.com/locus/devices/0/participant/1',
            state: 'IDLE'
          }];
          const memberships = participantsToCallMemberships(spark, locus);
  
          assert.isTrue(Array.isArray(memberships));
          assert.equal(memberships.length, 2);
        });
  
        it('creates memberships from unassigned devices', () => {
          const locus = makeLocus({});
  
          // Add a device to locus participants
          locus.participants.push({
            id: 'a',
            person: {
              id: '88888888-4444-4444-4444-AAAAAAAAAAA1-ROOM'
            },
            type: 'RESOURCE_ROOM',
            url: 'https://example.com/locus/devices/0/participant/1',
            state: 'IDLE'
          });
          const memberships = participantsToCallMemberships(spark, locus);
  
          assert.isTrue(Array.isArray(memberships));
          assert.equal(memberships.length, 3);
        });
      });
    });
  });
  