import assert from 'node:assert/strict'; import {readFile} from 'node:fs/promises'; import {pathToFileURL} from 'node:url'; import test from 'node:test'; const ts = await import(process.env.KEFU_SSO_TYPESCRIPT ? pathToFileURL(process.env.KEFU_SSO_TYPESCRIPT + '/lib/typescript.js').href : 'typescript'); const dataModule = value => `data:text/javascript;base64,${Buffer.from(value).toString('base64')}`; let current; globalThis[Symbol.for('kefu-home-fixture')] = () => current; const request = dataModule("export const requestClient={get:async()=>globalThis[Symbol.for('kefu-home-fixture')]()};"); const source = await readFile(new URL('../src/api/core/user.ts',import.meta.url),'utf8'); const compiled = ts.transpileModule(source,{compilerOptions:{module:ts.ModuleKind.ESNext,target:ts.ScriptTarget.ES2022}}).outputText .replace("from '#/api/request'", `from '${request}'`); const {getUserInfoApi} = await import(dataModule(compiled)); test('自定义只读角色不被默认送到无权限统计页,保留原管理员首页和强制改密',async()=>{ for (const [permissions,forced,expected] of [ [[],false,'/group/welcome'],[['model:read'],false,'/group/welcome'], [['user:read'],false,'/group/welcome'],[['stats:read'],false,undefined], [['stats:read'],true,'/profile/password']]) { current={id:7,username:'fixture',role:'custom',permissions,must_change_password:forced}; const user=await getUserInfoApi();assert.equal(user.homePath,expected);assert.deepEqual(user.roles,permissions); } delete globalThis[Symbol.for('kefu-home-fixture')]; });