前端(任务卡进度条): 10 页接上实时进度,3 页没分母的显式写 null
后端已把心跳里的 current/total 透到 progress/light,但 TaskItemView.progress 是可空字段:
页面少写一行不会报错也不会红,界面上就是安静地没有进度条——11 类客户端任务里 7 类正是这样。
- 新增 src/shared/api/task-progress-live.ts:进度走独立通道,模块级缓存 + 60s 新鲜窗口
为什么不并进任务对象:各页 {...prev.task, ...detail.task} 合并里有 5 处带「变更签名」守卫
(rowsVersion|status|fileStatus|fileReady)。进度每轮都在动,而签名在「正在翻页、还没攒够一批
回传」的时段不变——照那条路走,进度条会一路停在上一次回传的数字上,看着像卡死
- 写入点挂在 light 出口(progress-light.ts)而不是聚合层:4 个页面直接调 getModuleProgressLight
不经过 getPollingProgressBatch,挂聚合层它们永远拿不到;命中缓存也要刷一遍读数,
否则进度会在缓存窗口里被自己的新鲜判定踢掉、凭空消失
- toBatchItems 与 LightPollingItem 补三字段:这里的键集是逐字段挑的,少挑一个就把后端给的数丢掉
- 10 页接 liveProgress:解决风控、跟价、定时匹配、状态查询、提款、巡店删除、商品管理采集等
- Convert/Dedupe/Split 三页写死 progress: null 并说明原因:它们卡列的是完成记录、没有可数的分母。
显式 null 与省略字段在代码里必须长得不一样,否则下一个人无法区分「没得画」和「忘了接」
- 新增 tests/task-progress-view-wiring.test.ts:适配器清单由文件系统给出、总数硬钉 16,
判据同时认 `progress:` 与 ES6 简写 `progress,`(只认冒号把 BrandBrandTab 误报成缺口,
而误报的尺子会被人当噪音绕过去);变异自证三处全 KILLED——抽掉跟价页的接线、
摘掉批次白名单的 progressTotal、去掉分母塌 0 时的旧读数清理,还原后字节哈希一致
- tests/task-progress-polling.test.ts 的整份形状断言同步加三字段(它本来就该在我改白名单时红)
This commit is contained in:
@@ -210,6 +210,8 @@ function toConvertTaskView(item: ConvertResultItem): TaskItemView {
|
||||
...(item.outputFilename ? [`压缩包文件:${item.outputFilename}`] : []),
|
||||
...(item.error ? [`错误信息:${userNotice(item.error)}`] : []),
|
||||
],
|
||||
// 显式给 null:这一类是点了就跑完的一次性操作,卡片列的是完成记录,没有可数的分母。写死 null 而不是省略字段,是为了让「没得画」和「忘了接」在代码里长得不一样(守卫:tests/task-progress-view-wiring.test.ts)。
|
||||
progress: null,
|
||||
source: item,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,6 +234,8 @@ function toDedupeTaskView(item: DedupeResultItem): TaskItemView {
|
||||
...(item.outputFilename ? [`输出文件:${item.outputFilename}`] : []),
|
||||
...(item.error ? [`错误信息:${userNotice(item.error)}`] : []),
|
||||
],
|
||||
// 显式给 null:这一类是点了就跑完的一次性操作,卡片列的是完成记录,没有可数的分母。写死 null 而不是省略字段,是为了让「没得画」和「忘了接」在代码里长得不一样(守卫:tests/task-progress-view-wiring.test.ts)。
|
||||
progress: null,
|
||||
source: item,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,6 +297,7 @@ import { isRecordMissingError } from '@/shared/utils/task-queue-state.ts'
|
||||
import { useTablePaging } from '@/shared/composables/useTablePaging.ts'
|
||||
import { getModuleProgressLight } from '@/shared/api/progress-light.ts'
|
||||
import { createRowsVersionTracker } from '@/shared/api/task-progress-polling.ts'
|
||||
import { liveProgress } from '@/shared/api/task-progress-live.ts'
|
||||
import { confirmTaskDeletion } from '@/shared/task-delete-confirm';
|
||||
import { TERMINAL_TASK_STATUSES, isTerminalTaskStatus, waitForTaskTerminal as waitForTaskTerminalShared } from '@/shared/utils/task-terminal'
|
||||
|
||||
@@ -397,6 +398,7 @@ function toPatrolTaskView(item: PatrolDeleteHistoryItem): TaskItemView {
|
||||
`模板: ${formatTemplateSummary(item)}`,
|
||||
...(formatTaskGroupErrors(item) ? [`错误: ${userNotice(formatTaskGroupErrors(item))}`] : []),
|
||||
],
|
||||
progress: liveProgress(Number(item.taskId)),
|
||||
source: item,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,6 +277,7 @@ import {
|
||||
type PriceTrackTaskDetailVo,
|
||||
} from '@/shared/api/java-modules'
|
||||
import { getPollingProgressBatch } from '@/shared/api/task-progress-polling.ts'
|
||||
import { liveProgress } from '@/shared/api/task-progress-live.ts'
|
||||
import { formatDateTime } from '@/shared/utils/datetime'
|
||||
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
|
||||
import { isTerminalTaskStatus, waitForTaskTerminal as waitForTaskTerminalShared } from '@/shared/utils/task-terminal'
|
||||
@@ -1574,6 +1575,7 @@ function toPriceTrackTaskView(item: PriceTrackHistoryItem): TaskItemView {
|
||||
...(item.outputFilename ? [`文件:${item.outputFilename}`] : []),
|
||||
...(item.error ? [`错误:${userNotice(item.error)}`] : []),
|
||||
],
|
||||
progress: liveProgress(Number(item.taskId)),
|
||||
source: item,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,6 +193,7 @@ import { runBatchDelete } from '@/shared/utils/batch-delete'
|
||||
import { isRecordMissingError } from '@/shared/utils/task-queue-state.ts'
|
||||
import { useTablePaging } from '@/shared/composables/useTablePaging.ts'
|
||||
import { getPollingProgressBatch } from '@/shared/api/task-progress-polling.ts'
|
||||
import { liveProgress } from '@/shared/api/task-progress-live.ts'
|
||||
import { confirmTaskDeletion } from '@/shared/task-delete-confirm'
|
||||
|
||||
const shopInput = ref('')
|
||||
@@ -724,6 +725,7 @@ function toRiskTaskView(item: ProductRiskHistoryItem): TaskItemView {
|
||||
...(item.outputFilename ? [`文件:${item.outputFilename}`] : []),
|
||||
...(item.error ? [`错误:${userNotice(item.error)}`] : []),
|
||||
],
|
||||
progress: liveProgress(Number(item.taskId)),
|
||||
source: item,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,6 +252,7 @@ import { isRecordMissingError, parseTaskStartTimes, removeTaskStartTime, upsertT
|
||||
import { useTablePaging } from '@/shared/composables/useTablePaging.ts'
|
||||
import { getModuleProgressLight } from '@/shared/api/progress-light.ts'
|
||||
import { createRowsVersionTracker } from '@/shared/api/task-progress-polling.ts'
|
||||
import { liveProgress } from '@/shared/api/task-progress-live.ts'
|
||||
import { confirmTaskDeletion } from '@/shared/task-delete-confirm';
|
||||
import { TERMINAL_TASK_STATUSES, isTerminalTaskStatus, waitForTaskTerminal as waitForTaskTerminalShared } from '@/shared/utils/task-terminal'
|
||||
import { dispatchedPruneNotice, mergeDispatchedArchive, pruneRows } from '@/shared/utils/dispatch-row-prune';
|
||||
@@ -355,6 +356,7 @@ function toQueryAsinTaskView(item: QueryAsinHistoryItem): TaskItemView {
|
||||
`ASIN结构: ${formatTemplateSummary(item)}`,
|
||||
...(item.error ? [`错误: ${userNotice(item.error)}`] : []),
|
||||
],
|
||||
progress: liveProgress(Number(item.taskId)),
|
||||
source: item,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -170,6 +170,7 @@ import { runBatchDelete } from '@/shared/utils/batch-delete'
|
||||
import { useTablePaging } from '@/shared/composables/useTablePaging.ts'
|
||||
import { getModuleProgressLight } from '@/shared/api/progress-light.ts'
|
||||
import { createRowsVersionTracker } from '@/shared/api/task-progress-polling.ts'
|
||||
import { liveProgress } from '@/shared/api/task-progress-live.ts'
|
||||
import { confirmTaskDeletion } from '@/shared/task-delete-confirm'
|
||||
|
||||
const COUNTRY_OPTIONS = [
|
||||
@@ -256,6 +257,7 @@ function toShopDataCrawlTaskView(item: ShopDataCrawlHistoryItem): TaskItemView {
|
||||
...(item.platform ? [`平台:${item.platform}`] : []),
|
||||
...(item.outputFilename ? [`文件:${item.outputFilename}`] : []),
|
||||
],
|
||||
progress: liveProgress(Number(item.taskId)),
|
||||
source: item,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,6 +129,7 @@ import ZiniaoVersionSetting from '@/shared/components/ZiniaoVersionSetting.vue'
|
||||
import { LISTING_FILTER_OPTIONS, type ListingFilterValue } from '@/pages/brand/components/listingFilters'
|
||||
import { activateShopMatchTask, addShopMatchCandidate, createShopMatchTask, deleteShopMatchCandidate, deleteShopMatchHistory, deleteShopMatchTask, getShopMatchCountryPreference, getShopMatchDashboard, getShopMatchHistory, getShopMatchResultDownloadUrl, getShopMatchTaskProgressBatch, listShopMatchCandidates, matchShopMatchShops, putShopMatchCountryPreference, type ShopMatchCandidateVo, type ShopMatchCreateTaskItem, type ShopMatchDashboardVo, type ShopMatchHistoryItem, type ShopMatchShopQueueItem, type ShopMatchTaskDetailVo } from '@/shared/api/java-modules'
|
||||
import { getPollingProgressBatch } from '@/shared/api/task-progress-polling.ts'
|
||||
import { liveProgress } from '@/shared/api/task-progress-live.ts'
|
||||
import { getPywebviewApi } from '@/shared/bridges/pywebview'
|
||||
import { getTaskPollIntervalMs } from '@/shared/task-progress-config'
|
||||
import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
|
||||
@@ -236,6 +237,7 @@ function toShopMatchTaskView(item: ShopMatchHistoryItem): TaskItemView {
|
||||
...(item.outputFilename ? [`文件: ${item.outputFilename}`] : []),
|
||||
...(item.error ? [`错误: ${userNotice(item.error)}`] : []),
|
||||
],
|
||||
progress: liveProgress(Number(item.taskId)),
|
||||
source: item,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,6 +233,8 @@ function toSplitTaskView(item: SplitResultItem): TaskItemView {
|
||||
...(item.entries && item.entries.length ? [`压缩包内容:${formatSplitEntries(item.entries)}`] : []),
|
||||
...(item.error ? [`错误信息:${userNotice(item.error)}`] : []),
|
||||
],
|
||||
// 显式给 null:这一类是点了就跑完的一次性操作,卡片列的是完成记录,没有可数的分母。写死 null 而不是省略字段,是为了让「没得画」和「忘了接」在代码里长得不一样(守卫:tests/task-progress-view-wiring.test.ts)。
|
||||
progress: null,
|
||||
source: item,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,6 +279,7 @@ import { isRecordMissingError, parseTaskStartTimes, removeTaskStartTime, upsertT
|
||||
import { useTablePaging } from '@/shared/composables/useTablePaging.ts'
|
||||
import { getModuleProgressLight } from '@/shared/api/progress-light.ts'
|
||||
import { createRowsVersionTracker } from '@/shared/api/task-progress-polling.ts'
|
||||
import { liveProgress } from '@/shared/api/task-progress-live.ts'
|
||||
import { confirmTaskDeletion } from '@/shared/task-delete-confirm'
|
||||
import { waitForTaskTerminal as waitForTaskTerminalShared } from '@/shared/utils/task-terminal'
|
||||
|
||||
@@ -386,6 +387,7 @@ function toWithdrawTaskView(item: WithdrawTaskGroupItem): TaskItemView {
|
||||
statusText: statusText(item.taskStatus),
|
||||
statusClass: statusClass(item.taskStatus),
|
||||
extraLines: item.error ? [`错误:${userNotice(item.error)}`] : [],
|
||||
progress: liveProgress(Number(item.taskId)),
|
||||
source: item,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { buildJavaUrl } from './url.ts'
|
||||
import { API_ENDPOINTS } from './endpoints.ts'
|
||||
import { getTaskProgressCacheTtlMs, getTaskProgressTimeoutMs } from '../task-progress-config.ts'
|
||||
import { createTaskProgressRequestCache } from '../task-progress-request-cache.ts'
|
||||
import { rememberLiveProgress } from './task-progress-live.ts'
|
||||
import type { TaskProgressBatchVo } from './types/task.ts'
|
||||
|
||||
/**
|
||||
@@ -44,6 +45,13 @@ export interface TaskProgressLightItem {
|
||||
updatedAt?: string | null
|
||||
/** 结果行版本(「最后变更时间毫秒#行数」):未变说明行数据没动过,可跳过重型 batch。 */
|
||||
rowsVersion?: string | null
|
||||
/**
|
||||
* 客户端心跳上报的执行进度(2026-10-10)。后端保证三者同生同灭:
|
||||
* 读不到位置、或分母不是正数时三个都是 null,所以这里的「没有」不等于 0%。
|
||||
*/
|
||||
progressCurrent?: number | null
|
||||
progressTotal?: number | null
|
||||
progressLabel?: string | null
|
||||
}
|
||||
|
||||
export interface GetModuleProgressLightOptions {
|
||||
@@ -85,6 +93,9 @@ export function getModuleProgressLight(
|
||||
const cacheKey = `${path}::${normalizedTaskIds.join(",")}`
|
||||
const cached = progressLightCache.get(cacheKey)
|
||||
if (!options.force && cached !== undefined) {
|
||||
// 命中缓存也要刷新一遍读数:进度按「最后一次被任何一轮 light 看到」计时,
|
||||
// 只在真发请求时刷新会让进度条在缓存窗口里被判成过期而凭空消失。
|
||||
rememberLiveProgress(cached.items)
|
||||
return Promise.resolve(cached)
|
||||
}
|
||||
const inflight = progressLightCache.getInflight(cacheKey)
|
||||
@@ -104,6 +115,7 @@ export function getModuleProgressLight(
|
||||
missingTaskIds: data?.missingTaskIds || [],
|
||||
}
|
||||
progressLightCache.set(cacheKey, normalized)
|
||||
rememberLiveProgress(normalized.items)
|
||||
return normalized
|
||||
})
|
||||
.finally(() => {
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* 心跳上报的实时进度(模块级缓存,2026-10-10)。
|
||||
*
|
||||
* 为什么不直接把 progressCurrent/progressTotal 并进任务对象:各页把 light 结果
|
||||
* `{...prev.task, ...detail.task}` 合并进快照时,有 5 处带「变更签名」守卫
|
||||
* (签名 = rowsVersion|status|fileStatus|fileReady)。进度每轮都在动,而签名在
|
||||
* 「正在翻页、还没攒够一批回传」的时段里不变——照那条路走,进度条会一路停在上一次
|
||||
* 回传时的数字,看着像卡住。签名守卫是为「行数据按需拉取」省流量而设的,不该为它让步。
|
||||
*
|
||||
* 所以进度走这条独立通道:轮询出口写入一次,页面渲染时按 taskId 读。
|
||||
* 读不到(老后端、老客户端没报位置、或超过 FRESH_WINDOW_MS 没刷新)就返回 null,
|
||||
* 由卡片决定**不画**——「没有进度」和「进度 0%」是两件事。
|
||||
*/
|
||||
|
||||
export interface LiveTaskProgress {
|
||||
percent: number
|
||||
stage?: string
|
||||
}
|
||||
|
||||
interface ProgressEntry {
|
||||
current: number
|
||||
total: number
|
||||
label?: string
|
||||
seenAt: number
|
||||
}
|
||||
|
||||
/** 超过这个时长没被任何一轮 light 刷新,就当作已经没有活的进度。 */
|
||||
const FRESH_WINDOW_MS = 60_000
|
||||
/** 缓存上限:正常一页在册任务远小于它,触顶时按最久未刷新淘汰(防无界增长)。 */
|
||||
const MAX_ENTRIES = 800
|
||||
|
||||
const entries = new Map<number, ProgressEntry>()
|
||||
|
||||
function clampPercent(current: number, total: number): number {
|
||||
const percent = Math.floor((current * 100) / total)
|
||||
if (!Number.isFinite(percent)) return 0
|
||||
return Math.min(100, Math.max(0, percent))
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录一轮 light 响应里的进度。
|
||||
*
|
||||
* 后端已保证「读不到位置」时三个字段一起为 null,这里再守一道:分母非正数就不登记,
|
||||
* 免得 0/0 被算成一个 0% 的空轨(客户端归一化在四级 key 全缺时正是发 0/0)。
|
||||
*/
|
||||
export function rememberLiveProgress(lightItems: unknown[] | null | undefined): void {
|
||||
const now = Date.now()
|
||||
for (const raw of lightItems || []) {
|
||||
const item = raw as { taskId?: number; progressTotal?: number | null; progressCurrent?: number | null; progressLabel?: string | null } | null
|
||||
if (!item || typeof item.taskId !== 'number' || item.taskId <= 0) continue
|
||||
const total = typeof item.progressTotal === 'number' ? item.progressTotal : 0
|
||||
if (total <= 0) {
|
||||
entries.delete(item.taskId)
|
||||
continue
|
||||
}
|
||||
const current = typeof item.progressCurrent === 'number' && item.progressCurrent > 0 ? item.progressCurrent : 0
|
||||
const label = typeof item.progressLabel === 'string' && item.progressLabel.trim() ? item.progressLabel.trim() : undefined
|
||||
entries.set(item.taskId, { current, total, label, seenAt: now })
|
||||
}
|
||||
if (entries.size > MAX_ENTRIES) {
|
||||
const stale = Array.from(entries.entries())
|
||||
.sort((left, right) => left[1].seenAt - right[1].seenAt)
|
||||
.slice(0, entries.size - MAX_ENTRIES)
|
||||
for (const [taskId] of stale) entries.delete(taskId)
|
||||
}
|
||||
}
|
||||
|
||||
/** 取某任务的实时进度;没有活的读数时返回 null(调用方不得折成 0%)。 */
|
||||
export function liveProgress(taskId: number | null | undefined, now: number = Date.now()): LiveTaskProgress | null {
|
||||
if (typeof taskId !== 'number' || taskId <= 0) return null
|
||||
const entry = entries.get(taskId)
|
||||
if (!entry) return null
|
||||
if (now - entry.seenAt > FRESH_WINDOW_MS) {
|
||||
entries.delete(taskId)
|
||||
return null
|
||||
}
|
||||
return { percent: clampPercent(entry.current, entry.total), stage: entry.label }
|
||||
}
|
||||
|
||||
/** 任务结束或页面卸载时清掉读数。 */
|
||||
export function forgetLiveProgress(taskId: number): void {
|
||||
entries.delete(taskId)
|
||||
}
|
||||
|
||||
export function resetLiveProgress(): void {
|
||||
entries.clear()
|
||||
}
|
||||
@@ -25,6 +25,9 @@ export interface LightPollingItem {
|
||||
fileError?: string | null
|
||||
fileReady?: boolean | null
|
||||
updatedAt?: string | null
|
||||
progressCurrent?: number | null
|
||||
progressTotal?: number | null
|
||||
progressLabel?: string | null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +36,8 @@ export function toBatchItems(lightItems: unknown[]): LightPollingItem[] {
|
||||
for (const raw of lightItems || []) {
|
||||
const item = raw as { taskId?: number; status?: string | null; statusCode?: string | null
|
||||
fileStatus?: string | null; fileError?: string | null; fileReady?: boolean | null
|
||||
updatedAt?: string | null } | null
|
||||
updatedAt?: string | null; progressCurrent?: number | null; progressTotal?: number | null
|
||||
progressLabel?: string | null } | null
|
||||
if (!item || typeof item.taskId !== 'number' || item.taskId <= 0) {
|
||||
continue
|
||||
}
|
||||
@@ -48,6 +52,9 @@ export function toBatchItems(lightItems: unknown[]): LightPollingItem[] {
|
||||
fileError: item.fileError ?? null,
|
||||
fileReady: item.fileReady ?? null,
|
||||
updatedAt: item.updatedAt ?? null,
|
||||
progressCurrent: item.progressCurrent ?? null,
|
||||
progressTotal: item.progressTotal ?? null,
|
||||
progressLabel: item.progressLabel ?? null,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import {
|
||||
forgetLiveProgress,
|
||||
liveProgress,
|
||||
rememberLiveProgress,
|
||||
resetLiveProgress,
|
||||
} from '../src/shared/api/task-progress-live.ts'
|
||||
|
||||
test('心跳读数换算成百分比与阶段文案', () => {
|
||||
resetLiveProgress()
|
||||
rememberLiveProgress([
|
||||
{ taskId: 1, progressCurrent: 12, progressTotal: 50, progressLabel: '国家 US(3/5)' },
|
||||
])
|
||||
|
||||
assert.deepEqual(liveProgress(1, 1_000), { percent: 24, stage: '国家 US(3/5)' })
|
||||
})
|
||||
|
||||
test('分母非正数一律不登记(没有进度不等于 0%)', () => {
|
||||
resetLiveProgress()
|
||||
rememberLiveProgress([
|
||||
{ taskId: 2, progressCurrent: 0, progressTotal: 0 },
|
||||
{ taskId: 3, progressCurrent: 5, progressTotal: null },
|
||||
])
|
||||
|
||||
assert.equal(liveProgress(2), null, '0/0 必须读成「没有进度」,否则卡片画一条 0% 空轨')
|
||||
assert.equal(liveProgress(3), null)
|
||||
})
|
||||
|
||||
test('分母塌回 0 时把旧读数清掉,进度条不许停在最后一次回传的数字上', () => {
|
||||
resetLiveProgress()
|
||||
rememberLiveProgress([{ taskId: 4, progressCurrent: 30, progressTotal: 40 }])
|
||||
assert.equal(liveProgress(4)?.percent, 75)
|
||||
|
||||
rememberLiveProgress([{ taskId: 4, progressCurrent: 0, progressTotal: 0 }])
|
||||
|
||||
assert.equal(liveProgress(4), null)
|
||||
})
|
||||
|
||||
test('百分比钳在 0..100,超过分母不给 114%', () => {
|
||||
resetLiveProgress()
|
||||
rememberLiveProgress([
|
||||
{ taskId: 5, progressCurrent: 57, progressTotal: 50 },
|
||||
{ taskId: 6, progressCurrent: -3, progressTotal: 50 },
|
||||
])
|
||||
|
||||
assert.equal(liveProgress(5)?.percent, 100)
|
||||
assert.equal(liveProgress(6)?.percent, 0)
|
||||
})
|
||||
|
||||
test('超过新鲜窗口不再报进度(心跳停了就别说还在跑)', () => {
|
||||
resetLiveProgress()
|
||||
rememberLiveProgress([{ taskId: 7, progressCurrent: 1, progressTotal: 4 }])
|
||||
const seenAt = Date.now()
|
||||
|
||||
assert.equal(liveProgress(7, seenAt + 59_000)?.percent, 25)
|
||||
assert.equal(liveProgress(7, seenAt + 61_000), null, '60 秒没被任何一轮 light 刷新就当作没有活进度')
|
||||
assert.equal(liveProgress(7), null, '过期条目要顺手清掉,不能留着让 Map 无界增长')
|
||||
})
|
||||
|
||||
test('forget 与非法 taskId 安全', () => {
|
||||
resetLiveProgress()
|
||||
rememberLiveProgress([{ taskId: 8, progressCurrent: 1, progressTotal: 2 }])
|
||||
assert.equal(liveProgress(8)?.percent, 50)
|
||||
|
||||
forgetLiveProgress(8)
|
||||
assert.equal(liveProgress(8), null)
|
||||
assert.equal(liveProgress(null), null)
|
||||
assert.equal(liveProgress(0), null)
|
||||
assert.equal(liveProgress(-5), null)
|
||||
})
|
||||
|
||||
test('文案为空白时不产出空 stage', () => {
|
||||
resetLiveProgress()
|
||||
rememberLiveProgress([{ taskId: 9, progressCurrent: 1, progressTotal: 2, progressLabel: ' ' }])
|
||||
|
||||
assert.deepEqual(liveProgress(9), { percent: 50, stage: undefined })
|
||||
})
|
||||
@@ -16,9 +16,22 @@ test('轻量项归一为重型 batch 的嵌套形状', () => {
|
||||
fileError: null,
|
||||
fileReady: false,
|
||||
updatedAt: '2026-09-14T06:00:00',
|
||||
progressCurrent: null,
|
||||
progressTotal: null,
|
||||
progressLabel: null,
|
||||
})
|
||||
})
|
||||
|
||||
test('心跳进度三字段照抄进嵌套 task,不被白名单丢掉', () => {
|
||||
const items = toBatchItems([
|
||||
{ taskId: 8, status: 'RUNNING', progressCurrent: 12, progressTotal: 50, progressLabel: 'US(3/5)' },
|
||||
])
|
||||
|
||||
assert.equal(items[0].task?.progressCurrent, 12)
|
||||
assert.equal(items[0].task?.progressTotal, 50)
|
||||
assert.equal(items[0].task?.progressLabel, 'US(3/5)')
|
||||
})
|
||||
|
||||
test('缺少 taskId 的项被丢弃,非法 taskId 同样丢弃', () => {
|
||||
const items = toBatchItems([{ status: 'RUNNING' }, { taskId: 0, status: 'RUNNING' }, { taskId: 3 }])
|
||||
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync, readdirSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
|
||||
/**
|
||||
* 任务卡片的「进度条接线」普查。
|
||||
*
|
||||
* 为什么要它:TaskItemView.progress 是可空字段,页面少写一行不会报错、不会红,
|
||||
* 界面上就是安静地没有进度条。11 个任务页各写一处,漏哪一处全靠这条出声。
|
||||
*
|
||||
* 分母不手抄:扫 src/pages/brand/components 下所有写了 `to*TaskView(...): TaskItemView`
|
||||
* 适配器的页面,清单由文件系统给出;再把总数硬钉住——新增页面没接进度、或有人把
|
||||
* 适配器改名躲开扫描,都在这条上出声(只比总数会被「内部搬家」绕过,所以逐个点名)。
|
||||
*/
|
||||
|
||||
const COMPONENTS = join(import.meta.dirname, '..', 'src', 'pages', 'brand', 'components')
|
||||
const ADAPTER_PATTERN = /function (to\w*TaskView)\(/
|
||||
|
||||
/**
|
||||
* 判据只看「适配器返回的对象里出现了 progress 属性」,两种写法都认:
|
||||
* `progress: {…}` 与 ES6 简写 `progress,`(BrandBrandTab 用后者,只认冒号会把它误报成缺口,
|
||||
* 而误报的尺子很快会被人当噪音绕过去)。
|
||||
*/
|
||||
const PROGRESS_KEY = /^\s*(?:progress\s*:|progress\s*,)/m
|
||||
|
||||
function adaptersOf(file: string) {
|
||||
const lines = readFileSync(join(COMPONENTS, file), 'utf8').split(/\r?\n/)
|
||||
const found: { name: string; start: number; end: number }[] = []
|
||||
for (let index = 0; index < lines.length; index += 1) {
|
||||
const matched = ADAPTER_PATTERN.exec(lines[index])
|
||||
if (!matched) continue
|
||||
let end = index
|
||||
while (end + 1 < lines.length && lines[end + 1] !== '}') end += 1
|
||||
found.push({ name: matched[1], start: index, end })
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
const pages = readdirSync(COMPONENTS).filter((name) => name.endsWith('.vue'))
|
||||
const seen = pages.flatMap((file) => adaptersOf(file).map((a) => ({ file, ...a })))
|
||||
|
||||
test('被扫描的任务卡适配器数量必须硬钉(少扫或多扫都说明扫描面变了)', () => {
|
||||
assert.equal(seen.length, 16, `扫到 ${seen.length} 个 to*TaskView 适配器:${seen.map((s) => `${s.file}#${s.name}`).join(', ')}`)
|
||||
})
|
||||
|
||||
test('每个任务卡适配器都必须给出进度读数,或明确给出 null', () => {
|
||||
const offenders = seen
|
||||
.filter((entry) => {
|
||||
const lines = readFileSync(join(COMPONENTS, entry.file), 'utf8').split(/\r?\n/)
|
||||
const body = lines.slice(entry.start, entry.end + 1).join('\n')
|
||||
return !PROGRESS_KEY.test(body)
|
||||
})
|
||||
.map((entry) => `${entry.file}#${entry.name}`)
|
||||
assert.deepEqual(offenders, [], '这些任务页画不出进度条:' + offenders.join(', '))
|
||||
})
|
||||
|
||||
test('实时进度通道的写入点只有一个,且在 light 出口上', () => {
|
||||
const sharedDir = join(import.meta.dirname, '..', 'src', 'shared', 'api')
|
||||
const callers = readdirSync(sharedDir)
|
||||
.filter((name) => name.endsWith('.ts') && name !== 'task-progress-live.ts')
|
||||
.filter((name) => readFileSync(join(sharedDir, name), 'utf8').includes('rememberLiveProgress('))
|
||||
assert.deepEqual(callers, ['progress-light.ts'],
|
||||
'rememberLiveProgress 的调用者必须是 light 出口一处;多处写会让刷新时机和过期判定分家:' + callers.join(', '))
|
||||
})
|
||||
|
||||
test('light 白名单与后端 VO 的进度三字段同名同形', () => {
|
||||
const light = readFileSync(join(import.meta.dirname, '..', 'src', 'shared', 'api', 'progress-light.ts'), 'utf8')
|
||||
const polling = readFileSync(join(import.meta.dirname, '..', 'src', 'shared', 'api', 'task-progress-polling.ts'), 'utf8')
|
||||
for (const field of ['progressCurrent', 'progressTotal', 'progressLabel']) {
|
||||
assert.ok(light.includes(`${field}?:`), `progress-light.ts 的白名单缺 ${field}`)
|
||||
// toBatchItems 的键集是逐字段挑的,少挑一个就把后端给的数丢掉
|
||||
assert.ok(polling.includes(`${field}: item.${field} ?? null`), `task-progress-polling.ts 归一时丢了 ${field}`)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user