const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const test = require('node:test'); const ProfilePages = require('../public/js/profile-pages.js'); const RegionPages = require('../public/js/region-pages.js'); const UploadPages = require('../public/js/upload-pages.js'); test('profile view reads only the documented profile fields', () => { assert.deepEqual(ProfilePages.buildProfileView({ nickName: '小林', realName: '林某', phone: '13800000000', birthday: '1990-01-01', userName: '不应读取', mobile: '不应读取' }), { displayName: '小林', realName: '林某', avatarText: '小', avatarStatus: '未设置', phone: '13800000000', sex: '待填写', birthday: '1990-01-01', email: '待填写', regionText: '当前资料接口未提供' }); assert.deepEqual(ProfilePages.buildProfileView({ userName: '不应读取', phonenumber: '不应读取', mobile: '不应读取' }), { displayName: '未设置昵称', realName: '待填写', avatarText: '家', avatarStatus: '未设置', phone: '未绑定', sex: '待填写', birthday: '待填写', email: '待填写', regionText: '当前资料接口未提供' }); }); test('profile loading failure has an explicit non-demo view', () => { assert.deepEqual(ProfilePages.buildUnavailableProfileView(), { displayName: '账户资料暂时无法读取', realName: '暂无数据', avatarText: '!', avatarStatus: '暂无数据', phone: '暂无数据', birthday: '暂无数据', email: '暂无数据', sex: '暂无数据', regionText: '暂无数据' }); }); test('profile update emits only the six fields defined by ProfileUpdateBody', () => { assert.deepEqual(ProfilePages.buildProfileUpdateBody({ nickName: '小林', realName: '林某', avatar: '', sex: '0', birthday: '1990-01-01', email: 'lin@example.com', avatarOssId: 'legacy-avatar', provinceCode: '11' }), { nickName: '小林', realName: '林某', sex: '0', birthday: '1990-01-01', email: 'lin@example.com' }); assert.deepEqual(ProfilePages.buildProfileUpdateBody({ avatar: '123' }), { avatar: '123' }); assert.deepEqual( ProfilePages.buildProfileUpdateBody({ avatar: '2060000000000000001' }), { avatar: '2060000000000000001' } ); assert.throws(() => ProfilePages.buildProfileUpdateBody({ avatar: 'not-an-id' }), /头像 OSS ID 必须是整数/); assert.equal(UploadPages.normalizeUploadResult({ ossId: '2060000000000000000' }).ossId, '2060000000000000000'); }); test('个人中心概览不读取资料接口未声明的地区字段', () => { const source = fs.readFileSync(path.join(__dirname, '..', 'public', 'js', 'profile-pages.js'), 'utf8'); assert.match(source, /regionText: '当前资料接口未提供'/); assert.doesNotMatch(source, /provinceCode|cityCode|districtCode|regionPath|applyProfileRegionSelection/); }); test('profile update rejects an empty payload and an invalid email before requesting the PC API', () => { assert.equal(ProfilePages.validateProfileUpdateBody({}), '请至少填写一项资料'); assert.equal(ProfilePages.validateProfileUpdateBody({ email: 'invalid-email' }), '请输入正确的邮箱地址'); assert.equal(ProfilePages.validateProfileUpdateBody({ nickName: '小林' }), ''); }); test('region selector accepts only documented region fields and levels', () => { assert.deepEqual(RegionPages.normalizeList({ data: [{ regionCode: '11' }] }), []); assert.equal(RegionPages.getRegionCode({ value: '11' }), ''); assert.equal(RegionPages.getRegionName({ label: '北京市' }), '未命名地区'); assert.equal(RegionPages.getRegionLevel({ regionCode: '110101' }), 0); assert.deepEqual(RegionPages.buildRegionSelection([ { regionCode: '11', regionName: '北京市', regionLevel: 1 }, { regionCode: '1101', regionName: '市辖区', regionLevel: 2 }, { regionCode: '110101', regionName: '东城区', regionLevel: 3 } ]), { provinceCode: '11', cityCode: '1101', districtCode: '110101' }); }); test('profile data page exposes every editable ProfileUpdateBody field without a manual OSS ID input', () => { const source = fs.readFileSync(path.join(__dirname, '..', 'profile-data.html'), 'utf8'); assert.match(source, /data-profile-form novalidate/); assert.match(source, /data-profile-form/); assert.match(source, /data-profile-text="realName"/); assert.match(source, /name="nickName"/); assert.match(source, /name="realName"/); assert.match(source, /name="avatar"/); assert.match(source, /name="sex"[\s\S]*