Files
crawler-plugin/frontend-vue/tests/jd-publish-shop.test.ts
T
huangzd1997 1b10db4bf9 feat(jd): 上架按账号授权 + 建单钉来源店铺 + 店铺不匹配整单判死
2026-11-24 客户(心向大海)两条口径:
「如果没有店铺登入情况 软件怎么知道我们要上架是哪一个店铺?」
「上架的账户 我是这个权限不开给他的」

后端半边(用户端页头与会话核对见 frontend-vue 同笔;客户端发布前核对见 app_client 0f8bcdf)。

一、按账号授权(拒绝型能力,quiet fail 全靠本类判据)
- JdPublishPermissionSupport:V209 app 菜单组键 jd_tools 或子键 jd-publish 命中即放行,
  超管直放;有效键集按 AdminMenuPlatformScope 过滤到账号平台(与登录分发同源)。
  无权限抛 403 + 中文动作句(告诉管理员去哪儿勾)。
- JdPublishController:建单/改品牌重试/选品/面板/历史/批量进度六个用户面入口一律先判;
  客户端执行面(items/chunk/activate/heartbeat/补值/删除)不判——执行器本来就只跑
  已建的任务,判在这里会让进行中的任务断在半路。

二、建单钉来源店铺(一次上架只发一家店)
- resolveSingleShop:按勾选商品快照判店铺,跨店勾选建单即拒并点名两家店;
  同店两种形状(有 id / 只有店名)归一到 id 再比。
- JdPublishTaskCreateRequest 落 shopId/shopName → 队列载荷两键 → 明细页
  JdPublishItemsPageVo.shopId/shopName 下发;旧任务载荷没这两键时回落明细行
  shop_name(V210 就有),保证老任务至少能按店名核对。

三、店铺不匹配整单判死
- JdPublishStaleTaskService.SHOP_MISMATCH_MARK/terminalizeOnJdShopMismatch:
  CAS RUNNING→FAILED、0/1 计数、原文追加动作句;不按累计行数收窄(确定性失败)。
- JdPublishIngestService.fail:命中标记排在凭据致命档之后、页面结构变更之前接线,
  回 accept=true 让客户端收口。

判据:JdPublishPermissionSupportTest 5 支(新)+TaskService +3/Stale +2/Ingest +2,
后端 JdPublish 九类 77 支全绿;四支既有护栏(catch 静默/请求身份/超管单出口/SQL 分层)绿。
2026-11-24 15:21:27 +08:00

162 lines
8.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from 'node:assert/strict'
import { existsSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import test from 'node:test'
import {
canUseJdPublish,
JD_PUBLISH_DENIED_TEXT,
resolveJdPublishAccess,
} from '../src/pages/jd/jd-publish-access.ts'
import {
evaluateShopGuard,
normalizeShopRef,
shopGuardBlocks,
shopLabel,
shopMismatchMessage,
SHOP_MISMATCH_MARK,
sourceShopRefs,
} from '../src/pages/jd/jd-publish-shop.ts'
/**
* 2026-11-24 客户口径两件(京东「商品上架」):
* 1. 「如果没有店铺登入情况 软件怎么知道我们要上架是哪一个店铺?」——页面必须显形
* 来源店铺与京麦当前登录店铺,不一致强拦截;
* 2. 「上架的账户 我是这个权限不开给他的」——按账号授权:入口隐藏(卡片过滤)+
* 直达路由守卫+服务端 403 三层。
*
* 本文件管纯判据与源码绊线;页面接线由 tests/mount/jd-publish-dispatch.mount.test.ts 覆盖。
*/
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('来源店铺按 id 去重、同名单店归一、空值全部丢弃', () => {
const refs = sourceShopRefs([
{ shopId: ' 228346061 ', shopName: ' 家居日用数据库店 ' },
{ shopId: '228346061', shopName: '家居日用数据库店' },
{ shopId: '', shopName: '家居日用数据库店' },
{ shopName: '花屿巷旗舰店' },
{ shopId: null, shopName: ' ' },
{},
])
assert.deepEqual(refs, [
{ shopId: '228346061', shopName: '家居日用数据库店' },
{ shopName: '花屿巷旗舰店' },
], '同一家店不同行只算一家;店名回退 id 时不能把空 id 当身份')
})
test('同一家店两种形状(只有 id / 只有店名)在来源集里能归一', () => {
// id 口径优先:一行有 id 一行只有名字时,页面无法确定同店,按两家报出来让用户分开勾
const refs = sourceShopRefs([{ shopId: '1' }, { shopName: '某店' }])
assert.equal(refs.length, 2)
})
test('店铺标签:名+id 全显示,缺一半只报有的那半,全空=未记录店铺', () => {
assert.equal(shopLabel({ shopId: '1', shopName: '某店' }), '某店(shopId=1)')
assert.equal(shopLabel({ shopName: '某店' }), '某店')
assert.equal(shopLabel({ shopId: '1' }), 'shopId=1')
assert.equal(shopLabel(null), '未记录店铺')
assert.equal(normalizeShopRef({ shopId: ' ' }), null)
})
test('不匹配文案:标记开头、点名两家店、给动作句且不写「稍后重试」', () => {
const message = shopMismatchMessage(
{ shopId: '228346061', shopName: '家居日用数据库店' },
{ shopId: '165515933', shopName: '花屿巷旗舰店' },
)
assert.ok(message.startsWith(SHOP_MISMATCH_MARK))
assert.ok(message.includes('家居日用数据库店') && message.includes('228346061'))
assert.ok(message.includes('花屿巷旗舰店') && message.includes('165515933'))
assert.ok(message.includes('切换'))
assert.ok(!message.includes('稍后重试'), '确定性失败不允许写「稍后重试」')
})
test('判据四态:未勾选 unknown / 未检测 undetected / 同店 match / 异店 mismatch', () => {
assert.equal(evaluateShopGuard([], { shopId: '1' }).state, 'unknown')
assert.equal(evaluateShopGuard([{ shopId: '1' }], null).state, 'undetected')
const match = evaluateShopGuard([{ shopId: '1', shopName: '某店' }], { shopId: '1', shopName: '某店' })
assert.equal(match.state, 'match')
const mismatch = evaluateShopGuard([{ shopId: '1', shopName: '某店' }], { shopId: '2', shopName: '别店' })
assert.equal(mismatch.state, 'mismatch')
assert.ok(shopGuardBlocks(mismatch))
assert.ok(!shopGuardBlocks(match) && !shopGuardBlocks(evaluateShopGuard([], null)))
})
test('id 优先于店名:id 不同但同名也不放行', () => {
const guard = evaluateShopGuard([{ shopId: '1', shopName: '同名店' }], { shopId: '2', shopName: '同名店' })
assert.equal(guard.state, 'mismatch')
})
test('只有店名时按店名比对;比不了(一边只有 id)按未检测处理,不误拦', () => {
assert.equal(evaluateShopGuard([{ shopName: '某店' }], { shopName: '某店' }).state, 'match')
assert.equal(evaluateShopGuard([{ shopName: '某店' }], { shopName: '别店' }).state, 'mismatch')
assert.equal(evaluateShopGuard([{ shopId: '1' }], { shopName: '某店' }).state, 'undetected')
})
test('跨店勾选直接拦,文案与服务端建单拒绝同口径(按店分开勾)', () => {
const guard = evaluateShopGuard([{ shopId: '1', shopName: '甲店' }, { shopId: '2', shopName: '乙店' }], null)
assert.equal(guard.state, 'multi')
assert.ok(shopGuardBlocks(guard))
if (guard.state === 'multi') {
assert.ok(guard.message.includes('甲店') && guard.message.includes('乙店'))
assert.ok(guard.message.includes('分开勾选'))
}
})
// ---------------------------------------------------------------- 菜单权限判据
test('权限判据:组键 jd_tools 或子键 jd-publish 命中即放行(大小写/空白归一)', () => {
assert.ok(canUseJdPublish(['jd_tools']))
assert.ok(canUseJdPublish([' JD-PUBLISH ']))
assert.ok(!canUseJdPublish(['jd-collect']), '只授「获取店铺数据」不包含「商品上架」')
assert.ok(!canUseJdPublish([]))
assert.ok(!canUseJdPublish(['jd_tools_extra']), '前缀相近不算命中')
})
test('路由判据三态:拿到空键集=denied,拿不到键集(接口失败)=unknown 放行', () => {
assert.equal(resolveJdPublishAccess(['jd-collect']), 'denied')
assert.equal(resolveJdPublishAccess(null), 'unknown')
assert.equal(resolveJdPublishAccess(['jd-publish']), 'allowed')
assert.ok(JD_PUBLISH_DENIED_TEXT.includes('菜单权限'))
assert.ok(JD_PUBLISH_DENIED_TEXT.includes('商品上架'))
})
// ---------------------------------------------------------------- 源码绊线(接线不许漏)
test('上架页显形来源店铺与京麦当前店铺,并在建单前核对', () => {
const tab = read('src/pages/jd/JdPublishTab.vue', '商品上架页')
assert.ok(tab.includes('get_jd_shop_info'), '页面必须调桥 get_jd_shop_info 现拉京麦当前店铺')
assert.ok(tab.includes('ensureShopChecked'), '建单前必须走店铺核对(strong block)')
assert.ok(tab.includes('shopGuardBlocks'), '不匹配/跨店必须禁掉上架按钮')
assert.ok(tab.includes('product.shopName'), '商品行要显形自己的来源店铺')
const createAt = tab.indexOf('createJdPublishTask(')
const checkAt = tab.indexOf('await ensureShopChecked()')
assert.ok(checkAt > 0 && createAt > checkAt, '核对必须早于建单')
})
test('上架页不得自己拼店铺身份(只用勾选商品快照 + 桥结果)', () => {
const tab = read('src/pages/jd/JdPublishTab.vue', '商品上架页')
assert.ok(!/shopId\s*=\s*['"]\d+/.test(tab), '页面不得写死任何 shopId')
assert.ok(!tab.includes('jd-shop-info'), '页面不得自拼店铺读取 URL(唯一口是桥)')
})
test('/jd-publish 直达路由挂权限守卫,权限接口失败按放行处理', () => {
const router = read('src/router/index.ts', 'router/index.ts')
assert.ok(router.includes('resolveJdPublishAccess'), '路由必须用统一判据模块')
assert.ok(/path: '\/jd-publish'[\s\S]{0,200}?beforeEnter:/.test(router), '/jd-publish 缺 beforeEnter 守卫')
assert.ok(router.includes('getCurrentUserAppColumnKeys'), '守卫必须读当前用户 app 列权限')
assert.ok(router.includes("path: '/jd-console'"), '没权限时回工作台(那里只显示有权限的卡片)')
})
test('桥类型声明 get_jd_shop_info(老客户端按缺方法置灰)', () => {
const bridge = read('src/shared/bridges/pywebview.ts', 'pywebview.ts')
assert.ok(bridge.includes('get_jd_shop_info?:'), 'PywebviewApi 缺 get_jd_shop_info 声明')
})