import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const requests = []; let disconnected = 0; let observed = 0; const frameQueue = new Map(); let nextFrame = 0; const flushFrames = () => { const frames = [...frameQueue.values()]; frameQueue.clear(); for (const cb of frames) cb(); }; class FakeElement { constructor(tagName = 'div') { this.tagName = tagName; this.children = []; this.className = ''; this.style = {}; this.isConnected = false; this.parentNode = null; this.listeners = new Map(); this.classList = { add: (...names) => { this.className = [...new Set([...this.className.split(' ').filter(Boolean), ...names])].join(' '); }, contains: name => this.className.split(' ').includes(name), }; this._src = ''; } set src(value) { this._src = String(value); requests.push({element: this, value: this._src}); } get src() { return this._src; } attachShadow() { this.shadowRoot = new FakeElement('shadow-root'); this.shadowRoot.parentNode = this; return this.shadowRoot; } connect(connected) { this.isConnected = connected; if (this.shadowRoot) this.shadowRoot.connect(connected); for (const child of this.children) child.connect(connected); } appendChild(child) { child.parentNode = this; this.children.push(child); child.connect(this.isConnected); return child; } replaceChildren(...children) { for (const child of this.children) child.connect(false); this.children = []; for (const child of children) this.appendChild(child); } querySelector(selector) { const wanted = selector.startsWith('.') ? selector.slice(1) : selector.split('.').at(-1); for (const child of this.children) { if (child.classList.contains(wanted)) return child; const match = child.querySelector(selector); if (match) return match; } return null; } getBoundingClientRect() { return {width: this.classList.contains('logo') ? 100 : this.classList.contains('bi-logo') ? 60 : 100}; } addEventListener(type, cb) { if (!this.listeners.has(type)) this.listeners.set(type, new Set()); this.listeners.get(type).add(cb); } removeEventListener(type, cb) { this.listeners.get(type)?.delete(cb); } dispatchEvent(event) { for (const cb of this.listeners.get(event.type) || []) cb(event); } } const registered = new Map(); globalThis.HTMLElement = FakeElement; globalThis.customElements = { get: name => registered.get(name), define: (name, cls) => registered.set(name, cls) }; globalThis.document = { createElement: tag => new FakeElement(tag) }; globalThis.ResizeObserver = class { observe() { observed++; } disconnect() { disconnected++; } }; globalThis.window = { location: { href: 'https://destination.example/app/index' }, requestAnimationFrame: callback => { const id = ++nextFrame; frameQueue.set(id, callback); return id; }, cancelAnimationFrame: id => frameQueue.delete(id), getComputedStyle: element => element.classList.contains('logo') ? {paddingLeft:'8px', paddingRight:'8px'} : {paddingLeft:'0px',paddingRight:'0px'}, addEventListener() {}, removeEventListener() {}, }; await import('../dist/remote/AppHeader.js'); const AppHeaderElement = registered.get('uikit-app-header'); const mount = props => { const header = new AppHeaderElement(); header.connect(true); header.connectedCallback(); header.componentProps = props; flushFrames(); return header; }; test('default BI logo ships as the exact original 500×500 image, not Base64 or a thumbnail', () => { const header = mount({title:'Example',showBi:true}); const bi = header.shadowRoot.querySelector('.bi-logo'); assert.ok(bi); assert.match(bi.src, /\/bi-logo\.webp$/); assert.doesNotMatch(bi.src, /^data:/); const source = fs.readFileSync(path.join(root, 'src/assets/bi-logo.webp')); const packaged = fs.readFileSync(path.join(root, 'dist/remote/bi-logo.webp')); assert.deepEqual(packaged, source, 'prebuilt remote must use the untouched original source image'); assert.equal(source.length, 64_536); assert.equal(fs.existsSync(path.join(root, 'dist/remote/bi-logo-header.webp')), false, 'thumbnail must not ship'); const remoteSource = fs.readFileSync(path.join(root, 'src/remote/ui/AppHeader/AppHeader.remote.jsx'), 'utf8'); assert.match(remoteSource, /new URL\(DEFAULT_BI_LOGO_FILE, import\.meta\.url\)/); assert.match(remoteSource, /const DEFAULT_BI_LOGO_FILE = "bi-logo\.webp"/); const shippedJs = fs.readFileSync(path.join(root, 'dist/remote/AppHeader.js'), 'utf8'); assert.match(shippedJs, /new URL\(DEFAULT_BI_LOGO_FILE, import\.meta\.url\)/); assert.doesNotMatch(shippedJs, /data:image\/[^;]+;base64/); assert.doesNotMatch(shippedJs, /^import\s/m, 'AppHeader remote should remain self-contained'); assert.ok(fs.statSync(path.join(root,'dist/remote/AppHeader.js')).size < 20_000); header.disconnectedCallback(); }); test('independent BI & main opt-in, resizing, fallback, and lifecycle', () => { const previousDisconnected=disconnected; const header = mount({ title:'Header', logoSrc:'/main.svg', logoOptimize:true, biLogoSrc:'https://uikit.example/bi-logo.webp', biLogoOptimize:true, showBi:true, }); const main = header.shadowRoot.querySelector('.logo'); const bi = header.shadowRoot.querySelector('.bi-logo'); assert.ok(main && bi); assert.match(main.src, /\/rs:auto:84:84:0\/q:100\/g:sm\/plain\/https:\/\/destination\.example\/main\.svg@webp/); assert.match(bi.src, /\/rs:auto:60:60:0\/q:100\/g:sm\/plain\/https:\/\/uikit\.example\/bi-logo\.webp@webp/); assert.ok(bi.classList.contains('bi-logo--optimized')); bi.dispatchEvent({type:'error'}); assert.equal(bi.src, 'https://uikit.example/bi-logo.webp'); assert.equal(main.src.includes('img.d.aiengines.ir'), true, 'BI fallback must not alter the main logo'); // Prop updates with same values must not tear down header and observers. const immutableProps = {...header.componentProps}; header.componentProps = immutableProps; assert.equal(disconnected, previousDisconnected); header.componentProps={...immutableProps,biLogoOptimize:false}; flushFrames(); assert.equal(disconnected, previousDisconnected + 2); assert.equal(header.shadowRoot.querySelector('.bi-logo').src,'https://uikit.example/bi-logo.webp'); assert.equal(header.shadowRoot.querySelector('.bi-logo').classList.contains('bi-logo--optimized'),false); header.disconnectedCallback(); assert.equal(disconnected, previousDisconnected + 3); }); test('BI remains optional and the wrapper publishes the optimize prop', () => { const header = mount({title:'No BI',showBi:false}); assert.equal(header.shadowRoot.querySelector('.bi-logo'),null); header.disconnectedCallback(); for (const filename of ['src/remote/ui/AppHeader/index.jsx','dist/remote-ui-app-header.js']) { assert.match(fs.readFileSync(path.join(root,filename),'utf8'),/biLogoOptimize/); } assert.match(fs.readFileSync(path.join(root,'dist/remote-ui-app-header.d.ts'),'utf8'),/biLogoOptimize\?: boolean/); }); test('default BI optimize selects original file as its input and keeps remote file identity', () => { // Node import.meta.url is file://, so the production image service must not be called. // Browser deployments use https:// and are covered in the preceding HTTP-source test. const header = mount({title:'Default BI optimized', showBi:true, biLogoOptimize:true}); const bi = header.shadowRoot.querySelector('.bi-logo'); assert.ok(bi); assert.match(bi.src, /\/bi-logo\.webp$/); assert.doesNotMatch(bi.src, /bi-logo-header|data:image/); header.disconnectedCallback(); }); test('regular build:remote always rebuilds AppHeader from source and copies its original logo', () => { const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')); assert.match(pkg.scripts['build:remote'], /vite build --mode remote.*build-app-header-remote\.mjs/); assert.match(pkg.version, /^1\.(?:[6-9]|[1-9]\d+)\.\d+$/, 'AppHeader fix requires UI Kit >= 1.6.2'); const builder = fs.readFileSync(path.join(root, 'scripts/build-app-header-remote.mjs'), 'utf8'); assert.match(builder, /src\/assets\/bi-logo\.webp/); assert.match(builder, /dist\/remote/); assert.match(builder, /copyFile\(originalLogoPath, outputLogoPath\)/); assert.match(builder, /writeFile\(outputJsPath, js/); });