import assert from 'node:assert/strict' import test from 'node:test' import { existsSync, readFileSync } from 'node:fs' import { join } from 'node:path' import { clampPageSize, markButtonLabel, markDelistedNotice, onShelfEmptyText, quotaNearLimit, quotaReadoutText, } from '../src/pages/jd/jd-on-shelf.ts' /** * 在架商品面板纯函数与接线绊线(2026-11-25 客户口径「上限=同时在架商品数, * 下架 10 个就能再上 10 个换着卖」)。 * * 绊线盯的是「安静地不做」类回退:面板绕过 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('空态文案:未占用点名「采集入库不占额度」,翻页态不冒充空账', () => { assert.match(onShelfEmptyText(false, 0), /采集入库不占/) assert.match(onShelfEmptyText(true, 0), /上架成功后.*计入额度/s) assert.equal(onShelfEmptyText(false, 25), '本页暂无更多在架商品,可翻页查看') }) 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('绊线:在架端点只在 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/) }) test('绊线:JdPublishTab 必须挂上在架面板(功能整面失踪类回退)', () => { const tab = read('src/pages/jd/JdPublishTab.vue', '上架页') assert.match(tab, /import JdOnShelfPanel from '@\/pages\/jd\/JdOnShelfPanel\.vue'/) assert.match(tab, //) })