import { describe, expect, it, vi } from 'vitest'; import { MatrixUsernameResolver } from '../src/services/chat/matrix/username-resolver.js'; describe('MatrixUsernameResolver', () => { it('resolves Reddit t2 IDs from authenticated Matrix profile display names', async () => { const getUserProfile = vi.fn(async (mxid: string) => { const profiles: Record = { '@t2_26q364ua7n:reddit.com': { displayname: 'MilkyBabe_10' }, '@t2_j8e83d38:reddit.com': { displayname: 'IssueLesss' }, }; return profiles[mxid] ?? {}; }); const resolver = new MatrixUsernameResolver({ getUserProfile }); await expect( resolver.resolveT2Ids(['t2_26q364ua7n', 'j8e83d38']), ).resolves.toEqual({ t2_26q364ua7n: 'MilkyBabe_10', t2_j8e83d38: 'IssueLesss', }); expect(getUserProfile).toHaveBeenCalledWith('@t2_26q364ua7n:reddit.com'); expect(getUserProfile).toHaveBeenCalledWith('@t2_j8e83d38:reddit.com'); }); it('ignores missing and t2-shaped display names without failing the batch', async () => { const getUserProfile = vi.fn(async (mxid: string) => { if (mxid === '@t2_missing:reddit.com') throw new Error('not found'); return { displayname: 't2_unresolved' }; }); const resolver = new MatrixUsernameResolver({ getUserProfile }); await expect( resolver.resolveT2Ids(['t2_missing', 't2_unresolved']), ).resolves.toEqual({}); }); it('uses replacement Matrix credentials for subsequent profile requests', async () => { const staleClient = { getUserProfile: vi.fn().mockRejectedValue(new Error('expired')) }; const freshClient = { getUserProfile: vi.fn().mockResolvedValue({ displayname: 'alice' }), }; const resolver = new MatrixUsernameResolver(staleClient); resolver.setMatrixClient(freshClient); await expect(resolver.resolveT2Ids(['t2_alice'])).resolves.toEqual({ t2_alice: 'alice' }); expect(staleClient.getUserProfile).not.toHaveBeenCalled(); expect(freshClient.getUserProfile).toHaveBeenCalledWith('@t2_alice:reddit.com'); }); });