refactor(oidb): namespace-per-cmd service layer — vertical slice (3 cmds)
Introduce a new way to organise OIDB cmd implementations:
- one TypeScript namespace per (cmd, subcmd) tuple
- the namespace's exported members ARE the OidbCallSpec (structural
typing — no `implements` clause)
- a single invokeOidb() template method drives every call:
envelope build → encode → send → errorCode check → decode →
deserialize transformation
- each namespace declares its own minimal `Deps` capability via
Pick / intersection on BridgeContext (Interface Segregation):
most cmds need only OidbSender (the `sendRawPacket` method)
The facade Api classes (InteractionApi etc.) stay for now, with the
migrated methods reduced to one-line forwarders that hand off to the
namespace. Callers still write `bridge.apis.X.Y(...)`; the OIDB
wire-encoding work just moved to a self-contained file per cmd.
Vertical slice scope: 3 cmds (the reaction line)
- SetReaction (0x9082_1 / _2; dynamic subcmd)
- FetchReactionSummary (0x9084_1)
- GetEmojiLikes (0x9083_1; legacy stub)
Infrastructure:
- new packages/bridge/src/oidb-service.ts:
OidbSender (sendRawPacket-only capability),
OidbCallSpec<TReq, TResp, TParams, TResult>,
invokeOidb(), buildOidbRequest(), parseOidbResponse(), OidbError
- runOidb() refactored to accept OidbSender instead of BridgeContext
(structurally compatible — no caller changes anywhere)
- new OidbEmpty placeholder type in proto-defs/oidb.ts for cmds whose
response carries no body (proton needs a real message interface,
not `void` / `never`)
- empty-body responses substituted with {} in invokeOidb so
deserialize always receives a defined object
Tests (65 in @snowluma/bridge, all green):
- oidb-service.test.ts (20): every helper branch — static + dynamic
subcmd, uinForm flag, error code propagation as OidbError, empty
body substitution, sender failure modes (success=false /
gotResponse=false / both with and without errorMessage),
timeoutMs propagation
- set-reaction.test.ts: const declarations, dynamic subcmd both
ways, type=1/2 boundary on code length, e2e envelope inspection
- fetch-reaction-summary.test.ts: BigInt serialization, catalog-tail
filtering, default emojiId/Type/lastReactionTime
- get-emoji-likes.test.ts: BigInt serialization, default count /
emojiType, base64 cookie round-trip, uin=0 filtering, isLast
computation, e2e
Existing interaction.test.ts moved off the now-bypassed runOidb mock
onto sendRawPacket mock (the new invokeOidb path routes there).
Workspace-wide typecheck: green. Workspace test totals:
proton 99/99, bridge 65/65, core 302/302, onebot 257/257
(1 pre-existing onebot suite failure, network-manager native addon
load — unrelated to this change.)
This commit is contained in:
14 files changed
+1066
-122
No files matched your search
@@ -56,11 +56,13 @@ describe('apis/interaction', () => {
|
||||
});
|
||||
|
||||
it('setReaction picks _1 for set and _2 for unset', async () => {
|
||||
// setReaction now forwards through the SetReaction namespace, which
|
||||
// calls bridge.sendRawPacket directly (bypassing runOidb).
|
||||
const bridge = mockBridge();
|
||||
const api = new InteractionApi(bridge as any);
|
||||
await api.setReaction(12345, 99, '128516', true);
|
||||
await api.setReaction(12345, 99, '128516', false);
|
||||
const cmds = vi.mocked(oidb.runOidb).mock.calls.map(c => c[1]);
|
||||
const cmds = bridge.sendRawPacket.mock.calls.map((c: any[]) => c[0]);
|
||||
expect(cmds).toEqual(['OidbSvcTrpcTcp.0x9082_1', 'OidbSvcTrpcTcp.0x9082_2']);
|
||||
});
|
||||
|
||||
@@ -75,14 +77,15 @@ describe('apis/interaction', () => {
|
||||
|
||||
it('getEmojiLikes decodes user list, base64-encodes cookie, and reports isLast', async () => {
|
||||
const bridge = mockBridge();
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<Oidb0x9083Resp>>({
|
||||
bridge.sendRawPacket.mockResolvedValueOnce({
|
||||
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<OidbBase<Oidb0x9083Resp>>({
|
||||
body: {
|
||||
inner: { userInfo: [{ uin: 10001n }, { uin: 20002n }] },
|
||||
cookie: new Uint8Array([0xCA, 0xFE]),
|
||||
} as any,
|
||||
}),
|
||||
);
|
||||
})),
|
||||
});
|
||||
const out = await new InteractionApi(bridge as any).getEmojiLikes(12345, 99, '128516');
|
||||
expect(out.users).toEqual([{ uin: 10001 }, { uin: 20002 }]);
|
||||
expect(out.cookie).toBe(Buffer.from([0xCA, 0xFE]).toString('base64'));
|
||||
@@ -91,9 +94,10 @@ describe('apis/interaction', () => {
|
||||
|
||||
it('getEmojiLikes reports isLast=true when no cookie comes back', async () => {
|
||||
const bridge = mockBridge();
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<Oidb0x9083Resp>>({ body: {} }),
|
||||
);
|
||||
bridge.sendRawPacket.mockResolvedValueOnce({
|
||||
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<OidbBase<Oidb0x9083Resp>>({ body: {} })),
|
||||
});
|
||||
const out = await new InteractionApi(bridge as any).getEmojiLikes(12345, 99, '128516');
|
||||
expect(out.users).toEqual([]);
|
||||
expect(out.isLast).toBe(true);
|
||||
|
||||
Reference in new issue
Block a user