Files
s41r4j-phomber/tests/article-template.test.mjs
T
2026-11-20 15:34:50 +05:30

33 lines
3.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {mkdtemp,rm} from 'node:fs/promises';
import {tmpdir} from 'node:os';
import {join} from 'node:path';
import {Workspace} from '../dist/workspace/index.js';
import {investigate} from '../dist/core/engine.js';
import {validateArticleSections,mapArticleCitations} from '../dist/core/article.js';
import {validateInvestigation} from '../dist/core/schema.js';
import {renderBiography} from '../dist/reports/article.js';
import {brandMark,favicon} from '../dist/reports/branding.js';
test('provider-neutral blocks preserve narrative order, references, metadata, escaping and shared favicon',async t=>{
const root=await mkdtemp(join(tmpdir(),'phomber-template-'));t.after(()=>rm(root,{recursive:true,force:true}));const w=new Workspace(root);await w.init();
const v=await investigate({schema_version:'1.0',subject:{name:'Template Person'},research:{offline:true}},w);
v.sources=[{id:'s1',adapter:'fixture',url:'https://example.org',status:'ok',retrieved_at:'2026-11-11T12:00:00Z'}];
v.findings=[{id:'f1',category:'career',field:'role',value:'Researcher',source_ids:['s1'],status:'lead',identity:'candidate',confidence:{score:null,explanation:'Candidate'}}];
v.evidence=[{id:'e1',source_id:'s1',finding_id:'f1',excerpt:'A source says research work.',retrieved_at:'2026-11-11T12:00:00Z'}];
const data={sections:[{title:'Unusual research activities',blocks:[{type:'paragraph',text:'Before the table \u2014 <script>unsafe</script>',evidence_ids:['E1']},{type:'table',caption:'Evidence table',columns:['Work','Year'],rows:[{cells:['Example','2026'],evidence_ids:['E1']}]},{type:'paragraph',text:'After the table',evidence_ids:['E1']},{type:'subheading',title:'Additional work'},{type:'list',ordered:true,items:[{text:'Public work',evidence_ids:['E1']}]},{type:'quote',text:'research work.',evidence_ids:['E1']}]}]};
mapArticleCitations(data,v);v.ai={provider:'Future provider',model:'future-test-model',status:'complete',limitations:[],sections:validateArticleSections(data,v)};
v.research_timing={started_at:'2026-11-11T12:00:00Z',finished_at:'2026-11-11T12:01:00Z',duration_ms:60000};
v.usage={calls:[],totals:{model_calls:1,search_calls:2,fetch_calls:3,prompt_tokens:1234,completion_tokens:567},estimated_usd_min:.01,estimated_usd_max:.02,pricing_source:'https://example.org',pricing_note:'Fixture estimate, not an invoice.'};
validateInvestigation(v);await w.save(v);assert.equal((await w.load(v.id)).ai.sections[0].blocks[1].type,'table');
const html=renderBiography(v);assert.ok(html.indexOf('Before the table')<html.indexOf('<caption>Evidence table'));
assert.ok(html.indexOf('<caption>Evidence table')<html.indexOf('After the table'));
assert.match(html,/&lt;script&gt;unsafe&lt;\/script&gt;/);assert.ok(!html.includes('\u2014'));
assert.match(html,/Future provider/);assert.match(html,/future-test-model/);assert.match(html,/1,234 \/ 567/);assert.match(html,/\$0.010000 to \$0.020000/);assert.match(html,/60.0 seconds/);
assert.ok(html.includes(brandMark));assert.ok(html.includes(favicon.replaceAll("'",'&#39;')));assert.ok(decodeURIComponent(favicon.split(',')[1])===brandMark);
assert.match(html,/href="#source-s1">\[1\]/);assert.ok(html.indexOf('<h2>Article metadata')>html.indexOf('<h2>References'));
const bad=structuredClone(data);bad.sections[0].blocks[1].rows[0].evidence_ids=['unknown'];assert.throws(()=>validateArticleSections(bad,v),/unsupported evidence/);
bad.sections[0].blocks[1].rows[0].evidence_ids=['e1'];bad.sections[0].blocks[1].rows[0].cells.pop();assert.throws(()=>validateArticleSections(bad,v),/row width/);
assert.throws(()=>validateArticleSections({sections:[{title:'Test',blocks:[{type:'quote',text:'invented quote',evidence_ids:['e1']}]}]},v),/match cited evidence/);
});