import type { Room } from '../../../../shared-kernel';
import { SYSTEM_ROLE_IDS } from '../constants/access-control.constants';
import { normalizeRoomAccessControl } from './room.rules';

function buildRoom(overrides: Partial<Room> = {}): Room {
  return {
    id: 'room-1',
    name: 'Room',
    hostId: 'host-1',
    isPrivate: false,
    createdAt: 1,
    userCount: 1,
    members: [
      {
        id: 'user-1',
        oderId: 'oder-1',
        username: 'alice',
        displayName: 'Alice',
        role: 'admin',
        joinedAt: 1,
        lastSeenAt: 1
      }
    ],
    ...overrides
  };
}

describe('normalizeRoomRoleAssignments', () => {
  it('uses legacy member roles when assignments are missing', () => {
    const room = normalizeRoomAccessControl(buildRoom());

    expect(room.roleAssignments).toEqual([
      {
        userId: 'user-1',
        oderId: 'oder-1',
        roleIds: [SYSTEM_ROLE_IDS.admin]
      }
    ]);

    expect(room.members?.[0]?.role).toBe('admin');
  });

  it('honors an explicit empty assignment list', () => {
    const room = normalizeRoomAccessControl(buildRoom({ roleAssignments: [] }));

    expect(room.roleAssignments).toEqual([]);
    expect(room.members?.[0]?.role).toBe('member');
  });
});
