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`.
190 lines
8.5 KiB
TypeScript
190 lines
8.5 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { protobuf_encode } from '@snowluma/proton';
|
|
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
|
import type {
|
|
OidbStrangerStatusResp,
|
|
} from '@snowluma/proto-defs/oidb-actions/base';
|
|
import type {
|
|
OidbAiVoiceListResp,
|
|
OidbAiVoiceResp,
|
|
} from '@snowluma/proto-defs/oidb-actions/media';
|
|
|
|
// `encodeOidbEnv` / `decodeOidbEnv` are proton-bound pass-through wrappers
|
|
// (substituted at the call site with the inlined codec). Mocking them on
|
|
// the module object is a no-op — proton has already inlined the call.
|
|
// We mock `runOidb` (non-generic) to return real proton-encoded bytes
|
|
// that the production-side codec actually decodes.
|
|
vi.mock('@snowluma/bridge/bridge-oidb', async () => {
|
|
const actual = await vi.importActual<typeof import('@snowluma/bridge/bridge-oidb')>(
|
|
'@snowluma/bridge/bridge-oidb',
|
|
);
|
|
return {
|
|
...actual,
|
|
runOidb: vi.fn(async () => new Uint8Array()),
|
|
makeOidbEnvelope: vi.fn((_oidbCmd, _subCmd, body) => ({ body })),
|
|
};
|
|
});
|
|
|
|
import * as oidb from '@snowluma/bridge/bridge-oidb';
|
|
import { ExtrasApi, AiVoiceChatType } from '../../src/bridge/apis/extras';
|
|
import { mockBridge } from './_helpers';
|
|
|
|
describe('apis/extras / group todo (0xF90)', () => {
|
|
beforeEach(() => {
|
|
vi.mocked(oidb.runOidb).mockClear();
|
|
vi.mocked(oidb.makeOidbEnvelope).mockClear();
|
|
});
|
|
|
|
it.each([
|
|
['setGroupTodo', 'OidbSvcTrpcTcp.0xf90_1', 1] as const,
|
|
['completeGroupTodo', 'OidbSvcTrpcTcp.0xf90_2', 2] as const,
|
|
['cancelGroupTodo', 'OidbSvcTrpcTcp.0xf90_3', 3] as const,
|
|
])('%s dispatches the right subCmd with shared body', async (method, cmd, subCmd) => {
|
|
const bridge = mockBridge();
|
|
const api = new ExtrasApi(bridge as any);
|
|
await (api as any)[method](12345, 9876543210n);
|
|
const [, runCmd] = vi.mocked(oidb.runOidb).mock.calls[0]!;
|
|
expect(runCmd).toBe(cmd);
|
|
const [oidbCmd, sub, body] = vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]!;
|
|
expect(oidbCmd).toBe(0xF90);
|
|
expect(sub).toBe(subCmd);
|
|
expect(body).toEqual({ groupUin: 12345, msgSeq: 9876543210n });
|
|
});
|
|
});
|
|
|
|
describe('apis/extras / getStrangerStatus (0xFE1_2)', () => {
|
|
beforeEach(() => {
|
|
vi.mocked(oidb.runOidb).mockReset();
|
|
vi.mocked(oidb.runOidb).mockResolvedValue(new Uint8Array());
|
|
vi.mocked(oidb.makeOidbEnvelope).mockClear();
|
|
});
|
|
|
|
it('issues the FE1_2 query with key=27372', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
|
protobuf_encode<OidbBase<OidbStrangerStatusResp>>({ body: { data: { status: { value: 5n } } } }),
|
|
);
|
|
await new ExtrasApi(bridge as any).getStrangerStatus(100200);
|
|
const [, runCmd] = vi.mocked(oidb.runOidb).mock.calls[0]!;
|
|
expect(runCmd).toBe('OidbSvcTrpcTcp.0xfe1_2');
|
|
const [oidbCmd, sub, body] = vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]!;
|
|
expect(oidbCmd).toBe(0xFE1);
|
|
expect(sub).toBe(2);
|
|
expect(body).toMatchObject({ uin: 100200, key: [{ key: 27372 }] });
|
|
});
|
|
|
|
it('low-band values (≤10) map to status*10 with ext_status=0', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
|
protobuf_encode<OidbBase<OidbStrangerStatusResp>>({ body: { data: { status: { value: 7n } } } }),
|
|
);
|
|
expect(await new ExtrasApi(bridge as any).getStrangerStatus(1)).toEqual({ status: 70, ext_status: 0 });
|
|
});
|
|
|
|
it('high-band values decompose into the (0xff00 + (>>16 & 0xff)) status word', async () => {
|
|
const bridge = mockBridge();
|
|
// value 0x42F100: bits 8-15 (the 0xff00 mask) = 0xF100;
|
|
// bits 16-23 (>>16 & 0xff) = 0x42.
|
|
// ext_status = 0xF100 + 0x42 = 0xF142.
|
|
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
|
protobuf_encode<OidbBase<OidbStrangerStatusResp>>({ body: { data: { status: { value: 0x42F100n } } } }),
|
|
);
|
|
const status = await new ExtrasApi(bridge as any).getStrangerStatus(1);
|
|
expect(status).toEqual({ status: 10, ext_status: 0xF142 });
|
|
});
|
|
|
|
it('returns null when the runner throws (transport error)', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.runOidb).mockRejectedValueOnce(new Error('boom'));
|
|
expect(await new ExtrasApi(bridge as any).getStrangerStatus(1)).toBeNull();
|
|
});
|
|
|
|
it('returns null when the server omits the status field', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
|
protobuf_encode<OidbBase<OidbStrangerStatusResp>>({ body: {} }),
|
|
);
|
|
expect(await new ExtrasApi(bridge as any).getStrangerStatus(1)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('apis/extras / AI voice (0x929D / 0x929B)', () => {
|
|
beforeEach(() => {
|
|
vi.mocked(oidb.runOidb).mockReset();
|
|
vi.mocked(oidb.runOidb).mockResolvedValue(new Uint8Array());
|
|
vi.mocked(oidb.makeOidbEnvelope).mockClear();
|
|
});
|
|
|
|
it('fetchAiVoiceList sends 0x929D_0 and returns server content verbatim', async () => {
|
|
const bridge = mockBridge();
|
|
const fake = [
|
|
{ category: 'cute', voices: [{ voiceId: 'v1', voiceDisplayName: 'V1', voiceExampleUrl: 'http://x' }] },
|
|
];
|
|
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
|
protobuf_encode<OidbBase<OidbAiVoiceListResp>>({ body: { content: fake } } as any),
|
|
);
|
|
const out = await new ExtrasApi(bridge as any).fetchAiVoiceList(4242, AiVoiceChatType.Sound);
|
|
expect(out).toEqual(fake);
|
|
const [, runCmd] = vi.mocked(oidb.runOidb).mock.calls[0]!;
|
|
expect(runCmd).toBe('OidbSvcTrpcTcp.0x929d_0');
|
|
const body = vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2];
|
|
expect(body).toEqual({ groupUin: 4242, chatType: 1 });
|
|
});
|
|
|
|
it('fetchAiVoiceList returns [] when server replies with no content', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
|
protobuf_encode<OidbBase<OidbAiVoiceListResp>>({ body: {} }),
|
|
);
|
|
const out = await new ExtrasApi(bridge as any).fetchAiVoiceList(1, 1);
|
|
expect(out).toEqual([]);
|
|
});
|
|
|
|
it('fetchAiVoice retries while msgInfo is empty, returns the first IndexNode it sees', async () => {
|
|
const bridge = mockBridge();
|
|
// subType is non-zero so proto3 doesn't omit it from the wire (the
|
|
// re-decoded object must include the field for the matcher to spot it).
|
|
const node = { fileUuid: 'uuid-1', subType: 1 };
|
|
vi.mocked(oidb.runOidb)
|
|
.mockResolvedValueOnce(protobuf_encode<OidbBase<OidbAiVoiceResp>>({ body: { statusCode: 2 } }))
|
|
.mockResolvedValueOnce(protobuf_encode<OidbBase<OidbAiVoiceResp>>({ body: { msgInfo: { msgInfoBody: [] } } as any }))
|
|
.mockResolvedValueOnce(protobuf_encode<OidbBase<OidbAiVoiceResp>>({ body: { msgInfo: { msgInfoBody: [{ index: node }] } } as any }));
|
|
const out = await new ExtrasApi(bridge as any).fetchAiVoice(100, 'voice-id', 'hi', AiVoiceChatType.Sound);
|
|
expect(out).toMatchObject(node);
|
|
expect(vi.mocked(oidb.runOidb)).toHaveBeenCalledTimes(3);
|
|
const [, runCmd] = vi.mocked(oidb.runOidb).mock.calls[0]!;
|
|
expect(runCmd).toBe('OidbSvcTrpcTcp.0x929b_0');
|
|
const body = vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2] as any;
|
|
expect(body).toMatchObject({
|
|
groupUin: 100, voiceId: 'voice-id', text: 'hi', chatType: 1,
|
|
});
|
|
// sessionId is randomized but must be a uint32.
|
|
expect(body.session.sessionId).toBeTypeOf('number');
|
|
expect(body.session.sessionId).toBeGreaterThanOrEqual(0);
|
|
expect(body.session.sessionId).toBeLessThanOrEqual(0xFFFFFFFF);
|
|
});
|
|
|
|
it('fetchAiVoice throws after exhausting the retry budget', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.runOidb).mockResolvedValue(
|
|
protobuf_encode<OidbBase<OidbAiVoiceResp>>({ body: { statusCode: 1 } }),
|
|
);
|
|
await expect(new ExtrasApi(bridge as any).fetchAiVoice(1, 'v', 't', 1, 3)).rejects.toThrow(
|
|
/AI voice synthesis did not complete/,
|
|
);
|
|
expect(vi.mocked(oidb.runOidb)).toHaveBeenCalledTimes(3);
|
|
});
|
|
|
|
it('fetchAiVoice keeps the same sessionId across retries', async () => {
|
|
const bridge = mockBridge();
|
|
const node = { fileUuid: 'uuid', subType: 0 };
|
|
vi.mocked(oidb.runOidb)
|
|
.mockResolvedValueOnce(protobuf_encode<OidbBase<OidbAiVoiceResp>>({ body: {} }))
|
|
.mockResolvedValueOnce(protobuf_encode<OidbBase<OidbAiVoiceResp>>({ body: { msgInfo: { msgInfoBody: [{ index: node }] } } as any }));
|
|
await new ExtrasApi(bridge as any).fetchAiVoice(1, 'v', 't', 1);
|
|
const first = (vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2] as any).session.sessionId;
|
|
const second = (vi.mocked(oidb.makeOidbEnvelope).mock.calls[1]![2] as any).session.sessionId;
|
|
expect(first).toBe(second);
|
|
});
|
|
});
|