Initial commit: update

This commit is contained in:
chris
2026-10-21 19:34:42 +00:00
parent 1814ea3a0f
commit 67a7fb6385
103 changed files with 3366 additions and 780 deletions

View File

@@ -5,25 +5,41 @@ import path from 'node:path';
import {pathToFileURL} from 'node:url';
import ts from 'typescript';
const source=fs.readFileSync(new URL('../src/widgets/mesClient.ts',import.meta.url),'utf8');
const javascript=ts.transpileModule(source,{fileName:'mesClient.ts',compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ESNext}}).outputText;
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'mes-check-'));const file=path.join(dir,'client.mjs');fs.writeFileSync(file,javascript);
// Native Widgets use the server-verified IMSAPI-HMI session, never sessionStorage.globallogin.
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'mes-check-'));
const widgetsDir=path.join(dir,'widgets');fs.mkdirSync(widgetsDir);
const transpile=(source,fileName)=>ts.transpileModule(source,{fileName,compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ESNext}}).outputText;
const root=new URL('../src/',import.meta.url);
const apiSource=fs.readFileSync(new URL('api.ts',root),'utf8');
const mesSource=fs.readFileSync(new URL('widgets/mesClient.ts',root),'utf8');
fs.writeFileSync(path.join(dir,'api.mjs'),transpile(apiSource,'api.ts'));
fs.writeFileSync(path.join(widgetsDir,'mesClient.mjs'),transpile(mesSource,'mesClient.ts').replace("from '../api'","from '../api.mjs'"));
try{
globalThis.window={location:{origin:'https://demo.local'},setTimeout,clearTimeout};
globalThis.location={origin:'https://demo.local'};
globalThis.sessionStorage={getItem(k){return k==='globallogin'?JSON.stringify({result:{sessionContext:{sessionId:'T-1',persId:'P-1'}}}):k==='url'?'https://demo.local':null}};
const lib=await import(pathToFileURL(file).href);
const api=await import(pathToFileURL(path.join(dir,'api.mjs')).href);
const lib=await import(pathToFileURL(path.join(widgetsDir,'mesClient.mjs')).href);
assert.equal(lib.readMesSession(),null,'No implied login via browser storage');
assert.equal(lib.structuredRows([1,'A',2,'B'],['x','y']).length,2);
assert.throws(()=>lib.structuredRows([1,2,3],['x','y']),/Ergebnis-Keys/);
assert.equal(lib.normalizeLayer('TOP'),'0');assert.equal(lib.layerLabel('2'),'INDEPENDENT');
assert.equal(lib.stationValid('../bad'),false);assert.equal(lib.serialValid('36051005094090005'),true);
const actions=[];
globalThis.fetch=async(url,opts)=>{const name=String(url).split('/').at(-1);const request=JSON.parse(opts.body);actions.push([name,request]);
const response=name==='trGetSerialNumberHistoryData'?{return_value:0,bookingResultValues:['S1','0','Werk','01','ST','WO','Montage','1',1000,'0','5','2'],failureDataResultValues:[],failureSlipDataResultValues:[],measureDataResultValues:[]}:
globalThis.fetch=async (url,opts)=>{
const action=String(url).split('/').at(-1);
const request=opts.body ? JSON.parse(opts.body):{};
actions.push({action,request,headers:opts.headers});
if(action==='csrf')return {ok:true,status:200,text:async()=>JSON.stringify({user:'worker',station:'ST',csrf:'secret-csrf',designer:false,publisher:false,admin:false})};
const result=action==='trGetSerialNumberHistoryData'?{return_value:0,bookingResultValues:['S1','0','Werk','01','ST','WO','Montage','1',1000,'0','5','2'],failureDataResultValues:[],failureSlipDataResultValues:[],measureDataResultValues:[]}:
{return_value:0,mergePartsResultValues:[],setupResultValues:[]};
return {ok:true,json:async()=>({result:response})};};
const h=await lib.getHistory('ST','S1');assert.equal(h.sections.booking.length,1);assert.equal(actions.length,3);assert.equal(actions[0][1].desolvingSerialNumber,'1');
globalThis.fetch=async()=>({ok:true,json:async()=>({result:{return_value:-110,errorString:'Serial not found'}})});
return {ok:true,status:200,text:async()=>JSON.stringify({result})};
};
await api.signInContext();
assert.ok(lib.readMesSession(),'server-provided HMI identity recognized');
const history=await lib.getHistory('ST','S1');assert.equal(history.sections.booking.length,1);
const requests=actions.filter(a=>a.action!=='csrf');assert.equal(requests.length,3);
assert.equal(requests[0].request.desolvingSerialNumber,'1');
assert.equal(requests[0].request.sessionContext,undefined,'client must not serialize IMSAPI context');
assert.equal(requests[0].headers['X-CSRF-Token'],'secret-csrf');
globalThis.fetch=async()=>({ok:true,status:200,text:async()=>JSON.stringify({result:{return_value:-110,errorString:'Serial not found'}})});
await assert.rejects(()=>lib.getHistory('ST','S1'),error=>error instanceof lib.MesError&&error.code===-110);
console.log('MES client tests passed (parsing, session, normalized layer, 6 data requests, error codes)');
console.log('PASS: MES client uses server session/CSRF, parses normalized history, handles IMSAPI errors');
}finally{fs.rmSync(dir,{recursive:true,force:true});}

View File

@@ -0,0 +1,14 @@
import assert from 'node:assert/strict';
import { navigationRoots,descendantPageIds } from '../src/navigation.ts';
const p=(id,order,parentPageId=null,menuGroup='Fertigung')=>({id,key:id,title:id,order,parentPageId,menuGroup,enabled:true});
const pages=[p('root',2),p('child',4,'root'),p('nested',1,'child'),p('second',1),p('outside',1,null,'Service')];
const roots=navigationRoots(pages);
assert.deepEqual(roots.map(r=>r.name),['Fertigung','Service']);
assert.deepEqual(roots[0].children.map(n=>n.page.key),['second','root']);
assert.deepEqual(roots[0].children[1].children.map(n=>n.page.key),['child']);
assert.deepEqual(roots[0].children[1].children[0].children.map(n=>n.page.key),['nested']);
assert.deepEqual([...descendantPageIds(pages,'root')].sort(),['child','nested']);
assert.deepEqual(navigationRoots([p('cycle-a',1,'cycle-b'),p('cycle-b',2,'cycle-a')])[0].children.map(n=>n.page.key),['cycle-a','cycle-b']);
const missing=navigationRoots([p('orphan',1,'not-existing')]);
assert.equal(missing[0].children[0].page.key,'orphan');
console.log('Navigation: 6 tests passed');

View File

@@ -0,0 +1,23 @@
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';
import assert from 'node:assert/strict';
import ts from 'typescript';
const base=path.resolve(path.dirname(url.fileURLToPath(import.meta.url)),'../src');
const code=fs.readFileSync(path.join(base,'widgetPackage.ts'),'utf8');
const js=ts.transpileModule(code,{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText;
const defs={'ui.text':{type:'ui.text'},'ui.script':{type:'ui.script'},'ui.image':{type:'ui.image'}};
const m={exports:{}}; new Function('module','exports','require',js)(m,m.exports,()=>({catalogByType:defs}));
const {parseWidgetPackage,makeExportPackage}=m.exports;
const widget={id:'text-1',type:'ui.text',x:0,y:0,w:4,h:2,mandatory:false,personalizable:true,visibleToRoles:[],props:{title:'Test'}};
const pkg=makeExportPackage(widget);
assert.equal(parseWidgetPackage(JSON.stringify(pkg)).widget.id,'text-1');
const invalid=JSON.parse(JSON.stringify(pkg));invalid.widget.type='__proto__';
assert.throws(()=>parseWidgetPackage(JSON.stringify(invalid)));
const invalidRole=JSON.parse(JSON.stringify(pkg));invalidRole.widget.mandatory=true;invalidRole.widget.visibleToRoles=['hmi-special'];
assert.throws(()=>parseWidgetPackage(JSON.stringify(invalidRole)));
const invalidScript=JSON.parse(JSON.stringify(pkg));invalidScript.widget.type='ui.script';invalidScript.widget.props={script:'x'.repeat(8001)};
assert.throws(()=>parseWidgetPackage(JSON.stringify(invalidScript)));
const image=JSON.parse(JSON.stringify(pkg));image.widget.type='ui.image';image.image={name:'logo.png',mime:'text/html',base64:'PGh0bWw+'};
assert.throws(()=>parseWidgetPackage(JSON.stringify(image)));
console.log('Widget-Format: 5 Prüfungen erfolgreich');