Spaces:
Sleeping

benchmark / test_comparison.mjs
Mushari440's picture
Fix symmetric comparison rounding and clarify score precision
c8f896c verified
Raw History Blame Contribute Delete
4.05 kB
// Protect B-minus-A direction, ID-based matching, missing-score handling,
// denominator/split/version boundaries and safe rendering of model-supplied labels.
import fs from 'node:fs';
import assert from 'node:assert/strict';
const base=new URL('.',import.meta.url);
const smoke=fs.readFileSync(new URL('smoke_board.mjs',base),'utf8');
new Function(smoke.slice(smoke.indexOf('const el ='),smoke.indexOf('let threw =')))();
const script=fs.readFileSync(new URL('index.html',base),'utf8').match(/<script>([\s\S]*)<\/script>/)[1];
const ctx=new Function(script+';return {compare:typeof compareModels==="function"?compareModels:null,html:typeof comparisonTableHTML==="function"?comparisonTableHTML:null};')();
assert.equal(typeof ctx.compare,'function','Two-model comparison must be implemented');
const dims=[['arabic','Arabic',5],['math','Math',2]];
const a={name:'org/A',benchmarkVersion:'v12',avg:50,scores:{arabic:50,math:null},subtasks:{version:'v12',split:'test',split_sha256:'same',rows:[
{task_id:'arabic',task_label:'Arabic',subtask_id:'x',label:'Correction',score:0,count:2,unresolved_count:1,error_count:1},
{task_id:'arabic',task_label:'Arabic',subtask_id:'y',label:'Diacritization',score:1,count:3,unresolved_count:0,error_count:0}]}};
const b={name:'org/B',benchmarkVersion:'v12',avg:60,scores:{arabic:60,math:0},subtasks:{version:'v12',split:'test',split_sha256:'same',rows:[
{task_id:'arabic',task_label:'Arabic',subtask_id:'y',label:'Diacritization',score:.5,count:3,unresolved_count:0,error_count:0},
{task_id:'arabic',task_label:'Arabic',subtask_id:'x',label:'Correction',score:.5,count:2,unresolved_count:0,error_count:0}]}};
let got=ctx.compare(a,b,'tasks','v12',dims);
assert.equal(got.rows.length,2);assert.equal(got.rows[0].delta,10);assert.equal(got.rows[1].a,null);assert.equal(got.rows[1].b,0);assert.equal(got.rows[1].delta,null,'Missing A must not become zero');
got=ctx.compare(a,b,'subtasks','v12',dims);
assert.equal(got.rows.find(r=>r.id==='arabic|x').delta,50,'Match by task/subtask ID, never array position');
assert.equal(got.rows.find(r=>r.id==='arabic|y').delta,-50);
assert.equal(ctx.compare(b,a,'subtasks','v12',dims).rows.find(r=>r.id==='arabic|x').delta,-50,'Swapping reverses the difference');
const missing=structuredClone(a);missing.subtasks=null;got=ctx.compare(missing,b,'subtasks','v12',dims);assert.equal(got.rows[0].delta,null);assert.match(got.notice,/unavailable/i);
const mismatch=structuredClone(b);mismatch.subtasks.split_sha256='other';got=ctx.compare(a,mismatch,'subtasks','v12',dims);assert.equal(got.rows.length,0);assert.match(got.notice,/split/i);
const countMismatch=structuredClone(b);countMismatch.subtasks.rows[1].count=3;got=ctx.compare(a,countMismatch,'subtasks','v12',dims);assert.equal(got.rows.find(r=>r.id==='arabic|x').delta,null);assert.match(got.notice,/count/i);
const otherVersion=structuredClone(b);otherVersion.benchmarkVersion='v11';assert.equal(ctx.compare(a,otherVersion,'tasks','v12',dims).rows.length,0);
assert.match(ctx.compare(a,a,'tasks','v12',dims).notice,/different models/i);
const hostile=ctx.compare(a,b,'subtasks','v12',dims);hostile.rows[0].label='<img src=x onerror=alert(1)>';
let html=ctx.html(hostile,'gain');assert.ok(!html.includes('<img'));assert.match(html,/&lt;img/);assert.match(html,/0\.00%/);assert.match(html,/\+50\.00/);assert.ok(html.indexOf('Correction')<0);
html=ctx.html(ctx.compare(a,b,'subtasks','v12',dims),'loss');assert.ok(html.indexOf('Diacritization')<html.indexOf('Correction'),'Largest B loss first');
// Half-cent rounding must be symmetric in both directions.
const halfA={name:'half/A',scores:{arabic:0},benchmarkVersion:'v12'};
const halfB={name:'half/B',scores:{arabic:0.125},benchmarkVersion:'v12'};
assert.equal(ctx.compare(halfA,halfB,'tasks','v12',dims).rows[0].delta,0.13);
assert.equal(ctx.compare(halfB,halfA,'tasks','v12',dims).rows[0].delta,-0.13);
console.log('PASS: task/subtask comparison, delta direction, swapped models, missing data, split/count/version guards, ordering and escaping');
process.exit(0);