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 { KICKED_CODE, KICK_NOTICE_KEY, SESSION_EXPIRED_NOTICE_KEY, UNAUTHORIZED_CODE, isUnauthorizedPayload, } 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 = {}) { 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 = {}, pathname = '/home') { const localStorage = createStorage(initial) const assigned: string[] = [] ;(globalThis as Record).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', aiimage_device_id: 'dev-fingerprint', company_name: '厦门米椿花', } } /** 后端把「未登录 / 令牌过期 / 用户不存在」都编译成 HTTP 200 + body.code=401。 */ function unauthorizedEnvelope() { return { success: false, code: UNAUTHORIZED_CODE, message: '用户不存在' } } test('isUnauthorizedPayload 只认 code=401,不吞 4011 与其他码', () => { assert.equal(isUnauthorizedPayload(unauthorizedEnvelope()), true) assert.equal(isUnauthorizedPayload({ success: false, code: KICKED_CODE }), false) assert.equal(isUnauthorizedPayload({ success: false, code: 403, message: '需要管理员权限' }), false) assert.equal(isUnauthorizedPayload({ success: true, data: {} }), false) assert.equal(isUnauthorizedPayload(null), false) assert.equal(isUnauthorizedPayload('401'), false) }) test('删号/令牌过期必须强制下线:清登录态、关自动登录、跳登录页', () => { const { localStorage, assigned } = setupWindow(loggedInStorage()) const returned = onResponseFulfilled({ data: unauthorizedEnvelope() }) assert.equal(returned.success, undefined, '拦截器仍原样返回响应对象') assert.equal(localStorage.getItem('aiimage_auth_token'), null) assert.equal(localStorage.getItem('uid'), null) assert.equal(localStorage.getItem('company_name'), null, '残留公司名会把上一账号的归属显示给下一个账号') assert.equal(localStorage.getItem('aiimage_auto_login'), '0', '不关自动登录=被删账号每次启动静默重试') assert.deepEqual(assigned, ['/login?expired=1']) }) test('成因文案不能混用:401 不得点亮「已在其他设备登录」标记', () => { const { localStorage } = setupWindow({ ...loggedInStorage(), [KICK_NOTICE_KEY]: '1' }) onResponseFulfilled({ data: unauthorizedEnvelope() }) assert.equal(localStorage.getItem(KICK_NOTICE_KEY), null, '登录态失效不能说成被顶下线') assert.equal(localStorage.getItem(SESSION_EXPIRED_NOTICE_KEY), '1') }) test('反向也要成立:4011 亮互踢标记并清掉失效标记', () => { const { localStorage, assigned } = setupWindow({ ...loggedInStorage(), [SESSION_EXPIRED_NOTICE_KEY]: '1' }) onResponseFulfilled({ data: { success: false, code: KICKED_CODE } }) assert.equal(localStorage.getItem(SESSION_EXPIRED_NOTICE_KEY), null) assert.equal(localStorage.getItem(KICK_NOTICE_KEY), '1') assert.deepEqual(assigned, ['/login?kicked=1']) }) test('真实 HTTP 401 同样收口(不只有 200 信封)', async () => { const { localStorage, assigned } = setupWindow(loggedInStorage()) await assert.rejects( () => onResponseRejected({ response: { status: 401, data: { message: '' } }, message: '请求失败' }), /请求失败/, ) assert.equal(localStorage.getItem('uid'), null) assert.deepEqual(assigned, ['/login?expired=1']) }) test('匿名请求的 401 不收口:本地没有令牌就不该清存储、不该整页跳', () => { const { localStorage, assigned } = setupWindow({ aiimage_remember_user: 'someone' }) onResponseFulfilled({ data: unauthorizedEnvelope() }) assert.deepEqual(assigned, []) assert.equal(localStorage.getItem(SESSION_EXPIRED_NOTICE_KEY), null) assert.equal(localStorage.getItem('aiimage_remember_user'), 'someone') }) test('已在登录页时不重复跳转(仍清理登录态)', () => { const { localStorage, assigned } = setupWindow(loggedInStorage(), '/login') onResponseFulfilled({ data: unauthorizedEnvelope() }) assert.equal(localStorage.getItem('uid'), null) assert.deepEqual(assigned, []) }) test('403 等业务错误不触发下线:那是权限缺口,不能说成登录坏了', () => { const { localStorage, assigned } = setupWindow(loggedInStorage()) onResponseFulfilled({ data: { success: false, code: 403, message: '无权访问查询ASIN' } }) assert.equal(localStorage.getItem('uid'), '1206') assert.deepEqual(assigned, []) }) // ---------- 登录页接线(.vue 无挂载测试,源码尺子钉三条) ---------- test('登录页认 expired 导航:显示失效提示并关掉自动登录', () => { assert.match(loginPageSource, /function isSessionExpiredNav\(\): boolean/, '缺 expired 判定入口') assert.match(loginPageSource, /get\('expired'\) === '1'/, '未读 ?expired=1(整页跳转带的标记)') assert.match(loginPageSource, /lsGet\(SESSION_EXPIRED_NOTICE_KEY\)/, '未读 localStorage 兜底标记(守卫会吞 query)') assert.match(loginPageSource, /expiredNotice\.value = true/, '判定到了却没显示提示') assert.match(loginPageSource, /kickedNotice\.value \|\| expiredNotice\.value/, '自动登录没被一起关掉') }) test('失效提示不得复用互踢那句文案', () => { const expiredLine = loginPageSource .split('\n') .find((line) => line.includes('v-if="expiredNotice"')) || '' assert.ok(expiredLine, '模板里没有 expiredNotice 那一行') assert.ok(!expiredLine.includes('其他设备'), '失效提示不能说成"已在其他设备登录"') assert.match(expiredLine, /请重新登录/) })