Files
client-web/src/modules/auth/views/AuthViews.test.mjs
T

119 lines
6.5 KiB
JavaScript
Raw Normal View History

2026-07-30 17:59:42 +08:00
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
const loginSource = readFileSync(new URL('./LoginView.vue', import.meta.url), 'utf8')
const registerSource = readFileSync(new URL('./RegisterView.vue', import.meta.url), 'utf8')
2026-08-04 15:31:08 +08:00
const authShellSource = readFileSync(new URL('../components/AuthShell.vue', import.meta.url), 'utf8')
2026-08-03 20:50:32 +08:00
const legalSource = readFileSync(new URL('./LegalDocumentView.vue', import.meta.url), 'utf8')
const legalDocumentsSource = readFileSync(new URL('../legalDocuments.ts', import.meta.url), 'utf8')
const authRoutesSource = readFileSync(new URL('../routes.ts', import.meta.url), 'utf8')
2026-08-03 18:52:30 +08:00
const authStoreSource = readFileSync(new URL('../store/auth.ts', import.meta.url), 'utf8')
const httpSource = readFileSync(new URL('../../../api/http.ts', import.meta.url), 'utf8')
const sessionSource = readFileSync(new URL('../../../api/session.ts', import.meta.url), 'utf8')
2026-07-30 17:59:42 +08:00
const authApiSource = readFileSync(new URL('../api/auth.ts', import.meta.url), 'utf8')
2026-08-03 18:52:30 +08:00
const mainSource = readFileSync(new URL('../../../main.ts', import.meta.url), 'utf8')
2026-08-04 15:31:08 +08:00
const stylesSource = readFileSync(new URL('../../../styles/index.css', import.meta.url), 'utf8')
test('uses one prototype-inspired responsive shell for login and registration', () => {
assert.match(loginSource, /<AuthShell/)
assert.match(registerSource, /<AuthShell/)
assert.match(authShellSource, /auth-ocean\.jpg/)
assert.match(authShellSource, /class="auth-visual"/)
assert.match(stylesSource, /\.auth-panel \{[^}]*grid-template-columns:/s)
assert.match(stylesSource, /@media \(max-width: 960px\)[\s\S]*?\.auth-visual \{ display: none; \}/)
assert.match(stylesSource, /@media \(max-width: 720px\)[\s\S]*?\.register-form-grid \{ grid-template-columns: 1fr; \}/)
})
test('groups the full registration contract without adding out-of-scope sign-in methods', () => {
for (const section of ['账号信息', '企业信息', '联系信息']) assert.match(registerSource, new RegExp(section))
assert.doesNotMatch(loginSource + registerSource, /微信登录|企业微信登录|短信验证码|邮箱验证码|lang-switcher/)
})
2026-07-30 17:59:42 +08:00
test('matches the backend username login contract', () => {
assert.match(loginSource, /label="用户名"/)
assert.match(loginSource, /\^\[A-Za-z0-9_\]/)
2026-07-31 15:34:22 +08:00
assert.match(loginSource, /typeof route\.query\.account === 'string'/)
assert.doesNotMatch(loginSource, /developmentHint|demo-tip|merchant_admin|联调账号|本地后端/)
2026-07-30 17:59:42 +08:00
assert.match(loginSource, /form\.remember/)
assert.match(loginSource, /忘记密码/)
})
test('persists both rotating tokens according to the remember choice', () => {
assert.match(sessionSource, /ACCESS_TOKEN_KEY/)
assert.match(sessionSource, /REFRESH_TOKEN_KEY/)
assert.match(sessionSource, /remember \? localStorage : sessionStorage/)
assert.match(sessionSource, /clearSessionTokens/)
})
test('implements every backend registration field and real submission', () => {
for (const field of ['username', 'companyName', 'displayName', 'legalPerson', 'legalPhone', 'contactEmail', 'countryCode', 'address', 'password', 'agreement']) {
assert.match(registerSource, new RegExp(`prop="${field}"`))
}
assert.match(registerSource, /registerMerchant\(/)
assert.match(registerSource, /4–64 位/)
assert.match(registerSource, /8–72 位/)
})
test('shows immediate login after registration and separates company review from account activation', () => {
assert.match(registerSource, /企业账号注册成功/)
assert.match(registerSource, /无需等待注册审核/)
2026-08-04 09:05:01 +08:00
assert.match(registerSource, /提交订单前还需完成企业审核/)
2026-08-04 14:15:24 +08:00
assert.match(registerSource, /营业执照和法人身份证/)
assert.match(registerSource, /货代资质证书、银行开户许可证可选填/)
assert.match(registerSource, /其他资质可选填/)
2026-07-30 17:59:42 +08:00
assert.match(registerSource, /立即登录/)
})
2026-08-03 20:50:32 +08:00
test('opens complete service and privacy documents from registration', () => {
assert.match(registerSource, /to="\/legal\/terms" target="_blank"/)
assert.match(registerSource, /to="\/legal\/privacy" target="_blank"/)
assert.doesNotMatch(registerSource, /showDocument/)
assert.match(authRoutesSource, /path: '\/legal\/terms'/)
assert.match(authRoutesSource, /path: '\/legal\/privacy'/)
assert.match(legalSource, /document\.sections/)
assert.match(legalDocumentsSource, /跨境物流中的信息出境/)
assert.match(legalDocumentsSource, /违禁品/)
})
2026-07-30 17:59:42 +08:00
test('uses real auth endpoints and single-flight refresh handling', () => {
assert.match(authApiSource, /\/auth\/login/)
assert.match(authApiSource, /\/auth\/register/)
assert.match(authApiSource, /\/auth\/logout/)
assert.match(httpSource, /refreshPromise/)
assert.match(httpSource, /\/auth\/refresh/)
2026-08-11 20:02:41 +08:00
assert.match(httpSource, /persistTokenPair\(tokens\)/)
assert.match(httpSource, /request\._retriedAfterRefresh = true/)
assert.match(httpSource, /return await transport\.request\(request\)/)
assert.match(httpSource, /envelope\.code !== '200'/)
assert.match(httpSource, /redirectToLoginAfterSessionExpiry/)
assert.match(httpSource, /window\.location\.replace\(`\/login\?redirect=\$\{encodeURIComponent\(redirect\)\}`\)/)
2026-07-30 17:59:42 +08:00
assert.match(authStoreSource, /decodeMerchantToken/)
assert.doesNotMatch(authStoreSource, /mock-client:/)
})
2026-07-30 21:23:06 +08:00
2026-08-13 12:22:39 +08:00
test('handles impersonation exchange as public authentication and exposes failures', () => {
assert.match(httpSource, /publicAuthPaths = \[[^\]]*'\/auth\/impersonations\/exchange'/)
assert.match(authStoreSource, /\/login\?impersonation=failed/)
assert.match(loginSource, /route\.query\.impersonation === 'failed'/)
assert.match(loginSource, /代登录失败或授权已失效,请返回管理后台重新发起/)
})
test('treats only body code 200 as success', () => {
assert.match(httpSource, /if \(envelope\.code !== '200'\)/)
assert.doesNotMatch(httpSource, /if \(envelope\.code !== '0'\)/)
})
2026-07-30 21:23:06 +08:00
test('keeps a valid local session during temporary context and refresh outages', () => {
assert.match(authStoreSource, /function isExpiredSessionError/)
assert.match(authStoreSource, /if \(isExpiredSessionError\(error\)\)/)
assert.match(authStoreSource, /sessionRestored\.value = true\s+return/)
assert.match(httpSource, /if \(mappedError\.status === 401 \|\| mappedError\.code === 'A0401'\)/)
assert.doesNotMatch(httpSource, /catch \(refreshError\) \{\s+clearSessionTokens\(\)/)
})
test('loads styles for programmatic Element Plus messages and dialogs', () => {
assert.match(mainSource, /element-plus\/es\/components\/message\/style\/css/)
assert.match(mainSource, /element-plus\/es\/components\/message-box\/style\/css/)
})