import test from 'node:test' import assert from 'node:assert/strict' import { readSource, occurrences } from './helpers.ts' // 10-04 三端性能审计(我逐行核过的两条重复请求): // ① DuplicateCheckPage.reAnalyze() 先 await fetchDuplicateConsole(全空参数) 把返回值丢掉, // 再 loadConsole() 拉一次 —— 服务端 console 端点纯读(loadLatest 按行号缓存,无"预热"必要), // 所以那一次是白跑的整集查询。 // ② TaskCenterBoardPage.refreshAll() 的 Promise.all 里无条件带 loadShopOptions(), // 而它是 30s 心跳(POLL_INTERVAL_MS)走的同一条函数 —— 店铺候选是字典数据 // (后端 SELECT DISTINCT 无缓存),进热路径等于每轮心跳多打一次全库去重。 // 两条都用「排他 + 精确锚」断言:既钉住修好的形状,也钉住"旧的无条件写法"不得回流。 /** 取顶层函数体(从签名后的第一个 { 到配对的 })。 */ function functionBody(source: string, signature: string): string { const start = source.indexOf(signature) assert.notEqual(start, -1, `源码里找不到 ${signature}`) const open = source.indexOf('{', start + signature.length) assert.notEqual(open, -1, `${signature} 后面没有函数体`) let depth = 0 for (let i = open; i < source.length; i += 1) { if (source[i] === '{') depth += 1 else if (source[i] === '}') { depth -= 1 if (depth === 0) return source.slice(open + 1, i) } } assert.fail(`${signature} 的函数体括号没闭合`) } test('perf_dup_reanalyze_does_not_fetch_console_twice', () => { const page = readSource('src/pages/tasks/DuplicateCheckPage.vue') const body = functionBody(page, 'async function reAnalyze(') // 丢弃返回值的整集查询必须消失(修复前正是这一行让它翻倍) assert.equal(occurrences(body, 'fetchDuplicateConsole('), 0, 'reAnalyze 不应自己再拉一次 duplicate-check-console') assert.equal(occurrences(body, 'loadConsole('), 1, 'reAnalyze 只经 loadConsole() 拉一次') }) test('perf_board_shop_options_stay_out_of_heartbeat', () => { const board = readSource('src/pages/task-center/TaskCenterBoardPage.vue') const body = functionBody(board, 'async function refreshAll(') assert.ok(/silent\s*.*loadShopOptions/s.test(body.split('\n').filter((l) => /loadShopOptions/.test(l)).join('\n')), 'loadShopOptions 必须由 silent 分档,只在进页/手动刷新/切页签时重拉') // 无条件那一行(修复前的形状)不得回流 assert.equal(occurrences(body, '\n loadShopOptions(),'), 0, 'refreshAll 里不得保留无条件的 loadShopOptions() 直列') }) test('perf_board_heartbeat_is_the_silent_path', () => { // 上一那条断言只有"心跳确实走 silent"才成立;这里钉住这个前提本身。 const board = readSource('src/pages/task-center/TaskCenterBoardPage.vue') assert.match(board, /setInterval\([\s\S]{0,200}refreshAll\(true\)[\s\S]{0,200}POLL_INTERVAL_MS/, '30s 心跳必须以 silent=true 调 refreshAll') assert.match(board, /POLL_INTERVAL_MS = 30_000/, '心跳周期仍是 30s(改动没顺手调周期)') })