15b8e46af8
- 新后台新增「公司管理」页(列/新建/改名/启停,不提供删除),路由 menuKey 与 columns 表的 admin_companies 逐字一致,由用例钉住;操作指引补该菜单一条。 - 建号弹窗对超管必须显式选一家(后端对"不传"仍落操作者自己公司,是给旧后台兜底); admin 只能落自己公司,越权直接 403。账号列表显示 company_name,可按公司筛。 - 客户端登录后把 companyId/companyName 落到 localStorage 并在顶栏显示; 两个登出出口(handleKicked 与切号/登出)连公司名一起清 —— 残留会把上一个账号 的归属显示给下一个账号。 - 契约分层:列表端点用 snake_case(company_id/company_name),current-user 与登录 回包用 camelCase;解析器读到另一套写法必须忽略,防止前后端各猜一套。 admin-frontend-vue 1678/1678、frontend-vue 672/672,两边 build(含 vue-tsc --noEmit)干净。
183 lines
6.7 KiB
TypeScript
183 lines
6.7 KiB
TypeScript
import { test } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { onResponseFulfilled, onResponseRejected } from '../src/shared/api/http.ts'
|
|
import {
|
|
KICK_NOTICE_KEY,
|
|
KICKED_CODE,
|
|
handleKicked,
|
|
isKickedPayload,
|
|
} from '../src/shared/auth/kick-handler.ts'
|
|
|
|
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),
|
|
has: (key: string) => store.has(key),
|
|
}
|
|
}
|
|
|
|
function setupWindow(initial: Record<string, string> = {}, pathname = '/home') {
|
|
const localStorage = createStorage(initial)
|
|
const assigned: string[] = []
|
|
const listeners = new Map<string, Array<() => void>>()
|
|
const fakeWindow: Record<string, unknown> = {
|
|
localStorage,
|
|
location: {
|
|
pathname,
|
|
search: '',
|
|
assign: (url: string) => {
|
|
assigned.push(url)
|
|
},
|
|
},
|
|
addEventListener: (type: string, handler: () => void) => {
|
|
const list = listeners.get(type) || []
|
|
list.push(handler)
|
|
listeners.set(type, list)
|
|
},
|
|
}
|
|
const dispatch = (type: string) => {
|
|
for (const handler of listeners.get(type) || []) handler()
|
|
}
|
|
;(globalThis as Record<string, unknown>).window = fakeWindow
|
|
return { localStorage, assigned, dispatch, fakeWindow }
|
|
}
|
|
|
|
/** 登录态齐全的初始存储(含桌面端设备号,互踢下线不应清掉它) */
|
|
function loggedInStorage() {
|
|
return {
|
|
aiimage_auth_token: 'jwt-token',
|
|
uid: '1095',
|
|
username: 'ceshi001',
|
|
aiimage_auto_login: '1',
|
|
aiimage_device_id: 'dev-fingerprint',
|
|
aiimage_remember_user: 'ceshi001',
|
|
}
|
|
}
|
|
|
|
test('isKickedPayload 只认 code=4011', () => {
|
|
assert.equal(isKickedPayload({ success: false, code: KICKED_CODE, message: '该账号已在其他设备登录' }), true)
|
|
assert.equal(isKickedPayload({ success: false, code: 401, message: '未登录' }), false)
|
|
assert.equal(isKickedPayload({ success: true, data: {} }), false)
|
|
assert.equal(isKickedPayload(null), false)
|
|
assert.equal(isKickedPayload('4011'), false)
|
|
})
|
|
|
|
test('handleKicked 清登录态并关自动登录,跳登录页带 kicked 标记', () => {
|
|
const { localStorage, assigned } = setupWindow(loggedInStorage())
|
|
|
|
handleKicked()
|
|
|
|
assert.equal(localStorage.getItem('aiimage_auth_token'), null)
|
|
assert.equal(localStorage.getItem('uid'), null)
|
|
assert.equal(localStorage.getItem('username'), null)
|
|
// 被踢后不再自动重登,否则本机每次启动都会静默重登把对方又顶下线
|
|
assert.equal(localStorage.getItem('aiimage_auto_login'), '0')
|
|
assert.equal(localStorage.getItem(KICK_NOTICE_KEY), '1')
|
|
// 设备号与记住的账号保留:同设备重登不算新设备,账号回填方便手动重登
|
|
assert.equal(localStorage.getItem('aiimage_device_id'), 'dev-fingerprint')
|
|
assert.equal(localStorage.getItem('aiimage_remember_user'), 'ceshi001')
|
|
assert.deepEqual(assigned, ['/login?kicked=1'])
|
|
})
|
|
|
|
test('handleKicked 已在登录页时不跳转(仍清理登录态)', () => {
|
|
const { localStorage, assigned } = setupWindow(loggedInStorage(), '/login')
|
|
|
|
handleKicked()
|
|
|
|
assert.equal(localStorage.getItem('uid'), null)
|
|
assert.equal(localStorage.getItem(KICK_NOTICE_KEY), '1')
|
|
assert.deepEqual(assigned, [])
|
|
})
|
|
|
|
test('handleKicked 并发重复触发只跳转一次且状态一致', () => {
|
|
const { localStorage, assigned } = setupWindow(loggedInStorage())
|
|
|
|
handleKicked()
|
|
handleKicked()
|
|
|
|
assert.equal(localStorage.getItem('uid'), null)
|
|
assert.equal(localStorage.getItem('aiimage_auto_login'), '0')
|
|
assert.deepEqual(assigned, ['/login?kicked=1'])
|
|
})
|
|
|
|
test('handleKicked 桌面端同步清 Python 侧 current_uid', () => {
|
|
const { localStorage, fakeWindow } = setupWindow(loggedInStorage())
|
|
const saved: Array<Record<string, unknown>> = []
|
|
fakeWindow.pywebview = { api: { save_config: (data: Record<string, unknown>) => void saved.push(data) } }
|
|
|
|
handleKicked()
|
|
|
|
assert.deepEqual(saved, [{ current_uid: '' }])
|
|
})
|
|
|
|
test('handleKicked 桥未就绪时等 pywebviewready 补清 current_uid', () => {
|
|
// 真机复现:被顶下线发生在页面刚加载时,pywebview 桥尚未就绪,立即调用会被丢弃
|
|
const { dispatch, fakeWindow } = setupWindow(loggedInStorage())
|
|
const saved: Array<Record<string, unknown>> = []
|
|
|
|
handleKicked()
|
|
assert.deepEqual(saved, [], '桥未就绪时不应有调用结果')
|
|
|
|
// 桥就绪:注入 api 并触发就绪事件,应补一次清理
|
|
fakeWindow.pywebview = { api: { save_config: (data: Record<string, unknown>) => void saved.push(data) } }
|
|
dispatch('pywebviewready')
|
|
|
|
assert.deepEqual(saved, [{ current_uid: '' }])
|
|
})
|
|
|
|
test('handleKicked 网页形态(无 pywebview)不受影响', () => {
|
|
const { localStorage, assigned } = setupWindow(loggedInStorage())
|
|
|
|
handleKicked()
|
|
|
|
assert.equal(localStorage.getItem('uid'), null)
|
|
assert.deepEqual(assigned, ['/login?kicked=1'])
|
|
})
|
|
|
|
// ---------- 拦截器接线:Java 错误信封走 HTTP 200,成功分支也必须判 ----------
|
|
|
|
test('响应成功分支:body.code=4011 触发下线', () => {
|
|
const { localStorage, assigned } = setupWindow(loggedInStorage())
|
|
const response = { data: { success: false, code: KICKED_CODE, message: '该账号已在其他设备登录,本设备已下线' } }
|
|
|
|
const returned = onResponseFulfilled(response as never)
|
|
|
|
assert.equal(returned, response)
|
|
assert.equal(localStorage.getItem('uid'), null)
|
|
assert.equal(localStorage.getItem('aiimage_auto_login'), '0')
|
|
assert.deepEqual(assigned, ['/login?kicked=1'])
|
|
})
|
|
|
|
test('响应失败分支:错误响应体 code=4011 触发下线', async () => {
|
|
const { localStorage, assigned } = setupWindow(loggedInStorage())
|
|
|
|
await assert.rejects(
|
|
() => onResponseRejected({ response: { data: { code: KICKED_CODE } }, message: '请求失败' }),
|
|
/请求失败/,
|
|
)
|
|
|
|
assert.equal(localStorage.getItem('uid'), null)
|
|
assert.deepEqual(assigned, ['/login?kicked=1'])
|
|
})
|
|
|
|
test('普通业务错误(403 等)不触发下线', () => {
|
|
const { localStorage, assigned } = setupWindow(loggedInStorage())
|
|
|
|
onResponseFulfilled({ data: { success: false, code: 403, message: '需要管理员权限' } } as never)
|
|
|
|
assert.equal(localStorage.getItem('uid'), '1095')
|
|
assert.deepEqual(assigned, [])
|
|
})
|
|
|
|
// ---------- 公司身份(设计文档 §2.4):下线必须把公司名一起清掉 ----------
|
|
|
|
test('handleKicked 连公司名一起清:残留会把上一账号的归属显示给下一个账号', () => {
|
|
const { localStorage } = setupWindow({ ...loggedInStorage(), company_name: '厦门米椿花' })
|
|
|
|
handleKicked()
|
|
|
|
assert.equal(localStorage.getItem('company_name'), null, '被踢后不得残留上一账号的公司名')
|
|
})
|