Merge pull request #52 from Stlara-F/dev
fix(request-actions): match group add/invite flags by UID and add regression tests
This commit is contained in:
2 files changed
+77
-1
No files matched your search
@@ -9,11 +9,20 @@ export async function handleGroupAddRequest(
|
||||
// flag format: "add:groupId:uid" or "invite:groupId:uid" (from event-converter)
|
||||
const parts = flag.split(':');
|
||||
if (parts.length < 3) throw new Error('invalid group request flag');
|
||||
const requestType = parts[0];
|
||||
const groupId = parseInt(parts[1], 10);
|
||||
const targetUid = parts.slice(2).join(':');
|
||||
if (!groupId) throw new Error('invalid group_id in flag');
|
||||
if (!targetUid) throw new Error('invalid request target in flag');
|
||||
|
||||
const requests = await bridge.fetchGroupRequests();
|
||||
const matching = requests.find(r => r.groupId === groupId);
|
||||
const matching = requests.find((r) => {
|
||||
if (r.groupId !== groupId) return false;
|
||||
if (requestType === 'add') return r.targetUid === targetUid;
|
||||
if (requestType === 'invite') return r.invitorUid === targetUid;
|
||||
return false;
|
||||
}) || requests.find(r => r.groupId === groupId);
|
||||
|
||||
if (!matching) {
|
||||
throw new Error('matching group request not found');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { handleGroupAddRequest } from '../../src/onebot/modules/request-actions';
|
||||
import type { BridgeInterface } from '../../src/bridge/bridge-interface';
|
||||
import type { GroupRequestInfo } from '../../src/bridge/qq-info';
|
||||
|
||||
function fakeBridge(overrides: Partial<BridgeInterface> = {}): BridgeInterface {
|
||||
return new Proxy(overrides as BridgeInterface, {
|
||||
get(target, prop) {
|
||||
if (prop in target) return (target as any)[prop];
|
||||
throw new Error(`fakeBridge: '${String(prop)}' was not stubbed`);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function fakeRequest(overrides: Partial<GroupRequestInfo> = {}): GroupRequestInfo {
|
||||
return {
|
||||
groupId: 999,
|
||||
groupName: 'g',
|
||||
targetUid: 'u_t',
|
||||
targetUin: 5555,
|
||||
targetName: 'target',
|
||||
invitorUid: 'u_i',
|
||||
invitorUin: 7777,
|
||||
invitorName: 'inviter',
|
||||
operatorUid: 'u_o',
|
||||
operatorUin: 8888,
|
||||
operatorName: 'op',
|
||||
sequence: 42,
|
||||
state: 1,
|
||||
eventType: 7,
|
||||
comment: 'pls',
|
||||
filtered: false,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe('onebot/modules/request-actions / handleGroupAddRequest', () => {
|
||||
it('matches add requests by groupId and targetUid', async () => {
|
||||
const setGroupAddRequest = vi.fn(async () => {});
|
||||
const bridge = fakeBridge({
|
||||
fetchGroupRequests: vi.fn(async () => [
|
||||
fakeRequest({ groupId: 999, targetUid: 'u_t', sequence: 42, eventType: 7, filtered: false }),
|
||||
]),
|
||||
setGroupAddRequest: setGroupAddRequest as any,
|
||||
});
|
||||
|
||||
await handleGroupAddRequest(bridge, 'add:999:u_t', true, 'ok');
|
||||
|
||||
expect(setGroupAddRequest).toHaveBeenCalledOnce();
|
||||
expect(setGroupAddRequest).toHaveBeenCalledWith(999, 42, 7, true, 'ok', false);
|
||||
});
|
||||
|
||||
it('matches invite requests by groupId and invitorUid', async () => {
|
||||
const setGroupAddRequest = vi.fn(async () => {});
|
||||
const bridge = fakeBridge({
|
||||
fetchGroupRequests: vi.fn(async () => [
|
||||
fakeRequest({ groupId: 999, invitorUid: 'u_i', sequence: 97, eventType: 8, filtered: false }),
|
||||
]),
|
||||
setGroupAddRequest: setGroupAddRequest as any,
|
||||
});
|
||||
|
||||
await handleGroupAddRequest(bridge, 'invite:999:u_i', false, 'no');
|
||||
|
||||
expect(setGroupAddRequest).toHaveBeenCalledOnce();
|
||||
expect(setGroupAddRequest).toHaveBeenCalledWith(999, 97, 8, false, 'no', false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user