HS: fetch URL parser fix + IO mock responses

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) <noreply@anthropic.com>
This commit is contained in:
2026-04-17 14:02:32 +00:00
parent f276c4a56a
commit b9c9216409
2 changed files with 23 additions and 2 deletions

View File

@@ -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

View File

@@ -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: '<div>yay</div>' },
'/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)');