fix(protocol): treat a successful group kick as success
QQ reports a per-member kick outcome in the command body. A zero outcome means the member was removed; treat that as success instead of a false failure. Still reject a non-zero outcome so callers do not see success when the kick did not take effect. Fixes #413
This commit is contained in:
6 files changed
+144
-24
No files matched your search
@@ -5,9 +5,22 @@ import type { OidbKickMember } from '@snowluma/proto-defs/oidb-actions/base';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
|
||||
import { KickMember } from '../../../src/oidb-services/group-admin/kick-member';
|
||||
import { m, s, v } from '../_pb-oracle';
|
||||
|
||||
function makeDeps() {
|
||||
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: Buffer.alloc(0) };
|
||||
/** 24-byte uid: success items on the wire are `{ result:0, uid }` with uid length 0x18. */
|
||||
const TRACE_UID = 'u_abcdefghijklmnopqrstuv';
|
||||
|
||||
function kickResultItem(result: number, uid: string): number[] {
|
||||
return [...v(1, result), ...s(2, uid)];
|
||||
}
|
||||
|
||||
function kickResponseBytes(groupUin: number, items: number[][]): Buffer {
|
||||
const body = [...v(1, groupUin), ...items.flatMap(item => m(2, item))];
|
||||
return Buffer.from(m(4, body));
|
||||
}
|
||||
|
||||
function makeDeps(responseData: Buffer = Buffer.alloc(0)) {
|
||||
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
|
||||
return {
|
||||
sendRawPacket: vi.fn(async () => r),
|
||||
resolveUserUid: vi.fn(async () => 'resolved-uid'),
|
||||
@@ -40,4 +53,42 @@ describe('KickMember namespace', () => {
|
||||
expect(env.body?.reason ?? '').toBe('');
|
||||
expect(env.body?.rejectAddRequest ?? false).toBe(false);
|
||||
});
|
||||
|
||||
it('treats an empty body as success', async () => {
|
||||
await expect(KickMember.invoke(makeDeps(), {
|
||||
groupId: 1, userId: 2, reject: false,
|
||||
})).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('treats a zero per-member result plus uid as success (#413)', async () => {
|
||||
const bytes = kickResponseBytes(12345, [kickResultItem(0, TRACE_UID)]);
|
||||
const decoded = KickMember.decode(bytes);
|
||||
expect(decoded.body?.groupUin).toBe(12345);
|
||||
expect(decoded.body?.results).toEqual([{ result: 0, uid: TRACE_UID }]);
|
||||
|
||||
await expect(KickMember.invoke(makeDeps(bytes), {
|
||||
groupId: 12345, userId: 67890, reject: false,
|
||||
})).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('rejects a non-zero per-member result with the numeric code, not the uid (#298)', async () => {
|
||||
const bytes = kickResponseBytes(12345, [kickResultItem(7, TRACE_UID)]);
|
||||
await expect(KickMember.invoke(makeDeps(bytes), {
|
||||
groupId: 12345, userId: 67890, reject: false,
|
||||
})).rejects.toThrow('kick member failed: result=7');
|
||||
});
|
||||
|
||||
it('deserialize: omitted/zero results succeed; non-zero throws', () => {
|
||||
expect(() => KickMember.deserialize({} as never, {})).not.toThrow();
|
||||
expect(() => KickMember.deserialize({} as never, { results: [] })).not.toThrow();
|
||||
expect(() => KickMember.deserialize({} as never, {
|
||||
results: [{ result: 0, uid: TRACE_UID }],
|
||||
})).not.toThrow();
|
||||
expect(() => KickMember.deserialize({} as never, {
|
||||
results: [{ uid: TRACE_UID }],
|
||||
})).not.toThrow();
|
||||
expect(() => KickMember.deserialize({} as never, {
|
||||
results: [{ result: 1, uid: TRACE_UID }],
|
||||
})).toThrow('kick member failed: result=1');
|
||||
});
|
||||
});
|
||||
@@ -5,9 +5,25 @@ import type { Oidb0x8a0Req } from '@snowluma/proto-defs/oidb-actions/base';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
|
||||
import { KickMembers } from '../../../src/oidb-services/group-admin/kick-members';
|
||||
import { m, s, v } from '../_pb-oracle';
|
||||
|
||||
function makeDeps(resolveSequence: string[] = ['uid-a', 'uid-b']) {
|
||||
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: Buffer.alloc(0) };
|
||||
const TRACE_UID_A = 'u_abcdefghijklmnopqrstuv';
|
||||
const TRACE_UID_B = 'u_bbbbbbbbbbbbbbbbbbbbbb';
|
||||
|
||||
function kickResultItem(result: number, uid: string): number[] {
|
||||
return [...v(1, result), ...s(2, uid)];
|
||||
}
|
||||
|
||||
function kickResponseBytes(groupUin: number, items: number[][]): Buffer {
|
||||
const body = [...v(1, groupUin), ...items.flatMap(item => m(2, item))];
|
||||
return Buffer.from(m(4, body));
|
||||
}
|
||||
|
||||
function makeDeps(
|
||||
resolveSequence: string[] = ['uid-a', 'uid-b'],
|
||||
responseData: Buffer = Buffer.alloc(0),
|
||||
) {
|
||||
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
|
||||
const resolveUserUid = vi.fn();
|
||||
for (const uid of resolveSequence) resolveUserUid.mockResolvedValueOnce(uid);
|
||||
return {
|
||||
@@ -41,4 +57,36 @@ describe('KickMembers namespace', () => {
|
||||
const env = protobuf_decode<OidbBase<Oidb0x8a0Req>>(bytes);
|
||||
expect(env.body?.rejectAddRequest).toBe(1);
|
||||
});
|
||||
|
||||
it('treats an empty body as success', async () => {
|
||||
await expect(KickMembers.invoke(makeDeps(['u']), {
|
||||
groupId: 1, userIds: [2], reject: false,
|
||||
})).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('treats zero per-member results as success (#413)', async () => {
|
||||
const bytes = kickResponseBytes(12345, [
|
||||
kickResultItem(0, TRACE_UID_A),
|
||||
kickResultItem(0, TRACE_UID_B),
|
||||
]);
|
||||
const decoded = KickMembers.decode(bytes);
|
||||
expect(decoded.body?.results).toEqual([
|
||||
{ result: 0, uid: TRACE_UID_A },
|
||||
{ result: 0, uid: TRACE_UID_B },
|
||||
]);
|
||||
|
||||
await expect(KickMembers.invoke(makeDeps(['uid-a', 'uid-b'], bytes), {
|
||||
groupId: 12345, userIds: [11, 22], reject: false,
|
||||
})).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('rejects when any per-member result is non-zero, with the numeric code', async () => {
|
||||
const bytes = kickResponseBytes(12345, [
|
||||
kickResultItem(0, TRACE_UID_A),
|
||||
kickResultItem(7, TRACE_UID_B),
|
||||
]);
|
||||
await expect(KickMembers.invoke(makeDeps(['uid-a', 'uid-b'], bytes), {
|
||||
groupId: 12345, userIds: [11, 22], reject: false,
|
||||
})).rejects.toThrow('kick members failed: result=7');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user