Files
crawler-plugin/frontend-vue/tests/pending-phase4-用户端稳定性.test.ts
T

92 lines
4.9 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-16/AC-17/AC-18(用户端侧,R7/S1-F1、S1-F3、P1-F2、P1-F4)
*
* Pending Phase 4 激活。现状(`docs/audit/稳定性与性能综合审计-2026-10-28.md` §3.4/§4.3):
*
* - **S1-F1** `src/shared/auth/ensure-auth.ts:50-57`:`ensureAuth()` 的 `catch` 不分原因,
* 任何异常(弱网、5xx、超时)都清掉 `aiimage_auth_token` 并返回 false ⇒ 路由守卫把用户
* 踢回登录页。HTTP 层的 401/4011 收口本来是对的(见 `session-expired-signout.test.ts`),
* 坏的是这条"什么错都算登录失效"的旁路。
* - **S1-F3** `src/shared/api/upload.ts`:大文件走单次 multipart + `timeout: 120000`,
* 大视频一断就从 0 重传,且没有进度。
* - **P1-F2 / P1-F4** `src/shared/composables/useTablePaging.ts` 只切渲染窗口(数据全量驻内存),
* `CostGuardAlert.vue`/`DeliveryVideoWorkspace.vue` 的 30s/5s 轮询不看页面可见性。
*
* 组件层本树无挂载测试的部分按既有惯例用源码尺子钉(见
* `tests/session-expired-signout.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, '..')
const read = (rel: string) => readFileSync(resolve(root, rel), 'utf-8')
const PENDING_AUTH = 'Pending Phase 4: AC-16 网络错误/5xx 不许清登录态'
const PENDING_UPLOAD = 'Pending Phase 4: AC-17 大文件分片续传与进度'
const PENDING_PERF = 'Pending Phase 4: AC-18 表格窗口化与轮询可见性'
test('pending_ac16_ensureAuth_只在鉴权失败时清态', () => {
const source = read('src/shared/auth/ensure-auth.ts')
const catchBlock = source.match(/catch\s*\([^)]*\)\s*\{[\s\S]*?\n\s*\}/g)?.join('\n') ?? ''
assert.ok(catchBlock, 'ensureAuth 必须有可判定的异常分支')
assert.match(catchBlock, /isUnauthorized|UNAUTHORIZED_CODE|status\s*===?\s*401|isAuthError/,
'S1-F1:清态前必须判定"这是 401/4011",不能任何异常都当登录失效')
assert.doesNotMatch(catchBlock, /removeItem\(AUTH_TOKEN_KEY\)[\s\S]{0,20}\}/,
'S1-F1:无条件 removeItem(AUTH_TOKEN_KEY) 的旁路必须收掉(弱网不该把人踢回登录页)')
})
test('pending_ac16_网络错误保留态后仍返回可续用', async () => {
const { onResponseRejected } = await import('../src/shared/api/http.ts')
const store = new Map<string, string>([['aiimage_auth_token', 'jwt-token'], ['uid', '1206']])
;(globalThis as Record<string, unknown>).window = {
localStorage: {
getItem: (k: string) => (store.has(k) ? store.get(k)! : null),
setItem: (k: string, v: string) => void store.set(k, v),
removeItem: (k: string) => void store.delete(k),
},
location: { pathname: '/home', search: '', assign: () => undefined },
addEventListener: () => undefined,
}
// 无 response 的对象=axios 网络层错误(断网/DNS/超时),不是 401
await assert.rejects(() => onResponseRejected({ message: 'Network Error' }), /Network Error/)
assert.equal(store.get('aiimage_auth_token'), 'jwt-token', 'AC-16:网络错误必须保留令牌')
assert.equal(store.get('uid'), '1206', 'AC-16:网络错误不许清用户身份')
})
test('pending_ac17_上传必须分片且不再单次 120s', () => {
const source = read('src/shared/api/upload.ts')
assert.match(source, /partSize|uploadPart|createMultipartUpload|chunk/,
'S1-F3:大文件必须走分片/断点续传')
assert.doesNotMatch(source, /timeout:\s*120000/,
'S1-F3:单次 120s 超时的整包 multipart 必须被分片通道取代')
assert.match(source, /onProgress|progress/, '进度必须可见')
// P3-5:桌面桥的浏览器降级直传也是大档出口,固定 120s 会在同一把尺子上漏半条链。
const bridge = read('src/shared/bridges/web-fallback.ts')
assert.doesNotMatch(bridge, /timeout:\s*120_?000/,
'P3-5:web-fallback 的大档上传仍写死 120s,慢链路传 Excel/zip 会被自己掐死')
assert.match(bridge, /uploadTimeoutForBytes/,
'P3-5:超时预算只有一个出口(upload.ts::uploadTimeoutForBytes),桥侧必须复用它')
})
test('pending_ac18_表格必须窗口化渲染或走服务端分页', () => {
const source = read('src/shared/composables/useTablePaging.ts')
assert.match(source, /serverSide|virtual|fetchPage|loadPage/,
'P1-F2:只切渲染窗口=数据仍全量驻内存,必须补服务端分页/虚拟滚动')
})
test('pending_ac18_轮询必须看页面可见性', () => {
for (const rel of ['src/shared/components/CostGuardAlert.vue',
'src/pages/image-video/components/DeliveryVideoWorkspace.vue']) {
const source = read(rel)
assert.match(source, /visibilityState|document\.hidden|visibilitychange/,
`P1-F4:${rel} 的定时轮询必须在隐藏标签页暂停`)
}
})