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`.
317 lines
12 KiB
TypeScript
317 lines
12 KiB
TypeScript
// First real OneBot unit test that exercises a module purely through
|
|
// BridgeInterface — no concrete Bridge, no SQLite, no native packet
|
|
// sender, no BridgeManager. This is the testability win that PR4 + PR5
|
|
// were supposed to unlock; the file exists to prove it.
|
|
|
|
import { describe, expect, it, vi } from 'vitest';
|
|
import type { BridgeInterface } from '../../src/bridge/bridge-interface';
|
|
import type {
|
|
FriendInfo, GroupMemberInfo, QQGroupInfo, UserProfileInfo,
|
|
} from '@snowluma/bridge/qq-info';
|
|
import {
|
|
getFriendList,
|
|
getGroupInfo,
|
|
getGroupList,
|
|
getGroupMemberInfo,
|
|
getGroupMemberList,
|
|
getLoginInfo,
|
|
getStrangerInfo,
|
|
} from '../../src/onebot/modules/contact-actions';
|
|
import type { OneBotInstanceContext } from '../../src/onebot/instance-context';
|
|
|
|
/**
|
|
* A typed BridgeInterface where every property is undefined by default;
|
|
* accessing an un-stubbed property throws with a clear message so tests
|
|
* fail loudly if they exercise a code path the fake hasn't been told
|
|
* about. Tests just spread the methods they care about.
|
|
*/
|
|
// `apisAutoPromote` lets tests written before #6 (which stubbed flat
|
|
// methods like `fetchFriendList: vi.fn()` on bridge directly) keep
|
|
// working without restructuring — the helper rewrites flat stubs into
|
|
// the new `apis.<area>.method` shape automatically. New code can also
|
|
// pass `apis: { contacts: { … } }` explicitly; the two merge.
|
|
const APIS_ROUTING: Record<string, string> = {
|
|
fetchFriendList: 'contacts', fetchGroupList: 'contacts',
|
|
fetchGroupMemberList: 'contacts', fetchUserProfile: 'contacts',
|
|
fetchGroupRequests: 'contacts', fetchDownloadRKeys: 'contacts',
|
|
};
|
|
|
|
function fakeBridge(overrides: Record<string, any> = {}): BridgeInterface {
|
|
const apisSynth: Record<string, Record<string, any>> = {};
|
|
for (const [k, v] of Object.entries(overrides)) {
|
|
const area = APIS_ROUTING[k];
|
|
if (area) {
|
|
if (!apisSynth[area]) apisSynth[area] = {};
|
|
apisSynth[area][k] = v;
|
|
}
|
|
}
|
|
const merged = { ...overrides, apis: { ...apisSynth, ...(overrides.apis ?? {}) } };
|
|
return new Proxy(merged as BridgeInterface, {
|
|
get(target, prop) {
|
|
if (prop in target) return (target as any)[prop];
|
|
throw new Error(`fakeBridge: '${String(prop)}' was not stubbed for this test`);
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Same Proxy trick for the IdentityService surface that contact-actions touches. */
|
|
function fakeIdentity(overrides: Record<string, unknown> = {}): BridgeInterface['identity'] {
|
|
return new Proxy(overrides as any, {
|
|
get(target, prop) {
|
|
if (prop in target) return target[prop];
|
|
throw new Error(`fakeIdentity: '${String(prop)}' was not stubbed for this test`);
|
|
},
|
|
});
|
|
}
|
|
|
|
// ─── Fixture builders ───
|
|
|
|
function makeFriend(uin: number, nickname: string, remark = ''): FriendInfo {
|
|
return { uin, uid: `u_${uin}`, nickname, remark };
|
|
}
|
|
|
|
function makeGroup(groupId: number, groupName: string, members: GroupMemberInfo[] = []): QQGroupInfo {
|
|
return {
|
|
groupId, groupName, remark: '',
|
|
memberCount: members.length, memberMax: 500,
|
|
members: new Map(members.map((m) => [m.uin, m])),
|
|
};
|
|
}
|
|
|
|
function makeMember(uin: number, nickname: string, card = ''): GroupMemberInfo {
|
|
return {
|
|
uin, uid: `u_${uin}`, nickname, card,
|
|
role: 'member', level: 1, title: '',
|
|
joinTime: 0, lastSentTime: 0, shutUpTime: 0,
|
|
};
|
|
}
|
|
|
|
function makeProfile(uin: number, nickname: string, sex: 'male' | 'female' | 'unknown' = 'unknown', age = 0): UserProfileInfo {
|
|
return {
|
|
uin, uid: `u_${uin}`, nickname, remark: '', qid: '', sex, age, sign: '', avatar: '',
|
|
};
|
|
}
|
|
|
|
// ─── Tests ───
|
|
|
|
describe('onebot/contact-actions / getLoginInfo', () => {
|
|
it('returns user_id parsed from uin and nickname from identity', () => {
|
|
const ref = {
|
|
uin: '10001',
|
|
bridge: fakeBridge({ identity: fakeIdentity({ nickname: 'self-nick' }) }),
|
|
} as unknown as OneBotInstanceContext;
|
|
expect(getLoginInfo(ref)).toEqual({ userId: 10001, nickname: 'self-nick' });
|
|
});
|
|
|
|
it('falls back to the uin string when identity nickname is empty', () => {
|
|
const ref = {
|
|
uin: '10001',
|
|
bridge: fakeBridge({ identity: fakeIdentity({ nickname: '' }) }),
|
|
} as unknown as OneBotInstanceContext;
|
|
expect(getLoginInfo(ref)).toEqual({ userId: 10001, nickname: '10001' });
|
|
});
|
|
});
|
|
|
|
describe('onebot/contact-actions / getFriendList', () => {
|
|
it('returns the fetched list mapped to OneBot shape', async () => {
|
|
const bridge = fakeBridge({
|
|
fetchFriendList: vi.fn(async () => [makeFriend(22222, 'alice', 'best-friend')]),
|
|
});
|
|
const out = await getFriendList(bridge);
|
|
expect(out).toEqual([{ user_id: 22222, nickname: 'alice', remark: 'best-friend' }]);
|
|
expect(bridge.apis.contacts.fetchFriendList).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it('falls back to identity.friends on fetch failure', async () => {
|
|
const cached = [makeFriend(33333, 'bob')];
|
|
const bridge = fakeBridge({
|
|
fetchFriendList: vi.fn(async () => { throw new Error('network down'); }),
|
|
identity: fakeIdentity({ friends: cached }),
|
|
});
|
|
const out = await getFriendList(bridge);
|
|
expect(out).toEqual([{ user_id: 33333, nickname: 'bob', remark: '' }]);
|
|
});
|
|
});
|
|
|
|
describe('onebot/contact-actions / getGroupList', () => {
|
|
it('triggers fetch when the in-memory roster is empty', async () => {
|
|
const fetched = [makeGroup(100, 'Group A')];
|
|
// `groups` starts empty; the fetch callback flips it to mimic
|
|
// bridge.apis.contacts.fetchGroupList writing back through identity.rememberGroups.
|
|
let groups: QQGroupInfo[] = [];
|
|
const bridge = fakeBridge({
|
|
fetchGroupList: vi.fn(async () => { groups = fetched; return fetched; }),
|
|
identity: fakeIdentity({
|
|
get groups() { return groups; },
|
|
}),
|
|
});
|
|
const out = await getGroupList(bridge);
|
|
expect(bridge.apis.contacts.fetchGroupList).toHaveBeenCalledOnce();
|
|
expect(out).toEqual([{
|
|
group_id: 100, group_name: 'Group A',
|
|
member_count: 0, max_member_count: 500,
|
|
}]);
|
|
});
|
|
|
|
it('skips fetch when cache is populated and noCache is omitted', async () => {
|
|
const cached = [makeGroup(200, 'Group B')];
|
|
const bridge = fakeBridge({
|
|
fetchGroupList: vi.fn(async () => []),
|
|
identity: fakeIdentity({ groups: cached }),
|
|
});
|
|
await getGroupList(bridge);
|
|
expect(bridge.apis.contacts.fetchGroupList).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('forces fetch when noCache=true even with a populated cache', async () => {
|
|
const cached = [makeGroup(300, 'Group C')];
|
|
const bridge = fakeBridge({
|
|
fetchGroupList: vi.fn(async () => cached),
|
|
identity: fakeIdentity({ groups: cached }),
|
|
});
|
|
await getGroupList(bridge, true);
|
|
expect(bridge.apis.contacts.fetchGroupList).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it('serves the stale cache when the fetch path throws', async () => {
|
|
const cached = [makeGroup(400, 'Group D')];
|
|
const bridge = fakeBridge({
|
|
fetchGroupList: vi.fn(async () => { throw new Error('boom'); }),
|
|
identity: fakeIdentity({ groups: cached }),
|
|
});
|
|
const out = await getGroupList(bridge, true);
|
|
expect(out[0]).toMatchObject({ group_id: 400, group_name: 'Group D' });
|
|
});
|
|
});
|
|
|
|
describe('onebot/contact-actions / getGroupInfo', () => {
|
|
it('returns the cached group without refreshing when group is known and noCache is false', async () => {
|
|
const cached = makeGroup(500, 'Group E');
|
|
const findGroup = vi.fn((groupId: number) => groupId === 500 ? cached : null);
|
|
const bridge = fakeBridge({
|
|
fetchGroupList: vi.fn(async () => []),
|
|
identity: fakeIdentity({ findGroup }),
|
|
});
|
|
const out = await getGroupInfo(bridge, 500);
|
|
expect(out).toMatchObject({ group_id: 500, group_name: 'Group E' });
|
|
expect(bridge.apis.contacts.fetchGroupList).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('triggers fetch when the group is unknown to the cache', async () => {
|
|
const cached = makeGroup(600, 'Group F');
|
|
let known = false;
|
|
const findGroup = vi.fn((groupId: number) => (known && groupId === 600) ? cached : null);
|
|
const fetchGroupList = vi.fn(async () => { known = true; return [cached]; });
|
|
const bridge = fakeBridge({
|
|
fetchGroupList,
|
|
identity: fakeIdentity({ findGroup }),
|
|
});
|
|
const out = await getGroupInfo(bridge, 600);
|
|
expect(fetchGroupList).toHaveBeenCalledOnce();
|
|
expect(out).toMatchObject({ group_id: 600, group_name: 'Group F' });
|
|
});
|
|
|
|
it('returns null when the group remains unknown after fetch', async () => {
|
|
const bridge = fakeBridge({
|
|
fetchGroupList: vi.fn(async () => []),
|
|
identity: fakeIdentity({ findGroup: () => null }),
|
|
});
|
|
expect(await getGroupInfo(bridge, 700)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('onebot/contact-actions / getGroupMemberList', () => {
|
|
it('returns the fetched roster mapped to OneBot shape', async () => {
|
|
const members = [makeMember(11, 'alice', 'A'), makeMember(22, 'bob', 'B')];
|
|
const bridge = fakeBridge({
|
|
fetchGroupMemberList: vi.fn(async () => members),
|
|
});
|
|
const out = await getGroupMemberList(bridge, 800);
|
|
expect(out).toHaveLength(2);
|
|
expect(out[0]).toMatchObject({ group_id: 800, user_id: 11, nickname: 'alice', card: 'A' });
|
|
});
|
|
|
|
it('falls back to the cached roster when fetch fails', async () => {
|
|
const cached = makeGroup(900, '', [makeMember(33, 'cached')]);
|
|
const bridge = fakeBridge({
|
|
fetchGroupMemberList: vi.fn(async () => { throw new Error('net'); }),
|
|
identity: fakeIdentity({
|
|
findGroup: (gid: number) => gid === 900 ? cached : null,
|
|
}),
|
|
});
|
|
const out = await getGroupMemberList(bridge, 900);
|
|
expect(out).toHaveLength(1);
|
|
expect(out[0]).toMatchObject({ user_id: 33, nickname: 'cached' });
|
|
});
|
|
|
|
it('returns [] when no roster is known and fetch fails', async () => {
|
|
const bridge = fakeBridge({
|
|
fetchGroupMemberList: vi.fn(async () => { throw new Error('net'); }),
|
|
identity: fakeIdentity({ findGroup: () => null }),
|
|
});
|
|
expect(await getGroupMemberList(bridge, 999)).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('onebot/contact-actions / getGroupMemberInfo', () => {
|
|
it('returns the cached member when present and noCache is false', async () => {
|
|
const member = makeMember(44, 'dave', 'D');
|
|
const bridge = fakeBridge({
|
|
fetchGroupMemberList: vi.fn(async () => []),
|
|
identity: fakeIdentity({
|
|
findGroupMember: (gid: number, uin: number) =>
|
|
gid === 1000 && uin === 44 ? member : null,
|
|
}),
|
|
});
|
|
const out = await getGroupMemberInfo(bridge, 1000, 44);
|
|
expect(out).toMatchObject({ user_id: 44, nickname: 'dave', card: 'D' });
|
|
expect(bridge.apis.contacts.fetchGroupMemberList).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('triggers fetch when member is unknown and re-queries the cache', async () => {
|
|
const member = makeMember(55, 'eve');
|
|
let known = false;
|
|
const findGroupMember = vi.fn((gid: number, uin: number) =>
|
|
(known && gid === 1100 && uin === 55) ? member : null,
|
|
);
|
|
const fetchGroupMemberList = vi.fn(async () => { known = true; return [member]; });
|
|
const bridge = fakeBridge({
|
|
fetchGroupMemberList,
|
|
identity: fakeIdentity({ findGroupMember }),
|
|
});
|
|
const out = await getGroupMemberInfo(bridge, 1100, 55);
|
|
expect(fetchGroupMemberList).toHaveBeenCalledOnce();
|
|
expect(out).toMatchObject({ user_id: 55, nickname: 'eve' });
|
|
});
|
|
});
|
|
|
|
describe('onebot/contact-actions / getStrangerInfo', () => {
|
|
it('returns a fetched profile', async () => {
|
|
const bridge = fakeBridge({
|
|
fetchUserProfile: vi.fn(async () => makeProfile(55555, 'Eve', 'female', 25)),
|
|
});
|
|
const out = await getStrangerInfo(bridge, 55555);
|
|
expect(out).toMatchObject({ user_id: 55555, nickname: 'Eve', sex: 'female', age: 25 });
|
|
});
|
|
|
|
it('falls back to identity.findUserProfile when fetch fails but the profile is cached', async () => {
|
|
const bridge = fakeBridge({
|
|
fetchUserProfile: vi.fn(async () => { throw new Error('net'); }),
|
|
identity: fakeIdentity({
|
|
findUserProfile: (uin: number) =>
|
|
uin === 66666 ? makeProfile(66666, 'Frank', 'male', 30) : null,
|
|
}),
|
|
});
|
|
const out = await getStrangerInfo(bridge, 66666);
|
|
expect(out).toMatchObject({ user_id: 66666, nickname: 'Frank' });
|
|
});
|
|
|
|
it('returns null when neither fetch nor cache produces a profile', async () => {
|
|
const bridge = fakeBridge({
|
|
fetchUserProfile: vi.fn(async () => { throw new Error('net'); }),
|
|
identity: fakeIdentity({ findUserProfile: () => null }),
|
|
});
|
|
expect(await getStrangerInfo(bridge, 99999)).toBeNull();
|
|
});
|
|
});
|