b963561043
- CR-04:全局代理加 proxy_uid 归属,跨账号不再静默回退/补报 - CR-12:取消静默继承,设置页显式确认;同 OS 作用域、locked 代理排除、无效快照清除、uid+epoch 双校验、登出 capture 超时 - CR-26/27:关键词分页初始值与档位一致;两页面请求序号闸丢弃旧响应 vue-tsc 两前端 rc=0;定向 35 + 8 + 2 例全绿。
188 lines
7.9 KiB
TypeScript
188 lines
7.9 KiB
TypeScript
import { test } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readFileSync } from 'node:fs'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { resolve, dirname } from 'node:path'
|
|
import { onResponseFulfilled, onResponseRejected } from '../src/shared/api/http.ts'
|
|
import {
|
|
ACCOUNT_DELETED_NOTICE_KEY,
|
|
ACCOUNT_DISABLED_NOTICE_KEY,
|
|
KICK_NOTICE_KEY,
|
|
SESSION_EXPIRED_NOTICE_KEY,
|
|
UNAUTHORIZED_CODE,
|
|
authReasonOf,
|
|
} from '../src/shared/auth/kick-handler.ts'
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url))
|
|
|
|
/** 登录页是 .vue 组件,本树无挂载测试(无 @vue/test-utils),按既有惯例用源码尺子钉。 */
|
|
const loginPageSource = readFileSync(
|
|
resolve(here, '../src/pages/login/DesktopLoginPage.vue'),
|
|
'utf-8',
|
|
)
|
|
|
|
function createStorage(initial: Record<string, string> = {}) {
|
|
const store = new Map(Object.entries(initial))
|
|
return {
|
|
getItem: (key: string) => (store.has(key) ? (store.get(key) as string) : null),
|
|
setItem: (key: string, value: string) => void store.set(key, String(value)),
|
|
removeItem: (key: string) => void store.delete(key),
|
|
}
|
|
}
|
|
|
|
function setupWindow(initial: Record<string, string> = {}, pathname = '/home') {
|
|
const localStorage = createStorage(initial)
|
|
const assigned: string[] = []
|
|
;(globalThis as Record<string, unknown>).window = {
|
|
localStorage,
|
|
location: {
|
|
pathname,
|
|
search: '',
|
|
assign: (url: string) => {
|
|
assigned.push(url)
|
|
},
|
|
},
|
|
addEventListener: () => undefined,
|
|
}
|
|
return { localStorage, assigned }
|
|
}
|
|
|
|
function loggedInStorage() {
|
|
return {
|
|
aiimage_auth_token: 'jwt-token',
|
|
uid: '1206',
|
|
username: '先锋',
|
|
aiimage_auto_login: '1',
|
|
}
|
|
}
|
|
|
|
/** 后端把「停用/删除/过期」都编译成 HTTP 200 + body.code=401;成因在 X-Auth-Reason 头上。 */
|
|
function unauthorizedEnvelope(message = '账号已停用,请联系管理员') {
|
|
return { success: false, code: UNAUTHORIZED_CODE, message }
|
|
}
|
|
|
|
test('authReasonOf 只认 disabled/deleted,其余回落空串', () => {
|
|
assert.equal(authReasonOf({ 'x-auth-reason': 'disabled' }), 'disabled')
|
|
assert.equal(authReasonOf({ 'x-auth-reason': 'deleted' }), 'deleted')
|
|
assert.equal(authReasonOf({ 'x-auth-reason': 'expired' }), '')
|
|
assert.equal(authReasonOf({}), '')
|
|
assert.equal(authReasonOf(undefined), '')
|
|
})
|
|
|
|
test('停用必须强制下线且点名停用:清登录态、亮停用标记、清掉其它成因', () => {
|
|
const { localStorage, assigned } = setupWindow({
|
|
...loggedInStorage(),
|
|
[KICK_NOTICE_KEY]: '1',
|
|
})
|
|
|
|
onResponseFulfilled({ data: unauthorizedEnvelope(), headers: { 'x-auth-reason': 'disabled' } })
|
|
|
|
assert.equal(localStorage.getItem('aiimage_auth_token'), null, '停用不清令牌=用户停在原页面')
|
|
assert.equal(localStorage.getItem('aiimage_auto_login'), '0', '不关自动登录=被停用账号每次启动静默重试')
|
|
assert.equal(localStorage.getItem(ACCOUNT_DISABLED_NOTICE_KEY), '1')
|
|
assert.equal(localStorage.getItem(KICK_NOTICE_KEY), null, '停用不能说成被顶下线')
|
|
assert.deepEqual(assigned, ['/login?expired=1'])
|
|
})
|
|
|
|
test('删除点名:亮删除标记并清掉停用/互踢标记', () => {
|
|
const { localStorage } = setupWindow({
|
|
...loggedInStorage(),
|
|
[ACCOUNT_DISABLED_NOTICE_KEY]: '1',
|
|
})
|
|
|
|
onResponseFulfilled({ data: unauthorizedEnvelope('用户不存在'), headers: { 'x-auth-reason': 'deleted' } })
|
|
|
|
assert.equal(localStorage.getItem(ACCOUNT_DELETED_NOTICE_KEY), '1')
|
|
assert.equal(localStorage.getItem(ACCOUNT_DISABLED_NOTICE_KEY), null, '删除不能说成停用')
|
|
assert.equal(localStorage.getItem(KICK_NOTICE_KEY), null)
|
|
})
|
|
|
|
test('401 头里没有成因:回落通用「登录已失效」,不得误亮停用/删除标记', () => {
|
|
const { localStorage } = setupWindow(loggedInStorage())
|
|
|
|
onResponseFulfilled({ data: unauthorizedEnvelope('登录已过期,请重新登录') })
|
|
|
|
assert.equal(localStorage.getItem(SESSION_EXPIRED_NOTICE_KEY), '1')
|
|
assert.equal(localStorage.getItem(ACCOUNT_DISABLED_NOTICE_KEY), null)
|
|
assert.equal(localStorage.getItem(ACCOUNT_DELETED_NOTICE_KEY), null)
|
|
})
|
|
|
|
test('成因只改标记不改收口动作:带头 401 照样跳 /login?expired=1', async () => {
|
|
const { assigned } = setupWindow(loggedInStorage())
|
|
|
|
await assert.rejects(
|
|
() => onResponseRejected({
|
|
response: { status: 401, data: { message: '' }, headers: { 'x-auth-reason': 'disabled' } },
|
|
message: '请求失败',
|
|
}),
|
|
/请求失败/,
|
|
)
|
|
|
|
assert.deepEqual(assigned, ['/login?expired=1'])
|
|
})
|
|
|
|
// ---------- 登录页接线(.vue 无挂载测试,源码尺子钉) ----------
|
|
|
|
test('登录页认停用/删除标记:各有判定入口、各自关自动登录', () => {
|
|
assert.match(loginPageSource, /function isAccountDisabledNav\(\): boolean/, '缺停用判定入口')
|
|
assert.match(loginPageSource, /function isAccountDeletedNav\(\): boolean/, '缺删除判定入口')
|
|
assert.match(loginPageSource, /lsGet\(ACCOUNT_DISABLED_NOTICE_KEY\)/)
|
|
assert.match(loginPageSource, /lsGet\(ACCOUNT_DELETED_NOTICE_KEY\)/)
|
|
assert.match(loginPageSource, /disabledNotice\.value = true/, '判定到了却没显示停用提示')
|
|
assert.match(loginPageSource, /deletedNotice\.value = true/, '判定到了却没显示删除提示')
|
|
// 自动登录豁免必须包含全部四种成因:漏一种,那种账号每次启动就静默重登
|
|
assert.match(
|
|
loginPageSource,
|
|
/kickedNotice\.value \|\| expiredNotice\.value \|\| disabledNotice\.value \|\| deletedNotice\.value/,
|
|
'自动登录豁免漏了停用/删除成因',
|
|
)
|
|
})
|
|
|
|
test('登出先留继承快照再清缓存,且清缓存必须早于移除 uid', () => {
|
|
const cleanupStart = loginPageSource.indexOf('const clearSignedOutState = () => {')
|
|
const captureCall = loginPageSource.indexOf('void captureApiSecretsForInheritance()')
|
|
assert.ok(cleanupStart > 0, '登出清理函数不见了')
|
|
assert.ok(captureCall > cleanupStart, '登出时应先调用继承快照再执行清理')
|
|
const cleanupBody = loginPageSource.slice(cleanupStart, captureCall)
|
|
const clearAt = cleanupBody.indexOf('clearApiSecretCache()')
|
|
const removeUidAt = cleanupBody.indexOf("removeItem('uid')")
|
|
assert.ok(clearAt > 0, '登出清理没有清密钥缓存')
|
|
assert.ok(removeUidAt > clearAt, '密钥镜像键带 uid,先移除 uid 会删错键、把本账号明文留在本机')
|
|
assert.match(
|
|
loginPageSource,
|
|
/captureApiSecretsForInheritance\(\)\.then\(clearSignedOutState, clearSignedOutState\)/,
|
|
'继承快照失败也必须照常登出',
|
|
)
|
|
})
|
|
|
|
test('登出清理期间禁用提交,且清理结束才恢复(CR-12)', () => {
|
|
assert.match(loginPageSource, /logoutCleanupPending = ref\(false\)/, '缺清理中状态')
|
|
assert.match(
|
|
loginPageSource,
|
|
/:disabled="loggingIn \|\| logoutCleanupPending"/,
|
|
'清理期间登录按钮必须禁用',
|
|
)
|
|
assert.match(
|
|
loginPageSource,
|
|
/if \(loggingIn\.value \|\| logoutCleanupPending\.value\) return/,
|
|
'回车/表单提交也要挡住,不能只靠按钮 disabled',
|
|
)
|
|
assert.match(loginPageSource, /logoutCleanupPending\.value = true/, '登出时应进入清理中状态')
|
|
assert.match(loginPageSource, /logoutCleanupPending\.value = false/, '清理结束(含超时)必须恢复')
|
|
})
|
|
|
|
test('停用/删除文案必须互不冒充:停用句不说删除、删除句不说停用', () => {
|
|
const disabledLine = loginPageSource
|
|
.split('\n')
|
|
.find((line) => line.includes('v-if="disabledNotice"')) || ''
|
|
const deletedLine = loginPageSource
|
|
.split('\n')
|
|
.find((line) => line.includes('v-if="deletedNotice"')) || ''
|
|
assert.ok(disabledLine, '模板里没有 disabledNotice 那一行')
|
|
assert.ok(deletedLine, '模板里没有 deletedNotice 那一行')
|
|
assert.match(disabledLine, /停用/)
|
|
assert.ok(!disabledLine.includes('删除'), '停用提示不能说成被删除')
|
|
assert.match(deletedLine, /删除/)
|
|
assert.ok(!deletedLine.includes('停用'), '删除提示不能说成被停用')
|
|
})
|