import { buildPseudoMessageFromDb } from '../../../../../src/modules/netaclaw/runtime/weixin_db/build_pseudo.js';

describe('buildPseudoMessageFromDb', () => {
  const room = { tableName: 'Msg_x', username: 'gA@chatroom', roomName: '产品研发群', isGroup: true };

  it('parses wxid_xxx:\\n prefix into sender + body', () => {
    const m = buildPseudoMessageFromDb({
      channelId: 7,
      room,
      row: {
        localId: 1, serverId: BigInt(999), localType: 1, realSenderId: BigInt(42), createTime: 1700000000,
        content: 'wxid_alice:\n@小神 hi', tableName: 'Msg_x',
      } as any,
    });
    expect(m.from_user_id).toBe('wxid_alice');
    expect(m.item_list[0].text_item.text).toBe('@小神 hi');
    expect(m.room_id).toBe('7:room:gA@chatroom');
    expect(m.room_name).toBe('产品研发群');
    expect(m.message_id).toBe('999');
    expect(m.__weixin_db).toBe(true);
  });

  it('includes contact identity for parsed sender wxid', () => {
    const m = buildPseudoMessageFromDb({
      channelId: 7,
      room,
      row: {
        localId: 1, serverId: BigInt(999), localType: 1, realSenderId: BigInt(42), createTime: 1700000000,
        content: 'wxid_alice:\nhello', tableName: 'Msg_x',
      } as any,
      resolveContact: username => {
        if (username !== 'wxid_alice') return undefined;
        return {
          username,
          remark: 'Alice备注',
          nickName: 'Alice昵称',
          displayName: 'Alice备注',
        };
      },
    });
    expect(m.from_user_id).toBe('wxid_alice');
    expect(m.sender_remark).toBe('Alice备注');
    expect(m.sender_nick_name).toBe('Alice昵称');
    expect(m.sender_display_name).toBe('Alice备注');
  });

  it('falls back to realSenderId when no prefix', () => {
    const m = buildPseudoMessageFromDb({
      channelId: 1,
      room,
      row: {
        localId: 1, serverId: null, localType: 1, realSenderId: BigInt(99), createTime: 1,
        content: '系统消息', tableName: 'Msg_x',
      } as any,
    });
    expect(m.from_user_id).toBe('99');
    expect(m.item_list[0].text_item.text).toBe('系统消息');
    expect(m.message_id.length).toBe(40); // sha1 hex
  });

  it('keeps numeric sender id but includes resolved identity for no-prefix self sender', () => {
    const m = buildPseudoMessageFromDb({
      channelId: 7,
      room,
      row: {
        localId: 2, serverId: BigInt(1000), localType: 1, realSenderId: BigInt(2), createTime: 1700000001,
        content: 'hello from owner', tableName: 'Msg_x',
      } as any,
      resolveRoomSenderIdentity: (roomUsername, realSenderId) => {
        if (roomUsername !== 'gA@chatroom' || realSenderId !== '2') return undefined;
        return {
          username: 'shin_seed',
          remark: null,
          nickName: 'if',
          displayName: 'if',
        };
      },
    });
    expect(m.from_user_id).toBe('2');
    expect(m.sender_wxid).toBe('shin_seed');
    expect(m.sender_nick_name).toBe('if');
    expect(m.sender_display_name).toBe('if');
  });

  it('falls back to "unknown" when neither prefix nor realSenderId', () => {
    const m = buildPseudoMessageFromDb({
      channelId: 1,
      room,
      row: {
        localId: 1, serverId: null, localType: 1, realSenderId: null, createTime: 1,
        content: 'foo', tableName: 'Msg_x',
      } as any,
    });
    expect(m.from_user_id).toBe('unknown');
  });

  it('uses localType in item_list type, defaults to 1', () => {
    const m1 = buildPseudoMessageFromDb({
      channelId: 1, room,
      row: { localId: 1, serverId: null, localType: 3, realSenderId: BigInt(1), createTime: 1, content: 'x', tableName: 'Msg_x' } as any,
    });
    expect(m1.item_list[0].type).toBe(3);
    const m2 = buildPseudoMessageFromDb({
      channelId: 1, room,
      row: { localId: 1, serverId: null, localType: undefined as any, realSenderId: BigInt(1), createTime: 1, content: 'x', tableName: 'Msg_x' } as any,
    });
    expect(m2.item_list[0].type).toBe(1);
  });
});
