Files
SnowLuma/packages/onebot/tests/qzone-actions.test.ts
T
DrSmoothl 5be852f394 refactor(onebot): drop redundant per-action try/catch in qzone/album/admin
阶段 2a:删除 qzone(7)、group-album(8)、group-admin(1) 共 16 个「纯兜底再包一层」
的 try/catch,让抛错自然落到唯一 seam runAction(阶段 1 已统一为 ACTION_FAILED 100
+ error.message)。qzone/album 原返回 1200 → 归一 100(本任务有意);group-admin
原本就是 100,仅去冗余包装。

- 保留:qzone helper uploadQzoneImages 的带前缀 re-throw catch、group-info 的
  honor-info 前缀 catch、message 的吞值 catch(best-effort 保留旧 URL)——均未动。
- 清理三文件不再使用的 RETCODE/failedResponse import。
- qzone-actions 测试断言 1200 → 100(wording 前缀经 seam 仍保留)。
- onebot 全量 516 全绿、typecheck 干净;对抗性 Agent 逐个对比 HEAD 确认纯兜底、
  无其它漂移、无误删漏改。
2026-07-01 16:20:26 +08:00

66 lines
2.7 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import type { BridgeInterface } from '@snowluma/core/bridge-interface';
import { ApiHandler, type ApiActionContext } from '../src/api-handler';
function makeHandler(qzone: Record<string, unknown>): ApiHandler {
const bridge = { apis: { qzone } } as unknown as BridgeInterface;
const ctx = { bridge } as ApiActionContext;
return new ApiHandler(ctx);
}
describe('qzone actions', () => {
it('send_qzone_msg uploads images and publishes richvals joined with tab', async () => {
const uploadImageFromSource = vi.fn()
.mockResolvedValueOnce({ richval: 'RV1', url: 'URL1' })
.mockResolvedValueOnce({ richval: 'RV2', url: 'URL2' });
const publish = vi.fn().mockResolvedValue({ tid: 'T1', time: 1 });
const handler = makeHandler({ uploadImageFromSource, publish });
const res = await handler.handle('send_qzone_msg', {
content: 'hello',
images: ['file:///a.jpg', 'file:///b.jpg'],
});
expect(res).toMatchObject({ status: 'ok', retcode: 0, data: { tid: 'T1', time: 1 } });
expect(uploadImageFromSource).toHaveBeenNthCalledWith(1, 'file:///a.jpg');
expect(uploadImageFromSource).toHaveBeenNthCalledWith(2, 'file:///b.jpg');
expect(publish).toHaveBeenCalledWith('hello', 1, 'RV1\tRV2', 1, undefined);
});
it('comment_qzone uploads images and comments direct urls joined with tab', async () => {
const uploadImageFromSource = vi.fn()
.mockResolvedValueOnce({ richval: 'RV1', url: 'URL1' })
.mockResolvedValueOnce({ richval: 'RV2', url: 'URL2' });
const comment = vi.fn().mockResolvedValue({ comment_id: 'C1' });
const handler = makeHandler({ uploadImageFromSource, comment });
const res = await handler.handle('comment_qzone', {
tid: 'T1',
content: 'nice',
target_uin: 20002,
images: ['file:///a.jpg', 'file:///b.jpg'],
});
expect(res).toMatchObject({ status: 'ok', retcode: 0, data: { comment_id: 'C1' } });
expect(comment).toHaveBeenCalledWith('T1', 'nice', 20002, 1, 'URL1\tURL2');
});
it('returns a clear error and skips publish when one image upload fails', async () => {
const uploadImageFromSource = vi.fn()
.mockResolvedValueOnce({ richval: 'RV1', url: 'URL1' })
.mockRejectedValueOnce(new Error('network down'));
const publish = vi.fn();
const handler = makeHandler({ uploadImageFromSource, publish });
const res = await handler.handle('send_qzone_msg', {
content: 'hello',
images: ['file:///a.jpg', 'file:///b.jpg'],
});
expect(res).toMatchObject({ status: 'failed', retcode: 100 });
expect(res.wording).toContain('第 2 张图片上传失败: network down');
expect(publish).not.toHaveBeenCalled();
});
});