Phase 2 of the 4-package split (RoadMap #3). Moves ~35 files — events / event-bus / identity-service / packet-pipeline / element- builder / bridge-oidb / qq-info / msg-push/* / highway/* / web/* + the bridge-side formatters — out of `packages/core/src/bridge/` into the new `@snowluma/bridge` workspace package. Everything that translates QQ wire bytes ↔ typed events/elements now lives there. Why --- - `packages/core/src/bridge/` was actually two layers: a low-level "protocol decoder + I/O" set (msg-push, highway, packet-pipeline, identity, element-builder) AND the Api-class host (bridge.ts + apis/*). The Api layer can't move without the protocol layer moving first, but the two had grown into one directory. - Bridge-internal consumers (highway uploaders, OIDB envelope helper, element-builder, msg-push parsers) only ever needed `bridge.identity` + `bridge.sendRawPacket` + the upload-metadata cache. Splitting them out lets that minimal surface live in the bridge package as `BridgeContext` while the api-layer's extended context (with `apis: ApiHub`) stays in @snowluma/core. What stays in @snowluma/core (apis layer): - bridge.ts ── the god-class that composes the protocol building blocks + mounts apis - bridge-interface.ts ── public façade for OneBot consumers - manager.ts ── per-uin BridgeManager hub - apis/* ── the 13 typed Api classes - bridge-context.ts (NEW) ── extends @snowluma/bridge's slim BridgeContext with `apis: ApiHub` so the 13 Api classes keep their typed `this.ctx.apis.X.method()` access. Layout ------ Moved via `git mv` (history preserved): events / event-bus / identity-service / packet-pipeline / element-builder / bridge-oidb / qq-info / msg-push/* / highway/* / web/* → @snowluma/bridge/<sub> utils/event-format.ts → @snowluma/bridge/format.ts (the last one renamed because the destination directory only has bridge-related files; `format.ts` reads cleaner than `event-format`. Listed as a rename in git's `RM` output — content unchanged.) Layering fixes (functionality preserved, just type signatures) -------------------------------------------------------------- - `element-builder.SendContext.bridge` → typed as `BridgeContext` instead of `Bridge`. Reads only `bridge.identity` and the upload-cache (both on BridgeContext); the concrete `Bridge` instance still passes via free upcast. - `bridge-oidb.runOidb` signature widened from `Bridge` to `BridgeContext` — same reason. - `highway/{highway-client,pipeline,image-upload,ptt-upload, video-upload}.ts` — same. - `format.ts::formatReply` parameter typed as a new local `ReplyEventLookup` structural type (`{ findEvent(id): JsonObject | null }`) instead of `MessageStore` from @snowluma/onebot. The OneBot `MessageStore` is a structural superset; consumers pass it through unchanged. - JSON value types moved to a new `@snowluma/common/json` subpath (consumed by format.ts) — `onebot/types.ts` re-exports them so external `import { JsonValue } from '<…>/onebot/types'` paths keep working. - `UploadedFileMeta` interface moved from `core/src/bridge/bridge.ts` into `@snowluma/bridge/bridge-context.ts` — element-builder + the forward-builder both read the upload cache through BridgeContext, so the type has to live in @snowluma/bridge. `bridge.ts` keeps its re-export for back-compat. Changes ------- - New package `packages/bridge/` (package.json with per-file subpath exports mirroring legacy `core/src/bridge/<x>` layout 1:1, tsconfig.json, typecheck script). - 35 `.ts` files moved via `git mv` (3 files counted by git as RM — rename + modify because Bridge → BridgeContext type swap). - 150+ import sites rewritten across `packages/core/src` and `packages/core/tests`: `'../bridge/<x>'`, `'../../bridge/<x>'`, `'../src/bridge/<x>'`, `'@/bridge/web/<x>'`, and vitest `vi.mock(...)` literals all collapse to `@snowluma/bridge/<x>`. - `@snowluma/core` depends on `@snowluma/bridge` (workspace:*). - `@snowluma/bridge` depends on `@snowluma/common`, `@snowluma/proto-defs`, `@snowluma/proton`. - `packages/core/src/utils/` directory removed (empty). Verification ------------ - Full workspace `pnpm -r typecheck`: all 10 projects clean. - core test suite: 552 passed / 552 total (pre-existing websocket-darwin-arm64.node addon failure unchanged — same baseline as the previous refactor commits). - Vite build (with stubbed native binary): clean — 143 modules, 2.84 MB bundle / 224 KB gzip, identical envelope to the pre-split build. Phase 3 (next): OneBot + WebUI move into @snowluma/onebot. The runnable `index.ts` stays in @snowluma/core as the application entry, importing `OneBotManager` from `@snowluma/onebot`.
253 lines
8.4 KiB
TypeScript
253 lines
8.4 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { IdentityService } from '@snowluma/bridge/identity-service';
|
|
import type { GroupMemberInfo, QQGroupInfo, UserProfileInfo } from '@snowluma/bridge/qq-info';
|
|
|
|
const SELF_UIN = '10001';
|
|
const GROUP_ID = 123456789;
|
|
|
|
const dbs: string[] = [];
|
|
|
|
function tempDbPath(label: string): string {
|
|
const dbPath = path.join(
|
|
'data',
|
|
'test',
|
|
`snowluma-identity-${label}-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}.db`,
|
|
);
|
|
dbs.push(dbPath);
|
|
return dbPath;
|
|
}
|
|
|
|
function cleanupDb(dbPath: string): void {
|
|
for (const ext of ['', '-wal', '-shm']) {
|
|
try { fs.unlinkSync(dbPath + ext); } catch { /* ignore */ }
|
|
}
|
|
}
|
|
|
|
function makeGroup(): QQGroupInfo {
|
|
return {
|
|
groupId: GROUP_ID,
|
|
groupName: 'group',
|
|
remark: '',
|
|
memberCount: 0,
|
|
memberMax: 500,
|
|
members: new Map(),
|
|
};
|
|
}
|
|
|
|
function makeMember(uin: number, uid: string, card = ''): GroupMemberInfo {
|
|
return {
|
|
uin,
|
|
uid,
|
|
nickname: `nick-${uin}`,
|
|
card,
|
|
role: 'member',
|
|
level: 1,
|
|
title: '',
|
|
joinTime: 10,
|
|
lastSentTime: 20,
|
|
shutUpTime: 0,
|
|
};
|
|
}
|
|
|
|
afterEach(() => {
|
|
for (const dbPath of dbs.splice(0)) cleanupDb(dbPath);
|
|
});
|
|
|
|
describe('IdentityService', () => {
|
|
it('persists friends, groups, and active group members', () => {
|
|
const dbPath = tempDbPath('persist');
|
|
|
|
{
|
|
const identity = new IdentityService(SELF_UIN, dbPath);
|
|
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();
|
|
}
|
|
|
|
{
|
|
const identity = new IdentityService(SELF_UIN, dbPath);
|
|
|
|
expect(identity.findFriend(22222)?.uid).toBe('u_friend');
|
|
expect(identity.findGroup(GROUP_ID)?.groupName).toBe('group');
|
|
expect(identity.findGroupMember(GROUP_ID, 33333)?.card).toBe('card');
|
|
expect(identity.findUidByUin(33333, GROUP_ID)).toBe('u_member');
|
|
expect(identity.findUinByUid('u_member', GROUP_ID)).toBe(33333);
|
|
|
|
identity.close();
|
|
}
|
|
});
|
|
|
|
it('marks missing members inactive only after a successful full refresh', () => {
|
|
const dbPath = tempDbPath('inactive-refresh');
|
|
const first = makeMember(33333, 'u_first');
|
|
const second = makeMember(44444, 'u_second');
|
|
|
|
{
|
|
const identity = new IdentityService(SELF_UIN, dbPath);
|
|
identity.rememberGroups([makeGroup()]);
|
|
identity.rememberGroupMembers(GROUP_ID, [first, second]);
|
|
identity.rememberGroupMembers(GROUP_ID, [second]);
|
|
identity.close();
|
|
}
|
|
|
|
{
|
|
const identity = new IdentityService(SELF_UIN, dbPath);
|
|
|
|
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();
|
|
}
|
|
});
|
|
|
|
it('marks missing friends and groups inactive after successful full refreshes', () => {
|
|
const dbPath = tempDbPath('inactive-lists');
|
|
const member = makeMember(33333, 'u_member');
|
|
|
|
{
|
|
const identity = new IdentityService(SELF_UIN, dbPath);
|
|
identity.rememberFriends([{ uin: 22222, uid: 'u_friend', nickname: 'friend', remark: '' }]);
|
|
identity.rememberGroups([makeGroup()]);
|
|
identity.rememberGroupMembers(GROUP_ID, [member]);
|
|
identity.rememberFriends([]);
|
|
identity.rememberGroups([]);
|
|
identity.close();
|
|
}
|
|
|
|
{
|
|
const identity = new IdentityService(SELF_UIN, dbPath);
|
|
|
|
expect(identity.findFriend(22222)).toBeNull();
|
|
expect(identity.findGroup(GROUP_ID)).toBeNull();
|
|
// Identity mappings remain useful for historical events/actions.
|
|
expect(identity.findUidByUin(22222)).toBe('u_friend');
|
|
expect(identity.findUidByUin(member.uin, GROUP_ID)).toBe(member.uid);
|
|
|
|
identity.close();
|
|
}
|
|
});
|
|
|
|
it('can mark one member inactive without losing the identity mapping', () => {
|
|
const dbPath = tempDbPath('inactive-event');
|
|
const member = makeMember(33333, 'u_member');
|
|
|
|
{
|
|
const identity = new IdentityService(SELF_UIN, dbPath);
|
|
identity.rememberGroups([makeGroup()]);
|
|
identity.rememberGroupMembers(GROUP_ID, [member]);
|
|
identity.markGroupMemberInactive(GROUP_ID, { uid: member.uid, uin: member.uin });
|
|
identity.close();
|
|
}
|
|
|
|
{
|
|
const identity = new IdentityService(SELF_UIN, dbPath);
|
|
|
|
expect(identity.findGroupMember(GROUP_ID, member.uin)).toBeNull();
|
|
expect(identity.findUinByUid(member.uid, GROUP_ID)).toBe(member.uin);
|
|
|
|
identity.close();
|
|
}
|
|
});
|
|
|
|
it('persists identities learned from request events', () => {
|
|
const dbPath = tempDbPath('request-events');
|
|
|
|
{
|
|
const identity = new IdentityService(SELF_UIN, dbPath);
|
|
identity.rememberRequestIdentity({
|
|
uid: 'u_friend_request',
|
|
uin: 55555,
|
|
source: 'friend_request',
|
|
});
|
|
identity.rememberRequestIdentity({
|
|
groupId: GROUP_ID,
|
|
uid: 'u_group_request',
|
|
uin: 66666,
|
|
source: 'group_request',
|
|
});
|
|
identity.close();
|
|
}
|
|
|
|
{
|
|
const identity = new IdentityService(SELF_UIN, dbPath);
|
|
|
|
expect(identity.findUidByUin(55555)).toBe('u_friend_request');
|
|
expect(identity.findUinByUid('u_friend_request')).toBe(55555);
|
|
expect(identity.findGroup(GROUP_ID)?.groupId).toBe(GROUP_ID);
|
|
expect(identity.findUidByUin(66666)).toBe('u_group_request');
|
|
|
|
identity.close();
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('IdentityService.resolveUid', () => {
|
|
function makeProfile(uin: number, uid: string): UserProfileInfo {
|
|
return { uin, uid, nickname: '', remark: '', qid: '', sex: 'unknown', age: 0, sign: '', avatar: '' };
|
|
}
|
|
|
|
it('returns the cached uid without invoking the fetcher', async () => {
|
|
const identity = IdentityService.memory(SELF_UIN);
|
|
identity.rememberRequestIdentity({ uid: 'u_known', uin: 12345 });
|
|
const fetchProfile = vi.fn(async (uin: number) => makeProfile(uin, 'should-not-be-used'));
|
|
identity.setFetcher({ fetchProfile });
|
|
|
|
await expect(identity.resolveUid(12345)).resolves.toBe('u_known');
|
|
expect(fetchProfile).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('falls back to fetchProfile on miss and returns the resolved uid', async () => {
|
|
const identity = IdentityService.memory(SELF_UIN);
|
|
const fetchProfile = vi.fn(async (uin: number) => {
|
|
// Simulate the bridge writing the result back via rememberUserProfile.
|
|
identity.rememberUserProfile(makeProfile(uin, 'u_fetched'));
|
|
return makeProfile(uin, 'u_fetched');
|
|
});
|
|
identity.setFetcher({ fetchProfile });
|
|
|
|
await expect(identity.resolveUid(99999)).resolves.toBe('u_fetched');
|
|
expect(fetchProfile).toHaveBeenCalledWith(99999);
|
|
});
|
|
|
|
it('throws when neither cache nor fetcher can produce a uid', async () => {
|
|
const identity = IdentityService.memory(SELF_UIN);
|
|
identity.setFetcher({ fetchProfile: async (uin) => makeProfile(uin, '') });
|
|
|
|
await expect(identity.resolveUid(99999)).rejects.toThrow(/failed to resolve UID/);
|
|
});
|
|
|
|
it('tries fetchGroupMemberList before fetchProfile when groupId is provided', async () => {
|
|
const identity = IdentityService.memory(SELF_UIN);
|
|
identity.rememberGroups([{
|
|
groupId: GROUP_ID, groupName: '', remark: '',
|
|
memberCount: 0, memberMax: 0, members: new Map(),
|
|
}]);
|
|
|
|
const fetchProfile = vi.fn(async (uin: number) => makeProfile(uin, 'u_via_profile'));
|
|
const fetchGroupMemberList = vi.fn(async (groupId: number) => {
|
|
// Roster fetch should populate the cache.
|
|
identity.rememberGroupMembers(groupId, [makeMember(77777, 'u_via_roster')]);
|
|
return [];
|
|
});
|
|
identity.setFetcher({ fetchProfile, fetchGroupMemberList });
|
|
|
|
await expect(identity.resolveUid(77777, GROUP_ID)).resolves.toBe('u_via_roster');
|
|
expect(fetchGroupMemberList).toHaveBeenCalledWith(GROUP_ID);
|
|
expect(fetchProfile).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('throws on invalid (zero / negative) uin without calling fetcher', async () => {
|
|
const identity = IdentityService.memory(SELF_UIN);
|
|
const fetchProfile = vi.fn();
|
|
identity.setFetcher({ fetchProfile });
|
|
|
|
await expect(identity.resolveUid(0)).rejects.toThrow(/invalid uin/);
|
|
expect(fetchProfile).not.toHaveBeenCalled();
|
|
});
|
|
});
|