Files
SGF/scripts/stock-read-ui-check.mjs
2026-11-02 20:29:34 +02:00

24 lines
2.2 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import React from 'react';
import {renderToStaticMarkup} from 'react-dom/server';
import {createServer} from 'vite';
const server=await createServer({configFile:false,server:{middlewareMode:true,watch:null},appType:'custom',optimizeDeps:{noDiscovery:true}});
try{
const {default:Workspace}=await server.ssrLoadModule('/resources/js/StockMenuWorkspace.jsx');
const {milliQuantity}=await server.ssrLoadModule('/resources/js/StockReadWorkspace.jsx');
const context={token:'qa',company:{id:1,name:'Synthetic company'},unit:{id:7,name:'Synthetic unit'}};
const company=p=>p==='company.view',stock=(p,u)=>company(p)||(p==='stock.view'&&u===7);
const informational=renderToStaticMarkup(React.createElement(Workspace,{section:'stock_balances',context,can:company,navigate:()=>{},api:()=>{throw Error('SSR must not fetch');}}));
assert(informational.includes('Consulta de existências indisponível'));assert(!informational.includes('<form'));
const reading=renderToStaticMarkup(React.createElement(Workspace,{section:'stock_balances',context,can:stock,navigate:()=>{},api:()=>{throw Error('SSR must not fetch');}}));
for(const text of ['Somente leitura','Existências físicas','Movimentos','Armazéns','Artigos'])assert(reading.includes(text));
assert(!reading.includes('Consulta de existências indisponível'));assert(!reading.includes('value="0"'));
assert.equal(milliQuantity(null),'Indeterminado');assert.equal(milliQuantity('-2000'),'-2,000');
assert.equal(milliQuantity('9007199254740993000'),'9007199254740993,000');
const denied=renderToStaticMarkup(React.createElement(Workspace,{section:'stock_balances',context,can:()=>false,navigate:()=>{},api:()=>{}}));
assert(!denied.includes('<form'));
await fs.writeFile('docs/proofs/stock-read-ui-20261102.json',JSON.stringify({kind:'static_render_and_exact_quantity',company_permission_not_stock_permission:true,read_only_controls:true,unknown_not_zero:true,bigint_exact:true,backend_requests:0,visual_review:false},null,2)+'\n');
console.log('Stock read UI: permission separation, read-only controls, unavailable state and exact quantities passed; visual review pending.');
}finally{await server.close();}