import { describe, expect, test } from 'bun:test';

import { SfuSessionRegistry } from '../media/sfu/registry';

const buildSession = (streamSessionId: string) => ({
  streamSessionId,
  ownerUserId: 'user-1',
  cameraDeviceId: 'camera-1',
  requesterDeviceId: 'client-1',
  state: 'starting' as const,
  createdAt: '2026-04-16T12:00:00.000Z',
});

describe('SFU session registry', () => {
  test('stores and retrieves a session descriptor without internal metadata', () => {
    const registry = new SfuSessionRegistry();
    registry.set(buildSession('stream-1'));

    expect(registry.get('stream-1')).toEqual(buildSession('stream-1'));
  });

  test('returns null when updating a missing session state', () => {
    const registry = new SfuSessionRegistry();

    expect(registry.updateState('missing-stream', 'live')).toBeNull();
  });

  test('updates state and lists all registered sessions', () => {
    const registry = new SfuSessionRegistry();
    registry.set(buildSession('stream-1'));
    registry.set({ ...buildSession('stream-2'), cameraDeviceId: 'camera-2' });

    expect(registry.updateState('stream-1', 'live')).toEqual({
      ...buildSession('stream-1'),
      state: 'live',
    });

    expect(registry.list()).toEqual([
      {
        ...buildSession('stream-1'),
        state: 'live',
      },
      {
        ...buildSession('stream-2'),
        cameraDeviceId: 'camera-2',
      },
    ]);
  });
});
