Download tests/layout.test.mjs from fffiloni/hf-picks: direct link, hf CLI and curl.
- Browser
- Download file 11.3 kB
-
https://huggingface.co/spaces/fffiloni/hf-picks/resolve/main/tests/layout.test.mjs
- Command line
-
hf download hf://spaces/fffiloni/hf-picks/tests/layout.test.mjs
-
curl -L -o layout.test.mjs https://huggingface.co/spaces/fffiloni/hf-picks/resolve/main/tests/layout.test.mjs
11.3 kB
| import test from 'node:test'; | |
| import assert from 'node:assert/strict'; | |
| import {titleLayout,byline,textBlockLayout,counterIconTop,spineTypography,spineGeometry,spineWidth,drawSpine} from '../static/render.js'; | |
| import {reorder,selected,applyMetadata} from '../static/state.js'; | |
| test('long and unbroken titles fit with bounded font reduction and limited lines',()=>{ | |
| const ctx={font:'',measureText(text){return {width:Array.from(text).length*Number(this.font.match(/(\d+)px/)[1])*.52};}}; | |
| for(const wide of [false,true])for(const title of ['Flux','A very long paper title about visual understanding and multimodal model evaluation '.repeat(5),'LongModelIdentifierWithoutSpaces'.repeat(15),'模型与图像生成研究'.repeat(40)]){ | |
| const {size,lines}=titleLayout(ctx,title,1140,wide); | |
| assert.ok(size>=(wide?56:52)&&size<=(wide?100:84));assert.ok(lines.length>0&&lines.length<=(wide?4:3)); | |
| assert.ok(lines.every(line=>ctx.measureText(line).width<=1140)); | |
| if(title.length>150)assert.ok(lines.at(-1).endsWith('…')); | |
| } | |
| }); | |
| test('byline adds only known task metadata, with a readable separator',()=>{ | |
| assert.equal(byline({author:'Black Forest Labs',task:'text to image'}),'Black Forest Labs · Text to image'); | |
| assert.equal(byline({author:'Author'}),'Author');assert.equal(byline({task:'image-to-image'}),'Image to image'); | |
| }); | |
| test('an oversized unbroken title never leaves one isolated letter on its second line',()=>{ | |
| const ctx={font:'',measureText(text){return {width:text.length*Number(this.font.match(/(\d+)px/)[1])*.5};}}; | |
| const {lines}=titleLayout(ctx,'MiniMax-H3-Image',800,'wider'); | |
| assert.equal(lines.length,2);assert.ok(lines.every(line=>line.length>=5)); | |
| }); | |
| test('the long paper title is preserved by bounded fitting in every width',()=>{ | |
| const ctx={font:'',measureText(text){const s=Number(this.font.match(/([\d.]+)px/)[1]);return {width:text.length*s*.48,actualBoundingBoxAscent:s*.72,actualBoundingBoxDescent:s*.18};}}; | |
| const title='What Really Works for Universal Multimodal Embeddings? A Controlled Study of Contrastive Training Strategies'; | |
| for(const width of [240,320,640]){ | |
| const r=spineTypography(ctx,title,'Duo Li et al.',width,1142,width===640?['A short source description.','arXiv · 2610.15035']:[]); | |
| assert.equal(r.lines.join(' '),title);assert.ok(r.lines.every(line=>line.split(' ').length>=2)); | |
| assert.ok(Number(r.titleFont.match(/([\d.]+)px/)[1])>=(width===240?52:56)); | |
| } | |
| }); | |
| test('Slim and Wide two-line titles are lighter and preserve a minimum inset on both sides',()=>{ | |
| const ctx={font:'',measureText(text){const s=Number(this.font.match(/([\d.]+)px/)[1]);return {width:text.length*s*.48,actualBoundingBoxAscent:s*.72,actualBoundingBoxDescent:s*.18};}}; | |
| for(const [width,margin,cap] of [[240,20,76],[320,30,90]]){ | |
| const r=spineTypography(ctx,'Universal Multimodal Embeddings Research','Author',width,1142); | |
| const size=Number(r.titleFont.match(/([\d.]+)px/)[1]); | |
| assert.equal(r.lines.length,2);assert.ok(size<=cap); | |
| assert.ok(r.layout.title[0]-size*.72>=margin); | |
| const authorSize=Number(r.authorFont.match(/([\d.]+)px/)[1]); | |
| assert.ok(width-r.layout.author-authorSize*.18>=margin); | |
| } | |
| }); | |
| test('one- and two-line typographic blocks are centered with a constant author gap',()=>{ | |
| for(const width of [240,320])for(const count of [1,2])for(const hasAuthor of [true,false]){ | |
| const metrics=Array.from({length:count},(_,i)=>({ascent:width===320?74:63,descent:i===0?16:0})); | |
| const author=hasAuthor?{ascent:28,descent:8}:null,layout=textBlockLayout(width,metrics,width===320?98:82,author); | |
| const left=Math.min(...layout.title.map((x,i)=>x-metrics[i].ascent)); | |
| const titleRight=Math.max(...layout.title.map((x,i)=>x+metrics[i].descent)); | |
| const right=author?layout.author+author.descent:titleRight; | |
| assert.equal(left,width-right);assert.ok(left>=14); | |
| if(author)assert.equal(layout.author-author.ascent-titleRight,14); | |
| else assert.equal(layout.author,null); | |
| } | |
| }); | |
| test('title/author baselines do not depend on descenders, capitalization or byline length',()=>{ | |
| const ctx={font:'',measureText(text){ | |
| const size=Number(this.font.match(/([\d.]+)px/)[1]); | |
| return {width:text.length*size*.5,actualBoundingBoxAscent:size*(/[A-Z]/.test(text)?.75:.52),actualBoundingBoxDescent:/[gjpqy]/.test(text)?size*.2:0}; | |
| }}; | |
| for(const width of [240,320,640]){ | |
| const short=spineTypography(ctx,'FLUX','OPENAI',width,1140); | |
| const descenders=spineTypography(ctx,'gpt','huggingface',width,1140); | |
| const longAuthor=spineTypography(ctx,'model','A very long author and task '.repeat(10),width,1140); | |
| assert.deepEqual(short.layout,descenders.layout);assert.deepEqual(short.layout,longAuthor.layout); | |
| assert.equal(short.authorFont,longAuthor.authorFont);assert.ok(longAuthor.authorText.endsWith('…')); | |
| const two=spineTypography(ctx,'A long paper title '.repeat(20),'Author',width,1140); | |
| assert.ok(two.lines.length>=2&&two.lines.length<=(width===640?5:width===320?4:3)); | |
| assert.ok(Math.abs(two.layout.title[1]-two.layout.title[0]-Math.round(Number(two.titleFont.match(/([\d.]+)px/)[1])*.98))<1e-6); | |
| } | |
| }); | |
| test('hiding rank lowers title and details together; details and rank reserve independent space',()=>{ | |
| for(const width of [240,320,640]){ | |
| const on=spineGeometry(width,{numbers:true,details:true},true); | |
| const noRank=spineGeometry(width,{numbers:false,details:true},true); | |
| for(const field of ['base','ruleY','firstValueY','labelY','communityY'])assert.equal(noRank[field]-on[field],128); | |
| const noDetails=spineGeometry(width,{numbers:true,details:false},true); | |
| const neither=spineGeometry(width,{numbers:false,details:false},true); | |
| assert.equal(noDetails.base-on.base,280);assert.equal(neither.base-noDetails.base,128); | |
| assert.deepEqual(spineGeometry(width,{numbers:true},false),noDetails); | |
| assert.ok(noRank.communityY+12<2048-80); | |
| assert.ok(on.inset/width>=.15);assert.ok(on.valueMin>=26); | |
| } | |
| }); | |
| test('likes and upvotes align their icon center to the number at the fitted font size',()=>{ | |
| for(const ascent of [15,21])for(const label of ['likes','upvotes']){ | |
| const ctx={font:'400 29px sans-serif',measureText(text){ | |
| assert.equal(text,'12.5k');return {actualBoundingBoxAscent:ascent,actualBoundingBoxDescent:0}; | |
| }}; | |
| const top=counterIconTop(ctx,`12.5k ${label}`,1680,28); | |
| assert.equal(top+14,(1680-ascent+1680)/2); | |
| } | |
| }); | |
| test('all five kinds share value, label and community baselines with one detail icon',()=>{ | |
| const OriginalPath=globalThis.Path2D;globalThis.Path2D=class{constructor(path){this.path=path;}}; | |
| const samples=[ | |
| [{kind:'space',hardware:'ZeroGPU',likes:42},'ZeroGPU','Hardware','42 likes'], | |
| [{kind:'model',downloads:999900,likes:12345,library:'transformers'},'999.9k','Downloads / 30d','12.3k likes'], | |
| [{kind:'dataset',downloads:0,likes:0},'0','Downloads / 30d','0 likes'], | |
| [{kind:'paper',publishedAt:'2017-06-12',upvotes:149},'2017','Published','149 upvotes'], | |
| [{kind:'blog',publishedAt:'2024-07-30',upvotes:7},'Jul 2024','Published','7 upvotes'], | |
| [{kind:'space'},'—','Hardware','— likes'], | |
| [{kind:'paper'},'—','Published','— upvotes'], | |
| ]; | |
| try{ | |
| for(const mode of ['slim','wide','wider'])for(const details of [true,false])for(const [sample,value,label,community] of samples){ | |
| const texts=[],paths=[],ctx={font:'',measureText(text){const size=Number(this.font.match(/([\d.]+)px/)?.[1]||40);return {width:text.length*size*.4,actualBoundingBoxAscent:size*.72,actualBoundingBoxDescent:size*.18};},fillText(text,x,y,...args){texts.push({text,x,y,args});},stroke(path){if(path)paths.push(path.path);},createLinearGradient(){return {addColorStop(){}};}}; | |
| for(const fn of ['save','restore','scale','translate','rotate','beginPath','moveTo','lineTo','fillRect'])ctx[fn]=()=>{}; | |
| const canvas={getContext:()=>ctx};drawSpine(canvas,{id:'org/item',title:'Example',author:'Org',...sample},0,{width:mode,details,icons:false}); | |
| assert.equal(canvas.width,spineWidth(mode));assert.equal(canvas.height,2048); | |
| const g=spineGeometry(canvas.width,{details},true); | |
| if(details){ | |
| assert.ok(texts.some(t=>t.text===value&&t.x===g.inset&&t.y===g.firstValueY)); | |
| assert.ok(texts.some(t=>t.text===label&&t.x===g.inset&&t.y===g.labelY)); | |
| assert.ok(texts.some(t=>t.text===community&&t.x===g.inset+g.iconSize+g.iconGap&&t.y===g.communityY)); | |
| assert.equal(paths.length,1); | |
| }else{assert.equal(paths.length,0);assert.equal(texts.some(t=>t.text===community||t.text===label),false);} | |
| assert.ok(texts.every(t=>t.args.length===0),'Never distort font horizontally to force a fit'); | |
| assert.equal(texts.some(t=>t.text==='transformers'),details&&mode==='wider'&&sample.kind==='model'); | |
| } | |
| }finally{globalThis.Path2D=OriginalPath;} | |
| }); | |
| test('rank separator matches the stats inset and disappears with rank; upvote uses an arrow',()=>{ | |
| const OriginalPath=globalThis.Path2D;globalThis.Path2D=class{constructor(path){this.path=path;}}; | |
| try{ | |
| for(const mode of ['slim','wide','wider'])for(const numbers of [true,false]){ | |
| const rules=[],paths=[],ctx={font:'',measureText(text){const s=Number(this.font.match(/([\d.]+)px/)?.[1]||40);return {width:text.length*s*.48};},createLinearGradient(){return {addColorStop(){}};},moveTo(x,y){rules.push([x,y]);},stroke(path){if(path)paths.push(path.path);}}; | |
| for(const fn of ['save','restore','scale','translate','rotate','beginPath','lineTo','fillRect','fillText'])ctx[fn]=()=>{}; | |
| const canvas={getContext:()=>ctx};drawSpine(canvas,{kind:'paper',id:'1706.03762',title:'Attention',publishedAt:'2017-06-12',upvotes:149},0,{width:mode,numbers}); | |
| const g=spineGeometry(spineWidth(mode),{numbers},true); | |
| assert.deepEqual(rules,[[g.inset,g.ruleY],...(numbers?[[g.inset,g.rankRuleY]]:[])]); | |
| assert.ok(paths.includes('M12 21V3 M5 10l7-7 7 7')); | |
| } | |
| }finally{globalThis.Path2D=OriginalPath;} | |
| }); | |
| const items=()=>Array.from({length:65},(_,i)=>({key:`space:org/${i}`,kind:'space',id:`org/${i}`,title:`Item ${i}`,metadataState:'stub'})); | |
| test('reorder moves both ways across the export boundary without dropping hidden reserve pages',()=>{ | |
| let rows=items(),original=rows; | |
| rows=reorder(rows,'space:org/45',{before:'space:org/2'}); | |
| assert.equal(rows[2].id,'org/45');assert.equal(rows[20].id,'org/19'); | |
| assert.deepEqual(rows.filter(i=>i.id!=='org/45').map(i=>i.id),original.filter(i=>i.id!=='org/45').map(i=>i.id)); | |
| rows=reorder(rows,'space:org/45',{after:'space:org/55'}); | |
| assert.equal(rows[55].id,'org/45');assert.equal(rows.length,65);assert.equal(new Set(rows.map(i=>i.key)).size,65); | |
| assert.equal(selected({items:rows}).length,20); | |
| }); | |
| test('reorder and asynchronous metadata preserve edits and ignore missing anchors',()=>{ | |
| let rows=items();rows[40].displayTitle='My title';rows=reorder(rows,rows[40].key,{after:rows[0].key}); | |
| rows=applyMetadata(rows,[{requestedKey:'space:org/40',item:{...rows[1],title:'Updated title',hardware:'CPU',metadataState:'ready'}}]); | |
| assert.equal(rows[1].displayTitle,'My title');assert.equal(rows[1].title,'Updated title'); | |
| assert.equal(reorder(rows,'removed',{before:rows[0].key}),rows); | |
| assert.equal(reorder(rows,rows[0].key,{before:'removed'}),rows); | |
| }); | |