import test from 'node:test' import assert from 'node:assert/strict' import { buildKeywordStatsSections, formatLogTime, mapKeywordShareLogs, } from '../src/shared/api/types/modules/keyword-share.ts' test('buildKeywordStatsSections:后端四段各自成段,二级/三级带上级分组', () => { const sections = buildKeywordStatsSections({ byCountry: [{ name: '英国', count: 1200 }, { name: '德国', count: 800 }], byCategory: [{ name: '3C', count: 900 }], bySubCategory: [{ category: '3C', name: '充电器', count: 300 }], byCategoryL3: [{ category: '3C', subCategory: '充电器', name: '无线充', count: 90 }], }) assert.deepEqual(sections.byCountry, [ { group: '', label: '英国', count: 1200 }, { group: '', label: '德国', count: 800 }, ]) assert.deepEqual(sections.byCategory, [{ group: '', label: '3C', count: 900 }]) assert.deepEqual(sections.bySubCategory, [{ group: '3C', label: '充电器', count: 300 }]) assert.deepEqual(sections.byCategoryL3, [{ group: '3C / 充电器', label: '无线充', count: 90 }]) }) test('buildKeywordStatsSections:缺段/脏行按空处理,不抛错(弹窗显示暂无数据)', () => { const sections = buildKeywordStatsSections({ byCountry: [{ name: '', count: 5 }, { name: '法国', count: null }, { name: '西班牙', count: 7 }], byCategory: null, }) assert.deepEqual(sections.byCountry, [{ group: '', label: '西班牙', count: 7 }]) assert.deepEqual(sections.byCategory, []) assert.deepEqual(sections.bySubCategory, []) assert.deepEqual(sections.byCategoryL3, []) assert.deepEqual(buildKeywordStatsSections(undefined).byCountry, []) }) test('mapKeywordShareLogs:userName/time 映射到 operator/createdAt,ISO 时间转可读', () => { const vo = mapKeywordShareLogs( { total: 2, rows: [ { id: 1, userName: '张三', country: '英国', keyword: 'wireless charger', time: '2026-10-23T21:12:34' }, { id: 2, userName: '李四', country: '德国', keyword: 'usb c hub', time: '2026-10-22T09:00:01.000' }, ], }, 'usage', ) assert.equal(vo.total, 2) assert.deepEqual(vo.rows[0], { id: 1, type: 'usage', country: '英国', keyword: 'wireless charger', operator: '张三', createdAt: '2026-10-23 21:12:34', }) assert.equal(vo.rows[1]?.createdAt, '2026-10-22 09:00:01') }) test('mapKeywordShareLogs:空返回与缺 total 时按行数兜底,不造出幽灵记录', () => { assert.deepEqual(mapKeywordShareLogs(undefined), { total: 0, rows: [] }) const vo = mapKeywordShareLogs({ rows: [{ userName: '王五' }] }) assert.equal(vo.total, 1) assert.equal(vo.rows[0]?.country, null) assert.equal(vo.rows[0]?.createdAt, '') }) test('formatLogTime:非 ISO 形态原样透出,空值给空串', () => { assert.equal(formatLogTime('2026-10-23 21:12:34'), '2026-10-23 21:12:34') assert.equal(formatLogTime('刚刚'), '刚刚') assert.equal(formatLogTime(null), '') })