From f3e0c4e45aedf48026c6eab14b0b5efbac325d0c Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sun, 19 Jul 2026 18:12:48 +0800 Subject: [PATCH] fix(onebot): allow clearing the self signature Accept explicit empty strings from both longNick and long_nick when callers need to clear the bot signature. Missing and non-string values remain rejected before reaching the bridge. Preserve the QQ-compatible clear representation by omitting the empty profile value, and add action-level and wire-level regression coverage. Live verification confirmed successful clearing and readback on the test bot. Fixes #247 --- packages/onebot/src/actions/extended.ts | 2 +- .../onebot/tests/extended-actions.test.ts | 39 +++++++++++++++++++ .../profile/set-self-long-nick.test.ts | 7 ++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/onebot/src/actions/extended.ts b/packages/onebot/src/actions/extended.ts index 8cc227a3..0c46e9ad 100644 --- a/packages/onebot/src/actions/extended.ts +++ b/packages/onebot/src/actions/extended.ts @@ -2050,7 +2050,7 @@ export const actions = [ summary: '设置个性签名(longNick/long_nick,严格 string)', params: { longNick: f.raw(), long_nick: f.raw() }, run: async (p, ctx) => { - const longNick = p.longNick || p.long_nick; + const longNick = p.longNick !== undefined ? p.longNick : p.long_nick; if (typeof longNick !== 'string') return failedResponse(RETCODE.BAD_REQUEST, 'invalid longNick'); await ctx.bridge.apis.profile.setSelfLongNick(longNick); return okResponse({}); diff --git a/packages/onebot/tests/extended-actions.test.ts b/packages/onebot/tests/extended-actions.test.ts index f8032286..50399ecb 100644 --- a/packages/onebot/tests/extended-actions.test.ts +++ b/packages/onebot/tests/extended-actions.test.ts @@ -121,6 +121,45 @@ function makeHandler(ctx: ApiActionContext): ApiHandler { return new ApiHandler(ctx); } +describe('extended-actions / set_self_longnick', () => { + it('accepts an empty longNick to clear the signature', async () => { + const setSelfLongNick = vi.fn(async () => undefined); + const bridge = fakeBridge({ setSelfLongNick }); + + const response = await makeHandler(fakeCtx(bridge)).handle('set_self_longnick', { + longNick: '', + }); + + expect(response).toMatchObject({ status: 'ok', retcode: 0 }); + expect(setSelfLongNick).toHaveBeenCalledWith(''); + }); + + it('accepts an empty long_nick compatibility alias', async () => { + const setSelfLongNick = vi.fn(async () => undefined); + const bridge = fakeBridge({ setSelfLongNick }); + + const response = await makeHandler(fakeCtx(bridge)).handle('set_self_longnick', { + long_nick: '', + }); + + expect(response).toMatchObject({ status: 'ok', retcode: 0 }); + expect(setSelfLongNick).toHaveBeenCalledWith(''); + }); + + it.each([ + ['missing value', {}], + ['non-string value', { longNick: 0 }], + ])('rejects %s without calling the bridge', async (_case, params) => { + const setSelfLongNick = vi.fn(async () => undefined); + const bridge = fakeBridge({ setSelfLongNick }); + + const response = await makeHandler(fakeCtx(bridge)).handle('set_self_longnick', params); + + expect(response).toMatchObject({ status: 'failed', retcode: 1400 }); + expect(setSelfLongNick).not.toHaveBeenCalled(); + }); +}); + describe('extended-actions / group notice options', () => { it('coerces and forwards every supported announcement option', async () => { const sendNotice = vi.fn(async () => ({ ec: 0 })); diff --git a/packages/protocol/tests/oidb-services/profile/set-self-long-nick.test.ts b/packages/protocol/tests/oidb-services/profile/set-self-long-nick.test.ts index 5e102578..ec912d48 100644 --- a/packages/protocol/tests/oidb-services/profile/set-self-long-nick.test.ts +++ b/packages/protocol/tests/oidb-services/profile/set-self-long-nick.test.ts @@ -36,6 +36,13 @@ describe('SetSelfLongNick namespace', () => { expect(env.body?.profile).toEqual({ tag: 102, value: 'hello world' }); }); + it('omits the value field when clearing the signature', async () => { + const deps = makeDeps(); + await SetSelfLongNick.invoke(deps, { longNick: '' }); + const [, bytes] = deps.sendRawPacket.mock.calls[0]!; + expect(Buffer.from(bytes).toString('hex')).toBe('08aa221002220708914e12020866'); + }); + it('coerces non-string longNick into a string (defensive)', async () => { const deps = makeDeps(); // simulate a misbehaving caller passing a number