Assessor_2.0/tests/dummy-results-excel-audit.mjs

43 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Read-only audit of the full Dummy through the normal Results presentation and Excel exporter.
import { build } from 'esbuild';
import { mkdir, writeFile, readFile } from 'node:fs/promises';
import { pathToFileURL } from 'node:url';
import { resolve } from 'node:path';
const previous=await readFile('tests/dummy-results-audit.mjs','utf8');
const loader=previous.split('contents: `')[1].split('const dataset=workspace.dataset;')[0];
const source=`${loader}
import React from 'react';import {renderToStaticMarkup} from 'react-dom/server';
import {JSDOM} from 'jsdom';import {read,utils} from 'xlsx';
import {FloatingResultsWindow} from './src/features/linked-results/FloatingResultsWindow';
import {resultSectionsFor} from './src/features/linked-results/result-message';
import {resultsExcelContent} from './src/features/linked-results/results-excel-content';
import {buildResultsWindowExcel} from './src/exports/results-window-excel';
const dom=new JSDOM('<!doctype html><body></body>');
Object.assign(globalThis,{window:dom.window,document:dom.window.document,DOMParser:dom.window.DOMParser,Node:dom.window.Node});
const counts={};let maxRows=0;
for(const sheet of workspace.dataset.sheets.filter(s=>s.isProjectSheet))for(const record of sheet.analysis.records){
const tool=toolIdForRecord(record,sheet.toolId);const sheets=recordResultSheets(record,workspace.dataset.sheets,sheet.id,tool);const sections=resultSectionsFor(record,tool);
if(!sheets.length&&!sections.length)continue;
const before=JSON.stringify({sheets,sections});
const html=renderToStaticMarkup(<FloatingResultsWindow id={record.id} title={'Results – '+record.title} finding={record} toolId={tool} outcome={resultOutcomeFor(record,tool)} sheets={sheets} sections={sections} color="" zIndex={1} offset={0} search="" onActivate={()=>{}} onClose={()=>{}}/>);
const host=document.createElement('div');host.innerHTML=html;
const content=resultsExcelContent(host.querySelector('.results-window-body'),record.title,sections,sheets);
const full=read(buildResultsWindowExcel(content),{type:'array'});
const exportedNames=full.SheetNames.filter(name=>/^Results(?: \\d+)?$/.test(name));
if(exportedNames.length!==content.tables.length)throw new Error('Table loss: '+record.id);
for(let index=0;index<content.tables.length;index++){
const original=content.tables[index];const rows=utils.sheet_to_json(full.Sheets[exportedNames[index]],{header:1,defval:''}).slice(1,1+original.rows.length);
if(rows.length!==original.rows.length)throw new Error('Row loss: '+record.id);
for(let row=0;row<original.rows.length;row++)for(let col=0;col<original.rows[row].length;col++){
if(original.rows[row][col].length<=32767 && rows[row]?.[col]!==original.rows[row][col])throw new Error('Cell changed: '+record.id);
}
maxRows=Math.max(maxRows,original.rows.length-1);
}
if(content.tables.length){const first=content.tables[0];const subset=read(buildResultsWindowExcel(content,new Set([first.id])),{type:'array'});if(subset.SheetNames.filter(name=>/^Results(?: \\d+)?$/.test(name)).length!==1)throw new Error('Subset loss');}
if(JSON.stringify({sheets,sections})!==before)throw new Error('Source mutation');
counts[tool]??={findings:0,tables:0,rows:0};counts[tool].findings++;counts[tool].tables+=content.tables.length;counts[tool].rows+=content.tables.reduce((n,t)=>n+Math.max(0,t.rows.length-1),0);
}
console.log(JSON.stringify({counts,maxRows,allCellsUnchanged:true},null,2));dom.window.close();`;
const bundle=await build({stdin:{resolveDir:process.cwd(),loader:'tsx',contents:source},jsx:'automatic',bundle:true,platform:'node',format:'esm',packages:'external',write:false});
await mkdir('node_modules/.cache',{recursive:true});const target=resolve('node_modules/.cache/dummy-results-excel-audit.mjs');await writeFile(target,bundle.outputFiles[0].contents);await import(pathToFileURL(target).href);