feat(core,onebot): implement trans_group_file action (#161)
This commit is contained in:
1 parent
2f374ea8c9
commit
9ca64bed10
11 files changed
+260
-10
No files matched your search
@@ -332,6 +332,10 @@
|
||||
"types": "./src/oidb-services/group-file/publish-group-file.ts",
|
||||
"default": "./src/oidb-services/group-file/publish-group-file.ts"
|
||||
},
|
||||
"./oidb-services/group-file/trans-group-file": {
|
||||
"types": "./src/oidb-services/group-file/trans-group-file.ts",
|
||||
"default": "./src/oidb-services/group-file/trans-group-file.ts"
|
||||
},
|
||||
"./oidb-services/group-file/get-group-file-count": {
|
||||
"types": "./src/oidb-services/group-file/get-group-file-count.ts",
|
||||
"default": "./src/oidb-services/group-file/get-group-file-count.ts"
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
// 0x6D9_0 — transfer a temporary group file into permanent group storage.
|
||||
// Mirrors LagrangeV2's TransFileReqBody shape under the 0x6D9 family.
|
||||
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
OidbGroupSendFileReq,
|
||||
OidbGroupSendFileResp,
|
||||
} from '@snowluma/proto-defs/oidb-actions/group-file';
|
||||
import { invokeOidb, type OidbSender } from '../../oidb-service';
|
||||
import { ensureRetCodeZero, toInt } from '../shared';
|
||||
|
||||
export interface TransGroupFileResult {
|
||||
saveBusId: number;
|
||||
saveFilePath: string;
|
||||
}
|
||||
|
||||
export namespace TransGroupFile {
|
||||
export const command = 0x6D9;
|
||||
export const subCommand = 0;
|
||||
|
||||
export interface Params { groupId: number; fileId: string; }
|
||||
export type Deps = OidbSender;
|
||||
|
||||
export const serialize = (_ctx: Deps, p: Params): OidbGroupSendFileReq => ({
|
||||
transFile: {
|
||||
groupUin: BigInt(p.groupId),
|
||||
appId: 7,
|
||||
busId: 102,
|
||||
fileId: p.fileId,
|
||||
},
|
||||
});
|
||||
|
||||
export const deserialize = (_ctx: Deps, body: OidbGroupSendFileResp): TransGroupFileResult => {
|
||||
const result = body.transFile;
|
||||
if (!result) throw new Error('group file transfer response missing');
|
||||
ensureRetCodeZero('group file transfer', result.retCode, result.retMsg, result.clientWording);
|
||||
return {
|
||||
saveBusId: toInt(result.saveBusId),
|
||||
saveFilePath: typeof result.saveFilePath === 'string' ? result.saveFilePath : '',
|
||||
};
|
||||
};
|
||||
|
||||
export const encode = (env: OidbBase<OidbGroupSendFileReq>): Uint8Array =>
|
||||
protobuf_encode<OidbBase<OidbGroupSendFileReq>>(env);
|
||||
|
||||
export const decode = (bytes: Uint8Array): OidbBase<OidbGroupSendFileResp> =>
|
||||
protobuf_decode<OidbBase<OidbGroupSendFileResp>>(bytes);
|
||||
|
||||
export const invoke = (deps: Deps, params: Params): Promise<TransGroupFileResult> =>
|
||||
invokeOidb(deps, TransGroupFile, params);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
OidbGroupSendFileReq, OidbGroupSendFileResp,
|
||||
} from '@snowluma/proto-defs/oidb-actions/group-file';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
|
||||
import { TransGroupFile } from '../../../src/oidb-services/group-file/trans-group-file';
|
||||
|
||||
function makeDeps(body?: OidbGroupSendFileResp) {
|
||||
const responseData = body !== undefined
|
||||
? Buffer.from(protobuf_encode<OidbBase<OidbGroupSendFileResp>>({ body }))
|
||||
: Buffer.alloc(0);
|
||||
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
|
||||
return { sendRawPacket: vi.fn(async () => r) };
|
||||
}
|
||||
|
||||
describe('TransGroupFile namespace', () => {
|
||||
it('declares 0x6D9_0', () => {
|
||||
expect(TransGroupFile.command).toBe(0x6D9);
|
||||
expect(TransGroupFile.subCommand).toBe(0);
|
||||
});
|
||||
|
||||
it('packages group + file id under the transFile slot', async () => {
|
||||
const deps = makeDeps({ transFile: { saveBusId: 102, saveFilePath: '/abc' } });
|
||||
const out = await TransGroupFile.invoke(deps, { groupId: 12345, fileId: 'fid' });
|
||||
expect(out).toEqual({ saveBusId: 102, saveFilePath: '/abc' });
|
||||
expect(deps.sendRawPacket).toHaveBeenCalledOnce();
|
||||
const [wire, bytes] = deps.sendRawPacket.mock.calls[0]!;
|
||||
expect(wire).toBe('OidbSvcTrpcTcp.0x6d9_0');
|
||||
const env = protobuf_decode<OidbBase<OidbGroupSendFileReq>>(bytes);
|
||||
expect(env.body?.transFile).toEqual({
|
||||
groupUin: 12345n,
|
||||
appId: 7,
|
||||
busId: 102,
|
||||
fileId: 'fid',
|
||||
});
|
||||
});
|
||||
|
||||
it('throws on missing transFile sub-message', async () => {
|
||||
const deps = makeDeps({});
|
||||
await expect(TransGroupFile.invoke(deps, { groupId: 1, fileId: 'f' }))
|
||||
.rejects.toThrow(/transfer response missing/);
|
||||
});
|
||||
|
||||
it('throws on non-zero retCode', async () => {
|
||||
const deps = makeDeps({ transFile: { retCode: 7, retMsg: 'denied' } });
|
||||
await expect(TransGroupFile.invoke(deps, { groupId: 1, fileId: 'f' }))
|
||||
.rejects.toThrow(/code=7/);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user