tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

conformance-worker.test.mjs (1845B)


      1 import test from 'node:test';
      2 import assert from 'node:assert/strict';
      3 import {readFile} from 'node:fs/promises';
      4 // A cheap actual compiler smoke keeps relocated test-only drivers executable.
      5 // No program runs in the site's production JS (there is no such backend).
      6 test('test-only JS driver compiles, checkpoints, restores and evaluates an expression',async()=>{
      7  const saved={fetch:globalThis.fetch,onmessage:globalThis.onmessage,postMessage:globalThis.postMessage};
      8  let timer;
      9  try{
     10   globalThis.fetch=async input=>{
     11    const url=new URL(input),hash=url.pathname.match(/\/_arboricx\/bundle\/hash\/([0-9a-f]{64})$/)?.[1];
     12    const file=hash?new URL('../bundles/'+hash+'.arboricx',import.meta.url):url;
     13    return new Response(await readFile(file));
     14   };
     15   const send=(message,type)=>new Promise((resolve,reject)=>{
     16    timer=setTimeout(()=>reject(Error('Conformance worker timeout')),30000);
     17    globalThis.postMessage=reply=>{
     18     if(reply.type==='error'){clearTimeout(timer);reject(Error(reply.message));}
     19     if(reply.type===type){clearTimeout(timer);resolve(reply);}
     20    };
     21    globalThis.onmessage({data:message});
     22   });
     23   await import('../dist/conformance/compiler-worker.js?compile');
     24   const compiled=await send({type:'compile',environment:'empty',source:'id = (x : x)'},'compiled');
     25   assert.deepEqual(compiled.names,['id']);assert.ok(compiled.draft);
     26   await import('../dist/conformance/compiler-worker.js?restore');
     27   globalThis.onmessage({data:{type:'restore',draft:structuredClone(compiled.draft)}});
     28   const done=await send({type:'run',export:'id',expression:'id 42',arguments:[],output:'number',mode:'pure',step:false},'done');
     29   assert.equal(done.result.value,'42');
     30  }finally{
     31   clearTimeout(timer);
     32   for(const [key,value] of Object.entries(saved))if(value===undefined)delete globalThis[key];else globalThis[key]=value;
     33  }
     34 });