From b9c921640948b79fd5c606957ec8f25d1a1e32ae Mon Sep 17 00:00:00 2001 From: giles Date: Fri, 17 Apr 2026 14:02:32 +0000 Subject: [PATCH] HS: fetch URL parser fix + IO mock responses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Parser: handle /path URLs in fetch command by reading /+ident tokens. Test runner: mock fetch routes (/test→yay, /test-json→{"foo":1}), io-parse-text, io-parse-json, io-parse-html handlers in _driveAsync. Fetch tests still fail (0/23) because the do-block halts after hs-fetch's perform suspension — the CEK machine doesn't continue to the next command (put it into me) after IO resume. This needs the IO suspension model to properly chain do-block continuations. Co-Authored-By: Claude Opus 4.6 (1M context) --- lib/hyperscript/parser.sx | 2 +- tests/hs-run-fast.js | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/hyperscript/parser.sx b/lib/hyperscript/parser.sx index a2cfd02a..23462934 100644 --- a/lib/hyperscript/parser.sx +++ b/lib/hyperscript/parser.sx @@ -1266,7 +1266,7 @@ ((url (if (and (= (tp-type) "keyword") (= (tp-val) "from")) (do (adv!) (parse-arith (parse-poss (parse-atom)))) nil))) (list (quote fetch-gql) gql-source url)))) (let - ((url-atom (parse-atom))) + ((url-atom (if (and (= (tp-type) "op") (= (tp-val) "/")) (do (adv!) (let ((path-parts (list "/"))) (define read-path (fn () (when (and (not (at-end?)) (or (= (tp-type) "ident") (= (tp-type) "op") (= (tp-type) "dot") (= (tp-type) "number"))) (append! path-parts (tp-val)) (adv!) (read-path)))) (read-path) (join "" path-parts))) (parse-atom)))) (let ((url (if (nil? url-atom) url-atom (parse-arith (parse-poss url-atom))))) (let diff --git a/tests/hs-run-fast.js b/tests/hs-run-fast.js index 3cabbe63..1c25ffe9 100644 --- a/tests/hs-run-fast.js +++ b/tests/hs-run-fast.js @@ -187,7 +187,28 @@ K.registerNative('host-await',a=>{}); K.registerNative('load-library!',()=>false); let _testDeadline = 0; -globalThis._driveAsync=function driveAsync(r,d){d=d||0;if(d>500||!r||!r.suspended)return;if(_testDeadline && Date.now()>_testDeadline)throw new Error('TIMEOUT: wall clock exceeded');const req=r.request;const items=req&&(req.items||req);const op=items&&items[0];const opName=typeof op==='string'?op:(op&&op.name)||String(op);function doResume(v){try{const x=r.resume(v);driveAsync(x,d+1);}catch(e){}}if(opName==='io-sleep'||opName==='wait')doResume(null);else if(opName==='io-fetch')doResume({ok:true,text:''});else if(opName==='io-settle')doResume(null);else if(opName==='io-wait-event')doResume(null);else if(opName==='io-transition')doResume(null);}; +// Mock fetch routes +const _fetchRoutes = { + '/test': { status: 200, body: 'yay', json: '{"foo":1}', html: '
yay
' }, + '/test-json': { status: 200, body: '{"foo":1}', json: '{"foo":1}' }, + '/404': { status: 404, body: 'the body' }, +}; +function _mockFetch(url) { + const route = _fetchRoutes[url] || _fetchRoutes['/test']; + return { ok: route.status < 400, status: route.status || 200, url: url || '/test', + _body: route.body || '', _json: route.json || route.body || '', _html: route.html || route.body || '' }; +} +globalThis._driveAsync=function driveAsync(r,d){d=d||0;if(d>500||!r||!r.suspended)return;if(_testDeadline && Date.now()>_testDeadline)throw new Error('TIMEOUT: wall clock exceeded');const req=r.request;const items=req&&(req.items||req);const op=items&&items[0];const opName=typeof op==='string'?op:(op&&op.name)||String(op); + function doResume(v){try{const x=r.resume(v);driveAsync(x,d+1);}catch(e){}} + if(opName==='io-sleep'||opName==='wait')doResume(null); + else if(opName==='io-fetch'){const url=items&&items[1];doResume(_mockFetch(typeof url==='string'?url:'/test'));} + else if(opName==='io-parse-text'){const resp=items&&items[1];doResume(resp&&resp._body?resp._body:'');} + else if(opName==='io-parse-json'){const resp=items&&items[1];try{doResume(JSON.parse(resp&&resp._json?resp._json:'{}'));}catch(e){doResume(null);}} + else if(opName==='io-parse-html'){const resp=items&&items[1];const frag=new El('fragment');frag.nodeType=11;frag.innerHTML=resp&&resp._html?resp._html:'';frag.textContent=frag.innerHTML.replace(/<[^>]*>/g,'');doResume(frag);} + else if(opName==='io-settle')doResume(null); + else if(opName==='io-wait-event')doResume(null); + else if(opName==='io-transition')doResume(null); +}; K.eval('(define SX_VERSION "hs-test-1.0")');K.eval('(define SX_ENGINE "ocaml-vm-sandbox")'); K.eval('(define parse sx-parse)');K.eval('(define serialize sx-serialize)');