Files
crawler-plugin/admin-frontend-vue/tests/pending-phase4-大档上传接线.test.ts
T

94 lines
4.1 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.
// AC-11(R4):后管版本/教程大档上传接线的「源码尺子」那一半。
//
// Pending Phase 4 激活。本尺子的前身只断言 `files/chunk` 出现在非注释行,而那一句匹配来自
// `version-api.ts`/`tutorial-api.ts` 里一对**全仓零引用的死常量**(SOFTWARE_VERSION_CHUNK_ENDPOINT/
// TUTORIAL_CHUNK_ENDPOINT)——把真调用 `uploadFileInChunks(…)` 删掉,尺子照样绿(评审 P2-6 假绿形状)。
// 现在判据钉在**真调用点**上:被接线的那个函数体里必须出现 `uploadFileInChunks(`,
// 且死常量不许回来(`CHUNK_ENDPOINT =` 一出现即红)。删调用必判红,加死常量也判红。
//
// 行为口径在 tests/pure/pending-phase4-大档上传接线.test.ts(vitest 真调 uploadSoftwareVersion/
// uploadTutorialPackage,删掉调用同一刻那一边也红);本文件只补"接线没被搬空"这一层。
//
// 跑法:npm test(node --test tests/*.test.ts)。
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const here = dirname(fileURLToPath(import.meta.url))
const root = resolve(here, '..')
/** 把注释整段涂白(保行数):接线只写在注释里不算数。 */
function blankComments(source: string): string {
return source
.replace(/\/\*[\s\S]*?\*\//g, (block) => block.replace(/[^\n]/g, ' '))
.replace(/\/\/[^\n]*/g, (line) => ' '.repeat(line.length))
}
/** 取 `function <name>(…) {…}` 的函数体:大括号配对,跳过字符串与模板串里的括号。 */
function functionBodyOf(source: string, name: string): string {
const code = blankComments(source)
const head = code.indexOf(`function ${name}(`)
assert.notEqual(head, -1, `找不到函数 ${name}()——接线被删了还是改名了?`)
const open = code.indexOf('{', head)
assert.notEqual(open, -1, `${name}() 没有函数体`)
let depth = 0
let index = open
let quote = ''
for (; index < code.length; index += 1) {
const char = code[index]
if (quote) {
if (char === '\\') index += 1
else if (char === quote) quote = ''
continue
}
if (char === '"' || char === "'" || char === '`') { quote = char; continue }
if (char === '{') depth += 1
else if (char === '}') {
depth -= 1
if (depth === 0) return code.slice(open + 1, index)
}
}
return code.slice(open + 1)
}
/**
* 两格共同判据(读盘点留在用例体内:把 readFileSync 挪进 helper 是本仓 source-pin 棘轮点名的"搬家不算改进"):
* ① 从 @/api/chunk-upload 把分片函数引进来;② 被接线的函数体里真的调它;
* ③ 阈值同源;④ 死常量不得回来。
*/
function assertChunkedWiring(source: string, file: string, wiredFunction: string): void {
assert.match(
source,
/import\s*\{[^}]*uploadFileInChunks[^}]*\}\s*from\s*'@\/api\/chunk-upload'/,
`AC-11:${file} 没有从 @/api/chunk-upload 引入分片函数`,
)
const body = functionBodyOf(source, wiredFunction)
assert.match(
body,
/uploadFileInChunks\s*\(/,
`AC-11:${file}::${wiredFunction}() 里没有真调用 uploadFileInChunks(…)(大档仍走一次 PUT 直传,512MB 断网从 0 重来)`,
)
assert.match(
body,
/CHUNK_UPLOAD_THRESHOLD/,
`AC-11:${wiredFunction}() 的选路阈值必须与 chunk-upload 同源,不得各自写死 8MB`,
)
assert.doesNotMatch(
source,
/CHUNK_ENDPOINT\s*=/,
`AC-11:${file} 又立了一个没人引用的 CHUNK_ENDPOINT 常量——它会顶掉本尺子的匹配来源,正是 P2-6 的假绿形状`,
)
}
test('后管安装包上传必须真的调分片通道', () => {
const file = 'src/pages/records/version-api.ts'
assertChunkedWiring(readFileSync(resolve(root, file), 'utf-8'), file, 'uploadSoftwareVersion')
})
test('后管教程包上传必须真的调分片通道', () => {
const file = 'src/pages/records/tutorial-api.ts'
assertChunkedWiring(readFileSync(resolve(root, file), 'utf-8'), file, 'uploadTutorialPackage')
})