import assert from 'node:assert/strict' import test from 'node:test' import { existsSync, readFileSync } from 'node:fs' import { join } from 'node:path' import { BRAND_OPTIONS_EMPTY_HINT, BRAND_OPTIONS_LOADING_HINT, BRAND_OPTIONS_READY_HINT, BRAND_OPTIONS_UNAVAILABLE_HINT, brandOptionLabel, brandOptionsFromRows, brandOptionsHint, filterBrandOptions, } from '../src/pages/jd/jd-brand-options.ts' /** 目标品牌候选(2026-11-28 客户口径「品牌看看能不能获取下来,也可以支持自己输入」): * * 京麦品牌只能按关键词远程搜索、没有全量清单,候选是客户端在发布表单里抓到的本店真名品牌 * (服务端 `biz_jd_shop_brand` 增量积累)。本文件钉三件事: * 1. 服务端行 → 下拉选项的裁剪(空名丢弃、按 id 去重、id 与公司名分字段); * 2. 本地过滤只影响「联想列表」,不影响输入框里的自由文本(手输始终可用); * 3. 四种状态的灰字文案都不为空(空串会让客户以为功能没做)。 */ test('服务端行转选项:空品牌名丢弃,同 id 只留第一条', () => { const options = brandOptionsFromRows([ { brandId: '1343010', brandName: ' 无 ', companySuffix: ' 某某贸易有限公司 ', lastSeenAt: '2026-11-28 14:00:00' }, { brandId: '1343010', brandName: '无(重复页签)' }, { brandId: '-1', brandName: '无品牌' }, { brandId: '9', brandName: ' ' }, { brandName: '没有 id 但有名' }, ]) assert.deepEqual(options.map((row) => row.brandName), ['无', '无品牌', '没有 id 但有名']) assert.equal(options[0].brandId, '1343010') assert.equal(options[0].companySuffix, '某某贸易有限公司') assert.equal(options[0].lastSeenAt, '2026-11-28 14:00:00') // 「无品牌」的负 id 必须原样保留(只用于去重,不参与上屏)。 assert.equal(options[1].brandId, '-1') assert.equal(options[1].companySuffix, '') }) test('没有 id 的行按名字去重,不会整批丢', () => { const options = brandOptionsFromRows([ { brandName: '手输品牌' }, { brandName: '手输品牌' }, { brandName: 'name:前缀形状' }, ]) assert.deepEqual(options.map((row) => row.brandName), ['手输品牌', 'name:前缀形状']) }) test('空输入与 null 安全:没有候选就是空列表,不抛', () => { assert.deepEqual(brandOptionsFromRows(null), []) assert.deepEqual(brandOptionsFromRows(undefined), []) assert.deepEqual(brandOptionsFromRows([]), []) assert.deepEqual(filterBrandOptions(undefined, '无'), []) }) test('过滤:空关键词出全量,品牌名与公司名都参与匹配且大小写不敏感', () => { const options = brandOptionsFromRows([ { brandId: '1', brandName: 'OTHER/其他', companySuffix: '甲公司' }, { brandId: '2', brandName: '铜精灵', companySuffix: '某某铜业' }, { brandId: '3', brandName: '无' }, ]) assert.equal(filterBrandOptions(options, '').length, 3) assert.equal(filterBrandOptions(options, ' ').length, 3) assert.deepEqual(filterBrandOptions(options, 'other').map((row) => row.brandName), ['OTHER/其他']) assert.deepEqual(filterBrandOptions(options, '铜').map((row) => row.brandName), ['铜精灵']) assert.deepEqual(filterBrandOptions(options, '铜业').map((row) => row.brandName), ['铜精灵']) assert.deepEqual(filterBrandOptions(options, '不存在'), []) }) test('下拉行文案:有公司名时补副标题,没有就只出品牌名', () => { const [withCompany, withoutCompany] = brandOptionsFromRows([ { brandId: '1', brandName: '无', companySuffix: '某某贸易有限公司' }, { brandId: '2', brandName: '无品牌' }, ]) assert.equal(brandOptionLabel(withCompany), '无 · 某某贸易有限公司') assert.equal(brandOptionLabel(withoutCompany), '无品牌') }) test('四种状态各有非空灰字:加载中/有候选/还没抓到/读取失败', () => { assert.equal(brandOptionsHint({ loading: true, count: 3 }), BRAND_OPTIONS_LOADING_HINT) assert.equal(brandOptionsHint({ count: 3 }), BRAND_OPTIONS_READY_HINT) assert.equal(brandOptionsHint({ count: 0 }), BRAND_OPTIONS_EMPTY_HINT) assert.equal(brandOptionsHint({ failed: true }), BRAND_OPTIONS_UNAVAILABLE_HINT) // 失败优先于计数(读失败时候选被清空,不能再报「有候选」)。 assert.equal(brandOptionsHint({ failed: true, count: 5 }), BRAND_OPTIONS_UNAVAILABLE_HINT) for (const hint of [BRAND_OPTIONS_LOADING_HINT, BRAND_OPTIONS_READY_HINT, BRAND_OPTIONS_EMPTY_HINT, BRAND_OPTIONS_UNAVAILABLE_HINT]) { assert.ok(hint.trim().length > 0) } // 空候选那句必须点明「还能手输」,否则客户会以为必须先有候选才能建单。 assert.ok(BRAND_OPTIONS_EMPTY_HINT.includes('手动输入')) }) // --------------------------------------------------------------------------- // 源码绊线(B113-01 同族):本页的目标品牌必须是**联想输入框**而不是只读下拉—— // el-select 的 filterable 在「打字但没回车」时 modelValue 仍是空,客户会以为填了品牌却 // 建不了单;el-autocomplete 的输入框值就是 modelValue,手输永远生效。 // --------------------------------------------------------------------------- const TAB = join(process.cwd(), 'src/pages/jd/JdPublishTab.vue') test('目标品牌用 el-autocomplete 联想+手输,不是 el-select 纯下拉', () => { assert.ok(existsSync(TAB), `商品上架页尚未落地:${TAB}`) const source = readFileSync(TAB, 'utf8') assert.ok(source.includes(' { const source = readFileSync(TAB, 'utf8') assert.ok(source.includes('watch(brandShopId'), '品牌候选必须随发布目标店铺变化重拉(immediate,挂载即拉)') assert.ok(source.includes('fetchJdShopBrands'), '候选唯一来源是 /api/jd-shop-brands(不许自拼 URL)') assert.ok(source.includes('brandOptions = ref'), '候选落页面 ref,联想只在本地过滤') })