refactor(protocol): own inbound UID-to-UIN resolution in Identity
Inbound person lookup used to be split across decoders, the packet pipeline, and Bridge closures. Missing identities were filled from unrelated packet headers, and "who is this person" was coupled to roster refresh and pending-request enrichment. Identity now owns both directions of resolution. The hot path stays synchronous and emits unresolved events on a miss. Only the paths that already waited before dispatch perform a network lookup, and that lookup is a profile fetch by UID rather than a member-list refresh. Pending-request comments and approval flags stay outside Identity. Decoders no longer invent a UIN from a group routing header. Private chat routing still uses a hop's own wire UIN when the hop is named that way. Inbound misses stay availability-first: they do not throw and do not mark Identity degraded.
This commit is contained in:
16 files changed
+334
-187
No files matched your search
@@ -974,3 +974,146 @@ describe('IdentityService.resolveUid', () => {
|
||||
expect(fetchProfile).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('IdentityService inbound UID→UIN', () => {
|
||||
function makeProfile(uin: number, uid: string): UserProfileInfo {
|
||||
return { uin, uid, nickname: '', remark: '', qid: '', sex: 'unknown', age: 0, sign: '', avatar: '', level: 0 };
|
||||
}
|
||||
|
||||
it('findUinByUid treats a numeric uid as that uin without consulting maps', () => {
|
||||
const identity = IdentityService.memory(SELF_UIN);
|
||||
identity.rememberRequestIdentity({ uid: '22999', uin: 11111 });
|
||||
expect(identity.findUinByUid('22999')).toBe(22999);
|
||||
expect(identity.findUinByUid('0')).toBeNull();
|
||||
expect(identity.findUinByUid('')).toBeNull();
|
||||
});
|
||||
|
||||
it('returns the cached uin without invoking fetchProfileByUid', async () => {
|
||||
const identity = IdentityService.memory(SELF_UIN);
|
||||
identity.rememberRequestIdentity({ uid: 'u_known', uin: 12345 });
|
||||
const fetchProfileByUid = vi.fn(async () => makeProfile(999, 'should-not-be-used'));
|
||||
identity.setFetcher({
|
||||
fetchProfile: async () => makeProfile(0, ''),
|
||||
fetchProfileByUid,
|
||||
});
|
||||
|
||||
await expect(identity.resolveUin('u_known')).resolves.toBe(12345);
|
||||
expect(fetchProfileByUid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not call fetchProfileByUid for a numeric uid', async () => {
|
||||
const identity = IdentityService.memory(SELF_UIN);
|
||||
const fetchProfileByUid = vi.fn(async () => makeProfile(1, '1'));
|
||||
identity.setFetcher({
|
||||
fetchProfile: async () => makeProfile(0, ''),
|
||||
fetchProfileByUid,
|
||||
});
|
||||
|
||||
await expect(identity.resolveUin('22999')).resolves.toBe(22999);
|
||||
expect(fetchProfileByUid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('falls back to fetchProfileByUid on miss and returns the profile uin', async () => {
|
||||
const identity = IdentityService.memory(SELF_UIN);
|
||||
const fetchProfileByUid = vi.fn(async (uid: string) => {
|
||||
identity.rememberUserProfile(makeProfile(88888, uid));
|
||||
return makeProfile(88888, uid);
|
||||
});
|
||||
identity.setFetcher({
|
||||
fetchProfile: async () => makeProfile(0, ''),
|
||||
fetchProfileByUid,
|
||||
});
|
||||
|
||||
await expect(identity.resolveUin('u_stranger')).resolves.toBe(88888);
|
||||
expect(fetchProfileByUid).toHaveBeenCalledWith('u_stranger');
|
||||
});
|
||||
|
||||
it('returns the profile uin even when the fetcher forgets to remember', async () => {
|
||||
const identity = IdentityService.memory(SELF_UIN);
|
||||
identity.setFetcher({
|
||||
fetchProfile: async () => makeProfile(0, ''),
|
||||
fetchProfileByUid: async () => makeProfile(77777, 'u_forgetful'),
|
||||
});
|
||||
|
||||
await expect(identity.resolveUin('u_forgetful')).resolves.toBe(77777);
|
||||
});
|
||||
|
||||
it('does not pull a group member list even when groupId is provided', async () => {
|
||||
const identity = IdentityService.memory(SELF_UIN);
|
||||
const fetchGroupMemberList = vi.fn(async () => []);
|
||||
const fetchProfileByUid = vi.fn(async () => makeProfile(66666, 'u_invitee'));
|
||||
identity.setFetcher({
|
||||
fetchProfile: async () => makeProfile(0, ''),
|
||||
fetchProfileByUid,
|
||||
fetchGroupMemberList,
|
||||
});
|
||||
|
||||
await expect(identity.resolveUin('u_invitee', GROUP_ID)).resolves.toBe(66666);
|
||||
expect(fetchGroupMemberList).not.toHaveBeenCalled();
|
||||
expect(fetchProfileByUid).toHaveBeenCalledWith('u_invitee');
|
||||
});
|
||||
|
||||
it('does not pull a group member list when the inbound fetcher is absent', async () => {
|
||||
const identity = IdentityService.memory(SELF_UIN);
|
||||
const fetchGroupMemberList = vi.fn(async () => []);
|
||||
identity.setFetcher({
|
||||
fetchProfile: async () => makeProfile(0, ''),
|
||||
fetchGroupMemberList,
|
||||
});
|
||||
|
||||
await expect(identity.resolveUin('u_none', GROUP_ID)).resolves.toBeNull();
|
||||
expect(fetchGroupMemberList).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('uses a mapping remembered before the inbound fetcher throws', async () => {
|
||||
const identity = IdentityService.memory(SELF_UIN);
|
||||
identity.setFetcher({
|
||||
fetchProfile: async () => makeProfile(0, ''),
|
||||
fetchProfileByUid: async (uid: string) => {
|
||||
identity.rememberUserProfile(makeProfile(55555, uid));
|
||||
throw new Error('transport reset after remember');
|
||||
},
|
||||
});
|
||||
|
||||
await expect(identity.resolveUin('u_remembered')).resolves.toBe(55555);
|
||||
});
|
||||
|
||||
it('returns null on miss, fetcher error, empty profile, or missing inbound fetcher', async () => {
|
||||
const identity = IdentityService.memory(SELF_UIN);
|
||||
|
||||
await expect(identity.resolveUin('u_none')).resolves.toBeNull();
|
||||
|
||||
identity.setFetcher({ fetchProfile: async () => makeProfile(0, '') });
|
||||
await expect(identity.resolveUin('u_none')).resolves.toBeNull();
|
||||
|
||||
identity.setFetcher({
|
||||
fetchProfile: async () => makeProfile(0, ''),
|
||||
fetchProfileByUid: async () => makeProfile(0, 'u_none'),
|
||||
});
|
||||
await expect(identity.resolveUin('u_none')).resolves.toBeNull();
|
||||
|
||||
identity.setFetcher({
|
||||
fetchProfile: async () => makeProfile(0, ''),
|
||||
fetchProfileByUid: async () => {
|
||||
throw new Error('oidb down');
|
||||
},
|
||||
});
|
||||
await expect(identity.resolveUin('u_none')).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it('does not enter Identity degraded when the inbound fetcher throws', async () => {
|
||||
const dbPath = tempDbPath('inbound-fetch-throw');
|
||||
const identity = new IdentityService(SELF_UIN, dbPath);
|
||||
identity.setFetcher({
|
||||
fetchProfile: async () => makeProfile(0, ''),
|
||||
fetchProfileByUid: async () => {
|
||||
throw new Error('oidb down');
|
||||
},
|
||||
});
|
||||
|
||||
await expect(identity.resolveUin('u_none')).resolves.toBeNull();
|
||||
expect(identity.persistenceStatus.state).toBe('healthy');
|
||||
expect(identity.persistenceStatus.pendingWrites).toBe(0);
|
||||
identity.close();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user