// @requirements REQ-PRODUCT-TERMS-ADAPTERS REQ-PRODUCT-ASTRO REQ-PRODUCT-TERMS-ACCEPTANCE
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {JSDOM} from 'jsdom';
import {mountAstroTermsAcceptance} from '@notabenda/core/astro/client';
import {termsFixture} from './terms-adapter-fixture.mjs';
const markup=(offerId,key='operation-one',id='signup')=>`
Read the terms or contact support.
`;
const page=f=>{const dom=new JSDOM(markup(f.offer.offerId),{url:'https://example.test/'});return {dom,document:dom.window.document,event:name=>dom.window.document.dispatchEvent(new dom.window.Event(name))};};
const mount=(f,p,extra={})=>mountAstroTermsAcceptance({acceptance:f.controller,id:'signup',contextKey:'operation-one',document:p.document,...extra});
test('Astro terms mounts one exact named control without submitting and leaves the host controller alive',async()=>{
const f=await termsFixture(),p=page(f);assert.throws(()=>mountAstroTermsAcceptance({acceptance:f.controller,id:'signup',contextKey:'operation-one'}),/browser document/);assert.throws(()=>mount(f,p,{contextKey:'not a key'}),/non-secret/);const bridge=mount(f,p);
for(let i=0;i<4;i++){bridge.refresh();p.event('astro:page-load');}assert.equal(p.document.querySelectorAll('.nb-terms-acceptance').length,1);assert.equal(p.document.querySelector('input').checked,false);assert.equal(f.commands.length,0);assert.equal(p.document.querySelector('[data-notabenda-terms-fallback]').hidden,true);bridge.destroy();assert.equal(p.document.querySelector('[data-notabenda-terms-fallback]').hidden,false);assert.equal(f.destroyed(),0);assert.equal(f.controller.getSnapshot().status,'idle');p.event('astro:page-load');assert.equal(p.document.querySelectorAll('.nb-terms-acceptance').length,0);f.controller.destroy();p.dom.window.close();
});
test('Astro swaps preserve an unchecked-or-checked draft; detached controls cannot edit or submit',async()=>{
const f=await termsFixture(),p=page(f),bridge=mount(f,p),checkbox=p.document.querySelector('input'),button=p.document.querySelector('button');checkbox.click();assert.equal(f.controller.getSnapshot().checked,true);p.event('astro:before-swap');checkbox.checked=false;checkbox.dispatchEvent(new p.dom.window.Event('change'));button.click();assert.equal(f.controller.getSnapshot().checked,true);assert.equal(f.commands.length,0);
p.document.body.innerHTML=markup(f.offer.offerId);p.event('astro:page-load');assert.equal(p.document.querySelector('input').checked,true);assert.equal(f.commands.length,0);p.event('astro:before-swap');p.document.body.innerHTML='Another page';p.event('astro:page-load');assert.equal(p.document.querySelectorAll('.nb-terms-acceptance').length,0);bridge.destroy();f.controller.destroy();p.dom.window.close();
});
test('same offer under another operation fails closed and a destroyed bridge cannot rename its controller context',async()=>{
const f=await termsFixture(),p=page(f),bridge=mount(f,p);f.controller.setChecked(true);p.document.body.innerHTML=markup(f.offer.offerId,'operation-two');assert.throws(()=>bridge.refresh(),/offer or operation changed/);assert.equal(p.document.querySelectorAll('.nb-terms-acceptance').length,0);assert.equal(p.document.querySelector('[data-notabenda-terms-fallback]').hidden,false);bridge.destroy();assert.throws(()=>mount(f,p,{contextKey:'operation-two'}),/fresh host controller/);assert.equal(f.commands.length,0);
const fresh=await termsFixture({operationId:'operation-two'}),replacement=mount(fresh,p,{contextKey:'operation-two'});assert.equal(p.document.querySelector('input').checked,false);replacement.destroy();f.controller.destroy();fresh.controller.destroy();p.dom.window.close();
});
test('changed offers, duplicate shells and malformed mounts reject without leaking ownership',async()=>{
const f=await termsFixture(),p=page(f),bridge=mount(f,p);p.document.body.innerHTML=markup('a'.repeat(64));assert.throws(()=>bridge.refresh(),/offer or operation changed/);const html=markup(f.offer.offerId);p.document.body.innerHTML=html+html;assert.throws(()=>bridge.refresh(),/one Astro TermsAcceptance/);assert.equal(p.document.querySelectorAll('.nb-terms-acceptance').length,0);bridge.destroy();assert.throws(()=>mount(f,p),/one Astro TermsAcceptance/);p.document.body.innerHTML=html.replace('data-notabenda-terms-mount','wrong-mount');assert.throws(()=>mount(f,p),/Use the Astro TermsAcceptance/);p.document.body.innerHTML=html;const recovered=mount(f,p);assert.equal(p.document.querySelectorAll('.nb-terms-acceptance').length,1);const other=await termsFixture();assert.throws(()=>mount(other,p),/per document field ID/);recovered.destroy();const next=mount(other,p);next.destroy();assert.equal(f.commands.length,0);f.controller.destroy();other.controller.destroy();p.dom.window.close();
});
test('pending and uncertain requests survive Astro remount without automatic retry or controller disposal',async()=>{
let release,lose=true;const pending=new Promise(r=>release=r),f=await termsFixture({afterRecord:async()=>{await pending;if(lose){lose=false;throw Error('Lost acknowledgment');}}}),p=page(f),bridge=mount(f,p);f.controller.setChecked(true);const submitted=f.controller.submit();await Promise.resolve();p.event('astro:before-swap');p.document.body.innerHTML=markup(f.offer.offerId);p.event('astro:page-load');assert.equal(f.controller.getSnapshot().status,'submitting');assert.equal(p.document.querySelector('input').disabled,true);release();await assert.rejects(submitted);assert.equal(f.controller.getSnapshot().status,'error');const command=f.commands[0];bridge.destroy();const next=mount(f,p);assert.equal(f.commands.length,1);assert.equal(p.document.querySelector('input').checked,true);await f.controller.submit();assert.deepEqual(f.commands,[command,command]);assert.match(p.document.body.textContent,/Agreement recorded/);assert.equal(f.destroyed(),0);next.destroy();f.controller.destroy();p.dom.window.close();
});