Loading files
442335385This file is pinned to the selected release. Download the original to open it in your design tool.
import {test} from 'node:test';import assert from 'node:assert/strict';import {mkdtempSync,readFileSync,writeFileSync,rmSync,chmodSync} from 'node:fs';import {tmpdir} from 'node:os';import {join} from 'node:path';import {fileURLToPath} from 'node:url';import {spawnSync} from 'node:child_process';import {inflateRawSync} from 'node:zlib';import {prepare,buildDocx,buildXlsx} from '../skills/sheetsdoc/scripts/ooxml.mjs';
const root=fileURLToPath(new URL('..',import.meta.url)),source=JSON.parse(readFileSync(join(root,'template/doc.json')));
function unzip(b){const files={};let p=0;while(b.readUInt32LE(p)===0x04034b50){const method=b.readUInt16LE(p+8),size=b.readUInt32LE(p+18),n=b.readUInt16LE(p+26),extra=b.readUInt16LE(p+28),name=b.subarray(p+30,p+30+n).toString(),start=p+30+n+extra,data=b.subarray(start,start+size);files[name]=(method===8?inflateRawSync(data):data).toString();p=start+size;}return files;}
test('one source drives accurate narrative, table totals and formula caches',()=>{const m=prepare(source);assert.equal(m.summary.prior,45500);assert.equal(m.summary.current,49200);assert.equal(m.summary.delta,3700);assert.deepEqual(m.summary.leaders,['South']);assert.match(m.paragraphs[0],/8.1%/);assert.match(m.paragraphs[0],/South/);const x=unzip(buildXlsx(source));assert.match(x['xl/worksheets/sheet1.xml'],/<f>SUM\(C7:C10\)<\/f><v>49200/);assert.match(x['xl/worksheets/sheet1.xml'],/IF\(B7=0,"n.a.",D7\/B7\)/);assert.match(x['xl/styles.xml'],/"USD "#,##0/);const d=unzip(buildDocx(source));assert.match(d['word/document.xml'],/South contributed the largest/);assert.match(d['word/document.xml'],/w:tblHeader/);});
test('changed inputs update prose and totals; zero denominators are unavailable',()=>{const s=structuredClone(source);s.comparison.rows[0].current=20000;assert.deepEqual(prepare(s).summary.leaders,['North']);s.comparison.rows.forEach(r=>r.prior=0);const m=prepare(s);assert.equal(m.summary.growth,null);assert.equal(m.table.at(-1).at(-1),'n.a.');});
test('invalid numbers, duplicate regions, XML controls and malformed rows fail',()=>{const s=structuredClone(source);s.comparison.rows[0].prior=Infinity;assert.throws(()=>prepare(s),/finite/);s.comparison.rows[0].prior=1;s.comparison.rows[1].region='North';assert.throws(()=>prepare(s),/unique/);assert.throws(()=>prepare({...source,title:'bad\u0001'}),/XML/);assert.throws(()=>prepare({title:'T',sheet:{rows:[['a','b'],[1]]}}),/rectangular/);});
test('generic text remains literal including formula-looking text and XML markup',()=>{const parts=unzip(buildXlsx({title:'T',sheet:{name:'Data',rows:[['a','b'],['=WEBSERVICE("x")','<script>']]}}));assert.match(parts['xl/worksheets/sheet1.xml'],/inlineStr/);assert.match(parts['xl/worksheets/sheet1.xml'],/<script>/);assert.doesNotMatch(parts['xl/worksheets/sheet1.xml'],/<f>/);});
test('failed conversion cannot bless an old PDF or overwrite previous Office artifacts',()=>{const ws=mkdtempSync(join(tmpdir(),'sheets-failure-'));try{writeFileSync(join(ws,'doc.json'),JSON.stringify(source));for(const name of ['out.pdf','out.docx','out.xlsx'])writeFileSync(join(ws,name),'previous');const binary=join(ws,'fixture');writeFileSync(binary,'#!/bin/sh\nexit 0\n');chmodSync(binary,0o755);const result=spawnSync(process.execPath,[join(root,'skills/sheetsdoc/scripts/build.mjs')],{env:{...process.env,HARNESS_WORKSPACE:ws,SOFFICE_BIN:binary},encoding:'utf8'});assert.equal(result.status,1);const verdict=JSON.parse(readFileSync(join(ws,'.harness/verdict.json')));assert.equal(verdict.ready,false);assert.equal(verdict.artifact,null);for(const name of ['out.pdf','out.docx','out.xlsx'])assert.equal(readFileSync(join(ws,name),'utf8'),'previous');}finally{rmSync(ws,{recursive:true,force:true});}});
test('a one-column generic workbook has valid column bounds and no self-merges',()=>{const parts=unzip(buildXlsx({title:'List',sheet:{name:'Items',rows:[['Item'],['One']]}})),xml=parts['xl/worksheets/sheet1.xml'];assert.doesNotMatch(xml,/min="2" max="1"|mergeCell ref="A1:A1"/);assert.match(xml,/<col min="1" max="1"/);});