uikit/tests/bi-image.remote.test.cjs
Mohamadzadeh 78e073bffb
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Successful in 1m2s
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Successful in 12s
uikit-v1.8.3-biimage-transparent-background-complete
2026-11-18 15:49:58 +03:30

122 lines
5.6 KiB
JavaScript

const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const built = fs.readFileSync('dist/remote/BiImage.js','utf8');
const src = fs.readFileSync('src/remote/ui/BiImage/BiImage.remote.js','utf8');
const libProcessImage = fs.readFileSync('src/utils/images.js','utf8');
assert.ok(src.includes('import { processImage } from "../../../utils/images.js"'), 'source must use the real library utility');
assert.ok(built.includes(libProcessImage.trim().replace('export const processImage =', 'const processImage =')), 'standalone must bundle library processImage');
assert.doesNotMatch(built, /from ['"](?:react|react-dom|@chakra-ui|@emotion|next|vite)/);
assert.doesNotMatch(built, /^import\s/m, 'downloadable remote should be a single JS file');
let remoteClass;
let observing;
class FakeImage {
constructor() {
this.style = {};
this.attributes = {};
this._src = '';
this.className = '';
this.alt = '';
this.naturalWidth = 0;
this.naturalHeight = 0;
this.events = {};
}
addEventListener(name, cb) { this.events[name] = cb; }
setAttribute(name, value) { this.attributes[name] = value; }
getAttribute(name) { return name === 'src' ? (this._src || null) : (this.attributes[name] || null); }
removeAttribute(name) {
if (name === 'src') this._src = '';
else if (name === 'style') this.style = {};
delete this.attributes[name];
}
set src(value) { this._src = value; this.attributes.src = value; }
get src() { return this._src; }
getBoundingClientRect() {
const rawW = this.style.width;
const rawH = this.style.height;
const w = /^[0-9.]+px$/.test(rawW) ? parseFloat(rawW) : rawW === '100%' ? (this.parentWidth ?? 200) : 0;
const h = /^[0-9.]+px$/.test(rawH) ? parseFloat(rawH) : 0;
if (w && h) return { width: w, height: h };
if (w) return { width:w, height: this.naturalWidth ? w * this.naturalHeight / this.naturalWidth : 0 };
if (h) return { width: this.naturalHeight ? h * this.naturalWidth / this.naturalHeight : 0, height:h };
return { width: this.naturalWidth, height: this.naturalHeight };
}
}
class FakeElement {
constructor(){this.style={}; this.isConnected=false;}
appendChild(node){this.image=node;}
contains(node){return this.image===node;}
}
class FakeObserver {
constructor(cb){this.cb=cb;}
observe(img){this.image=img;observing=this;}
disconnect(){}
}
const context={
HTMLElement:FakeElement,
document:{createElement:(tag)=>{assert.equal(tag,'img');return new FakeImage();}},
customElements:{get:()=>remoteClass,define:(tag,cls)=>{assert.equal(tag,'uikit-bi-image');remoteClass=cls;}},
ResizeObserver:FakeObserver,
window:{ addEventListener(){},removeEventListener(){} },
};
vm.runInNewContext(built, context);
assert.equal(typeof remoteClass, 'function');
function mount(p){
const el=new remoteClass();
el.componentProps=p;
el.isConnected=true;
el.connectedCallback();
return el;
}
const key='users/logo.jpg';
const cdn='https://cdn.d.aiengines.ir/users/logo.jpg';
const makeUrl=(url,w,h)=>`https://img.d.aiengines.ir/preset:sharp${w||h?`/rs:auto:${w}:${h}:0`:''}/q:100/g:sm/plain/${url}@webp`;
let el=mount({src:key,height:70,width:'auto',alt:'Portrait'});
assert.equal(el.image.src,makeUrl(cdn,0,70),'height-constrained image should preserve natural width');
assert.equal(el.image.style.backgroundColor, undefined, 'transparent by default');
el.image.naturalWidth=140;el.image.naturalHeight=70;el.image.events.load({type:'load'});
assert.equal(el.image.src,makeUrl(cdn,0,70),'natural width after loading must not trigger different size URL');
observing.cb();
assert.equal(el.image.src,makeUrl(cdn,0,70),'resize observer must not loop on natural image aspect');
el=mount({src:key,style:{width:75,height:"auto"}});
assert.equal(el.image.src,makeUrl(cdn,75,0),'width-constrained image should preserve natural height');
el=mount({src:key,width:80,height:60});
assert.equal(el.image.src,makeUrl(cdn,80,60));
el=mount({src:key,style:{width:'100%',height:'auto',borderRadius:'12px'}});
assert.equal(el.image.src,makeUrl(cdn,200,0));
el.image.parentWidth=320;observing.cb();
assert.equal(el.image.src,makeUrl(cdn,320,0),'recompute only the constrained axis');
el=mount({src:'https://images.example.org/photo.png',height:40});
assert.equal(el.image.src,makeUrl('https://images.example.org/photo.png',0,40));
el=mount({src:undefined,width:70,height:70});
assert.equal(el.image.src,'');
assert.equal(el.image.style.width,'70px');
assert.equal(el.image.style.backgroundColor, undefined, 'transparent by default');
el=mount({src:key,width:70,height:70,fallbackSrc:'/static/placeholder.svg'});
el.image.events.error({type:'error'});
assert.equal(el.image.src,'/static/placeholder.svg','fallback should not be proxied or CDN-resolved');
el.image.events.error({type:'error'});
assert.equal(el.image.src,'','broken fallback should not leave a broken image icon');
el=mount({src:key,width:70,height:70});
el.image.events.error({type:'error'});
assert.equal(el.image.src,'','broken image should not leave a broken image icon');
el=mount({src:'data:image/png;base64,ZmFrZQ==',width:80,height:60});
assert.equal(el.image.src,'data:image/png;base64,ZmFrZQ==','data images should bypass proxy');
const entry=fs.readFileSync('dist/remote-ui-bi-image.js','utf8');
assert.match(entry,/^"use client";/);
assert.doesNotMatch(entry, /@chakra|@emotion|next\/image/);
assert.match(fs.readFileSync('package.json','utf8'), /"\.\/remote\/ui\/BiImage"/);
assert.match(fs.readFileSync('dist/remote-ui.js','utf8'), /import\s*\{\s*BiImage\s+as\s+\w+\s*\}\s*from\s*[\"']\.\/remote-ui-bi-image\.js/);
console.log('PASS: image auto dimensions, resizing, fallback, CDN, remote file, isolated import');