gitoria
All repositories: gitoria
5.2 KB
import http from 'node:http';import assert from 'node:assert/strict';import {spawn} from 'node:child_process';import {readFile,writeFile,mkdir,copyFile} from 'node:fs/promises';import path from 'node:path';import {fileURLToPath} from 'node:url';import {launchBrowser} from '../../../tests/browser/cdp.mjs';const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'../../..');const work=process.env.TMPDIR;if(!work) throw Error('set TMPDIR to scratch');const bin=process.env.HL_BIN || path.join(root,'native/zig-out/bin/hybriel');const port=Number(process.env.HL_PORT_BASE || 28510), origin=`http://127.0.0.1:${port}`;const seen=[];const server=http.createServer(async(req,res)=>{const chunks=[]; for await(const chunk of req) chunks.push(chunk);const body=Buffer.concat(chunks); seen.push(req.url);if(req.url==='/client.js'){res.setHeader('content-type','text/javascript');res.end(await readFile(path.join(root,'plugins/fetch/client.js')));return;}if(req.url==='/hl-runtime.js'){res.setHeader('content-type','text/javascript');res.end((await run([bin,'--js-runtime'])).out);return;}if(req.url==='/'){res.end('<title>Fetch Bytes body</title><p>Fetch Bytes body fixture</p>');return;}if(req.url.startsWith('/redirect/')){res.writeHead(Number(req.url.split('/')[2]),{location:'/echo'});res.end();return;}res.setHeader('content-type','application/json');res.end(JSON.stringify({method:req.method,hex:body.toString('hex'),length:req.headers['content-length'] || null}));});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}));});}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]];const fixture=path.join(work,'bytes-body.hl');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`;let browser;try{await new Promise((r,j)=>{server.once('error',j);server.listen(port,'127.0.0.1',r)});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'));const jsdir=path.join(work,'bytes-js');await mkdir(path.join(jsdir,'hl-modules'),{recursive:true});await writeFile(path.join(jsdir,'package.json'),'{"type":"module"}');await copyFile(path.join(root,'plugins/fetch/server.js'),path.join(jsdir,'hl-modules/fetch.js'));assert.equal((await run([bin,'--native',fixture,'-o',path.join(work,'bytes-native')])).code,0);const emitted=await run([bin,'--js','--entry',fixture,'-o',path.join(jsdir,'probe.mjs')]);assert.equal(emitted.code,0,emitted.err);await writeFile(path.join(jsdir,'hl-runtime.js'),(await run([bin,'--js-runtime'])).out);for(const [target,args] of [['run',[bin,fixture]],['native',[path.join(work,'bytes-native')]],['JS',['node',path.join(jsdir,'probe.mjs')]]]){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);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));}console.log(`PASS ${target}: ${cases.length} exact body scenarios`);}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);}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/);}}console.log('PASS invalid bodies: 3 native + 8 JS rejections');browser=await launchBrowser();const page=await browser.newPage();await page.goto(origin);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;})()`);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');}console.log('PASS browser: 8 exact Bytes body scenarios');}finally{if(browser?.owned)await browser.close();else if(browser){for(const page of browser.pages)await page.close();browser.conn.close();}server.closeAllConnections();await new Promise(r=>server.listening?server.close(r):r());}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- 014d2b7fOne immutable readers snapshot per move for the left-organization event (#53)mre
- 1859d506Decide who hears a repo left its organization by its readers before the move (#53)mre
- 499f25f2Review fixes for the Settings organization move (#53)mre
- ed3b922b|||PR move a repository into an organization in Settings (#53)mre
- 74ec2af8Merge branch 'w54-repo-rename-pr' into mainGitoria
- 9f7ce8d8Preserve rename compatibility and live page titlesmre
- c3d95b6e|||PR separate names and addresses, rename repositories in Settings (#53)mre
- ce08920dMerge branch 'w52c-header-phone' into mainGitoria
- 0bab9e46|||PR header: ident status wraps on phones instead of clippingmre
- e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
- c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
- 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria
- 80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymre
- 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
- 155861f6Merge branch 'w52-person-approval' into mainGitoria
- b5435de8|||PR person approvals: second Kunibert round (#52)mre
- 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre
- 2a89d8d0|||PR person approvals: Approve button for people; merge needs the required checks or a listed person's approval (#52)mre
- 83f0c953Report person approval verificationmre
- 3ae94b86Record browser failures and verify owned tab cleanupmre