const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const ts = require('typescript'); const React = require('react'); const { renderToStaticMarkup } = require('react-dom/server'); const root = process.cwd(); function loader(overrides = {}, globals = {}) { const cache = new Map(); function load(file) { file = path.resolve(root, file); if (!path.extname(file)) file += fs.existsSync(file + '.tsx') ? '.tsx' : '.ts'; if (cache.has(file)) return cache.get(file); const module = { exports: {} }; cache.set(file, module.exports); const source = ts.transpileModule(fs.readFileSync(file, 'utf8'), { compilerOptions: { module: ts.ModuleKind.CommonJS, esModuleInterop: true, jsx: ts.JsxEmit.ReactJSX, target: ts.ScriptTarget.ES2022 } }).outputText; const resolve = spec => { if (Object.hasOwn(overrides, spec)) return overrides[spec]; if (spec.startsWith('@/')) return load('src/' + spec.slice(2)); if (spec.startsWith('.')) return load(path.resolve(path.dirname(file), spec)); return require(spec); }; vm.runInNewContext(source, { module, exports: module.exports, require: resolve, ...globals }, { filename: file }); return module.exports; } return load; } const nextMocks = { 'next/navigation': { usePathname: () => '/blogs/netherlands-visa-appointments-dublin' }, 'next/link': props => React.createElement('a', props, props.children), '@/lib/auth': { isAdmin: () => false, logout: () => {} }, }; const loadSSR = loader(nextMocks); const BlogNavbar = loadSSR('src/components/blog/BlogNavbar.tsx').default; const html = renderToStaticMarkup(React.createElement(BlogNavbar, { blogHref: '/blogs' })); for (const text of ['Services', 'Slot Check', 'Visa guides', 'Login / Sign up']) assert(html.includes(text), `Missing shared menu item: ${text}`); for (const href of ['/', '/blogs', '/services', '/slots', '/login']) assert(html.includes(`href="${href}"`)); assert(html.includes('lang="en"') && html.includes('aria-expanded="false"')); assert(html.includes('aria-label="STRATA home"')); assert(html.includes('aria-label="ZH / 中文: Switch navigation language'), 'Accessible language-control name must include its visible target'); assert(html.includes('Switch navigation language (article language stays unchanged)')); assert(!html.includes('服务工单库') && !html.includes('nav.services'), 'Blog navigation must SSR in English'); // Simulated provider hooks test browser-preference isolation and document-language ownership. function providerCase(options, expectedDefault, detect) { const state = []; const effects = []; let index = 0; let reads = 0; const writes = []; const fakeReact = { ...React, createContext: () => ({ Provider: 'context-provider' }), useState: initial => { const i = index++; if (!(i in state)) state[i] = initial; return [state[i], value => { state[i] = value; }]; }, useEffect: callback => effects.push(callback), }; const document = { documentElement: { lang: 'en' } }; const load = loader({ react: fakeReact }, { document, navigator: { language: 'zh-CN' }, localStorage: { getItem: () => { reads++; return expectedDefault === 'en' ? 'zh' : 'en'; }, setItem: (...args) => writes.push(args) } }); const Provider = load('src/lib/i18n/LanguageContext.tsx').LanguageProvider; const element = Provider({ children: null, ...options }); assert.equal(element.props.value.lang, expectedDefault); effects.forEach(fn => fn()); assert.equal(reads > 0, detect); assert.equal(state[0], 'en', 'Homepage honors stored English; blog ignores stored Chinese'); element.props.value.setLang('zh'); assert.equal(state[0], 'zh'); assert.deepEqual(writes[0], ['app_lang', 'zh']); assert.equal(document.documentElement.lang, options.updateDocumentLanguage === false ? 'en' : 'zh-CN'); } providerCase({ initialLang: 'en', detectPreference: false, updateDocumentLanguage: false }, 'en', false); providerCase({}, 'zh', true); // Simulated shared-Navbar hooks cover mobile/auth branches without a browser or real session. for (const [logged, admin, open] of [[false, false, false], [false, false, true], [true, false, true], [true, true, true]]) { let index = 0; const states = [logged, admin, open]; const effects = []; const changes = []; let logoutCalls = 0; const events = []; const fakeReact = { ...React, useState: () => { const i = index++; return [states[i], value => changes.push([i, value])]; }, useEffect: callback => effects.push(callback) }; const load = loader({ ...nextMocks, react: fakeReact, '@/lib/i18n/LanguageContext': { useLanguage: () => ({ t: key => key.split('.').pop(), lang: 'en', setLang: () => {} }) }, '@/lib/auth': { isAdmin: () => admin, logout: () => { logoutCalls++; } }, }, { localStorage: { getItem: () => logged ? 'test-session-marker' : null }, window: { addEventListener: (name, callback) => events.push([name, callback]), removeEventListener: () => {} } }); const tree = load('src/components/Navbar.tsx').default({ showBlog: true, blogHref: '/blogs', navigationLanguageOnly: true }); const nodes = []; function visit(node) { if (Array.isArray(node)) return node.forEach(visit); if (!node || typeof node !== 'object') return; nodes.push(node); visit(node.props?.children); } visit(tree); for (const href of ['/blogs', '/services', '/slots', logged ? '/dashboard' : '/login']) assert(nodes.some(n => n.props?.href === href)); assert.equal(nodes.some(n => n.props?.href === '/admin'), logged && admin); assert.equal(nodes.some(n => n.props?.id === 'site-mobile-menu'), open); const toggle = nodes.find(n => n.props?.['aria-label'] === 'Toggle Menu'); assert.equal(toggle.props['aria-expanded'], open); toggle.props.onClick(); assert(changes.some(([i, value]) => i === 2 && value === !open)); effects[0](); assert(events.some(([name]) => name === 'storage')); assert(changes.some(([i, value]) => i === 0 && value === logged)); if (logged) { nodes.find(n => n.props?.title === 'logout').props.onClick(); assert.equal(logoutCalls, 1); assert(changes.some(([i, value]) => i === 2 && value === false)); } if (open) { nodes.find(n => n.props?.className?.includes('fixed inset-0')).props.onClick(); assert(changes.some(([i, value]) => i === 2 && value === false)); } } console.log('PASS: shared navbar SSR links/actions and English labels; provider defaults and document-language isolation; simulated mobile toggle/overlay and logged-out/user/admin/logout/storage branches. This is not browser interaction or authenticated end-to-end QA.');