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 })); + }); +});