import { UsersService } from './users.service';

describe('UsersService safe current-user teams', () => {
  it('returns only the player and team fields required by team bootstrap', async () => {
    const rows = [
      {
        player_id: 11,
        first_name: 'Pat',
        last_name: 'Player',
        team_id: 4,
        team_name: 'Alpha',
        user_id: 7,
        provider: 'google',
        social_id: 'must-not-leak',
        balance: '100.00',
      },
    ];
    const query = chain({ getRawMany: jest.fn(() => rows) });
    const playersRepository = {
      createQueryBuilder: jest.fn(() => query),
    };
    const service = new UsersService({} as any, playersRepository as any);

    const result = await service.findMyTeams(7);

    expect(result).toEqual([
      {
        id: 11,
        firstName: 'Pat',
        lastName: 'Player',
        team: { id: 4, name: 'Alpha' },
      },
    ]);
    expect(JSON.stringify(result)).not.toMatch(
      /user|provider|social|role|status|password|hash|balance|createdAt|updatedAt/i,
    );
    expect(query.where).toHaveBeenCalledWith('player.userId = :userId', {
      userId: 7,
    });
  });

  function chain(overrides: Record<string, jest.Mock>) {
    const query: Record<string, jest.Mock> = {};
    ['innerJoin', 'select', 'where', 'orderBy'].forEach((method) => {
      query[method] = jest.fn(() => query);
    });
    return Object.assign(query, overrides);
  }
});
