import test from 'node:test' import assert from 'node:assert/strict' import { missingImports, readSource } from './helpers.ts' import { isLoginLocation, shouldRedirectForbidden, shouldRedirectUnauthorized, } from '../src/api/envelope.ts' import { LOGIN_FALLBACK_MESSAGE, loginErrorText, unwrapLoginResponse, } from '../src/pages/login/login-feedback.ts' /** * 登录失败必须有提示(2026-10-06)。 * * 两个各自独立的"吞掉提示"出口: * 1. Java 的登录失败一律 HTTP 200 + `success:false`(GlobalExceptionHandler 把 BusinessException * 编成 ApiResponse.fail),axios 视为成功 → 登录页不解包就永远走不到 catch,`errorMessage` 恒空; * 2. 紧接着 `session.initialize()` 打 current-user 拿到 401/403,拦截器整页 * `window.location.assign('/admin-vue/login?...')` —— 页面重刷,错误文案连同已填用户名一起没了。 * 守卫本该拦下"已经在登录页"的跳转,但它只比过路由内的 `/login`,而真实 pathname 是 `/admin-vue/login`。 */ /** 线上真实的登录失败响应体(Java:success/message/data/code,HTTP 200)。 */ const FAIL_BODIES: Array<{ name: string; body: unknown; reason: string }> = [ { name: '密码错', body: { success: false, message: '用户名或密码错误', data: null, code: null }, reason: '用户名或密码错误', }, { name: '撞库锁定', body: { success: false, message: '登录失败次数过多,请 12 分钟后再试', data: null, code: null }, reason: '登录失败次数过多,请 12 分钟后再试', }, { name: '账号到期', body: { success: false, message: '账号已于 2026-10-01 到期,请联系管理员续费', data: null, code: 403 }, reason: '账号已于 2026-10-01 到期,请联系管理员续费', }, { name: '正常账号无后台权限', body: { success: false, message: '需要管理员权限', data: null, code: 403 }, reason: '需要管理员权限', }, ] test('解包登录响应:每一种 HTTP 200 失败都要抛出服务端原因', () => { for (const { name, body, reason } of FAIL_BODIES) { assert.throws(() => unwrapLoginResponse(body), (error: unknown) => { assert.ok(error instanceof Error, `${name} 应抛出 Error`) assert.equal(error.message, reason, `${name} 要把服务端文案原样带出来`) return true }) } }) test('解包登录响应:成功体不得抛错(否则上面四支会恒真)', () => { assert.doesNotThrow(() => unwrapLoginResponse({ success: true, message: '登录成功', data: { userId: 1201, token: 'jwt', role: 'admin' }, code: null, })) }) test('服务端原因空时也有文案,不出现空提示', () => { assert.throws(() => unwrapLoginResponse({ success: false, message: '', data: null })) assert.equal(loginErrorText(new Error('')), LOGIN_FALLBACK_MESSAGE) assert.equal(loginErrorText(undefined), LOGIN_FALLBACK_MESSAGE) }) test('传输层技术串不外露:状态码串/网络错/HTML 错误页一律回兜底', () => { const cases: Array<[unknown, string]> = [ [new Error('Request failed with status code 500'), LOGIN_FALLBACK_MESSAGE], [{ response: { data: { success: false, message: '服务器内部错误,请稍后重试' } } }, '服务器内部错误,请稍后重试'], [new Error('Network Error'), LOGIN_FALLBACK_MESSAGE], [new Error('Timeout of 30000ms exceeded.'), LOGIN_FALLBACK_MESSAGE], [{ response: { data: '
502 Bad Gateway' } }, LOGIN_FALLBACK_MESSAGE], ] for (const [error, expected] of cases) { assert.equal(loginErrorText(error), expected) } }) test('登录页上的会话引导失败不再整页重刷:这是"啥都没提示"的那一步', () => { for (const pathname of ['/admin-vue/login', '/admin-vue/login?redirect=%2Fadmin-vue%2Faccount%2Fusers']) { assert.equal(isLoginLocation(pathname), true, `${pathname} 应在登录页判定内`) for (const url of ['/api/admin/current-user', '/api/admin/current-user/menus']) { assert.equal( shouldRedirectUnauthorized(pathname, url), false, `${pathname} 上 ${url} 的 401 不许再跳(跳了就是把提示刷没)`, ) } } }) test('收窄到登录页之外一律照旧:业务页 401 仍跳、引导端点 403 仍跳', () => { assert.equal(shouldRedirectUnauthorized('/admin-vue/account/users', '/api/admin/current-user'), true) assert.equal(shouldRedirectUnauthorized('/admin-vue/', '/api/admin/current-user'), true) // 10-04 那条 403 分流没动:只有会话引导端点的 403 按"进不了后台"处理 assert.equal(shouldRedirectForbidden('/api/admin/current-user'), true) assert.equal(shouldRedirectForbidden('/api/admin/companies'), false) // 相邻路径不许被误判成登录页(尾斜杠才算,/admin-vue/login-x 不算) assert.equal(isLoginLocation('/admin-vue/login-x'), false) assert.equal(isLoginLocation('/admin-vue/account/login'), false) }) test('登录页接线:解包 + 归一文案,且不得保留「用户名或密码错误」兜底', () => { const page = readSource('src/pages/login/LoginPage.vue') assert.match(page, /http\.post