import assert from 'node:assert/strict' import test from 'node:test' import { runInNewContext } from 'node:vm' import { loginRoutePreload } from './loginRoutePreload' function generate(base = '/') { const chunk = (fileName: string, facadeModuleId: string | null, imports: string[], css: string[] = [], dynamicImports: string[] = []) => ({ type: 'chunk', fileName, facadeModuleId, imports, dynamicImports, viteMetadata: { importedCss: new Set(css) }, }) const bundle = { 'assets/main.js': chunk('assets/main.js', '/project/index.html', ['assets/shared.js'], ['assets/main.css'], ['assets/editor.js']), 'assets/shared.js': chunk('assets/shared.js', null, [], ['assets/shared.css']), 'assets/login.js': chunk('assets/login.js', '/project/src/views/auth/Login.vue', ['assets/main.js', 'assets/password.js'], ['assets/login.css']), 'assets/password.js': chunk('assets/password.js', null, ['assets/login.js']), 'assets/editor.js': chunk('assets/editor.js', '/project/src/components/editor.vue', [], ['assets/editor.css']), } const plugin = loginRoutePreload() as any plugin.configResolved({ base }) return { hook: plugin.transformIndexHtml.handler, context: { path: '/index.html', filename: '/project/index.html', bundle } } } function execute(pathname: string, base = '/', supported = true) { const { hook, context } = generate(base) const tags = hook('', context) const links: any[] = [] const script = tags[0].children runInNewContext(script, { URL, location: { pathname, href: `https://example.test${pathname}?token=fixture#oidc_error=fixture` }, document: { createElement(tag: string) { assert.equal(tag, 'link') return { relList: { supports: (rel: string) => supported && rel === 'modulepreload' }, setAttribute() {} } }, head: { appendChild(link: any) { links.push(link) } }, }, }) return links } test('downloads only login-specific static resources, despite cycles and lazy imports', () => { const links = execute('/login') assert.deepEqual(links.map(link => link.href), ['/assets/login.js', '/assets/login.css', '/assets/password.js']) assert.deepEqual(links.map(link => [link.rel, link.as]), [['modulepreload', undefined], ['preload', 'style'], ['modulepreload', undefined]]) assert.ok(links.every(link => link.crossOrigin === '')) }) test('login and registration paths permit query and callback hashes without route changes', () => { for (const path of ['/login', '/login/', '/register', '/register/']) assert.equal(execute(path).length, 3, path) }) test('authenticated, root, embed, legacy, and similar routes do not fetch login resources', () => { for (const path of ['/', '/platform/knowledge-bases', '/embed/fixture', '/admin', '/admin/login', '/login-extra', '/login/nested']) { assert.equal(execute(path).length, 0, path) } }) test('honors deployment path and CDN base without matching sibling paths', () => { assert.equal(execute('/app/login', '/app/')[0].href, '/app/assets/login.js') assert.equal(execute('/application/login', '/app/').length, 0) assert.equal(execute('/app/register', 'https://cdn.example.test/app/')[0].href, 'https://cdn.example.test/app/assets/login.js') }) test('relative-base deployments keep normal Vite loading instead of guessing asset paths', () => { assert.equal(execute('/login/', './').length, 0) assert.equal(execute('/login/', '').length, 0) }) test('unsupported modulepreload preserves stylesheet preloading without fetching scripts as plain preload', () => { assert.deepEqual(execute('/login', '/', false).map(link => link.href), ['/assets/login.css']) }) test('embed output remains independent and incomplete main build fails visibly', () => { const { hook, context } = generate() assert.equal(hook('', { ...context, path: '/embed.html' }), undefined) assert.throws(() => hook('', { ...context, bundle: {} }), /could not resolve/) }) test('preload-only script does not evaluate app code, touch storage, or propagate a DOM failure', () => { const { hook, context } = generate() const script = hook('', context)[0].children assert.doesNotMatch(script, /\b(?:import|fetch|localStorage|sessionStorage)\b/) assert.doesNotThrow(() => runInNewContext(script, { URL, location: { pathname: '/login', href: 'https://example.test/login' }, document: { createElement() { throw new Error('unavailable') } } })) })