import assert from 'node:assert/strict' import test from 'node:test' import { existsSync, readFileSync } from 'node:fs' import { join } from 'node:path' import { clampPageSize, manageButtonLabel, markButtonLabel, markDelistedNotice, onShelfEmptyText, quotaFullWarning, quotaNearLimit, quotaReadoutText, } from '../src/pages/jd/jd-on-shelf.ts' /** * 在架面板纯函数与接线绊线(2026-11-25 客户口径「上限=同时在架商品数, * 下架 10 个就能再上 10 个换着卖」;2026-11-28 客户口径「只要个统计数, * 不需要显示上架的数据」——商品数据收进「管理」小入口,点开才见)。 * * 绊线盯的是「安静地不做」类回退:面板绕过 api 模块裸拼 URL(落点台账失效)、 * 文案在组件里硬拼(单出口失效)、旧「展开在架商品」大按钮回流(数据展示 * 重新常驻界面)、JdPublishTab 不挂面板(功能整面失踪)——行为测试全绿也 * 拦不住,只有源码绊线拦得住。 */ const ROOT = process.cwd() function read(relativePath: string, what: string): string { const path = join(ROOT, relativePath) assert.ok(existsSync(path), `${what} 尚未落地:${path}`) return readFileSync(path, 'utf8') } // ---------------------------------------------------------- 纯函数:余量文案 test('余量读数:正常档显「在架 X/Y」,不限档绝不显「/0」', () => { assert.equal(quotaReadoutText(120, 5000, false), '当前在架 120/5000') assert.equal(quotaReadoutText(120, 0, true), '当前在架 120/不限') // limit≤0 但 unlimited 没带(防御:服务端两键同值,坏一个也不出「/0」坏数) assert.equal(quotaReadoutText(3, 0, false), '当前在架 3/不限') assert.equal(quotaReadoutText(Number.NaN, 5000, false), '当前在架 0/5000') assert.equal(quotaReadoutText(12.7, 5000.9, false), '当前在架 12/5000') }) test('到顶判据:used≥limit 才提醒;不限档永不提醒', () => { assert.equal(quotaNearLimit(5000, 5000, false), true) assert.equal(quotaNearLimit(5001, 5000, false), true) assert.equal(quotaNearLimit(4999, 5000, false), false) assert.equal(quotaNearLimit(999999, 0, true), false) }) test('空态文案(2026-11-28 精简口径):一句讲完,机制解释句不再回流;翻页态不冒充空账', () => { assert.equal(onShelfEmptyText(false, 0), '当前没有已上架商品') assert.equal(onShelfEmptyText(true, 0), '当前没有已上架商品') assert.equal(onShelfEmptyText(false, 25), '本页暂无更多在架商品,可翻页查看') assert.ok(!onShelfEmptyText(false, 0).includes('采集入库不占'), '机制解释句已删,不得回流') }) test('分页钳制与服务端 MAX_PAGE_SIZE=100 同口径', () => { assert.equal(clampPageSize(9999), 100) assert.equal(clampPageSize(20), 20) assert.equal(clampPageSize(0), 20) assert.equal(clampPageSize(-1), 20) }) test('标记回执点名商品编码与确认义务,不写「操作成功」空话', () => { assert.match(markDelistedNotice('100365361600'), /商品 100365361600/) assert.match(markDelistedNotice('100365361600'), /额度已腾出/) assert.match(markDelistedNotice(), /已标记下架,额度已腾出/) assert.equal(markButtonLabel(), '标记已下架') }) test('管理入口与满额提醒文案(2026-11-28 精简口径):入口只说「管理」,提醒点名「管理」不再提「本列表」', () => { assert.equal(manageButtonLabel(false), '管理') assert.equal(manageButtonLabel(true), '收起') assert.match(quotaFullWarning(), /额度已满/) assert.match(quotaFullWarning(), /「管理」/) assert.match(quotaFullWarning(), /标记已下架/) assert.ok(!quotaFullWarning().includes('本列表'), '列表已收进管理入口,「本列表」指路不得回流') }) // ---------------------------------------------------------- 源码绊线:接线与单出口 test('绊线:在架端点只在 endpoints.ts 登记一处,面板必须走 api 模块出口', () => { const endpoints = read('src/shared/api/endpoints.ts', '端点表') assert.match(endpoints, /onShelf: '\/api\/jd-publish\/on-shelf'/) assert.match(endpoints, /markDelisted: '\/api\/jd-publish\/items\/\{itemId\}\/delisted'/) const apiModule = read('src/shared/api/types/modules/jd-publish.ts', '上架 api 模块') assert.match(apiModule, /API_ENDPOINTS\.jdPublish\.onShelf/) assert.match(apiModule, /API_ENDPOINTS\.jdPublish\.markDelisted/) const panel = read('src/pages/jd/JdOnShelfPanel.vue', '在架面板') assert.doesNotMatch(panel, /\/api\/jd-publish/, '面板不得裸拼后端 URL(落点必须走 api 模块)') assert.match(panel, /fetchJdOnShelf/) assert.match(panel, /markJdItemDelisted/) }) test('绊线:面板文案单出口——组件不硬拼「当前在架」,判据与界面读同一份', () => { const panel = read('src/pages/jd/JdOnShelfPanel.vue', '在架面板') assert.doesNotMatch(panel, /当前在架/, '余量文案必须经 quotaReadoutText 单出口') assert.match(panel, /quotaReadoutText/) assert.match(panel, /onShelfEmptyText/) assert.match(panel, /markDelistedNotice/) assert.match(panel, /manageButtonLabel/) assert.match(panel, /quotaFullWarning/) assert.doesNotMatch(panel, /展开在架商品/, '2026-11-28 精简口径:旧「展开在架商品」数据展示入口不得回流') assert.doesNotMatch(panel, /on-shelf-refresh/, '独立「刷新」按钮已删,余量随挂载与点开管理重取') }) test('绊线:JdPublishTab 必须挂上在架面板(功能整面失踪类回退)', () => { const tab = read('src/pages/jd/JdPublishTab.vue', '上架页') assert.match(tab, /import JdOnShelfPanel from '@\/pages\/jd\/JdOnShelfPanel\.vue'/) assert.match(tab, //) })