feat: add async SQLite on Node 24 and improve messaging diagnostics (#520)

Move database work off the main thread on Node.js 24 and improve message handling and runtime diagnostics.

Preserve migration, restart, and concurrency coverage with the async SQLite driver.
This commit is contained in:
MliKiowa authored and GitHub committed 2026-10-13 10:12:47 +08:00
1 parent 20f48d11ec
commit c7428363cb
170 files changed
+7748 -3913

No files matched your search

+209 -120
View File
@@ -3,6 +3,7 @@ import fs from 'fs';
import { DatabaseSync } from 'node:sqlite';
import path from 'path';
import { subscribeLogs } from '@snowluma/common/logger';
import { SqliteDatabase } from '@snowluma/sqlite';
import { IdentityService } from '@snowluma/protocol/identity-service';
import type { GroupMemberInfo, QQGroupInfo, UserProfileInfo } from '@snowluma/protocol/qq-info';
@@ -31,20 +32,19 @@ function cleanupDb(dbPath: string): void {
}
}
function holdWriteLock(dbPath: string): () => void {
const locker = new DatabaseSync(dbPath);
locker.exec('PRAGMA busy_timeout = 0');
locker.exec('BEGIN IMMEDIATE');
async function holdWriteLock(dbPath: string): Promise<() => Promise<void>> {
const locker = new SqliteDatabase(dbPath, { busyTimeout: 0 });
await locker.exec('BEGIN IMMEDIATE');
let released = false;
return () => {
return async () => {
if (released) {
return;
}
released = true;
locker.exec('ROLLBACK');
locker.close();
await locker.exec('ROLLBACK');
await locker.close();
};
}
@@ -84,21 +84,52 @@ afterEach(() => {
});
describe('IdentityService', () => {
it('persists friends, groups, and active group members', () => {
it('replays observations accepted during loading and drains them when closed before ready', async () => {
const dbPath = tempDbPath('async-startup');
const previous = new IdentityService(SELF_UIN, dbPath);
previous.rememberFriends([{ uin: 22222, uid: 'u_old', nickname: 'old', remark: '' }]);
await previous.close();
let release!: () => void;
const loading = new Promise<void>((resolve) => {
release = resolve;
});
const identity = new IdentityService(SELF_UIN, dbPath, loading);
const friends = [{ uin: 33333, uid: 'u_new', nickname: 'accepted', remark: '' }];
identity.rememberFriends(friends);
friends[0]!.nickname = 'mutated by caller';
expect(identity.findFriend(33333)?.nickname).toBe('accepted');
const closing = identity.close();
expect(() => identity.rememberFriends([])).toThrow(/closed/);
release();
await closing;
const reopened = new IdentityService(SELF_UIN, dbPath);
try {
await reopened.initialization;
expect(reopened.findFriend(22222)).toBeNull();
expect(reopened.findFriend(33333)?.nickname).toBe('accepted');
} finally {
await reopened.close();
}
});
it('persists friends, groups, and active group members', async () => {
const dbPath = tempDbPath('persist');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberFriends([
{ uin: 22222, uid: 'u_friend', nickname: 'friend', remark: 'remark' },
]);
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [makeMember(33333, 'u_member', 'card')]);
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findFriend(22222)?.uid).toBe('u_friend');
expect(identity.findGroup(GROUP_ID)?.groupName).toBe('group');
@@ -106,28 +137,30 @@ describe('IdentityService', () => {
expect(identity.findUidByUin(33333, GROUP_ID)).toBe('u_member');
expect(identity.findUinByUid('u_member', GROUP_ID)).toBe(33333);
identity.close();
await identity.close();
}
});
it('persists group member robot classification across restarts', () => {
it('persists group member robot classification across restarts', async () => {
const dbPath = tempDbPath('persist-robot');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [makeMember(33333, 'u_robot', '', true)]);
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findGroupMember(GROUP_ID, 33333)?.isRobot).toBe(true);
identity.close();
await identity.close();
}
});
it('migrates legacy member caches without inventing a robot classification', () => {
it('migrates legacy member caches without inventing a robot classification', async () => {
const dbPath = tempDbPath('migrate-robot');
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
const legacy = new DatabaseSync(dbPath);
@@ -177,8 +210,9 @@ describe('IdentityService', () => {
legacy.close();
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findGroupMember(GROUP_ID, 33333)?.isRobot).toBeUndefined();
identity.close();
await identity.close();
const migrated = new DatabaseSync(dbPath);
const columns = migrated.prepare('PRAGMA table_info(group_members)').all() as Array<{
@@ -188,52 +222,57 @@ describe('IdentityService', () => {
migrated.close();
});
it('keeps updateGroupMember changes after reopening the persistent cache', () => {
it('keeps updateGroupMember changes after reopening the persistent cache', async () => {
const dbPath = tempDbPath('update-member');
const member = makeMember(33333, 'u_member', 'old-card');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [member]);
identity.updateGroupMember(GROUP_ID, { ...member, card: 'new-card' });
expect(identity.findGroupMember(GROUP_ID, member.uin)?.card).toBe('new-card');
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findGroupMember(GROUP_ID, member.uin)?.card).toBe('new-card');
identity.close();
await identity.close();
}
});
it('persists an explicitly cleared member card instead of restoring the old value', () => {
it('persists an explicitly cleared member card instead of restoring the old value', async () => {
const dbPath = tempDbPath('clear-member-card');
const member = makeMember(33333, 'u_member', 'old-card');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [member]);
identity.updateGroupMember(GROUP_ID, { ...member, card: '' });
expect(identity.findGroupMember(GROUP_ID, member.uin)?.card).toBe('');
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findGroupMember(GROUP_ID, member.uin)?.card).toBe('');
identity.close();
await identity.close();
}
});
it('keeps friend remark updates after reopening the persistent cache', () => {
it('keeps friend remark updates after reopening the persistent cache', async () => {
const dbPath = tempDbPath('update-friend-remark');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberFriends([
{
uin: 22222,
@@ -245,37 +284,40 @@ describe('IdentityService', () => {
expect(identity.updateFriendRemark('u_friend', 22222, 'new-remark')).toBe(true);
expect(identity.findFriend(22222)?.remark).toBe('new-remark');
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findFriend(22222)?.remark).toBe('new-remark');
identity.close();
await identity.close();
}
});
it('keeps exact group remark updates after reopening the persistent cache', () => {
it('keeps exact group remark updates after reopening the persistent cache', async () => {
const dbPath = tempDbPath('update-group-remark');
const remark = ' project group ';
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberGroups([{ ...makeGroup(), remark: 'old-remark' }]);
expect(identity.updateGroupRemark(GROUP_ID, remark)).toBe(true);
expect(identity.findGroup(GROUP_ID)?.remark).toBe(remark);
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findGroup(GROUP_ID)?.remark).toBe(remark);
identity.close();
await identity.close();
}
});
it('does not invent a group while applying a remark update', () => {
it('does not invent a group while applying a remark update', async () => {
const identity = IdentityService.memory(SELF_UIN);
identity.rememberGroups([makeGroup()]);
@@ -283,18 +325,19 @@ describe('IdentityService', () => {
expect(identity.groups).toHaveLength(1);
expect(identity.findGroup(GROUP_ID)?.remark).toBe('');
expect(identity.findGroup(987654321)).toBeNull();
identity.close();
await identity.close();
});
it.each([
['empty', ''],
['whitespace-only', ' '],
['surrounding-whitespace', ' padded remark '],
])('preserves %s friend remarks exactly after reopening', (_label, remark) => {
])('preserves %s friend remarks exactly after reopening', async (_label, remark) => {
const dbPath = tempDbPath('exact-friend-remark');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberFriends([
{
uin: 22222,
@@ -306,13 +349,14 @@ describe('IdentityService', () => {
expect(identity.updateFriendRemark('u_friend', 22222, remark)).toBe(true);
expect(identity.findFriend(22222)?.remark).toBe(remark);
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findFriend(22222)?.remark).toBe(remark);
identity.close();
await identity.close();
}
});
@@ -334,7 +378,8 @@ describe('IdentityService', () => {
vi.useFakeTimers();
const dbPath = tempDbPath('retry-recovery');
const identity = new IdentityService(SELF_UIN, dbPath);
const releaseLock = holdWriteLock(dbPath);
await identity.initialization;
const releaseLock = await holdWriteLock(dbPath);
let closed = false;
try {
@@ -348,6 +393,7 @@ describe('IdentityService', () => {
]);
expect(identity.findFriend(22222)?.uid).toBe('u_retry');
await expect(identity.flush()).rejects.toThrow(/locked/i);
expect(identity.persistenceStatus).toMatchObject({
state: 'degraded',
pendingWrites: 1,
@@ -358,6 +404,7 @@ describe('IdentityService', () => {
expect(identity.persistenceStatus.nextRetryAt! - Date.now()).toBe(100);
await vi.advanceTimersByTimeAsync(100);
await expect(identity.flush()).rejects.toThrow(/locked/i);
expect(identity.persistenceStatus).toMatchObject({
state: 'degraded',
pendingWrites: 1,
@@ -365,8 +412,9 @@ describe('IdentityService', () => {
});
expect(identity.persistenceStatus.nextRetryAt! - Date.now()).toBe(500);
releaseLock();
await releaseLock();
await vi.advanceTimersByTimeAsync(500);
await identity.flush();
expect(identity.persistenceStatus).toMatchObject({
state: 'healthy',
@@ -374,17 +422,18 @@ describe('IdentityService', () => {
retryAttempt: 0,
nextRetryAt: null,
});
identity.close();
await identity.close();
closed = true;
const reopened = new IdentityService(SELF_UIN, dbPath);
await reopened.initialization;
expect(reopened.findFriend(22222)?.uid).toBe('u_retry');
reopened.close();
await reopened.close();
} finally {
releaseLock();
await releaseLock();
if (!closed) {
identity.close();
await identity.close();
}
}
});
@@ -393,7 +442,8 @@ describe('IdentityService', () => {
vi.useFakeTimers();
const dbPath = tempDbPath('retry-order');
const identity = new IdentityService(SELF_UIN, dbPath);
const releaseLock = holdWriteLock(dbPath);
await identity.initialization;
const releaseLock = await holdWriteLock(dbPath);
let closed = false;
try {
@@ -418,20 +468,21 @@ describe('IdentityService', () => {
expect(identity.findFriend(11111)).toBeNull();
expect(identity.findFriend(22222)?.uid).toBe('u_second');
releaseLock();
await releaseLock();
await vi.runOnlyPendingTimersAsync();
identity.close();
await identity.close();
closed = true;
const reopened = new IdentityService(SELF_UIN, dbPath);
await reopened.initialization;
expect(reopened.findFriend(11111)).toBeNull();
expect(reopened.findFriend(22222)?.uid).toBe('u_second');
reopened.close();
await reopened.close();
} finally {
releaseLock();
await releaseLock();
if (!closed) {
identity.close();
await identity.close();
}
}
});
@@ -440,10 +491,12 @@ describe('IdentityService', () => {
vi.useFakeTimers();
const dbPath = tempDbPath('retry-snapshot');
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
const member = makeMember(33333, 'u_member', 'before');
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [member]);
const releaseLock = holdWriteLock(dbPath);
await identity.flush();
const releaseLock = await holdWriteLock(dbPath);
const observed = { ...member, card: 'accepted' };
try {
@@ -451,16 +504,17 @@ describe('IdentityService', () => {
observed.card = 'caller-mutated';
expect(identity.findGroupMember(GROUP_ID, member.uin)?.card).toBe('accepted');
releaseLock();
await releaseLock();
await vi.runOnlyPendingTimersAsync();
identity.close();
await identity.close();
const reopened = new IdentityService(SELF_UIN, dbPath);
await reopened.initialization;
expect(reopened.findGroupMember(GROUP_ID, member.uin)?.card).toBe('accepted');
reopened.close();
await reopened.close();
} finally {
releaseLock();
identity.close();
await releaseLock();
await identity.close();
}
});
@@ -468,10 +522,12 @@ describe('IdentityService', () => {
vi.useFakeTimers();
const dbPath = tempDbPath('pending-member-group-refresh');
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
const member = makeMember(33333, 'u_member', 'persisted-card');
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [member]);
const releaseLock = holdWriteLock(dbPath);
await identity.flush();
const releaseLock = await holdWriteLock(dbPath);
try {
identity.updateGroupMember(GROUP_ID, { ...member, card: 'pending-card' });
@@ -480,17 +536,18 @@ describe('IdentityService', () => {
expect(identity.findGroup(GROUP_ID)?.groupName).toBe('refreshed-group');
expect(identity.findGroupMember(GROUP_ID, member.uin)?.card).toBe('pending-card');
releaseLock();
await releaseLock();
await vi.runOnlyPendingTimersAsync();
identity.close();
await identity.close();
const reopened = new IdentityService(SELF_UIN, dbPath);
await reopened.initialization;
expect(reopened.findGroup(GROUP_ID)?.groupName).toBe('refreshed-group');
expect(reopened.findGroupMember(GROUP_ID, member.uin)?.card).toBe('pending-card');
reopened.close();
await reopened.close();
} finally {
releaseLock();
identity.close();
await releaseLock();
await identity.close();
}
});
@@ -498,9 +555,11 @@ describe('IdentityService', () => {
vi.useFakeTimers();
const dbPath = tempDbPath('pending-rebind');
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberRequestIdentity({ uid: 'u_old_for_uin', uin: 22001 });
identity.rememberRequestIdentity({ uid: 'u_stable', uin: 22002 });
const releaseLock = holdWriteLock(dbPath);
await identity.flush();
const releaseLock = await holdWriteLock(dbPath);
try {
identity.rememberRequestIdentity({ uid: 'u_new_for_uin', uin: 22001 });
@@ -511,17 +570,18 @@ describe('IdentityService', () => {
expect(identity.findUinByUid('u_old_for_uin')).toBeNull();
expect(identity.findUidByUin(22002)).toBeNull();
releaseLock();
await releaseLock();
await vi.runOnlyPendingTimersAsync();
identity.close();
await identity.close();
const reopened = new IdentityService(SELF_UIN, dbPath);
await reopened.initialization;
expect(reopened.findUidByUin(22001)).toBe('u_new_for_uin');
expect(reopened.findUinByUid('u_stable')).toBe(22999);
reopened.close();
await reopened.close();
} finally {
releaseLock();
identity.close();
await releaseLock();
await identity.close();
}
});
@@ -529,7 +589,8 @@ describe('IdentityService', () => {
vi.useFakeTimers();
const dbPath = tempDbPath('retry-capacity');
const identity = new IdentityService(SELF_UIN, dbPath);
const releaseLock = holdWriteLock(dbPath);
await identity.initialization;
const releaseLock = await holdWriteLock(dbPath);
try {
const capacity = identity.persistenceStatus.queueCapacity;
@@ -561,9 +622,9 @@ describe('IdentityService', () => {
});
expect(identity.persistenceStatus.lastError).toMatch(/queue full/i);
} finally {
releaseLock();
await releaseLock();
await vi.runOnlyPendingTimersAsync();
identity.close();
await identity.close();
}
});
@@ -571,7 +632,8 @@ describe('IdentityService', () => {
vi.useFakeTimers();
const dbPath = tempDbPath('retry-exhausted');
const identity = new IdentityService(SELF_UIN, dbPath);
const releaseLock = holdWriteLock(dbPath);
await identity.initialization;
const releaseLock = await holdWriteLock(dbPath);
const seen: Array<{ level: string; message: string }> = [];
const unsubscribe = subscribeLogs((entry) => {
if (entry.scope === 'Identity' && entry.uin === Number(SELF_UIN)) {
@@ -590,8 +652,11 @@ describe('IdentityService', () => {
]);
identity.rememberRequestIdentity({ uid: 'u_abandoned_request', uin: 33333 });
await expect(identity.flush()).rejects.toThrow(/locked/i);
for (const delay of [100, 500, 2_000, 10_000, 30_000]) {
await vi.advanceTimersByTimeAsync(delay);
await identity.flush().catch(() => undefined);
}
expect(identity.persistenceStatus).toMatchObject({
@@ -655,10 +720,11 @@ describe('IdentityService', () => {
expect(skipLogs).toHaveLength(2);
expect(skipLogs[1]?.message).toMatch(/suppressed=50/);
releaseLock();
identity.close();
await releaseLock();
await identity.close();
const reopened = new IdentityService(SELF_UIN, dbPath);
await reopened.initialization;
expect(reopened.persistenceStatus).toMatchObject({
state: 'healthy',
suspended: false,
@@ -668,23 +734,25 @@ describe('IdentityService', () => {
expect(reopened.findUinByUid('u_abandoned_request')).toBeNull();
expect(reopened.findUinByUid('u_skipped_after_suspend')).toBeNull();
reopened.rememberRequestIdentity({ uid: 'u_after_restart', uin: 55555 });
reopened.close();
await reopened.close();
const persisted = new IdentityService(SELF_UIN, dbPath);
await persisted.initialization;
expect(persisted.findUinByUid('u_after_restart')).toBe(55555);
persisted.close();
await persisted.close();
} finally {
unsubscribe();
releaseLock();
identity.close();
await releaseLock();
await identity.close();
}
});
it('flushes pending writes synchronously during close when SQLite has recovered', () => {
it('flushes pending writes during asynchronous close when SQLite has recovered', async () => {
vi.useFakeTimers();
const dbPath = tempDbPath('close-flush');
const identity = new IdentityService(SELF_UIN, dbPath);
const releaseLock = holdWriteLock(dbPath);
await identity.initialization;
const releaseLock = await holdWriteLock(dbPath);
identity.rememberFriends([
{
@@ -696,8 +764,8 @@ describe('IdentityService', () => {
]);
expect(identity.persistenceStatus.pendingWrites).toBe(1);
releaseLock();
identity.close();
await releaseLock();
await identity.close();
expect(identity.persistenceStatus).toMatchObject({
state: 'closed',
@@ -707,15 +775,17 @@ describe('IdentityService', () => {
});
const reopened = new IdentityService(SELF_UIN, dbPath);
await reopened.initialization;
expect(reopened.findFriend(22222)?.uid).toBe('u_close_flush');
reopened.close();
await reopened.close();
});
it('warns and releases pending retry work when close cannot flush SQLite', () => {
it('warns and releases pending retry work when close cannot flush SQLite', async () => {
vi.useFakeTimers();
const dbPath = tempDbPath('close-pending');
const identity = new IdentityService(SELF_UIN, dbPath);
const releaseLock = holdWriteLock(dbPath);
await identity.initialization;
const releaseLock = await holdWriteLock(dbPath);
const seen: Array<{ level: string; message: string }> = [];
const unsubscribe = subscribeLogs((entry) => {
if (entry.scope === 'Identity' && entry.uin === Number(SELF_UIN)) {
@@ -734,7 +804,7 @@ describe('IdentityService', () => {
]);
expect(identity.persistenceStatus.pendingWrites).toBe(1);
identity.close();
await identity.close();
expect(identity.persistenceStatus).toMatchObject({
state: 'closed',
@@ -754,8 +824,8 @@ describe('IdentityService', () => {
);
} finally {
unsubscribe();
releaseLock();
identity.close();
await releaseLock();
await identity.close();
}
});
@@ -763,8 +833,9 @@ describe('IdentityService', () => {
vi.useFakeTimers();
const dbPath = tempDbPath('close-failure');
const identity = new IdentityService(SELF_UIN, dbPath);
const releaseLock = holdWriteLock(dbPath);
const database = (identity as unknown as { db: DatabaseSync }).db;
await identity.initialization;
const releaseLock = await holdWriteLock(dbPath);
const database = (identity as unknown as { db: SqliteDatabase }).db;
const closeSpy = vi.spyOn(database, 'close').mockImplementationOnce(() => {
throw new Error('injected close failure');
});
@@ -780,7 +851,8 @@ describe('IdentityService', () => {
]);
expect(identity.persistenceStatus.pendingWrites).toBe(1);
expect(() => identity.close()).toThrow(/injected close failure/);
await expect(identity.flush()).rejects.toThrow(/locked/i);
await expect(identity.close()).rejects.toThrow(/injected close failure/);
expect(identity.persistenceStatus).toMatchObject({
state: 'degraded',
pendingWrites: 1,
@@ -799,31 +871,33 @@ describe('IdentityService', () => {
expect(identity.persistenceStatus.pendingWrites).toBe(2);
closeSpy.mockRestore();
releaseLock();
await releaseLock();
await vi.runOnlyPendingTimersAsync();
await identity.flush();
expect(identity.persistenceStatus).toMatchObject({
state: 'healthy',
pendingWrites: 0,
});
identity.close();
await identity.close();
const reopened = new IdentityService(SELF_UIN, dbPath);
await reopened.initialization;
expect(reopened.findFriend(22222)?.uid).toBe('u_close_retry_friend');
expect(reopened.findUinByUid('u_after_close_failure')).toBe(33333);
reopened.close();
await reopened.close();
} finally {
closeSpy.mockRestore();
releaseLock();
identity.close();
await releaseLock();
await identity.close();
}
});
it('fails fast on every observation entry point after close', () => {
it('fails fast on every observation entry point after close', async () => {
const identity = IdentityService.memory(SELF_UIN);
const member = makeMember(33333, 'u_member', 'before-close');
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [member]);
identity.close();
await identity.close();
const profile: UserProfileInfo = {
uin: 44444,
@@ -916,39 +990,41 @@ describe('IdentityService', () => {
expect(identity.findUinByUid('u_request')).toBeNull();
});
it('does not treat identity-only sightings as live group members', () => {
it('does not treat identity-only sightings as live group members', async () => {
const identity = IdentityService.memory(SELF_UIN);
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMemberIdentity(GROUP_ID, { uid: 'u_seen', uin: 88888 });
expect(identity.findGroupMember(GROUP_ID, 88888)).toBeNull();
expect(identity.findUinByUid('u_seen')).toBe(88888);
identity.close();
await identity.close();
});
it('does not hydrate identity-only sightings as live members after reopen', () => {
it('does not hydrate identity-only sightings as live members after reopen', async () => {
const dbPath = tempDbPath('identity-only-not-roster');
const member = makeMember(33333, 'u_member', 'card');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [member]);
identity.rememberGroupMemberIdentity(GROUP_ID, { uid: 'u_operator', uin: 88888 });
expect(identity.findGroupMember(GROUP_ID, 88888)).toBeNull();
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findGroupMember(GROUP_ID, member.uin)?.card).toBe('card');
expect(identity.findGroupMember(GROUP_ID, 88888)).toBeNull();
expect(identity.findUinByUid('u_operator')).toBe(88888);
identity.close();
await identity.close();
}
});
it('records a thin live member on join without inventing roster details', () => {
it('records a thin live member on join without inventing roster details', async () => {
const identity = IdentityService.memory(SELF_UIN);
identity.rememberGroups([makeGroup()]);
const existing = makeMember(33333, 'u_existing', 'card');
@@ -968,23 +1044,25 @@ describe('IdentityService', () => {
});
expect(joined?.isRobot).toBeUndefined();
expect(identity.findGroupMember(GROUP_ID, 33333)?.card).toBe('card');
identity.close();
await identity.close();
});
it('persists a thin live member on join across reopen', () => {
it('persists a thin live member on join across reopen', async () => {
const dbPath = tempDbPath('join-live-roster');
const existing = makeMember(33333, 'u_existing', 'card');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [existing]);
identity.rememberGroupMemberJoined(GROUP_ID, { uid: 'u_join', uin: 44444 });
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
const joined = identity.findGroupMember(GROUP_ID, 44444);
expect(joined).toMatchObject({
uin: 44444,
@@ -995,11 +1073,11 @@ describe('IdentityService', () => {
});
expect(joined?.isRobot).toBeUndefined();
expect(identity.findGroupMember(GROUP_ID, 33333)?.card).toBe('card');
identity.close();
await identity.close();
}
});
it('drops a leaver from the live roster and keeps their identity mapping', () => {
it('drops a leaver from the live roster and keeps their identity mapping', async () => {
const identity = IdentityService.memory(SELF_UIN);
identity.rememberGroups([makeGroup()]);
const member = makeMember(33333, 'u_member', 'card');
@@ -1009,61 +1087,66 @@ describe('IdentityService', () => {
expect(identity.findGroupMember(GROUP_ID, member.uin)).toBeNull();
expect(identity.findUidByUin(member.uin, GROUP_ID)).toBe(member.uid);
identity.close();
await identity.close();
});
it('keeps a leaver out of the live roster after reopen', () => {
it('keeps a leaver out of the live roster after reopen', async () => {
const dbPath = tempDbPath('leave-live-roster');
const member = makeMember(33333, 'u_member', 'card');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [member]);
identity.markGroupMemberInactive(GROUP_ID, { uid: member.uid, uin: member.uin });
expect(identity.findGroupMember(GROUP_ID, member.uin)).toBeNull();
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findGroupMember(GROUP_ID, member.uin)).toBeNull();
expect(identity.findUidByUin(member.uin, GROUP_ID)).toBe(member.uid);
identity.close();
await identity.close();
}
});
it('marks missing members inactive only after a successful full refresh', () => {
it('marks missing members inactive only after a successful full refresh', async () => {
const dbPath = tempDbPath('inactive-refresh');
const first = makeMember(33333, 'u_first');
const second = makeMember(44444, 'u_second');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [first, second]);
identity.rememberGroupMembers(GROUP_ID, [second]);
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findGroupMember(GROUP_ID, first.uin)).toBeNull();
expect(identity.findGroupMember(GROUP_ID, second.uin)?.uid).toBe(second.uid);
// Historical identity remains available for UID/UIN resolution.
expect(identity.findUidByUin(first.uin, GROUP_ID)).toBe(first.uid);
identity.close();
await identity.close();
}
});
it('marks missing friends and groups inactive after successful full refreshes', () => {
it('marks missing friends and groups inactive after successful full refreshes', async () => {
const dbPath = tempDbPath('inactive-lists');
const member = makeMember(33333, 'u_member');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberFriends([
{ uin: 22222, uid: 'u_friend', nickname: 'friend', remark: '' },
]);
@@ -1071,11 +1154,12 @@ describe('IdentityService', () => {
identity.rememberGroupMembers(GROUP_ID, [member]);
identity.rememberFriends([]);
identity.rememberGroups([]);
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findFriend(22222)).toBeNull();
expect(identity.findGroup(GROUP_ID)).toBeNull();
@@ -1083,37 +1167,40 @@ describe('IdentityService', () => {
expect(identity.findUidByUin(22222)).toBe('u_friend');
expect(identity.findUidByUin(member.uin, GROUP_ID)).toBe(member.uid);
identity.close();
await identity.close();
}
});
it('can mark one member inactive without losing the identity mapping', () => {
it('can mark one member inactive without losing the identity mapping', async () => {
const dbPath = tempDbPath('inactive-event');
const member = makeMember(33333, 'u_member');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberGroups([makeGroup()]);
identity.rememberGroupMembers(GROUP_ID, [member]);
identity.markGroupMemberInactive(GROUP_ID, { uid: member.uid, uin: member.uin });
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findGroupMember(GROUP_ID, member.uin)).toBeNull();
expect(identity.findUinByUid(member.uid, GROUP_ID)).toBe(member.uin);
identity.close();
await identity.close();
}
});
it('persists identities learned from request events', () => {
it('persists identities learned from request events', async () => {
const dbPath = tempDbPath('request-events');
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
identity.rememberRequestIdentity({
uid: 'u_friend_request',
uin: 55555,
@@ -1125,18 +1212,19 @@ describe('IdentityService', () => {
uin: 66666,
source: 'group_request',
});
identity.close();
await identity.close();
}
{
const identity = new IdentityService(SELF_UIN, dbPath);
await identity.initialization;
expect(identity.findUidByUin(55555)).toBe('u_friend_request');
expect(identity.findUinByUid('u_friend_request')).toBe(55555);
expect(identity.findGroup(GROUP_ID)).toBeNull();
expect(identity.findUidByUin(66666)).toBe('u_group_request');
identity.close();
await identity.close();
}
});
});
@@ -1369,6 +1457,7 @@ describe('IdentityService inbound UID→UIN', () => {
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);
await identity.initialization;
identity.setFetcher({
fetchProfile: async () => makeProfile(0, ''),
fetchProfileByUid: async () => {
@@ -1379,6 +1468,6 @@ describe('IdentityService inbound UID→UIN', () => {
await expect(identity.resolveUin('u_none')).resolves.toBeNull();
expect(identity.persistenceStatus.state).toBe('healthy');
expect(identity.persistenceStatus.pendingWrites).toBe(0);
identity.close();
await identity.close();
});
});