gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchvendor-day59b37cc1da|||PR Vendor Hybriel day59Atlasvendor-day59/plugins/fetch/tests/bytes-body.mjs

5.2 KB

  1. import http from 'node:http';
  2. import assert from 'node:assert/strict';
  3. import {spawn} from 'node:child_process';
  4. import {readFile,writeFile,mkdir,copyFile} from 'node:fs/promises';
  5. import path from 'node:path';
  6. import {fileURLToPath} from 'node:url';
  7. import {launchBrowser} from '../../../tests/browser/cdp.mjs';
  8. const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'../../..');
  9. const work=process.env.TMPDIR;
  10. if(!work) throw Error('set TMPDIR to scratch');
  11. const bin=process.env.HL_BIN || path.join(root,'native/zig-out/bin/hybriel');
  12. const port=Number(process.env.HL_PORT_BASE || 28510), origin=`http://127.0.0.1:${port}`;
  13. const seen=[];
  14. const server=http.createServer(async(req,res)=>{
  15. const chunks=[]; for await(const chunk of req) chunks.push(chunk);
  16. const body=Buffer.concat(chunks); seen.push(req.url);
  17. if(req.url==='/client.js'){res.setHeader('content-type','text/javascript');res.end(await readFile(path.join(root,'plugins/fetch/client.js')));return;}
  18. if(req.url==='/hl-runtime.js'){res.setHeader('content-type','text/javascript');res.end((await run([bin,'--js-runtime'])).out);return;}
  19. if(req.url==='/'){res.end('<title>Fetch Bytes body</title><p>Fetch Bytes body fixture</p>');return;}
  20. if(req.url.startsWith('/redirect/')){res.writeHead(Number(req.url.split('/')[2]),{location:'/echo'});res.end();return;}
  21. res.setHeader('content-type','application/json');res.end(JSON.stringify({method:req.method,hex:body.toString('hex'),length:req.headers['content-length'] || null}));
  22. });
  23. function run(args){return new Promise((resolve,reject)=>{const p=spawn(args[0],args.slice(1),{cwd:root,env:process.env});let out='',err='';p.stdout.on('data',s=>out+=s);p.stderr.on('data',s=>err+=s);p.on('error',reject);p.on('close',code=>resolve({code,out,err}));});}
  24. const cases=[['binary','payload','0080ff41c328'],['string',"'héllo'",'68c3a96c6c6f'],['empty Bytes','toBytes(0)',''],['empty String',"''",''],['null','null',''],['307','payload','0080ff41c328',307],['308','payload','0080ff41c328',308],['303','payload','',303]];
  25. const fixture=path.join(work,'bytes-body.hl');
  26. const init=`import { fetch } from 'hl:fetch'\npayload = toBytes(6)\npayload[0] = 0\npayload[1] = 128\npayload[2] = 255\npayload[3] = 65\npayload[4] = 195\npayload[5] = 40\n`;
  27. let browser;
  28. try{
  29. await new Promise((r,j)=>{server.once('error',j);server.listen(port,'127.0.0.1',r)});
  30. await writeFile(fixture,init+cases.map(([name,body,hex,code])=>`console.log(fetch('${origin}${code?'/redirect/'+code:'/echo'}', {method = 'POST' body = ${body}}).text())`).join('\n'));
  31. const jsdir=path.join(work,'bytes-js');await mkdir(path.join(jsdir,'hl-modules'),{recursive:true});
  32. await writeFile(path.join(jsdir,'package.json'),'{"type":"module"}');
  33. await copyFile(path.join(root,'plugins/fetch/server.js'),path.join(jsdir,'hl-modules/fetch.js'));
  34. assert.equal((await run([bin,'--native',fixture,'-o',path.join(work,'bytes-native')])).code,0);
  35. const emitted=await run([bin,'--js','--entry',fixture,'-o',path.join(jsdir,'probe.mjs')]);assert.equal(emitted.code,0,emitted.err);
  36. await writeFile(path.join(jsdir,'hl-runtime.js'),(await run([bin,'--js-runtime'])).out);
  37. for(const [target,args] of [['run',[bin,fixture]],['native',[path.join(work,'bytes-native')]],['JS',['node',path.join(jsdir,'probe.mjs')]]]){
  38. const r=await run(args);assert.equal(r.code,0,r.err);const answers=r.out.trim().split('\n').map(JSON.parse);assert.equal(answers.length,cases.length);
  39. for(const [i,answer] of answers.entries()){const [name,,hex,code]=cases[i];assert.equal(answer.hex,hex,`${target} ${name}`);assert.equal(answer.method,code===303?'GET':'POST');assert.equal(answer.length,code===303 || (name==='null' && target!=='JS') ? null : String(hex.length/2));}
  40. console.log(`PASS ${target}: ${cases.length} exact body scenarios`);
  41. }
  42. for(const bad of ['42','true','{}']){await writeFile(fixture,`import { fetch } from 'hl:fetch'\nfetch('${origin}/echo', {method = 'POST' body = ${bad}})\n`);const before=seen.length;const r=await run([bin,fixture]);assert.notEqual(r.code,0);assert.match(r.err,/body must be a String or Bytes/);assert.equal(seen.length,before);}
  43. for(const file of ['server.js','client.js']){const mod=await import('../'+file);for(const body of [42,true,{},[]]){await assert.rejects(mod.fetch(origin+'/echo',{method:'POST',body}),/body must be a String or Bytes/);}}
  44. console.log('PASS invalid bodies: 3 native + 8 JS rejections');
  45. browser=await launchBrowser();const page=await browser.newPage();await page.goto(origin);
  46. const answers=await page.evaluate(`(async()=>{const {fetch}=await import('/client.js');const {hlMakeBytes}=await import('/hl-runtime.js');const out=[];for(const wrapped of [false,true]) for(const route of ['/echo','/redirect/307','/redirect/308','/redirect/303']){const r=await fetch(route,{method:'POST',body:wrapped?hlMakeBytes(new Uint8Array([0,128,255,65,195,40])):new Uint8Array([0,128,255,65,195,40])});out.push(r.json());}return out;})()`);
  47. for(const [i,a] of answers.entries()){assert.equal(a.hex,i%4===3?'':'0080ff41c328');assert.equal(a.length,i%4===3?null:'6');assert.equal(a.method,i%4===3?'GET':'POST');}
  48. console.log('PASS browser: 8 exact Bytes body scenarios');
  49. }finally{
  50. if(browser?.owned)await browser.close();else if(browser){for(const page of browser.pages)await page.close();browser.conn.close();}
  51. server.closeAllConnections();await new Promise(r=>server.listening?server.close(r):r());
  52. }

Branches

Latest commits

  • b37cc1da|||PR Vendor Hybriel day59Atlas
  • 05665102Merge branch 'vendor-day56' into mainAtlas
  • 687296fb|||PR Vendor Hybriel day56Atlas
  • a6166d4eMerge branch 'hl-standard-3' into mainAtlas
  • 6d0eb3b0|||PR Calendar day add buttons only when signed in (Kunibert minor)Atlas
  • add4b499|||PR Make calendar day actions keyboard accessibleAtlas
  • 79e49b4dMerge branch 'hl-standard' into mainAtlas
  • e9fec7bb|||PR HL-STANDARD clean-up: typed parameters, plain locals, by-reference cacheAtlas
  • eff166a4Re-vendor complete Hybriel plugins and runtime to day44mre
  • cef81394Wait for the DST fixture page instead of an unused socketmre
  • 42434f47Check lazy-session cookies after login with a stored anonymous fixturemre
  • 728e84e3Re-vendor complete Hybriel day41 plugin directoriesmre
  • cbd7ff43Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 59d1da0acalendar: plain declarations instead of needless let in 9 files (calendar#17): 49 declarations that are never reassigned; reassigned ones and the timer closure's p stay letmre
  • f5415cf9calendar: the Style blocks in the language's nested form (calendar#16): rules named after the View's own elements with their children nested, pseudo-classes and @media nested, merged duplicate select rule; same CSS, shots identical in Chrome at 4 sizesmre
  • edb7c947calendar: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 56+4 failing → 60 passedmre
  • bf8cc0fcStyles move into their components: the shell's styles into main.hl, the events and the editors into the month view's Style block; styles.hl keeps only the theme inherit, the accent and the message line; .gitignore ignores a bin symlink too; tests/styles-shots.mjs for the before/after pixel comparemre
  • 5d8579d7README: login.js stays by caramboleyo's explicit rulemre
  • cc759328Merge sb3-standby: calendar deploys through its standbymre
  • f2fcdc6dcalendar: a standby container (port 45015, profile standby, reminder clock off) and a /health page; deploy hands over through standby-swap.sh without a gapmre