From 0b4cfe65ed889aa8e8061ee4c5b7edddec0d10f5 Mon Sep 17 00:00:00 2001 From: Shxiao Date: Wed, 23 Sep 2026 17:52:24 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20set=5Fqq=5Fprofile=20=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E5=B0=86=E6=80=A7=E5=88=AB=E8=AE=BE=E7=BD=AE=E4=B8=BA=200?= =?UTF-8?q?=EF=BC=88=E6=9C=AA=E7=9F=A5=EF=BC=89=20(#2065)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../action/go-cqhttp/SetQQProfile.ts | 2 +- packages/napcat-test/setQQProfileSex.test.ts | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 packages/napcat-test/setQQProfileSex.test.ts diff --git a/packages/napcat-onebot/action/go-cqhttp/SetQQProfile.ts b/packages/napcat-onebot/action/go-cqhttp/SetQQProfile.ts index 13ca70cde..4bb9fed7b 100644 --- a/packages/napcat-onebot/action/go-cqhttp/SetQQProfile.ts +++ b/packages/napcat-onebot/action/go-cqhttp/SetQQProfile.ts @@ -26,7 +26,7 @@ export class SetQQProfile extends OneBotAction { return await this.core.apis.UserApi.modifySelfProfile({ nick: payload.nickname, longNick: (payload?.personal_note ?? OldProfile?.longNick) || '', - sex: parseInt(payload?.sex ? payload?.sex.toString() : OldProfile?.sex!.toString()), + sex: parseInt(payload.sex != null && payload.sex !== '' ? payload.sex.toString() : OldProfile?.sex!.toString()), birthday: { birthday_year: OldProfile?.birthday_year!.toString(), birthday_month: OldProfile?.birthday_month!.toString(), diff --git a/packages/napcat-test/setQQProfileSex.test.ts b/packages/napcat-test/setQQProfileSex.test.ts new file mode 100644 index 000000000..b2760e58b --- /dev/null +++ b/packages/napcat-test/setQQProfileSex.test.ts @@ -0,0 +1,66 @@ +// StreamBasic must be evaluated before OneBotAction: the two modules import each other, +// and loading OneBotAction first leaves BasicStream extending an uninitialized binding. +import '../napcat-onebot/action/stream/StreamBasic'; +import { afterEach, describe, expect, test, vi } from 'vitest'; +import { SetQQProfile } from '../napcat-onebot/action/go-cqhttp/SetQQProfile'; + +const OLD_PROFILE = { + sex: 1, + longNick: 'old note', + birthday_year: 2000, + birthday_month: 1, + birthday_day: 1, +}; + +function createAction () { + const modifySelfProfile = vi.fn().mockResolvedValue({}); + const action = new SetQQProfile(undefined as never, { + selfInfo: { uid: 'u1' }, + apis: { + UserApi: { + getUserDetailInfo: vi.fn().mockResolvedValue(OLD_PROFILE), + modifySelfProfile, + }, + }, + } as never); + return { action, modifySelfProfile }; +} + +describe('set_qq_profile sex handling', () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + // NTSex.GENDER_UNKOWN === 0, and the payload schema documents "0: 未知". + test('forwards sex: 0 instead of falling back to the current profile', async () => { + const { action, modifySelfProfile } = createAction(); + + await action._handle({ nickname: 'newnick', sex: 0 }); + + expect(modifySelfProfile).toHaveBeenCalledWith(expect.objectContaining({ nick: 'newnick', sex: 0 })); + }); + + test('forwards sex: 2', async () => { + const { action, modifySelfProfile } = createAction(); + + await action._handle({ nickname: 'newnick', sex: 2 }); + + expect(modifySelfProfile).toHaveBeenCalledWith(expect.objectContaining({ sex: 2 })); + }); + + test('falls back to the current profile when sex is omitted', async () => { + const { action, modifySelfProfile } = createAction(); + + await action._handle({ nickname: 'newnick' }); + + expect(modifySelfProfile).toHaveBeenCalledWith(expect.objectContaining({ sex: OLD_PROFILE.sex })); + }); + + test('falls back to the current profile when sex is an empty string', async () => { + const { action, modifySelfProfile } = createAction(); + + await action._handle({ nickname: 'newnick', sex: '' }); + + expect(modifySelfProfile).toHaveBeenCalledWith(expect.objectContaining({ sex: OLD_PROFILE.sex })); + }); +});