HS: cookie API (+3 tests, partial)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Three-part change: (a) tests/hs-run-filtered.js gets a per-test __hsCookieStore Map, a globalThis.cookies Proxy, and a document.cookie getter/setter that reads/writes the store. Per-test reset clears the store. (b) generate-sx-tests.py declares cookies in the test header and emits hand-rolled deftests for basic set / update / length-when-empty (the three tractable tests). (c) regenerated spec/tests/test-hyperscript-behavioral.sx via mcp_hs_test.regen. No .sx edits — `set cookies.foo to 'bar'` already compiles to (dom-set-prop cookies "foo" "bar") which routes through host-set!. Suite hs-upstream-expressions/cookies: 0/5 → 3/5. Smoke 0-195 unchanged at 170/195. Remaining `basic clear` (needs hs-method-call host-call dispatch) and `iterate` (needs hs-for-each host-array recognition) need runtime.sx edits — deferred to a future sx-tree worktree. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -327,6 +327,36 @@ const document = {
|
||||
createEvent(t){return new Ev(t);}, addEventListener(){}, removeEventListener(){},
|
||||
};
|
||||
globalThis.document=document; globalThis.window=globalThis; globalThis.HTMLElement=El; globalThis.Element=El;
|
||||
// cluster-33: cookie store + document.cookie + cookies Proxy.
|
||||
globalThis.__hsCookieStore = new Map();
|
||||
Object.defineProperty(document, 'cookie', {
|
||||
get(){ const out=[]; for(const[k,v] of globalThis.__hsCookieStore) out.push(k+'='+v); return out.join('; '); },
|
||||
set(s){
|
||||
const str=String(s||'');
|
||||
const m=str.match(/^\s*([^=]+?)\s*=\s*([^;]*)/);
|
||||
if(!m) return;
|
||||
const name=m[1].trim();
|
||||
const val=m[2];
|
||||
if(/expires=Thu,?\s*01\s*Jan\s*1970/i.test(str) || val==='') globalThis.__hsCookieStore.delete(name);
|
||||
else globalThis.__hsCookieStore.set(name, val);
|
||||
},
|
||||
configurable: true,
|
||||
});
|
||||
globalThis.cookies = new Proxy({}, {
|
||||
get(_, k){
|
||||
if(k==='length') return globalThis.__hsCookieStore.size;
|
||||
if(k==='clear') return (name)=>globalThis.__hsCookieStore.delete(String(name));
|
||||
if(typeof k==='symbol' || k==='_type' || k==='_order') return undefined;
|
||||
return globalThis.__hsCookieStore.has(k) ? globalThis.__hsCookieStore.get(k) : null;
|
||||
},
|
||||
set(_, k, v){ globalThis.__hsCookieStore.set(String(k), String(v)); return true; },
|
||||
has(_, k){ return globalThis.__hsCookieStore.has(k); },
|
||||
ownKeys(){ return Array.from(globalThis.__hsCookieStore.keys()); },
|
||||
getOwnPropertyDescriptor(_, k){
|
||||
if(globalThis.__hsCookieStore.has(k)) return {value: globalThis.__hsCookieStore.get(k), enumerable: true, configurable: true};
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
// cluster-28: test-name-keyed confirm/prompt/alert mocks. The upstream
|
||||
// ask/answer tests each expect a deterministic return value. Keyed on
|
||||
// globalThis.__currentHsTestName which the test loop sets before each test.
|
||||
@@ -540,6 +570,7 @@ for(let i=startTest;i<Math.min(endTest,testCount);i++){
|
||||
// Reset body
|
||||
_body.children=[];_body.childNodes=[];_body.innerHTML='';_body.textContent='';
|
||||
globalThis.__test_selection='';
|
||||
globalThis.__hsCookieStore.clear();
|
||||
globalThis.__currentHsTestName = name;
|
||||
|
||||
// Enable step limit for timeout protection
|
||||
|
||||
@@ -1832,6 +1832,36 @@ def generate_eval_only_test(test, idx):
|
||||
lines = []
|
||||
safe_name = sx_name(test['name'])
|
||||
|
||||
# Special case: cluster-33 cookie tests. Each test calls a sequence of
|
||||
# `_hyperscript("HS")` inside `page.evaluate(()=>{...})`. The runner backs
|
||||
# `cookies` with a Proxy over a per-test `__hsCookieStore` map (see
|
||||
# tests/hs-run-filtered.js). Tests handled: basic set, length-when-empty,
|
||||
# update. clear/iterate stay SKIP (need hs-method-call→host-call dispatch
|
||||
# and host-array iteration in hs-for-each — out of cluster-33 scope).
|
||||
if test['name'] == 'basic set cookie values work':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (assert (nil? (eval-hs "cookies.foo")))\n'
|
||||
f' (eval-hs "set cookies.foo to \'bar\'")\n'
|
||||
f' (assert= (eval-hs "cookies.foo") "bar"))'
|
||||
)
|
||||
if test['name'] == 'update cookie values work':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "set cookies.foo to \'bar\'")\n'
|
||||
f' (assert= (eval-hs "cookies.foo") "bar")\n'
|
||||
f' (eval-hs "set cookies.foo to \'doh\'")\n'
|
||||
f' (assert= (eval-hs "cookies.foo") "doh"))'
|
||||
)
|
||||
if test['name'] == 'length is 0 when no cookies are set':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (assert= (eval-hs "cookies.length") 0))'
|
||||
)
|
||||
|
||||
# Special case: logAll config test. Body sets `_hyperscript.config.logAll = true`,
|
||||
# then mutates an element's innerHTML and calls `_hyperscript.processNode`.
|
||||
# Our runtime exposes this via hs-set-log-all! + hs-log-captured; we reuse
|
||||
@@ -2612,6 +2642,7 @@ output.append(';; Bind `window` and `document` as plain SX symbols so HS code th
|
||||
output.append(';; references them (e.g. `window.tmp`) can resolve through the host.')
|
||||
output.append('(define window (host-global "window"))')
|
||||
output.append('(define document (host-global "document"))')
|
||||
output.append('(define cookies (host-global "cookies"))')
|
||||
output.append('')
|
||||
output.append('(define hs-test-el')
|
||||
output.append(' (fn (tag hs-src)')
|
||||
|
||||
Reference in New Issue
Block a user