feat(brand): 品牌检测建单可选本机/线上采集(V212),默认本机+排队相位可见
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { http } from '../src/shared/api/http.ts'
|
||||
import { runBrandLocal, runBrandNow, type BrandLocalBridge } from '../src/shared/api/types/modules/brand.ts'
|
||||
|
||||
function setupWindow() {
|
||||
;(globalThis as Record<string, unknown>).window = {
|
||||
localStorage: { getItem: () => '42' },
|
||||
location: { origin: 'http://localhost' },
|
||||
}
|
||||
}
|
||||
|
||||
function mockRequest(
|
||||
t: Parameters<typeof test>[1] extends (t: infer T) => unknown ? T : never,
|
||||
impl: (config: { url?: string; method?: string; data?: unknown }) => Promise<unknown>,
|
||||
) {
|
||||
t.mock.method(http, 'request', impl as never)
|
||||
}
|
||||
|
||||
const files = [{ fileKey: 'k1', originalFilename: '品牌样例.xlsx' }] as never
|
||||
|
||||
/**
|
||||
* 品牌检测的采集执行通道(V212):界面单选本机/线上,默认本机。
|
||||
*
|
||||
* <p>这里钉三件:① 线上这一档必须把 collect_channel 打进请求体(否则服务端按模块默认
|
||||
* 存成 LOCAL,界面选线上却在本机跑);② 不传通道时不编造字段(服务端按默认兜);
|
||||
* ③ 老客户端的桥没有 run_brand_task 时必须明确回答 available=false——页面据此把
|
||||
* 「本机采集」置灰并提示升级,而不是静默回落到线上(否则又排回那 2 个名额)。
|
||||
*/
|
||||
test('test_brand_local_without_bridge_reports_unavailable', async () => {
|
||||
setupWindow()
|
||||
assert.deepEqual(await runBrandLocal(files, 'Simple'), { available: false })
|
||||
assert.deepEqual(await runBrandLocal(files, 'Simple', {} as BrandLocalBridge), { available: false },
|
||||
'桥在但没有 run_brand_task(老客户端)同样置灰')
|
||||
})
|
||||
|
||||
test('test_brand_local_forwards_to_bridge', async () => {
|
||||
setupWindow()
|
||||
const calls: unknown[] = []
|
||||
const bridge: BrandLocalBridge = {
|
||||
run_brand_task: (payload) => {
|
||||
calls.push(payload)
|
||||
return Promise.resolve({ success: true, task_id: 5123 })
|
||||
},
|
||||
}
|
||||
|
||||
const result = await runBrandLocal(files, 'Terms', bridge)
|
||||
|
||||
assert.equal(result.available, true)
|
||||
assert.equal(result.success, true)
|
||||
assert.equal(result.taskId, 5123)
|
||||
assert.deepEqual(calls, [{ files, strategy: 'Terms', uid: 42 }],
|
||||
'桥调用要把 uid 一起带过去:本机 Flask 建单要按用户归属')
|
||||
})
|
||||
|
||||
test('test_brand_local_bridge_failure_is_reported', async () => {
|
||||
setupWindow()
|
||||
const bridge: BrandLocalBridge = {
|
||||
run_brand_task: () => Promise.resolve({ success: false, error: '本机建单失败' }),
|
||||
}
|
||||
|
||||
const result = await runBrandLocal(files, 'Simple', bridge)
|
||||
|
||||
assert.equal(result.available, true)
|
||||
assert.equal(result.success, false)
|
||||
assert.equal(result.error, '本机建单失败')
|
||||
})
|
||||
|
||||
test('test_brand_run_online_channel_in_payload', async (t) => {
|
||||
setupWindow()
|
||||
let captured: { url?: string; method?: string; data?: unknown } = {}
|
||||
mockRequest(t, (config) => {
|
||||
captured = config
|
||||
return Promise.resolve({ data: { success: true, data: { success: true, task_id: '77' } } })
|
||||
})
|
||||
|
||||
await runBrandNow(files, 'Simple', 'ONLINE')
|
||||
|
||||
assert.equal(captured.url, '/api/brand/run?userId=42')
|
||||
assert.equal(captured.method, 'POST')
|
||||
assert.deepEqual(captured.data, { files, strategy: 'Simple', taskType: 1, collect_channel: 'ONLINE' })
|
||||
})
|
||||
|
||||
test('test_brand_run_without_channel_does_not_invent_one', async (t) => {
|
||||
setupWindow()
|
||||
let captured: { data?: unknown } = {}
|
||||
mockRequest(t, (config) => {
|
||||
captured = config
|
||||
return Promise.resolve({ data: { success: true, data: { success: true } } })
|
||||
})
|
||||
|
||||
await runBrandNow(files, 'Simple')
|
||||
|
||||
assert.deepEqual(captured.data, { files, strategy: 'Simple', taskType: 1 },
|
||||
'不选通道时不带字段,由服务端按模块默认兜(BRAND 默认 LOCAL)')
|
||||
})
|
||||
@@ -18,7 +18,7 @@ function mockRequest(
|
||||
}
|
||||
|
||||
/**
|
||||
* 采集路由开关读取口:前端两个工具页据这两个布尔决定「启动」走本地客户端还是后台。
|
||||
* 采集路由开关读取口:前端工具页据这几个布尔决定「启动」走本地客户端还是后台。
|
||||
* 只钉 URL 与返回值原样透传——开关真值来自服务端,前端不自己造默认。
|
||||
*/
|
||||
test('test_collect_routing_url_and_payload', async (t) => {
|
||||
@@ -27,7 +27,11 @@ test('test_collect_routing_url_and_payload', async (t) => {
|
||||
mockRequest(t, (config) => {
|
||||
captured = config
|
||||
return Promise.resolve({
|
||||
data: { success: true, message: 'ok', data: { appearancePatentLocal: true, similarAsinLocal: false } },
|
||||
data: {
|
||||
success: true,
|
||||
message: 'ok',
|
||||
data: { appearancePatentLocal: true, similarAsinLocal: false, brandLocal: true },
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
@@ -37,4 +41,5 @@ test('test_collect_routing_url_and_payload', async (t) => {
|
||||
assert.equal(captured.method, 'GET')
|
||||
assert.equal(routing.appearancePatentLocal, true)
|
||||
assert.equal(routing.similarAsinLocal, false)
|
||||
assert.equal(routing.brandLocal, true)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user