修复:完善测试数据模式回退
This commit is contained in:
@@ -3,6 +3,7 @@ import { lstat, readFile, realpath } from 'node:fs/promises'
|
||||
import { test } from 'node:test'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import path from 'node:path'
|
||||
import * as testBatchContext from '../src/utils/testBatchContext.js'
|
||||
|
||||
const frontendRoot = fileURLToPath(new URL('..', import.meta.url))
|
||||
|
||||
@@ -837,30 +838,108 @@ function hasExpectedRequest(expectedRequest) {
|
||||
const source = (relativePath) => readFile(path.join(frontendRoot, relativePath), 'utf8')
|
||||
|
||||
test('Web 测试模式统一附加请求头并通过路由重建清空页面状态', async () => {
|
||||
const requestSource = await source('src/utils/request.js')
|
||||
const appMainSource = await source('src/layout/components/AppMain.vue')
|
||||
const navbarSource = await source('src/layout/components/Navbar.vue')
|
||||
|
||||
assert.match(requestSource, /X-Test-Batch-Id/)
|
||||
assert.match(requestSource, /TEST_BATCH_UNAVAILABLE/)
|
||||
assert.match(appMainSource, /scopeVersion/)
|
||||
assert.match(navbarSource, /TestBatchModeSwitch/)
|
||||
assert.match(navbarSource, /测试数据/)
|
||||
})
|
||||
|
||||
test('Web 测试批次上下文仅持久化 ID,登出清除 ID 且管理员标记严格来自后端布尔值', async () => {
|
||||
const contextSource = await source('src/utils/testBatchContext.js')
|
||||
const storeSource = await source('src/store/modules/testBatch.js')
|
||||
const userSource = await source('src/store/modules/user.js')
|
||||
|
||||
assert.match(contextSource, /sessionStorage/)
|
||||
assert.doesNotMatch(contextSource, /batchName|status|admin|permission/i)
|
||||
assert.match(storeSource, /getStoredTestBatchId/)
|
||||
assert.match(storeSource, /setStoredTestBatchId/)
|
||||
assert.match(storeSource, /clearStoredTestBatchId/)
|
||||
assert.match(userSource, /admin:\s*false/)
|
||||
assert.match(userSource, /this\.admin\s*=\s*user\.admin\s*===\s*true/)
|
||||
assert.match(userSource, /clearStoredTestBatchId\(\)/)
|
||||
assert.match(userSource, /logoutWithLocalCleanup/)
|
||||
})
|
||||
|
||||
test('Web 测试批次真实上下文逻辑只在登录测试模式附加请求头,并对并发失效做一次回退', async () => {
|
||||
const storage = new Map()
|
||||
globalThis.sessionStorage = {
|
||||
getItem: key => storage.get(key) || null,
|
||||
setItem: (key, value) => storage.set(key, value),
|
||||
removeItem: key => storage.delete(key)
|
||||
}
|
||||
|
||||
const formalConfig = { headers: {} }
|
||||
testBatchContext.applyTestBatchHeader(formalConfig, '', 'batch-1')
|
||||
assert.deepEqual(formalConfig.headers, {})
|
||||
|
||||
const testConfig = { headers: {} }
|
||||
testBatchContext.applyTestBatchHeader(testConfig, 'token', 'batch-1')
|
||||
assert.equal(testConfig.headers['X-Test-Batch-Id'], 'batch-1')
|
||||
|
||||
testBatchContext.setStoredTestBatchId('batch-1')
|
||||
let loadCount = 0
|
||||
let leaveCount = 0
|
||||
let notifyCount = 0
|
||||
const loadStore = async () => {
|
||||
loadCount += 1
|
||||
await Promise.resolve()
|
||||
return { leaveTestMode: () => { leaveCount += 1 } }
|
||||
}
|
||||
const recovery = {
|
||||
loadStore,
|
||||
notify: () => { notifyCount += 1 },
|
||||
message: '测试批次不可用'
|
||||
}
|
||||
await Promise.all([
|
||||
testBatchContext.recoverUnavailableTestBatch(recovery),
|
||||
testBatchContext.recoverUnavailableTestBatch(recovery),
|
||||
testBatchContext.recoverUnavailableTestBatch(recovery)
|
||||
])
|
||||
assert.equal(loadCount, 1)
|
||||
assert.equal(leaveCount, 1)
|
||||
assert.equal(notifyCount, 1)
|
||||
assert.equal(testBatchContext.getStoredTestBatchId(), '')
|
||||
|
||||
await testBatchContext.recoverUnavailableTestBatch(recovery)
|
||||
assert.equal(loadCount, 2)
|
||||
assert.equal(leaveCount, 2)
|
||||
assert.equal(notifyCount, 2)
|
||||
})
|
||||
|
||||
test('刷新失效批次复用统一回退提示,登出无论远端结果都清理管理员与批次上下文', async () => {
|
||||
const storeSource = await source('src/store/modules/testBatch.js')
|
||||
const userSource = await source('src/store/modules/user.js')
|
||||
assert.match(storeSource, /recoverUnavailableTestBatch/)
|
||||
assert.match(storeSource, /当前测试批次已不可用/)
|
||||
assert.match(userSource, /logoutWithLocalCleanup/)
|
||||
|
||||
globalThis.sessionStorage.setItem('test-batch-id', 'batch-2')
|
||||
const state = { admin: true }
|
||||
await testBatchContext.logoutWithLocalCleanup(() => Promise.resolve('ok'), state)
|
||||
assert.equal(state.admin, false)
|
||||
assert.equal(testBatchContext.getStoredTestBatchId(), '')
|
||||
|
||||
globalThis.sessionStorage.setItem('test-batch-id', 'batch-3')
|
||||
state.admin = true
|
||||
const remoteError = new Error('远端登出失败')
|
||||
await assert.rejects(
|
||||
testBatchContext.logoutWithLocalCleanup(() => Promise.reject(remoteError), state),
|
||||
remoteError
|
||||
)
|
||||
assert.equal(state.admin, false)
|
||||
assert.equal(testBatchContext.getStoredTestBatchId(), '')
|
||||
})
|
||||
|
||||
test('窄屏模式控件仍保留正式退出与测试批次切换入口', async () => {
|
||||
const switchSource = await source('src/components/TestBatchModeSwitch/index.vue')
|
||||
const navbarSource = await source('src/layout/components/Navbar.vue')
|
||||
const mobileSection = switchSource.slice(switchSource.indexOf('@media screen and (max-width: 767px)'))
|
||||
const navbarMobileSection = navbarSource.slice(navbarSource.indexOf('@media screen and (max-width: 767px)'))
|
||||
assert.match(mobileSection, /\.mode-selector[\s\S]*\.batch-selector/)
|
||||
assert.doesNotMatch(mobileSection, /display:\s*none/)
|
||||
assert.match(mobileSection, /max-width:\s*48px/)
|
||||
assert.match(mobileSection, /width:\s*72px/)
|
||||
assert.match(mobileSection, /padding-right:\s*2px/)
|
||||
assert.match(mobileSection, /padding-left:\s*2px/)
|
||||
assert.match(navbarMobileSection, /\.test-batch-mode\s*\{\s*padding:\s*0/)
|
||||
})
|
||||
|
||||
test('17 个页面的真实权限门禁逐项匹配独立硬编码契约', async () => {
|
||||
|
||||
Reference in New Issue
Block a user