refactor(oidb): migrate group-admin.ts OIDB cmds to namespaces
Splits the 14 OIDB cmds from GroupAdminApi into self-contained
namespaces under @snowluma/bridge/oidb-services/group-admin/. The
facade becomes one-line forwarders; per-cmd capability is narrowed
to OidbSender (+ resolveUserUid / identity where the cmd actually
needs it).
Notes:
- 0x89A_0 hosts MuteAll, SetAddOption, SetSearch, SetGroupName —
same (cmd, subcmd) tuple, four different proto bodies; each gets
its own namespace so proton can monomorphize independently.
- 0x8A0_1 splits KickMember (single) and KickMembers (batch) for
the same reason.
- SetAddRequest carries uinForm=true and dynamic subCommand
(filtered ? 2 : 1) on the same namespace.
- GetAtAllRemain needs the bot's own uin → declares
Pick<BridgeContext, 'identity'> in its Deps.
Wire-byte equivalence preserved; corner case for getAtAllRemain
empty-envelope path now returns {false, 0, 0} instead of throwing —
the new infrastructure normalises empty bodies and the OneBot action
ergonomics are unchanged.
This commit is contained in:
31 files changed
+1248
-202
No files matched your search
@@ -1,72 +1,73 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { protobuf_encode } from '@snowluma/proton';
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type { Oidb0x8a7Resp } from '@snowluma/proto-defs/oidb-actions/base';
|
||||
import type {
|
||||
Oidb0x8a0Req,
|
||||
Oidb0x8a7Resp,
|
||||
Oidb0x89a_0AddOption,
|
||||
Oidb0x89a_0Search,
|
||||
Oidb0xf16Req,
|
||||
OidbGroupRequestAction,
|
||||
OidbKickMember,
|
||||
OidbLeaveGroup,
|
||||
OidbMuteAll,
|
||||
OidbMuteMember,
|
||||
OidbRenameGroup,
|
||||
OidbRenameMember,
|
||||
OidbSetAdmin,
|
||||
OidbSpecialTitle,
|
||||
} from '@snowluma/proto-defs/oidb-actions/base';
|
||||
|
||||
// `encodeOidbEnv` / `decodeOidbEnv` are proton-bound pass-through wrappers
|
||||
// that the plugin substitutes at the call site with the inlined codec, so
|
||||
// mocking them on the module object is a no-op. The only mockable point
|
||||
// is `runOidb` (non-generic, untouched by proton) returning real bytes
|
||||
// that the production-side codec then decodes. `makeOidbEnvelope` is a
|
||||
// pure TS helper, so its mock works for introspection.
|
||||
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';
|
||||
// Post-namespace migration: GroupAdminApi forwards through namespaces
|
||||
// under @snowluma/bridge/oidb-services/group-admin. Tests assert
|
||||
// against bridge.sendRawPacket directly — no module-level mocks.
|
||||
import { GroupAdminApi } from '../../src/bridge/apis/group-admin';
|
||||
import { mockBridge } from './_helpers';
|
||||
|
||||
describe('actions/group-admin', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(oidb.runOidb).mockReset();
|
||||
vi.mocked(oidb.runOidb).mockResolvedValue(new Uint8Array());
|
||||
vi.mocked(oidb.makeOidbEnvelope).mockClear();
|
||||
});
|
||||
function packResponse(body: Uint8Array) {
|
||||
return {
|
||||
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
|
||||
responseData: Buffer.from(body),
|
||||
};
|
||||
}
|
||||
|
||||
it('muteGroupMember resolves UID and dispatches 0x1253_1', async () => {
|
||||
describe('apis/group-admin', () => {
|
||||
it('muteMember resolves UID and dispatches 0x1253_1', async () => {
|
||||
const bridge = mockBridge();
|
||||
await new GroupAdminApi(bridge as any).muteMember(12345, 67890, 600);
|
||||
expect(bridge.resolveUserUid).toHaveBeenCalledWith(67890, 12345);
|
||||
expect(oidb.runOidb).toHaveBeenCalledOnce();
|
||||
const [, cmd] = vi.mocked(oidb.runOidb).mock.calls[0]!;
|
||||
const [cmd, bytes] = bridge.sendRawPacket.mock.calls[0]!;
|
||||
expect(cmd).toBe('OidbSvcTrpcTcp.0x1253_1');
|
||||
const [oidbCmd, subCmd, body] = vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]!;
|
||||
expect(oidbCmd).toBe(0x1253);
|
||||
expect(subCmd).toBe(1);
|
||||
expect(body).toMatchObject({
|
||||
const env = protobuf_decode<OidbBase<OidbMuteMember>>(bytes);
|
||||
expect(env.command).toBe(0x1253);
|
||||
expect(env.subCommand).toBe(1);
|
||||
expect(env.body).toMatchObject({
|
||||
groupUin: 12345,
|
||||
type: 1,
|
||||
body: { targetUid: 'resolved-uid', duration: 600 },
|
||||
});
|
||||
});
|
||||
|
||||
it('muteGroupAll flips the muteState flag based on enable', async () => {
|
||||
it('muteAll flips the muteState flag based on enable', async () => {
|
||||
const bridge = mockBridge();
|
||||
await new GroupAdminApi(bridge as any).muteAll(12345, true);
|
||||
expect(vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2]).toMatchObject({
|
||||
groupUin: 12345,
|
||||
muteState: { state: 0xFFFFFFFF },
|
||||
});
|
||||
const env1 = protobuf_decode<OidbBase<OidbMuteAll>>(bridge.sendRawPacket.mock.calls[0]![1]);
|
||||
expect(env1.body).toMatchObject({ groupUin: 12345, muteState: { state: 0xFFFFFFFF } });
|
||||
|
||||
await new GroupAdminApi(bridge as any).muteAll(12345, false);
|
||||
expect(vi.mocked(oidb.makeOidbEnvelope).mock.calls[1]![2]).toMatchObject({
|
||||
muteState: { state: 0 },
|
||||
});
|
||||
const env2 = protobuf_decode<OidbBase<OidbMuteAll>>(bridge.sendRawPacket.mock.calls[1]![1]);
|
||||
// proto3 default 0 fields are omitted on the wire — equivalent to "state=0".
|
||||
expect(env2.body?.muteState?.state ?? 0).toBe(0);
|
||||
});
|
||||
|
||||
it('kickGroupMember resolves UID per-group and forwards reject + reason', async () => {
|
||||
it('kickMember resolves UID per-group and forwards reject + reason', async () => {
|
||||
const bridge = mockBridge();
|
||||
await new GroupAdminApi(bridge as any).kickMember(12345, 67890, true, 'bad behaviour');
|
||||
expect(bridge.resolveUserUid).toHaveBeenCalledWith(67890, 12345);
|
||||
expect(vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2]).toMatchObject({
|
||||
const [cmd, bytes] = bridge.sendRawPacket.mock.calls[0]!;
|
||||
expect(cmd).toBe('OidbSvcTrpcTcp.0x8a0_1');
|
||||
const env = protobuf_decode<OidbBase<OidbKickMember>>(bytes);
|
||||
expect(env.body).toMatchObject({
|
||||
groupUin: 12345,
|
||||
targetUid: 'resolved-uid',
|
||||
rejectAddRequest: true,
|
||||
@@ -74,7 +75,7 @@ describe('actions/group-admin', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('kickGroupMembers resolves each UID in parallel', async () => {
|
||||
it('kickMembers resolves each UID in parallel', async () => {
|
||||
const bridge = mockBridge();
|
||||
vi.mocked(bridge.resolveUserUid)
|
||||
.mockResolvedValueOnce('uid-a')
|
||||
@@ -82,28 +83,30 @@ describe('actions/group-admin', () => {
|
||||
|
||||
await new GroupAdminApi(bridge as any).kickMembers(12345, [11, 22], false);
|
||||
expect(bridge.resolveUserUid).toHaveBeenCalledTimes(2);
|
||||
const payload: any = vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2];
|
||||
expect(payload.targetUids).toEqual(['uid-a', 'uid-b']);
|
||||
expect(payload.rejectAddRequest).toBe(0);
|
||||
const env = protobuf_decode<OidbBase<Oidb0x8a0Req>>(bridge.sendRawPacket.mock.calls[0]![1]);
|
||||
expect(env.body?.targetUids).toEqual(['uid-a', 'uid-b']);
|
||||
expect(env.body?.rejectAddRequest ?? 0).toBe(0);
|
||||
});
|
||||
|
||||
it('leaveGroup sends 0x1097_1 with the groupUin', async () => {
|
||||
it('leave sends 0x1097_1 with the groupUin', async () => {
|
||||
const bridge = mockBridge();
|
||||
await new GroupAdminApi(bridge as any).leave(12345);
|
||||
const [, cmd] = vi.mocked(oidb.runOidb).mock.calls[0]!;
|
||||
const [cmd, bytes] = bridge.sendRawPacket.mock.calls[0]!;
|
||||
expect(cmd).toBe('OidbSvcTrpcTcp.0x1097_1');
|
||||
expect(vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2]).toMatchObject({ groupUin: 12345 });
|
||||
const env = protobuf_decode<OidbBase<OidbLeaveGroup>>(bytes);
|
||||
expect(env.body).toMatchObject({ groupUin: 12345 });
|
||||
});
|
||||
|
||||
it('setGroupAdmin resolves UID and sends 0x1096_1', async () => {
|
||||
it('setAdmin resolves UID and sends 0x1096_1', async () => {
|
||||
const bridge = mockBridge();
|
||||
await new GroupAdminApi(bridge as any).setAdmin(12345, 67890, true);
|
||||
expect(vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2]).toMatchObject({
|
||||
groupUin: 12345, uid: 'resolved-uid', isAdmin: true,
|
||||
});
|
||||
const [cmd, bytes] = bridge.sendRawPacket.mock.calls[0]!;
|
||||
expect(cmd).toBe('OidbSvcTrpcTcp.0x1096_1');
|
||||
const env = protobuf_decode<OidbBase<OidbSetAdmin>>(bytes);
|
||||
expect(env.body).toMatchObject({ groupUin: 12345, uid: 'resolved-uid', isAdmin: true });
|
||||
});
|
||||
|
||||
it('setGroupCard / setGroupName / setGroupSpecialTitle / setGroupRemark / setGroupAddOption / setGroupSearch dispatch the right command', async () => {
|
||||
it('setCard / setName / setSpecialTitle / setRemark / setAddOption / setSearch dispatch the right command', async () => {
|
||||
const bridge = mockBridge();
|
||||
await new GroupAdminApi(bridge as any).setCard(1, 2, 'newCard');
|
||||
await new GroupAdminApi(bridge as any).setName(1, 'newName');
|
||||
@@ -112,7 +115,7 @@ describe('actions/group-admin', () => {
|
||||
await new GroupAdminApi(bridge as any).setAddOption(1, 2);
|
||||
await new GroupAdminApi(bridge as any).setSearch(1);
|
||||
|
||||
const cmds = vi.mocked(oidb.runOidb).mock.calls.map(call => call[1]);
|
||||
const cmds = bridge.sendRawPacket.mock.calls.map(call => call[0]);
|
||||
expect(cmds).toEqual([
|
||||
'OidbSvcTrpcTcp.0x8fc_3',
|
||||
'OidbSvcTrpcTcp.0x89a_15',
|
||||
@@ -121,34 +124,58 @@ describe('actions/group-admin', () => {
|
||||
'OidbSvcTrpcTcp.0x89a_0',
|
||||
'OidbSvcTrpcTcp.0x89a_0',
|
||||
]);
|
||||
|
||||
const cardEnv = protobuf_decode<OidbBase<OidbRenameMember>>(bridge.sendRawPacket.mock.calls[0]![1]);
|
||||
expect(cardEnv.body).toMatchObject({ groupUin: 1, body: { targetUid: 'resolved-uid', targetName: 'newCard' } });
|
||||
|
||||
const nameEnv = protobuf_decode<OidbBase<OidbRenameGroup>>(bridge.sendRawPacket.mock.calls[1]![1]);
|
||||
expect(nameEnv.body).toMatchObject({ groupUin: 1, body: { targetName: 'newName' } });
|
||||
|
||||
const titleEnv = protobuf_decode<OidbBase<OidbSpecialTitle>>(bridge.sendRawPacket.mock.calls[2]![1]);
|
||||
// expireTime is proto int_32 with -1 sentinel; after wire round-trip the
|
||||
// proton decoder surfaces it as the unsigned reinterpretation (-1 ≡ 0xFFFFFFFF).
|
||||
expect(titleEnv.body?.groupUin).toBe(1);
|
||||
expect(titleEnv.body?.body?.targetUid).toBe('resolved-uid');
|
||||
expect(titleEnv.body?.body?.specialTitle).toBe('newTitle');
|
||||
expect(titleEnv.body?.body?.expireTime).toBe(0xFFFFFFFF);
|
||||
|
||||
const remarkEnv = protobuf_decode<OidbBase<Oidb0xf16Req>>(bridge.sendRawPacket.mock.calls[3]![1]);
|
||||
expect(remarkEnv.body?.inner).toMatchObject({ groupId: 1n, remark: 'newRemark' });
|
||||
|
||||
const addOptEnv = protobuf_decode<OidbBase<Oidb0x89a_0AddOption>>(bridge.sendRawPacket.mock.calls[4]![1]);
|
||||
expect(addOptEnv.body).toMatchObject({ groupUin: 1n, settings: { addType: 2 } });
|
||||
|
||||
const searchEnv = protobuf_decode<OidbBase<Oidb0x89a_0Search>>(bridge.sendRawPacket.mock.calls[5]![1]);
|
||||
expect(searchEnv.body?.groupUin).toBe(1n);
|
||||
});
|
||||
|
||||
it('setGroupAddRequest picks _1 / _2 based on filtered flag', async () => {
|
||||
it('setAddRequest picks _1 / _2 based on filtered flag', async () => {
|
||||
const bridge = mockBridge();
|
||||
await new GroupAdminApi(bridge as any).setAddRequest(12345, 5, 1, true, 'ok', false);
|
||||
await new GroupAdminApi(bridge as any).setAddRequest(12345, 5, 1, false, 'no', true);
|
||||
|
||||
const cmds = vi.mocked(oidb.runOidb).mock.calls.map(call => call[1]);
|
||||
const cmds = bridge.sendRawPacket.mock.calls.map(call => call[0]);
|
||||
expect(cmds).toEqual([
|
||||
'OidbSvcTrpcTcp.0x10c8_1',
|
||||
'OidbSvcTrpcTcp.0x10c8_2',
|
||||
]);
|
||||
|
||||
const firstBody: any = vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2];
|
||||
expect(firstBody.accept).toBe(1);
|
||||
expect(firstBody.body.message).toBe('ok');
|
||||
const env1 = protobuf_decode<OidbBase<OidbGroupRequestAction>>(bridge.sendRawPacket.mock.calls[0]![1]);
|
||||
expect(env1.reserved).toBe(1); // uinForm
|
||||
expect(env1.body?.accept).toBe(1);
|
||||
expect(env1.body?.body?.message).toBe('ok');
|
||||
|
||||
const secondBody: any = vi.mocked(oidb.makeOidbEnvelope).mock.calls[1]![2];
|
||||
expect(secondBody.accept).toBe(2);
|
||||
const env2 = protobuf_decode<OidbBase<OidbGroupRequestAction>>(bridge.sendRawPacket.mock.calls[1]![1]);
|
||||
expect(env2.body?.accept).toBe(2);
|
||||
});
|
||||
|
||||
it('getGroupAtAllRemain decodes the response and converts BigInts', async () => {
|
||||
it('getAtAllRemain decodes the response and converts BigInts', async () => {
|
||||
const bridge = mockBridge();
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
bridge.sendRawPacket.mockResolvedValueOnce(packResponse(
|
||||
protobuf_encode<OidbBase<Oidb0x8a7Resp>>({
|
||||
body: { canAtAll: true, groupRemain: 12, uinRemain: 5 } as any,
|
||||
}),
|
||||
);
|
||||
));
|
||||
const out = await new GroupAdminApi(bridge as any).getAtAllRemain(12345);
|
||||
expect(out).toEqual({
|
||||
can_at_all: true,
|
||||
@@ -157,13 +184,19 @@ describe('actions/group-admin', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('getGroupAtAllRemain throws when the response is empty', async () => {
|
||||
it('getAtAllRemain falls back to zero / false when the response is empty', async () => {
|
||||
const bridge = mockBridge();
|
||||
// An empty OidbBase envelope (no body) — production code sees `body`
|
||||
// undefined and throws the "empty" error.
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
// Empty OidbBase envelope (no body) — invokeOidb substitutes `{}`
|
||||
// for the deserialize argument; canAtAll/groupRemain/uinRemain
|
||||
// become undefined and get coerced to false/0.
|
||||
bridge.sendRawPacket.mockResolvedValueOnce(packResponse(
|
||||
protobuf_encode<OidbBase<Oidb0x8a7Resp>>({}),
|
||||
);
|
||||
await expect(new GroupAdminApi(bridge as any).getAtAllRemain(12345)).rejects.toThrow(/empty/);
|
||||
));
|
||||
const out = await new GroupAdminApi(bridge as any).getAtAllRemain(12345);
|
||||
expect(out).toEqual({
|
||||
can_at_all: false,
|
||||
remain_at_all_count_for_group: 0,
|
||||
remain_at_all_count_for_uin: 0,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user