import {
  areRoomSignalSourcesEqual,
  buildRoomSignalSelector,
  buildRoomSignalSource,
  getSourceUrlFromSignalingUrl
} from './room-signal-source.logic';

describe('room-signal-source helpers', () => {
  it('converts signaling urls back to normalized source urls', () => {
    expect(getSourceUrlFromSignalingUrl('wss://signal.toju.app')).toBe('https://signal.toju.app');
    expect(getSourceUrlFromSignalingUrl('ws://46.59.68.77:3001')).toBe('http://46.59.68.77:3001');
  });

  it('prefers the resolved endpoint when normalizing a room source', () => {
    expect(buildRoomSignalSource({
      sourceId: 'stale-id',
      sourceName: 'Stale Source',
      sourceUrl: 'https://old.example.com',
      signalingUrl: 'wss://signal.toju.app'
    }, {
      id: 'primary-id',
      name: 'Primary Signal',
      url: 'https://signal.toju.app/'
    })).toEqual({
      sourceId: 'primary-id',
      sourceName: 'Primary Signal',
      sourceUrl: 'https://signal.toju.app'
    });
  });

  it('builds selectors from signaling urls when no source url is persisted yet', () => {
    expect(buildRoomSignalSelector({
      signalingUrl: 'wss://signal-sweden.toju.app',
      fallbackName: 'Toju Signal Sweden'
    })).toEqual({
      sourceUrl: 'https://signal-sweden.toju.app'
    });
  });

  it('treats equivalent persisted and signaling-derived sources as equal', () => {
    expect(areRoomSignalSourcesEqual(
      { sourceUrl: 'https://signal.toju.app/' },
      { signalingUrl: 'wss://signal.toju.app' }
    )).toBe(true);
  });
});
