refactor(oidb): replace 3 positional helpers with one named runOidb
bridge-oidb.ts used to expose three thin functions —
makeOidbRequest / sendOidbAndCheck / sendOidbAndDecode — and every
action file repeated the same five-step envelope assembly with long
positional argument lists. Collapse them into one `runOidb(bridge,
call)` taking an OidbCall object: cmd / oidbCmd / subCmd / request
{ schema, value, isUid? } / response? { schema }.
Behavioural contract is preserved:
- No `response`: verify success + gotResponse, peek envelope retCode
iff response data is present (fire-and-check semantics).
- With `response`: require responseData, decode through the base
schema, validate retCode, return the inner body typed as TResp.
Migration touches 51 call sites across 9 source files (bridge-contacts,
actions/{group-file, group-admin, profile, group-message, friend,
interaction, misc}, web-actions/cookies) plus 9 test files that used
to assert on positional `.mock.calls[0]![4]` indices and now read
through `.mock.calls[0]![1].request.value` / `.cmd` / `.oidbCmd`.
Net effect: callers now name what each piece is for, and adding
cross-cutting OIDB behaviour (retry / timeout / per-cmd logging) is a
single-site change inside runOidb instead of a sweep across 9 files.
This commit is contained in:
19 files changed
+637
-549
No files matched your search
@@ -1,9 +1,7 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
||||
sendOidbAndCheck: vi.fn(async () => undefined),
|
||||
sendOidbAndDecode: vi.fn(async () => ({})),
|
||||
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
||||
runOidb: vi.fn(async () => ({})),
|
||||
}));
|
||||
|
||||
import * as oidb from '../../src/bridge/bridge-oidb';
|
||||
@@ -12,43 +10,43 @@ import { mockBridge } from './_helpers';
|
||||
|
||||
describe('actions/interaction', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(oidb.sendOidbAndCheck).mockClear();
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockClear();
|
||||
vi.mocked(oidb.runOidb).mockReset();
|
||||
vi.mocked(oidb.runOidb).mockResolvedValue({});
|
||||
});
|
||||
|
||||
it('sendPoke group: groupUin set, friendUin=0', async () => {
|
||||
const bridge = mockBridge();
|
||||
await inter.sendPoke(bridge as any, true, 12345, 67890);
|
||||
const payload: any = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4];
|
||||
expect(payload).toMatchObject({ uin: 67890, groupUin: 12345, friendUin: 0 });
|
||||
const call = vi.mocked(oidb.runOidb).mock.calls[0]![1];
|
||||
expect(call.request.value).toMatchObject({ uin: 67890, groupUin: 12345, friendUin: 0 });
|
||||
});
|
||||
|
||||
it('sendPoke friend: friendUin set, groupUin=0, targetUin defaults to peer', async () => {
|
||||
const bridge = mockBridge();
|
||||
await inter.sendPoke(bridge as any, false, 67890);
|
||||
const payload: any = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4];
|
||||
expect(payload).toMatchObject({ uin: 67890, groupUin: 0, friendUin: 67890 });
|
||||
const call = vi.mocked(oidb.runOidb).mock.calls[0]![1];
|
||||
expect(call.request.value).toMatchObject({ uin: 67890, groupUin: 0, friendUin: 67890 });
|
||||
});
|
||||
|
||||
it('sendLike forwards target + count to 0x7e5_104', async () => {
|
||||
const bridge = mockBridge();
|
||||
await inter.sendLike(bridge as any, 10001, 3);
|
||||
const call = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]!;
|
||||
expect(call[1]).toBe('OidbSvcTrpcTcp.0x7e5_104');
|
||||
expect(call[4]).toMatchObject({ targetUin: 10001, count: 3 });
|
||||
const call = vi.mocked(oidb.runOidb).mock.calls[0]![1];
|
||||
expect(call.cmd).toBe('OidbSvcTrpcTcp.0x7e5_104');
|
||||
expect(call.request.value).toMatchObject({ targetUin: 10001, count: 3 });
|
||||
});
|
||||
|
||||
it('setGroupReaction picks _1 for set and _2 for unset', async () => {
|
||||
const bridge = mockBridge();
|
||||
await inter.setGroupReaction(bridge as any, 12345, 99, '128516', true);
|
||||
await inter.setGroupReaction(bridge as any, 12345, 99, '128516', false);
|
||||
const cmds = vi.mocked(oidb.sendOidbAndCheck).mock.calls.map(c => c[1]);
|
||||
const cmds = vi.mocked(oidb.runOidb).mock.calls.map(c => c[1].cmd);
|
||||
expect(cmds).toEqual(['OidbSvcTrpcTcp.0x9082_1', 'OidbSvcTrpcTcp.0x9082_2']);
|
||||
});
|
||||
|
||||
it('getEmojiLikes decodes user list, base64-encodes cookie, and reports isLast', async () => {
|
||||
const bridge = mockBridge();
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce({
|
||||
inner: { userInfo: { uin: 10001 } },
|
||||
cookie: new Uint8Array([0xCA, 0xFE]),
|
||||
});
|
||||
@@ -60,7 +58,7 @@ describe('actions/interaction', () => {
|
||||
|
||||
it('getEmojiLikes reports isLast=true when no cookie comes back', async () => {
|
||||
const bridge = mockBridge();
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({});
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce({});
|
||||
const out = await inter.getEmojiLikes(bridge as any, 12345, 99, '128516');
|
||||
expect(out.users).toEqual([]);
|
||||
expect(out.isLast).toBe(true);
|
||||
|
||||
Reference in new issue
Block a user