import { reconcileRoomSnapshotChannels, sanitizeRoomSnapshot } from './rooms.helpers';

describe('room snapshot helpers', () => {
  it('drops empty channel arrays from outgoing snapshots', () => {
    expect(sanitizeRoomSnapshot({ channels: [] }).channels).toBeUndefined();
  });

  it('keeps cached channels when incoming snapshot has none', () => {
    const generalChannel = { id: 'general', name: 'general', type: 'text', position: 0 } as const;
    const updatesChannel = { id: 'updates', name: 'updates', type: 'text', position: 1 } as const;
    const cachedChannels = [generalChannel, updatesChannel] as const;

    expect(reconcileRoomSnapshotChannels(cachedChannels as never, undefined)).toEqual(cachedChannels);
    expect(reconcileRoomSnapshotChannels(cachedChannels as never, [] as never)).toEqual(cachedChannels);
  });

  it('keeps richer cached channels when incoming snapshot is smaller', () => {
    const cachedChannels = [
      { id: 'general', name: 'general', type: 'text', position: 0 },
      { id: 'updates', name: 'updates', type: 'text', position: 1 },
      { id: 'voice', name: 'General', type: 'voice', position: 0 }
    ] as const;
    const incomingChannels = [{ id: 'general', name: 'general', type: 'text', position: 0 }] as const;

    expect(reconcileRoomSnapshotChannels(cachedChannels as never, incomingChannels as never)).toEqual(cachedChannels);
  });

  it('accepts incoming channels when snapshot is at least as complete', () => {
    const generalChannel = { id: 'general', name: 'general', type: 'text', position: 0 } as const;
    const updatesChannel = { id: 'updates', name: 'updates', type: 'text', position: 1 } as const;
    const cachedChannels = [generalChannel] as const;
    const incomingChannels = [generalChannel, updatesChannel] as const;

    expect(reconcileRoomSnapshotChannels(cachedChannels as never, incomingChannels as never)).toEqual(incomingChannels);
  });
});
