import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { createRequire } from 'node:module'; import { resolve } from 'node:path'; import test from 'node:test'; import { fileURLToPath, pathToFileURL } from 'node:url'; import vm from 'node:vm'; const require = createRequire(process.env.KEFU_SSO_TYPESCRIPT ? pathToFileURL(resolve(process.env.KEFU_SSO_TYPESCRIPT, 'package.json')) : new URL('../package.json', import.meta.url)); const ts = require('typescript'); const vue = require('vue'); const { parse, compileScript, compileTemplate } = require('@vue/compiler-sfc'); const { renderToString } = require('@vue/server-renderer'); function load(source, dependencies) { const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, }, reportDiagnostics: true }); assert.deepEqual(compiled.diagnostics, []); const module = { exports: {} }; vm.runInNewContext(compiled.outputText, { module, exports: module.exports, require(name) { assert.ok(name in dependencies, `Unexpected import ${name}`); return dependencies[name]; }, // Normal password login must not read browser redirect/storage APIs. get window() { assert.fail('Password login accessed window'); }, get localStorage() { assert.fail('Password login accessed localStorage'); }, console }); return module.exports; } const filename = fileURLToPath(new URL('../src/views/_core/authentication/login.vue', import.meta.url)); const { descriptor } = parse(await readFile(filename, 'utf8'), { filename }); const script = compileScript(descriptor, { id: 'password-login-page-test' }); const template = compileTemplate({ source: descriptor.template.content, filename, id: 'password-login-page-test', compilerOptions: { bindingMetadata: script.bindings } }); assert.deepEqual(template.errors, []); async function page(authLogin) { let submit; let formProps; const store = vue.reactive({ loginLoading: false, authLogin }); // Render the real page SFC. The shared form adapter displays its schema/slots // and exposes the submit listener assigned by the compiled page template. const AuthenticationLogin = vue.defineComponent({ props: ['formSchema', 'loading', 'title', 'subTitle', 'showCodeLogin', 'showForgetPassword', 'showQrcodeLogin', 'showRegister', 'showRememberMe', 'showThirdPartyLogin'], emits: ['submit'], setup(props, { slots }) { formProps = props; const instance = vue.getCurrentInstance(); submit = values => instance.vnode.props.onSubmit(values); return () => vue.h('section', {}, [ vue.h('h1', {}, props.title), vue.h('p', {}, props.subTitle), vue.h('form', {}, [ ...props.formSchema.map(field => vue.h('input', { name: field.fieldName, type: field.component === 'VbenInputPassword' ? 'password' : 'text', placeholder: field.componentProps.placeholder })), vue.h('button', { type: 'submit', disabled: props.loading }, '登录'), ]), slots['third-party-login']?.(), ]); }, }); // No config API, group URL helper or router is provided: an accidental import // or use fails this actual SFC test instead of silently becoming a mock call. const component = load(script.content, { vue, '@vben/common-ui': { AuthenticationLogin, z: { string: () => ({ min: () => ({}) }) } }, '#/locales/t': { t: value => value }, '#/store': { useAuthStore: () => store }, }).default; component.render = load(template.code, { vue }).render; const html = await renderToString(vue.createSSRApp(component)); return { html, submit, store, formProps }; } test('实际登录页只有一份账号密码表单和登录按钮,ZYT/本地均走普通提交', async () => { const calls = []; const view = await page(async values => { calls.push(values); }); assert.equal((view.html.match(/