refactor(onebot): declare every action param the handler reads

Action handlers now only see fields from the param table. Extra client
keys are still ignored. Garbage values on declared numbers fail instead
of becoming zero.
This commit is contained in:
DrSmoothl committed 2026-08-30 00:56:01 +08:00
1 parent 117335166f
commit f6adadcd12
14 files changed
+952 -235

No files matched your search

+16 -5
View File
@@ -152,16 +152,27 @@ describe('toHandler — BAD_REQUEST shaping + ctx threading', () => {
});
});
describe('run raw escape hatch — original params as 3rd arg', () => {
it('exposes untouched params for undeclared keys (alias fallbacks etc.)', async () => {
describe('run only sees declared fields', () => {
it('lets a declared alias through and ignores undeclared keys', async () => {
const spec = defineAction({
name: 't',
params: { user_id: f.uint() },
run: (_p, _ctx, raw) => okResponse({ extra: (raw.long_nick ?? raw.longNick ?? null) as never }),
params: { user_id: f.uint(), longNick: f.string().optional() },
run: (p) => okResponse({ extra: (p.longNick ?? null) as never }),
});
const res = await spec.toHandler(ctx)({ user_id: 1, longNick: 'hi' });
const res = await spec.toHandler(ctx)({ user_id: 1, longNick: 'hi', undeclared: 1 });
expect(res).toMatchObject({ status: 'ok', data: { extra: 'hi' } });
});
it('rejects garbage on a declared int instead of treating it as 0', async () => {
const spec = defineAction({
name: 't',
params: { group_id: f.int({ min: 0 }).optional() },
run: (p) => okResponse({ group_id: p.group_id ?? 0 }),
});
const res = await spec.toHandler(ctx)({ group_id: 'nope' });
expect(res.retcode).toBe(RETCODE.BAD_REQUEST);
expect(res.wording).toContain('group_id');
});
});
describe('toJsonSchema / inputSchema (for the MCP)', () => {
+33 -31
View File
@@ -241,7 +241,7 @@ describe('set_friend_add_request describe', () => {
});
describe('set_group_add_request parse', () => {
it('requires flag and defaults approve, reason, and the raw type fields', () => {
it('requires flag and defaults approve, reason, and the type fields', () => {
expect(setGroupAddRequest.parse({ flag: 'slreq:1:123456:999:22:1' })).toEqual({
ok: true,
value: {
@@ -254,22 +254,22 @@ describe('set_group_add_request parse', () => {
});
});
it('keeps raw sub_type and type values without coercing them', () => {
it('rejects non-string sub_type and type values', () => {
expect(setGroupAddRequest.parse({
flag: 'flag-group-9',
sub_type: { kind: 'invite' },
type: [1, 2],
approve: 0,
reason: 404,
})).toEqual({
ok: true,
value: {
flag: 'flag-group-9',
sub_type: { kind: 'invite' },
type: [1, 2],
approve: false,
reason: '404',
},
ok: false,
field: 'sub_type',
reason: 'expected a string',
});
expect(setGroupAddRequest.parse({
flag: 'flag-group-9',
type: [1, 2],
})).toEqual({
ok: false,
field: 'type',
reason: 'expected a string',
});
});
@@ -418,23 +418,25 @@ describe('set_group_add_request toHandler', () => {
});
it.each([
[{ sub_type: { kind: 'invite' }, type: 'invite' }, 'invite'],
[{ sub_type: ['invite'], type: 'add' }, 'add'],
[{ sub_type: null, type: 'invite' }, 'invite'],
[{ sub_type: null, type: null }, 'add'],
[{ type: { kind: 'add' } }, 'add'],
[{ type: null }, 'add'],
])('falls through a non-primitive locator %j to %s', async (params, subType) => {
[{ sub_type: { kind: 'invite' } }, 'sub_type'],
[{ sub_type: ['invite'] }, 'sub_type'],
[{ sub_type: null }, 'sub_type'],
[{ type: { kind: 'add' } }, 'type'],
[{ type: null }, 'type'],
])('rejects a non-string locator %j', async (params, field) => {
const { ctx, handleGroupRequest } = groupCtx();
await expect(setGroupAddRequest.toHandler(ctx)({
flag: 'flag-group-9',
...params,
})).resolves.toEqual({ status: 'ok', retcode: 0, data: null });
})).resolves.toEqual({
status: 'failed',
retcode: 1400,
data: null,
wording: `${field}: expected a string`,
});
expect(handleGroupRequest.mock.calls).toEqual([
['flag-group-9', subType, true, ''],
]);
expect(handleGroupRequest).not.toHaveBeenCalled();
});
it('stringifies a numeric flag and reason', async () => {
@@ -484,7 +486,7 @@ describe('set_group_add_request toHandler', () => {
});
describe('set_group_add_request describe', () => {
it('documents the group-request params including raw type fields', () => {
it('documents the group-request params including type fields', () => {
const doc = setGroupAddRequest.describe();
expect(doc.name).toBe('set_group_add_request');
@@ -504,17 +506,17 @@ describe('set_group_add_request describe', () => {
},
{
name: 'sub_type',
type: 'raw',
type: 'string',
required: false,
default: undefined,
schema: {},
schema: { type: 'string' },
},
{
name: 'type',
type: 'raw',
type: 'string',
required: false,
default: undefined,
schema: {},
schema: { type: 'string' },
},
{
name: 'approve',
@@ -535,8 +537,8 @@ describe('set_group_add_request describe', () => {
type: 'object',
properties: {
flag: { type: 'string', minLength: 1 },
sub_type: {},
type: {},
sub_type: { type: 'string' },
type: { type: 'string' },
approve: { type: 'boolean', default: true },
reason: { type: 'string', default: '' },
},
@@ -149,7 +149,7 @@ describe('ApiHandler TRACE lifecycle', () => {
const stream = defineStreamAction({
name: 'trace_stream',
params: {},
run: async (_params, _ctx, _raw, sink) => {
run: async (_params, _ctx, sink) => {
await sink.send({ type: StreamStatus.Stream, chunk: 'complete-frame' });
return {
status: 'ok',
@@ -149,6 +149,50 @@ function fakeEssenceMessage(
};
}
describe('extended-actions / send_forward_msg', () => {
it('treats group_id 0 as unused', async () => {
const sendForwardMsg = vi.fn(async () => ({ forwardId: 'fwd' }));
const ctx = fakeCtx(fakeBridge(), { sendForwardMsg });
const response = await makeHandler(ctx).handle('send_forward_msg', {
messages: 'hi',
group_id: 0,
});
expect(response).toMatchObject({ status: 'ok', retcode: 0 });
expect(sendForwardMsg).toHaveBeenCalledOnce();
});
it('rejects a non-numeric group_id instead of treating it as 0', async () => {
const sendForwardMsg = vi.fn();
const ctx = fakeCtx(fakeBridge(), { sendForwardMsg });
const response = await makeHandler(ctx).handle('send_forward_msg', {
messages: 'hi',
group_id: 'nope',
});
expect(response).toMatchObject({ status: 'failed', retcode: 1400 });
expect(sendForwardMsg).not.toHaveBeenCalled();
});
});
describe('extended-actions / get_forward_msg', () => {
it('uses a string message_id as the forward id', async () => {
const getForwardMsg = vi.fn(async () => []);
const ctx = fakeCtx(fakeBridge(), { getForwardMsg });
const response = await makeHandler(ctx).handle('get_forward_msg', { message_id: 'resid-1' });
expect(getForwardMsg).toHaveBeenCalledWith('resid-1');
expect(response).toMatchObject({ status: 'ok' });
});
});
describe('extended-actions / fetch_ptt_text', () => {
it('accepts a numeric message_id', async () => {
const fetchPttText = vi.fn(async () => 'hello');
const ctx = fakeCtx(fakeBridge(), { fetchPttText });
const response = await makeHandler(ctx).handle('fetch_ptt_text', { message_id: 12 });
expect(fetchPttText).toHaveBeenCalledWith(12);
expect(response).toMatchObject({ status: 'ok', data: 'hello' });
});
});
describe('extended-actions / set_self_longnick', () => {
it('accepts an empty longNick to clear the signature', async () => {
const setSelfLongNick = vi.fn(async () => undefined);
@@ -14,7 +14,7 @@ export function testAction(name: string, run: TestActionRun): RegisteredActionSp
return defineAction({
name,
params: {},
run: (_params, context, raw) => run(raw, context),
run: (params, context) => run(params, context),
});
}
+3 -3
View File
@@ -22,7 +22,7 @@ describe('Stream API plumbing', () => {
const action = defineStreamAction({
name: 'demo_stream',
params: { n: f.uint() },
run: async (p, _ctx, _raw, sink) => {
run: async (p, _ctx, sink) => {
for (let i = 0; i < p.n; i++) await sink.send({ type: 'stream', index: i });
return okResponse({ type: 'response', total: p.n });
},
@@ -85,7 +85,7 @@ describe('Stream API plumbing', () => {
const action = defineStreamAction({
name: 'demo_partial',
params: {},
run: async (_p, _ctx, _raw, sink) => {
run: async (_p, _ctx, sink) => {
await sink.send({ type: 'stream', index: 0 });
throw new Error('mid');
},
@@ -116,7 +116,7 @@ describe('Stream API plumbing', () => {
const action = defineStreamAction({
name: 'demo_abort',
params: {},
run: async (_p, _ctx, _raw, sink) => {
run: async (_p, _ctx, sink) => {
for (let i = 0; i < 5; i++) { await sink.send({ type: 'stream', index: i }); sent++; }
return okResponse({ type: 'response' });
},