import { RoomResolver, type RoomInfo } from '../../../../../src/modules/netaclaw/runtime/weixin_db/room_resolver.js';

function mk(over: Partial<RoomInfo>): RoomInfo {
  return {
    tableName: 'Msg_x',
    username: 'u',
    roomName: 'R',
    isGroup: true,
    ...over,
  };
}

describe('RoomResolver.findRoomsByName', () => {
  it('returns all rooms with matching roomName (groups only)', () => {
    const r = new RoomResolver();
    r._setMap(new Map([
      ['t1', mk({ tableName: 't1', username: 'g1@chatroom', roomName: '产品研发群', isGroup: true })],
      ['t2', mk({ tableName: 't2', username: 'g2@chatroom', roomName: '产品研发群', isGroup: true })],
      ['t3', mk({ tableName: 't3', username: 'wxid_x',     roomName: '产品研发群', isGroup: false })],
      ['t4', mk({ tableName: 't4', username: 'g3@chatroom', roomName: '家庭群', isGroup: true })],
    ]));
    const hits = r.findRoomsByName('产品研发群');
    expect(hits).toHaveLength(2);
    expect(hits.map(h => h.username).sort()).toEqual(['g1@chatroom', 'g2@chatroom']);
  });

  it('returns empty when no match', () => {
    const r = new RoomResolver();
    expect(r.findRoomsByName('不存在的群')).toEqual([]);
  });

  it('returns empty for empty/whitespace input', () => {
    const r = new RoomResolver();
    expect(r.findRoomsByName('')).toEqual([]);
    expect(r.findRoomsByName('   ')).toEqual([]);
  });

  it('trims both sides before comparison', () => {
    const r = new RoomResolver();
    r._setMap(new Map([
      ['t1', mk({ tableName: 't1', username: 'g1@chatroom', roomName: '产品研发群 ', isGroup: true })],
    ]));
    expect(r.findRoomsByName('  产品研发群  ')).toHaveLength(1);
  });
});
