fix(common-tools): 关键词共享按后端真实返回接统计四段与日志字段

/stats-grouped 是四段各自 GROUP BY(对齐桌面版弹窗),前端原先按
「国家×一级类目」交叉表建模 ⇒ 弹窗恒为暂无数据;/logs 返回 userName/time
而表格读 operator/createdAt ⇒ 两列恒为占位符。改为 API 层映射 + 四段渲染,
日志去掉后端不产出的「说明」列。
This commit is contained in:
2026-10-23 23:00:33 +08:00
parent 807ecaabe1
commit de4465730f
4 changed files with 193 additions and 406 deletions
@@ -0,0 +1,73 @@
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), '')
})
@@ -15,7 +15,7 @@ import ElementPlus from 'element-plus'
import KeywordSharePage from '@/pages/common/KeywordSharePage.vue'
// API 层整模块 mock:纯函数(normalizeKeywordStatsGrouped)保留真实现,
// API 层整模块 mock:纯函数(buildKeywordStatsSections / mapKeywordShareLogs)保留真实现,
// 网络函数按用例给桩。页面经 shared/api/types/modules/keyword-share 调用。
const api = vi.hoisted(() => ({
fetchKeywordShareFilters: vi.fn(),