feat(common-tools): 工具台新增常用工具分类,三个桌面工具网页化

- tool-catalog 最前插入「常用工具」组(brand_common_tools,23 工具·4 大分类),
  三工具各自权限键:pricing-calc / five-asin / keyword-share
- /pricing-calc:站点(英 GBP÷9、德法 EUR÷8)+ 买货价/运费 → 营业额/利润/
  利润率/换算汇率,公式与精度照抄桌面版,纯函数 node --test 钉住
- /five-asin:五国常量 + ASIN 10 位校验 → 亚马逊商品链接,window.open 与
  :href 出口全部过 safeHttpUrl(url-outlet 棘轮),邮编一键复制
- /keyword-share:筛选/四级类目级联/统计卡/表格分页 200/标记与取消使用/
  添加/删除/随机抽词/共享记录,对接 keywordshare 后端九端点
- 顶栏与 BrandTopBar 导航同步;router 注册三路由;补 test:mount 脚本
  (vitest mount 套件入口,与后管同口径)
This commit is contained in:
2026-10-23 21:56:56 +08:00
parent 2c85f47c3b
commit 208583e14c
18 changed files with 2424 additions and 17 deletions
+47 -2
View File
@@ -11,9 +11,9 @@ import {
TOOL_MAP,
} from '../src/pages/amazon/tool-catalog.ts'
test('工具目录:20 个工具 id 唯一且每个都有图标与渐变配色', () => {
test('工具目录:23 个工具 id 唯一且每个都有图标与渐变配色', () => {
const ids = TOOL_GROUPS.flatMap((g) => g.tools.map((t) => t.id))
assert.equal(ids.length, 20, '工具总数应为 20(前端8 + 运营10 + 后勤2)')
assert.equal(ids.length, 23, '工具总数应为 23(常用3 + 前端8 + 运营10 + 后勤2)')
assert.equal(new Set(ids).size, ids.length, '工具 id 不应重复')
for (const id of ids) {
assert.ok(TOOL_ICON_SHAPES[id], `图标形状缺失: ${id}`)
@@ -22,6 +22,51 @@ test('工具目录:20 个工具 id 唯一且每个都有图标与渐变配色'
}
})
test('工具目录:常用工具分组排在最前且三个工具都有页面与权限键', () => {
const common = TOOL_GROUPS[0]
assert.equal(common.key, 'common', '常用工具组应在 TOOL_GROUPS 最前')
assert.equal(common.columnKey, 'brand_common_tools')
assert.equal(common.name, '常用工具')
assert.equal(common.desc, '换算 · 查询 · 词库共享,高频小工具')
assert.ok(common.color, '常用工具组应有主色')
assert.equal(common.workflow, undefined, '常用工具组没有标准工作流')
const byId = new Map(common.tools.map((tool) => [tool.id, tool]))
assert.deepEqual(
[...byId.keys()],
['pricing-calc', 'five-asin', 'keyword-share'],
'常用工具组的三个工具顺序固定',
)
assert.equal(byId.get('pricing-calc')?.href, '/pricing-calc')
assert.equal(byId.get('five-asin')?.href, '/five-asin')
assert.equal(byId.get('keyword-share')?.href, '/keyword-share')
assert.deepEqual(byId.get('pricing-calc')?.permissionKeys, ['pricing-calc'])
assert.deepEqual(byId.get('five-asin')?.permissionKeys, ['five-asin'])
assert.deepEqual(byId.get('keyword-share')?.permissionKeys, ['keyword-share'])
})
test('权限过滤:常用工具组整组 / 单工具键都按口径过滤', () => {
// 组键命中:整组保留且排在最前
const withCommon = filterGroupsByPermission(TOOL_GROUPS, ['brand_common_tools'])
const common = withCommon.find((group) => group.key === 'common')
assert.ok(common, '组键 brand_common_tools 命中时常用工具组应整组可见')
assert.deepEqual(common.tools.map((tool) => tool.id), ['pricing-calc', 'five-asin', 'keyword-share'])
// 只给单个工具权限键:常用组只留命中工具,不整组放行
const partial = filterGroupsByPermission(TOOL_GROUPS, ['pricing-calc', 'keyword-share'])
const partialCommon = partial.find((group) => group.key === 'common')
assert.ok(partialCommon, '有可见工具时常用组不应整体隐藏')
assert.deepEqual(partialCommon.tools.map((tool) => tool.id), ['pricing-calc', 'keyword-share'])
assert.equal(partialCommon.tools.some((tool) => tool.id === 'five-asin'), false, '无权限的 five-asin 不应混入')
// 无任何常用权限:常用组整体隐藏
const none = filterGroupsByPermission(TOOL_GROUPS, ['brand_front_tools'])
assert.equal(none.some((group) => group.key === 'common'), false, '无常用权限时常用组应隐藏')
// 无任何权限:全部隐藏
assert.equal(filterGroupsByPermission(TOOL_GROUPS, []).length, 0)
})
test('工具目录:无页面的工具应在 catalog 里标记为未开通(无 href)', () => {
const pending = TOOL_GROUPS.flatMap((g) => g.tools).filter((t) => !t.href)
const pendingIds = pending.map((t) => t.id).sort()
+48
View File
@@ -0,0 +1,48 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import {
buildAmazonProductUrl,
FIVE_ASIN_COUNTRIES,
FIVE_ASIN_COUNTRY_MAP,
isValidAsin,
normalizeAsin,
} from '../src/pages/common/five-asin.ts'
test('五国站点常量:域名 / 邮编 / 城市与桌面版一致', () => {
assert.equal(FIVE_ASIN_COUNTRIES.length, 5)
const byCode = new Map(FIVE_ASIN_COUNTRIES.map((c) => [c.code, c]))
assert.deepEqual(byCode.get('uk'), {
code: 'uk', name: '英国', flag: '🇬🇧', domain: 'www.amazon.co.uk', postcode: 'SW1A 1AA', city: '伦敦', currency: '英镑',
})
assert.equal(byCode.get('de')?.postcode, '10115')
assert.equal(byCode.get('fr')?.postcode, '75001')
assert.equal(byCode.get('es')?.postcode, '28001')
assert.equal(byCode.get('it')?.postcode, '20121')
// 五国域名唯一且都带 https 之外的裸域名形状
assert.equal(new Set(FIVE_ASIN_COUNTRIES.map((c) => c.domain)).size, 5)
for (const country of FIVE_ASIN_COUNTRIES) {
assert.equal(FIVE_ASIN_COUNTRY_MAP.get(country.code), country, 'code→country 映射应完整')
}
})
test('ASIN 规范化:strip + 大写 + 去非字母数字', () => {
assert.equal(normalizeAsin(' b0c1234abc '), 'B0C1234ABC')
assert.equal(normalizeAsin('B0C-1234 abc'), 'B0C1234ABC')
assert.equal(normalizeAsin('asin/1234?5'), 'ASIN12345')
assert.equal(normalizeAsin(''), '')
})
test('ASIN 校验:规范化后必须恰好 10 位字母数字', () => {
assert.equal(isValidAsin('B0C1234ABC'), true)
assert.equal(isValidAsin(normalizeAsin(' b0c1234abc ')), true, '页面先规范化再校验,小写输入规范化后应合法')
assert.equal(isValidAsin('B0C1234AB'), false, '9 位应判不合法')
assert.equal(isValidAsin('B0C1234ABCD'), false, '11 位应判不合法')
assert.equal(isValidAsin(''), false)
assert.equal(isValidAsin('B0C1234AB!'), false, '非字母数字应判不合法')
})
test('商品页链接:https://{domain}/dp/{asin}', () => {
assert.equal(buildAmazonProductUrl('www.amazon.co.uk', 'B0C1234ABC'), 'https://www.amazon.co.uk/dp/B0C1234ABC')
assert.equal(buildAmazonProductUrl('www.amazon.de', 'B0C1234ABC'), 'https://www.amazon.de/dp/B0C1234ABC')
})
@@ -0,0 +1,160 @@
/**
* 五国关键词共享页挂载用例(2026-10-21 常用工具网页化)。
*
* 两件事都要判,缺一条就是假绿:
* ① 首屏真实走 API:列表行、统计卡都来自 /keywords 返回,页面不得自带假数据;
* ② 行内「标记已使用」真实打到 POST /usage/mark,且载荷是 country+keyword、
* 成功后重拉列表(防"点了没反应")。
*
* 跑法:cd frontend-vue && npx vitest run tests/mount/keyword-share-page.mount.test.ts
* (tests/mount/** 由 vitest.config.ts 收,node --test 的 glob 抓不到这里。)
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import ElementPlus from 'element-plus'
import KeywordSharePage from '@/pages/common/KeywordSharePage.vue'
// API 层整模块 mock:纯函数(normalizeKeywordStatsGrouped)保留真实现,
// 网络函数按用例给桩。页面经 shared/api/types/modules/keyword-share 调用。
const api = vi.hoisted(() => ({
fetchKeywordShareFilters: vi.fn(),
fetchKeywordShareKeywords: vi.fn(),
fetchKeywordShareStatsGrouped: vi.fn(),
addKeywordShareKeywords: vi.fn(),
deleteKeywordShareKeywords: vi.fn(),
markKeywordShareUsage: vi.fn(),
unmarkKeywordShareUsage: vi.fn(),
fetchKeywordShareLogs: vi.fn(),
fetchKeywordShareRandom: vi.fn(),
}))
vi.mock('@/shared/api/types/modules/keyword-share', async (importOriginal) => ({
...(await importOriginal<typeof import('@/shared/api/types/modules/keyword-share')>()),
...api,
}))
vi.mock('@/shared/api/permission', () => ({
getCurrentUserAppColumnKeys: vi.fn().mockResolvedValue([]),
}))
const ROWS = [
{
id: 1,
country: '英国',
keyword: 'wireless charger',
chinese: '无线充电器',
category: '电子',
subCategory: '配件',
categoryL3: '充电',
categoryL4: '无线充',
used: false,
usedTime: null,
usedUser: null,
},
{
id: 2,
country: '德国',
keyword: 'handtuch wärmer',
chinese: '毛巾架',
category: '家居',
subCategory: '浴室',
categoryL3: '毛巾架',
categoryL4: '加热',
used: true,
usedTime: '2026-10-20 10:00:00',
usedUser: '张三',
},
]
const LIST_VO = {
total: 2,
rows: ROWS,
stats: { hit: 2, used: 1, unused: 1, dup: 0 },
}
// happy-dom 没有ResizeObserver 时 el-table 会挂不上,补一个最小桩
class ResizeObserverStub {
observe() {}
unobserve() {}
disconnect() {}
}
let wrapper: VueWrapper | null = null
async function mountPage() {
wrapper = mount(KeywordSharePage, {
global: {
plugins: [ElementPlus],
stubs: {
RouterLink: true,
'router-link': true,
NotificationBell: true,
BrandApiSecretSettingsButton: true,
DownloadProgressPanel: true,
AmazonFooterBar: true,
},
},
})
await flushPromises()
await flushPromises()
return wrapper
}
function findButtonByText(text: string) {
const buttons = wrapper?.findAll('button') ?? []
return buttons.find((button) => button.text().trim() === text)
}
describe('五国关键词共享页', () => {
beforeEach(() => {
if (!(globalThis as Record<string, unknown>).ResizeObserver) {
;(globalThis as Record<string, unknown>).ResizeObserver = ResizeObserverStub
}
api.fetchKeywordShareFilters.mockResolvedValue({ countries: ['英国', '德国'], categories: [] })
api.fetchKeywordShareKeywords.mockResolvedValue(LIST_VO)
api.markKeywordShareUsage.mockResolvedValue({ marked: 1 })
api.unmarkKeywordShareUsage.mockResolvedValue({ unmarked: 1 })
})
afterEach(() => {
wrapper?.unmount()
wrapper = null
vi.clearAllMocks()
})
it('首屏渲染 API 返回的关键词行与统计卡(无假数据)', async () => {
const w = await mountPage()
expect(api.fetchKeywordShareKeywords).toHaveBeenCalledTimes(1)
const text = w.text()
expect(text, '行数据来自 /keywords 返回').toContain('wireless charger')
expect(text).toContain('handtuch wärmer')
expect(text).toContain('✓已使用·2026-10-20 10:00:00·张三')
expect(text).toContain('未使用')
// 统计卡四格来自返回的 stats
expect(text).toContain('命中总数')
expect(w.findAll('.stat-card .stat-value').map((node) => node.text())).toEqual(['2', '1', '1', '0'])
})
it('行内「标记已使用」打到 POST /usage/mark(country+keyword 载荷)并重拉列表', async () => {
await mountPage()
expect(api.markKeywordShareUsage).not.toHaveBeenCalled()
const markBtn = findButtonByText('标记已使用')
expect(markBtn, '未使用行应显示「标记已使用」').toBeTruthy()
await markBtn!.trigger('click')
await flushPromises()
expect(api.markKeywordShareUsage).toHaveBeenCalledTimes(1)
expect(api.markKeywordShareUsage).toHaveBeenCalledWith([{ country: '英国', keyword: 'wireless charger' }])
expect(api.fetchKeywordShareKeywords, '标记成功后应重拉列表').toHaveBeenCalledTimes(2)
})
it('已使用行的行内按钮是「标记未使用」,点击走 POST /usage/unmark', async () => {
await mountPage()
const unmarkBtn = findButtonByText('标记未使用')
expect(unmarkBtn, '已使用行应显示「标记未使用」').toBeTruthy()
await unmarkBtn!.trigger('click')
await flushPromises()
expect(api.unmarkKeywordShareUsage).toHaveBeenCalledWith([{ country: '德国', keyword: 'handtuch wärmer' }])
expect(api.markKeywordShareUsage).not.toHaveBeenCalled()
})
})
+84
View File
@@ -0,0 +1,84 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import {
calcPricing,
formatExchangeRate,
formatMoney,
formatProfitRate,
parseMoneyInput,
PRICING_STATIONS,
PRICING_STATION_MAP,
} from '../src/pages/common/pricing-calc.ts'
test('站点配置:英国 GBP 除数 9,德国·法国 EUR 除数 8', () => {
assert.deepEqual(PRICING_STATIONS.map((s) => s.key), ['uk', 'eu'])
assert.equal(PRICING_STATION_MAP.get('uk')?.currency, 'GBP')
assert.equal(PRICING_STATION_MAP.get('uk')?.divisor, 9)
assert.equal(PRICING_STATION_MAP.get('eu')?.currency, 'EUR')
assert.equal(PRICING_STATION_MAP.get('eu')?.divisor, 8)
})
test('金额解析:剥离 ¥/¥/千分位逗号/空白后 parseFloat', () => {
assert.equal(parseMoneyInput('25'), 25)
assert.equal(parseMoneyInput('¥1,280'), 1280)
assert.equal(parseMoneyInput('¥3.5'), 3.5)
assert.equal(parseMoneyInput(' 1,234.56 '), 1234.56)
assert.equal(parseMoneyInput('0'), 0)
assert.ok(parseMoneyInput('0.001') !== null)
})
test('金额解析:空串、非数字、负数一律判非法(返回 null)', () => {
assert.equal(parseMoneyInput(''), null)
assert.equal(parseMoneyInput(' '), null)
assert.equal(parseMoneyInput('abc'), null)
assert.equal(parseMoneyInput('12abc'), null)
assert.equal(parseMoneyInput('-5'), null)
assert.equal(parseMoneyInput('¥-5'), null)
})
test('计算口径:英国站逐项对上桌面版公式', () => {
const uk = PRICING_STATION_MAP.get('uk')
assert.ok(uk)
// 成本=100+10=110;营业额=110/0.5=220;平台费=44;利润=66;利润率=0.3;换算=220/9
const result = calcPricing(100, 10, uk)
assert.equal(result.cost, 110)
assert.equal(result.revenue, 220)
assert.equal(result.platformFee, 44)
assert.equal(result.profit, 66)
assert.ok(Math.abs(result.profitRate - 0.3) < 1e-12)
assert.ok(Math.abs(result.converted - 220 / 9) < 1e-12)
})
test('计算口径:德国·法国站按除数 8 反推', () => {
const eu = PRICING_STATION_MAP.get('eu')
assert.ok(eu)
const result = calcPricing(80, 0, eu)
assert.equal(result.cost, 80)
assert.equal(result.revenue, 160)
assert.equal(result.platformFee, 32)
assert.equal(result.profit, 48)
assert.equal(result.converted, 20)
})
test('计算边界:买货价与运费都为 0 时结果全 0,利润率不出现 NaN', () => {
const uk = PRICING_STATION_MAP.get('uk')
assert.ok(uk)
const result = calcPricing(0, 0, uk)
assert.equal(result.cost, 0)
assert.equal(result.revenue, 0)
assert.equal(result.platformFee, 0)
assert.equal(result.profit, 0)
assert.equal(result.profitRate, 0, '营业额为 0 时利润率应记 0 而不是 NaN')
assert.equal(result.converted, 0)
})
test('展示格式:金额千分位 2 位 / 利润率 1 位百分比 / 汇率 4 位小数', () => {
assert.equal(formatMoney(220), '220.00')
assert.equal(formatMoney(1234567.891), '1,234,567.89')
assert.equal(formatMoney(0), '0.00')
assert.equal(formatProfitRate(0.3), '30.0%')
assert.equal(formatProfitRate(0), '0.0%')
assert.equal(formatExchangeRate(220 / 9), (220 / 9).toFixed(4))
assert.equal(formatExchangeRate(20), '20.0000')
})