feat(onebot): add public Qzone feeds and interactions (#587)
Add public Qzone discovery, pagination, and interactions with SDK and MCP support.
This commit is contained in:
20 files changed
+4997
-6
No files matched your search
@@ -491,6 +491,20 @@ export const f = {
|
||||
{ type: 'bool', required: true, schema: { type: 'boolean' } },
|
||||
);
|
||||
},
|
||||
/** JSON object, preserving unknown fields without coercing scalars or arrays. */
|
||||
object(): Field<JsonObject> {
|
||||
return new FieldImpl<JsonObject>(
|
||||
(raw, field) =>
|
||||
typeof raw === 'object' && raw !== null && !Array.isArray(raw)
|
||||
? ok(raw)
|
||||
: err(field, 'expected an object'),
|
||||
{
|
||||
type: 'object',
|
||||
required: true,
|
||||
schema: { type: 'object', additionalProperties: true },
|
||||
},
|
||||
);
|
||||
},
|
||||
/** OneBot message union: string | segment[] | object. A string that looks
|
||||
* like a JSON array is parsed (matches legacy `asMessage`); otherwise the
|
||||
* value passes through. Output stays `JsonValue` — downstream ctx.* accept it. */
|
||||
|
||||
@@ -18,6 +18,7 @@ import { actions as requestActions } from './request';
|
||||
import { actions as extendedActions } from './extended';
|
||||
import { actions as groupAlbumActions } from './group-album';
|
||||
import { actions as qzoneActions } from './qzone';
|
||||
import { actions as qzonePublicActions } from './qzone-public';
|
||||
import { actions as streamFileActions } from './stream-file';
|
||||
import { actions as streamDownloadActions } from './stream-download';
|
||||
import { actions as systemFaceActions } from './system-face';
|
||||
@@ -295,7 +296,7 @@ export const ACTION_GROUPS: readonly ActionGroup[] = [
|
||||
{ category: '请求', actions: requestActions },
|
||||
{ category: '扩展', actions: extendedActions },
|
||||
{ category: '群相册', actions: groupAlbumActions },
|
||||
{ category: '空间', actions: qzoneActions },
|
||||
{ category: '空间', actions: [...qzoneActions, ...qzonePublicActions] },
|
||||
{ category: '系统表情', actions: systemFaceActions },
|
||||
{ category: '流式接口', actions: [...streamFileActions, ...streamDownloadActions] },
|
||||
];
|
||||
|
||||
@@ -0,0 +1,372 @@
|
||||
import { defineAction, f } from '../action-kit';
|
||||
import { okResponse, type JsonObject, type JsonValue } from '../types';
|
||||
|
||||
const feed = f
|
||||
.object()
|
||||
.describe('get_qzone_public_feeds 返回的完整 feeds 条目,保留所有原始 cell 字段');
|
||||
const comment = f.object().describe('详情或评论页返回的完整评论对象,保留 user 等原始字段');
|
||||
const reply = f.object().describe('评论或回复页返回的完整回复对象,保留 user 等原始字段');
|
||||
const content = f.message().describe('非空文本,或非空上游 RichItem 对象数组;不是 OneBot 消息段');
|
||||
const objectResult = { type: 'object', additionalProperties: true };
|
||||
const feedSource =
|
||||
'feed 必须原样取自 get_qzone_public_feeds 的 feeds 数组,不能用旧说说 tid 拼造;示例中的占位字段须整体替换。';
|
||||
const commentSource =
|
||||
'comment 可取 get_qzone_public_feed_detail 返回的 feed.feed.cell_comment.comments,或后续评论页;保留完整对象。';
|
||||
const feedExample: JsonObject = {
|
||||
cell_common: {
|
||||
union_id: {
|
||||
uf_key: '<uf_key-from-get_qzone_public_feeds>',
|
||||
ugc_id: '<ugc_id>',
|
||||
feed_id: '<feed_id>',
|
||||
ext_ids: [],
|
||||
},
|
||||
cur_like_key: '<like-key>',
|
||||
domain_type: 3,
|
||||
},
|
||||
cell_feed_transfer: {},
|
||||
cell_user_info: { user: { uin: '10001', uid: '<uid>', nick: '<nick>' } },
|
||||
};
|
||||
const commentExample: JsonObject = {
|
||||
id: '<comment-id-from-detail-or-comments>',
|
||||
user: { uin: '<uin>', uid: '<uid>', nick: '<nick>' },
|
||||
like: { id: '<like-id-from-comment>', count: 0, status: 0 },
|
||||
reply_attach_info: '',
|
||||
};
|
||||
const replyExample: JsonObject = {
|
||||
id: '<reply-id-from-comments-or-replies>',
|
||||
user: { uin: '<uin>', uid: '<uid>', nick: '<nick>' },
|
||||
like: { id: '<like-id-from-reply>', count: 0, status: 0 },
|
||||
};
|
||||
|
||||
function validContent(value: JsonValue): value is string | JsonObject[] {
|
||||
return typeof value === 'string'
|
||||
? value.length > 0
|
||||
: Array.isArray(value) &&
|
||||
value.length > 0 &&
|
||||
value.every(
|
||||
(item) => typeof item === 'object' && item !== null && !Array.isArray(item),
|
||||
);
|
||||
}
|
||||
|
||||
export const actions = [
|
||||
defineAction({
|
||||
name: 'get_qzone_public_capabilities',
|
||||
summary: '读取当前账号的空友爱看入口与推权限',
|
||||
readOnly: true,
|
||||
examples: [{ summary: '查询当前账号是否有空友爱看入口及推能力', params: {} }],
|
||||
returns: '当前账号的上游入口能力对象,保留未知字段。',
|
||||
returnsSchema: objectResult,
|
||||
params: {},
|
||||
run: async (_p, ctx) => okResponse(await ctx.bridge.apis.qzone.getPublicCapabilities()),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'get_qzone_public_feeds',
|
||||
summary: '读取空友爱看推荐动态,支持独立游标分页',
|
||||
readOnly: true,
|
||||
examples: [
|
||||
{ summary: '读取空友爱看推荐首页', params: {} },
|
||||
{
|
||||
summary: '继续读取推荐动态',
|
||||
params: { cursor: '<next_cursor-from-get_qzone_public_feeds>' },
|
||||
notes: 'cursor 原样使用上次返回的 next_cursor,仅适用于同一账号;has_more=false 时停止。',
|
||||
},
|
||||
],
|
||||
returns: '推荐动态原始 cell 对象及分页状态,保留其余上游页面字段。',
|
||||
returnsSchema: {
|
||||
type: 'object',
|
||||
additionalProperties: true,
|
||||
properties: {
|
||||
feeds: {
|
||||
type: 'array',
|
||||
items: objectResult,
|
||||
description: '原始动态 cell 对象,后续详情和互动需原样传入',
|
||||
},
|
||||
has_more: { type: 'boolean', description: '服务端是否报告还有下一页' },
|
||||
next_cursor: {
|
||||
type: ['string', 'null'],
|
||||
description: '下一页游标;无下一页时为 null',
|
||||
},
|
||||
},
|
||||
required: ['feeds', 'has_more', 'next_cursor'],
|
||||
},
|
||||
params: {
|
||||
cursor: f
|
||||
.string({ allowEmpty: false, maxLen: 65536 })
|
||||
.describe('上次返回的 next_cursor,省略则刷新首页')
|
||||
.optional(),
|
||||
},
|
||||
run: async (p, ctx) => okResponse(await ctx.bridge.apis.qzone.getPublicFeeds(p.cursor)),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'get_qzone_public_feed_detail',
|
||||
summary: '读取推荐动态详情及初始评论',
|
||||
readOnly: true,
|
||||
examples: [
|
||||
{ summary: '读取推荐动态详情', params: { feed: feedExample }, notes: feedSource },
|
||||
],
|
||||
returns:
|
||||
'上游详情 data;初始评论位于 feed.feed.cell_comment.comments,续页使用 attach_info。',
|
||||
returnsSchema: objectResult,
|
||||
params: { feed },
|
||||
run: async (p, ctx) => okResponse(await ctx.bridge.apis.qzone.getPublicFeedDetail(p.feed)),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'get_qzone_public_comments',
|
||||
summary: '继续读取推荐动态评论',
|
||||
readOnly: true,
|
||||
examples: [
|
||||
{
|
||||
summary: '读取详情之后的下一页评论',
|
||||
params: { feed: feedExample, cursor: '<attach_info-from-detail-or-comments>' },
|
||||
notes: `${feedSource} 先读详情获取初始评论及 attach_info,再原样传入 cursor。`,
|
||||
},
|
||||
],
|
||||
returns: '上游评论页对象,保留评论、分页和未知字段。',
|
||||
returnsSchema: objectResult,
|
||||
params: {
|
||||
feed,
|
||||
cursor: f.string({ allowEmpty: false }).describe('详情或上一评论页返回的 attach_info'),
|
||||
},
|
||||
run: async (p, ctx) =>
|
||||
okResponse(await ctx.bridge.apis.qzone.getPublicComments(p.feed, p.cursor)),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'get_qzone_public_replies',
|
||||
summary: '继续读取指定评论的回复',
|
||||
readOnly: true,
|
||||
examples: [
|
||||
{
|
||||
summary: '读取某条评论的后续回复',
|
||||
params: { feed: feedExample, comment: commentExample },
|
||||
notes: `${feedSource} ${commentSource} 首次续读使用评论自带的 reply_attach_info;之后传上一回复页的游标。`,
|
||||
},
|
||||
],
|
||||
returns: '上游回复页对象,保留回复、分页和未知字段。',
|
||||
returnsSchema: objectResult,
|
||||
params: {
|
||||
feed,
|
||||
comment,
|
||||
cursor: f
|
||||
.string()
|
||||
.describe('上一回复页的 reply_attach_info;省略则使用评论自带游标')
|
||||
.default(''),
|
||||
page_size: f.int({ min: 1 }).describe('请求的回复数量').default(20),
|
||||
},
|
||||
run: async (p, ctx) =>
|
||||
okResponse(
|
||||
await ctx.bridge.apis.qzone.getPublicReplies(
|
||||
p.feed,
|
||||
p.comment,
|
||||
p.cursor,
|
||||
p.page_size,
|
||||
),
|
||||
),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'set_qzone_public_upvote',
|
||||
summary: '推或取消推空友爱看动态(独立于普通赞)',
|
||||
readOnly: false,
|
||||
examples: [
|
||||
{
|
||||
summary: '推一条推荐动态',
|
||||
params: { feed: feedExample, enable: true },
|
||||
notes: feedSource,
|
||||
},
|
||||
],
|
||||
returns: '上游推操作结果,保留未知字段。',
|
||||
returnsSchema: objectResult,
|
||||
params: { feed, enable: f.bool().describe('true 推,false 取消推').default(true) },
|
||||
run: async (p, ctx) =>
|
||||
okResponse(await ctx.bridge.apis.qzone.setPublicUpvote(p.feed, p.enable)),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'set_qzone_public_like',
|
||||
summary: '点赞或取消赞推荐动态',
|
||||
readOnly: false,
|
||||
examples: [
|
||||
{
|
||||
summary: '点赞推荐动态',
|
||||
params: { feed: feedExample, enable: true },
|
||||
notes: feedSource,
|
||||
},
|
||||
],
|
||||
returns: '上游普通赞操作结果,保留未知字段。',
|
||||
returnsSchema: objectResult,
|
||||
params: { feed, enable: f.bool().describe('true 点赞,false 取消赞').default(true) },
|
||||
run: async (p, ctx) =>
|
||||
okResponse(await ctx.bridge.apis.qzone.setPublicLike(p.feed, p.enable)),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'set_qzone_public_comment_like',
|
||||
summary: '点赞或取消赞推荐动态中的评论或回复',
|
||||
readOnly: false,
|
||||
examples: [
|
||||
{
|
||||
summary: '点赞一条评论',
|
||||
params: { feed: feedExample, comment: commentExample },
|
||||
notes: `${feedSource} ${commentSource} 必须保留评论原始 like.id。`,
|
||||
},
|
||||
{
|
||||
summary: '取消赞某条回复',
|
||||
params: {
|
||||
feed: feedExample,
|
||||
comment: commentExample,
|
||||
reply: replyExample,
|
||||
enable: false,
|
||||
},
|
||||
notes: `${feedSource} reply 使用该评论下的完整回复对象,保留其原始 like.id。`,
|
||||
},
|
||||
],
|
||||
returns: '上游普通赞操作结果,保留未知字段。',
|
||||
returnsSchema: objectResult,
|
||||
params: {
|
||||
feed,
|
||||
comment,
|
||||
enable: f.bool().describe('true 点赞,false 取消赞').default(true),
|
||||
reply: reply.optional(),
|
||||
},
|
||||
run: async (p, ctx) =>
|
||||
okResponse(
|
||||
await ctx.bridge.apis.qzone.setPublicCommentLike(
|
||||
p.feed,
|
||||
p.comment,
|
||||
p.enable,
|
||||
p.reply,
|
||||
),
|
||||
),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'comment_qzone_public_feed',
|
||||
summary: '评论推荐动态,支持文本或上游富文本条目',
|
||||
readOnly: false,
|
||||
examples: [
|
||||
{
|
||||
summary: '评论推荐动态',
|
||||
params: { feed: feedExample, content: '感谢分享' },
|
||||
notes: feedSource,
|
||||
},
|
||||
],
|
||||
returns: '上游评论结果,包含新评论,保留未知字段。',
|
||||
returnsSchema: objectResult,
|
||||
params: { feed, content },
|
||||
rules: (r) => [
|
||||
r.rule('content must be non-empty text or a non-empty array of RichItem objects', (p) =>
|
||||
validContent(p.content),
|
||||
),
|
||||
],
|
||||
run: async (p, ctx) =>
|
||||
okResponse(
|
||||
await ctx.bridge.apis.qzone.commentPublicFeed(
|
||||
p.feed,
|
||||
p.content as string | JsonObject[],
|
||||
),
|
||||
),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'reply_qzone_public_feed',
|
||||
summary: '回复推荐动态的评论或其中某条回复',
|
||||
readOnly: false,
|
||||
examples: [
|
||||
{
|
||||
summary: '回复一条评论',
|
||||
params: { feed: feedExample, comment: commentExample, content: '感谢回复' },
|
||||
notes: `${feedSource} ${commentSource}`,
|
||||
},
|
||||
{
|
||||
summary: '回复评论中的某条回复',
|
||||
params: {
|
||||
feed: feedExample,
|
||||
comment: commentExample,
|
||||
content: [{ type: 0, content: '我也这么认为' }],
|
||||
reply: replyExample,
|
||||
},
|
||||
notes: `${feedSource} ${commentSource} reply 使用该评论下的完整回复对象。`,
|
||||
},
|
||||
],
|
||||
returns: '上游回复结果,包含新回复,保留未知字段。',
|
||||
returnsSchema: objectResult,
|
||||
params: { feed, comment, content, reply: reply.optional() },
|
||||
rules: (r) => [
|
||||
r.rule('content must be non-empty text or a non-empty array of RichItem objects', (p) =>
|
||||
validContent(p.content),
|
||||
),
|
||||
],
|
||||
run: async (p, ctx) =>
|
||||
okResponse(
|
||||
await ctx.bridge.apis.qzone.replyPublicFeed(
|
||||
p.feed,
|
||||
p.comment,
|
||||
p.content as string | JsonObject[],
|
||||
p.reply,
|
||||
),
|
||||
),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'delete_qzone_public_comment',
|
||||
summary: '删除推荐动态的一条评论(需有权限)',
|
||||
readOnly: false,
|
||||
examples: [
|
||||
{
|
||||
summary: '删除一条评论',
|
||||
params: { feed: feedExample, comment: commentExample },
|
||||
notes: `${feedSource} ${commentSource}`,
|
||||
},
|
||||
],
|
||||
returns: '上游删除结果,保留未知字段。',
|
||||
returnsSchema: objectResult,
|
||||
params: { feed, comment },
|
||||
run: async (p, ctx) =>
|
||||
okResponse(await ctx.bridge.apis.qzone.deletePublicComment(p.feed, p.comment)),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'delete_qzone_public_reply',
|
||||
summary: '删除推荐动态评论下的一条回复(需有权限)',
|
||||
readOnly: false,
|
||||
examples: [
|
||||
{
|
||||
summary: '删除一条回复',
|
||||
params: { feed: feedExample, comment: commentExample, reply: replyExample },
|
||||
notes: `${feedSource} ${commentSource} reply 使用该评论下的完整回复对象。`,
|
||||
},
|
||||
],
|
||||
returns: '上游删除结果,保留未知字段。',
|
||||
returnsSchema: objectResult,
|
||||
params: { feed, comment, reply },
|
||||
run: async (p, ctx) =>
|
||||
okResponse(await ctx.bridge.apis.qzone.deletePublicReply(p.feed, p.comment, p.reply)),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'set_qzone_public_follow',
|
||||
summary: '蹲或取消蹲推荐动态作者',
|
||||
readOnly: false,
|
||||
examples: [
|
||||
{
|
||||
summary: '蹲这条推荐动态的作者',
|
||||
params: { feed: feedExample, enable: true },
|
||||
notes: `${feedSource} feed 需保留作者 cell_user_info。`,
|
||||
},
|
||||
],
|
||||
returns: '上游关系操作结果,保留未知字段。',
|
||||
returnsSchema: objectResult,
|
||||
params: { feed, enable: f.bool().describe('true 蹲,false 取消蹲').default(true) },
|
||||
run: async (p, ctx) =>
|
||||
okResponse(await ctx.bridge.apis.qzone.setPublicFollow(p.feed, p.enable)),
|
||||
}),
|
||||
defineAction({
|
||||
name: 'forward_qzone_public_feed',
|
||||
summary: '将推荐动态转发到当前账号空间',
|
||||
readOnly: false,
|
||||
examples: [
|
||||
{
|
||||
summary: '转发推荐动态到我的空间',
|
||||
params: { feed: feedExample, reason: '分享这条动态' },
|
||||
notes: `${feedSource} 只转发,不同时评论或发送给 QQ 联系人。`,
|
||||
},
|
||||
],
|
||||
returns: '上游转发结果,保留未知字段。',
|
||||
returnsSchema: objectResult,
|
||||
params: { feed, reason: f.string().describe('转发附言,可为空').default('') },
|
||||
run: async (p, ctx) =>
|
||||
okResponse(await ctx.bridge.apis.qzone.forwardPublicFeed(p.feed, p.reason)),
|
||||
}),
|
||||
];
|
||||
@@ -114,6 +114,35 @@ describe('f.array(...).nonEmpty — per-element + non-empty', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('f.object — open JSON object', () => {
|
||||
const spec = defineAction({
|
||||
name: 't',
|
||||
params: { value: f.object() },
|
||||
run: () => okResponse(),
|
||||
});
|
||||
|
||||
it('preserves nested and unknown fields', () => {
|
||||
const value = { identity: { id: 'opaque' }, future: [1, null, { enabled: true }] };
|
||||
expect(expectOk(spec.parse({ value })).value).toBe(value);
|
||||
expect(f.object().toJsonSchema()).toEqual({ type: 'object', additionalProperties: true });
|
||||
});
|
||||
|
||||
it.each([null, [], 'text', 1, false])('rejects non-object %j', (value) => {
|
||||
expect(expectErr(spec.parse({ value })).field).toBe('value');
|
||||
});
|
||||
|
||||
it('uses the normal required and optional field policies', () => {
|
||||
expect(expectErr(spec.parse({})).reason).toBe('is required');
|
||||
const optional = defineAction({
|
||||
name: 't',
|
||||
params: { value: f.object().optional() },
|
||||
run: () => okResponse(),
|
||||
});
|
||||
expect(expectOk(optional.parse({}))).toEqual({ value: undefined });
|
||||
expect(expectErr(optional.parse({ value: null })).field).toBe('value');
|
||||
});
|
||||
});
|
||||
|
||||
describe('f.message — union passthrough + JSON-array string', () => {
|
||||
const spec = defineAction({ name: 't', params: { m: f.message() }, run: () => okResponse() });
|
||||
it('parses a JSON-array string, passes plain string / array through', () => {
|
||||
|
||||
@@ -0,0 +1,326 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import type { BridgeInterface } from '@snowluma/core/bridge-interface';
|
||||
import { ApiHandler, type ApiActionContext } from '../src/api-handler';
|
||||
import { ACTION_GROUPS } from '../src/actions';
|
||||
import { actions } from '../src/actions/qzone-public';
|
||||
import type { JsonObject } from '../src/types';
|
||||
|
||||
const feed = {
|
||||
cell_common: {
|
||||
union_id: { feed_id: 'opaque-id', ext_ids: [{ future: true }] },
|
||||
cur_like_key: 'like-key',
|
||||
},
|
||||
cell_feed_transfer: { opaque: [1, 'token'] },
|
||||
cell_future: { kept: true },
|
||||
};
|
||||
const comment = {
|
||||
id: 'comment-id',
|
||||
user: { uid: 'commenter' },
|
||||
reply_attach_info: 'comment-cursor',
|
||||
};
|
||||
const reply = { id: 'reply-id', user: { uid: 'recipient' }, future: true };
|
||||
const result = { unknown: { preserved: [1, 2] } };
|
||||
|
||||
function makeHandler(qzone: Record<string, unknown>): ApiHandler {
|
||||
const bridge = { apis: { qzone } } as unknown as BridgeInterface;
|
||||
return new ApiHandler({ bridge } as ApiActionContext);
|
||||
}
|
||||
|
||||
const cases: {
|
||||
action: string;
|
||||
method: string;
|
||||
params: JsonObject;
|
||||
args: unknown[];
|
||||
readOnly: boolean;
|
||||
}[] = [
|
||||
{
|
||||
action: 'get_qzone_public_capabilities',
|
||||
method: 'getPublicCapabilities',
|
||||
params: {},
|
||||
args: [],
|
||||
readOnly: true,
|
||||
},
|
||||
{
|
||||
action: 'get_qzone_public_feeds',
|
||||
method: 'getPublicFeeds',
|
||||
params: {},
|
||||
args: [undefined],
|
||||
readOnly: true,
|
||||
},
|
||||
{
|
||||
action: 'get_qzone_public_feed_detail',
|
||||
method: 'getPublicFeedDetail',
|
||||
params: { feed },
|
||||
args: [feed],
|
||||
readOnly: true,
|
||||
},
|
||||
{
|
||||
action: 'get_qzone_public_comments',
|
||||
method: 'getPublicComments',
|
||||
params: { feed, cursor: 'comments-cursor' },
|
||||
args: [feed, 'comments-cursor'],
|
||||
readOnly: true,
|
||||
},
|
||||
{
|
||||
action: 'get_qzone_public_replies',
|
||||
method: 'getPublicReplies',
|
||||
params: { feed, comment },
|
||||
args: [feed, comment, '', 20],
|
||||
readOnly: true,
|
||||
},
|
||||
{
|
||||
action: 'set_qzone_public_upvote',
|
||||
method: 'setPublicUpvote',
|
||||
params: { feed },
|
||||
args: [feed, true],
|
||||
readOnly: false,
|
||||
},
|
||||
{
|
||||
action: 'set_qzone_public_like',
|
||||
method: 'setPublicLike',
|
||||
params: { feed },
|
||||
args: [feed, true],
|
||||
readOnly: false,
|
||||
},
|
||||
{
|
||||
action: 'set_qzone_public_comment_like',
|
||||
method: 'setPublicCommentLike',
|
||||
params: { feed, comment },
|
||||
args: [feed, comment, true, undefined],
|
||||
readOnly: false,
|
||||
},
|
||||
{
|
||||
action: 'comment_qzone_public_feed',
|
||||
method: 'commentPublicFeed',
|
||||
params: { feed, content: 'hello' },
|
||||
args: [feed, 'hello'],
|
||||
readOnly: false,
|
||||
},
|
||||
{
|
||||
action: 'reply_qzone_public_feed',
|
||||
method: 'replyPublicFeed',
|
||||
params: { feed, comment, content: 'reply' },
|
||||
args: [feed, comment, 'reply', undefined],
|
||||
readOnly: false,
|
||||
},
|
||||
{
|
||||
action: 'delete_qzone_public_comment',
|
||||
method: 'deletePublicComment',
|
||||
params: { feed, comment },
|
||||
args: [feed, comment],
|
||||
readOnly: false,
|
||||
},
|
||||
{
|
||||
action: 'delete_qzone_public_reply',
|
||||
method: 'deletePublicReply',
|
||||
params: { feed, comment, reply },
|
||||
args: [feed, comment, reply],
|
||||
readOnly: false,
|
||||
},
|
||||
{
|
||||
action: 'set_qzone_public_follow',
|
||||
method: 'setPublicFollow',
|
||||
params: { feed },
|
||||
args: [feed, true],
|
||||
readOnly: false,
|
||||
},
|
||||
{
|
||||
action: 'forward_qzone_public_feed',
|
||||
method: 'forwardPublicFeed',
|
||||
params: { feed },
|
||||
args: [feed, ''],
|
||||
readOnly: false,
|
||||
},
|
||||
];
|
||||
|
||||
describe('qzone public actions', () => {
|
||||
it.each(cases)(
|
||||
'$action preserves upstream objects and applies documented defaults',
|
||||
async ({ action, method, params, args }) => {
|
||||
const invoke = vi.fn().mockResolvedValue(result);
|
||||
|
||||
expect(await makeHandler({ [method]: invoke }).handle(action, params)).toMatchObject({
|
||||
status: 'ok',
|
||||
retcode: 0,
|
||||
data: result,
|
||||
});
|
||||
expect(invoke).toHaveBeenCalledExactlyOnceWith(...args);
|
||||
},
|
||||
);
|
||||
|
||||
it('registers all public actions under the existing space category with valid examples and read/write modes', () => {
|
||||
const registered = ACTION_GROUPS.find((group) => group.category === '空间')!.actions;
|
||||
|
||||
expect(actions).toHaveLength(cases.length);
|
||||
|
||||
for (const { action, readOnly } of cases) {
|
||||
const spec = actions.find((entry) => entry.names.includes(action))!;
|
||||
const doc = spec.describe();
|
||||
expect(registered).toContain(spec);
|
||||
expect(doc.readOnly).toBe(readOnly);
|
||||
expect(doc.returnsSchema).toBeDefined();
|
||||
expect(doc.examples!.length).toBeGreaterThan(0);
|
||||
|
||||
for (const example of doc.examples!) {
|
||||
expect(spec.parse(example.params)).toMatchObject({ ok: true });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('keeps list metadata and explicit cursor values intact', async () => {
|
||||
const page = {
|
||||
feeds: [feed],
|
||||
has_more: true,
|
||||
next_cursor: 'next-cursor',
|
||||
server_metadata: { future: 1 },
|
||||
};
|
||||
const getPublicFeeds = vi.fn().mockResolvedValue(page);
|
||||
|
||||
expect(
|
||||
await makeHandler({ getPublicFeeds }).handle('get_qzone_public_feeds', {
|
||||
cursor: 'current-cursor',
|
||||
}),
|
||||
).toMatchObject({ status: 'ok', data: page });
|
||||
expect(getPublicFeeds).toHaveBeenCalledWith('current-cursor');
|
||||
});
|
||||
|
||||
it.each(['set_qzone_public_upvote', 'set_qzone_public_like', 'set_qzone_public_follow'])(
|
||||
'%s accepts false without replacing it with the true default',
|
||||
async (action) => {
|
||||
const { method } = cases.find((entry) => entry.action === action)!;
|
||||
const invoke = vi.fn().mockResolvedValue(result);
|
||||
|
||||
await makeHandler({ [method]: invoke }).handle(action, { feed, enable: 'false' });
|
||||
expect(invoke).toHaveBeenCalledWith(feed, false);
|
||||
},
|
||||
);
|
||||
|
||||
it('normalizes query-string reply pagination and preserves the parent object', async () => {
|
||||
const getPublicReplies = vi.fn().mockResolvedValue(result);
|
||||
|
||||
await makeHandler({ getPublicReplies }).handle('get_qzone_public_replies', {
|
||||
feed,
|
||||
comment,
|
||||
cursor: 'reply-cursor',
|
||||
page_size: '5',
|
||||
});
|
||||
expect(getPublicReplies).toHaveBeenCalledWith(feed, comment, 'reply-cursor', 5);
|
||||
});
|
||||
|
||||
it('routes a reply unlike with its original target and parent context', async () => {
|
||||
const setPublicCommentLike = vi.fn().mockResolvedValue(result);
|
||||
const originalReply = { ...reply, like: { id: 'reply-like-key', future: true } };
|
||||
|
||||
await makeHandler({ setPublicCommentLike }).handle('set_qzone_public_comment_like', {
|
||||
feed,
|
||||
comment,
|
||||
reply: originalReply,
|
||||
enable: 'false',
|
||||
});
|
||||
expect(setPublicCommentLike).toHaveBeenCalledExactlyOnceWith(
|
||||
feed,
|
||||
comment,
|
||||
false,
|
||||
originalReply,
|
||||
);
|
||||
});
|
||||
|
||||
it.each(['comment_qzone_public_feed', 'reply_qzone_public_feed'])(
|
||||
'%s preserves every rich item and optional reply recipient',
|
||||
async (action) => {
|
||||
const { method } = cases.find((entry) => entry.action === action)!;
|
||||
const invoke = vi.fn().mockResolvedValue(result);
|
||||
const rich = [
|
||||
{ type: 0, content: 'hello', future: { unknown: true } },
|
||||
{ type: 1, unknown: [1, 2] },
|
||||
];
|
||||
const params = { feed, comment, content: rich, reply };
|
||||
|
||||
await makeHandler({ [method]: invoke }).handle(action, params);
|
||||
expect(invoke).toHaveBeenCalledWith(
|
||||
...(action === 'comment_qzone_public_feed'
|
||||
? [feed, rich]
|
||||
: [feed, comment, rich, reply]),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it('accepts a JSON-array string through the existing message coercer', async () => {
|
||||
const commentPublicFeed = vi.fn().mockResolvedValue(result);
|
||||
|
||||
await makeHandler({ commentPublicFeed }).handle('comment_qzone_public_feed', {
|
||||
feed,
|
||||
content: '[{"type":0,"content":"hello"}]',
|
||||
});
|
||||
expect(commentPublicFeed).toHaveBeenCalledWith(feed, [{ type: 0, content: 'hello' }]);
|
||||
});
|
||||
|
||||
it.each(['comment_qzone_public_feed', 'reply_qzone_public_feed'])(
|
||||
'%s rejects missing, empty, scalar or mixed content without effects',
|
||||
async (action) => {
|
||||
const { method } = cases.find((entry) => entry.action === action)!;
|
||||
const invoke = vi.fn();
|
||||
const handler = makeHandler({ [method]: invoke });
|
||||
const invalid: JsonObject[] = [
|
||||
{},
|
||||
{ content: '' },
|
||||
{ content: [] },
|
||||
{ content: 1 },
|
||||
{ content: null },
|
||||
{ content: {} },
|
||||
{ content: [{ type: 0 }, null] },
|
||||
{ content: [{ type: 0 }, 'invalid'] },
|
||||
];
|
||||
|
||||
for (const params of invalid) {
|
||||
expect(await handler.handle(action, { feed, comment, ...params })).toMatchObject({
|
||||
status: 'failed',
|
||||
retcode: 1400,
|
||||
});
|
||||
}
|
||||
|
||||
expect(invoke).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
{ action: 'get_qzone_public_feeds', params: { cursor: '' } },
|
||||
{ action: 'get_qzone_public_feeds', params: { cursor: 'a'.repeat(65537) } },
|
||||
{ action: 'get_qzone_public_feed_detail', params: {} },
|
||||
{ action: 'get_qzone_public_feed_detail', params: { feed: [] } },
|
||||
{ action: 'get_qzone_public_comments', params: { feed } },
|
||||
{ action: 'get_qzone_public_comments', params: { feed, cursor: '' } },
|
||||
{ action: 'get_qzone_public_replies', params: { feed, comment: null } },
|
||||
{ action: 'get_qzone_public_replies', params: { feed, comment, page_size: 0 } },
|
||||
{ action: 'delete_qzone_public_comment', params: { feed } },
|
||||
{ action: 'set_qzone_public_comment_like', params: { feed } },
|
||||
{ action: 'set_qzone_public_comment_like', params: { feed, comment, reply: [] } },
|
||||
{ action: 'delete_qzone_public_reply', params: { feed, comment, reply: 'bad' } },
|
||||
{
|
||||
action: 'reply_qzone_public_feed',
|
||||
params: { feed, comment, content: 'reply', reply: [] },
|
||||
},
|
||||
])('$action rejects invalid parameters before dispatch', async ({ action, params }) => {
|
||||
const { method } = cases.find((entry) => entry.action === action)!;
|
||||
const invoke = vi.fn();
|
||||
|
||||
expect(
|
||||
await makeHandler({ [method]: invoke }).handle(action, params as JsonObject),
|
||||
).toMatchObject({ status: 'failed', retcode: 1400 });
|
||||
expect(invoke).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each(['get_qzone_public_feed_detail', 'set_qzone_public_upvote'])(
|
||||
'%s exposes upstream errors without retry',
|
||||
async (action) => {
|
||||
const { method, params } = cases.find((entry) => entry.action === action)!;
|
||||
const invoke = vi.fn().mockRejectedValue(new Error('public qzone denied'));
|
||||
|
||||
expect(await makeHandler({ [method]: invoke }).handle(action, params)).toMatchObject({
|
||||
status: 'failed',
|
||||
wording: 'public qzone denied',
|
||||
});
|
||||
expect(invoke).toHaveBeenCalledTimes(1);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -4,10 +4,19 @@ import { actions as extended } from '../src/actions/extended';
|
||||
import { actions as albums } from '../src/actions/group-album';
|
||||
import { actions as groupAdmin } from '../src/actions/group-admin';
|
||||
import { actions as qzone } from '../src/actions/qzone';
|
||||
import { actions as qzonePublic } from '../src/actions/qzone-public';
|
||||
import { actions as streamDownload } from '../src/actions/stream-download';
|
||||
import { actions as streamFile } from '../src/actions/stream-file';
|
||||
|
||||
const actions = [...extended, ...albums, ...groupAdmin, ...qzone, ...streamDownload, ...streamFile];
|
||||
const actions = [
|
||||
...extended,
|
||||
...albums,
|
||||
...groupAdmin,
|
||||
...qzone,
|
||||
...qzonePublic,
|
||||
...streamDownload,
|
||||
...streamFile,
|
||||
];
|
||||
const examples = Object.entries(actionParams).filter(
|
||||
([name]) => name !== '.handle_quick_operation_async',
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user