// @requirements REQ-PRODUCT-SERVICE-LOCALIZATION REQ-PRODUCT-SERVICE-CATALOG REQ-PRODUCT-A11Y import {test} from 'node:test'; import assert from 'node:assert/strict'; import {readFile} from 'node:fs/promises'; import {JSDOM} from 'jsdom'; import {serveHost} from '../host.mjs'; import {siteSections} from '../navigation.mjs'; const publicRoot=new URL('../dist/public/',import.meta.url),read=path=>readFile(new URL(path,publicRoot),'utf8'); test('Portuguese services are readable without script and linked with their exact policies, example and maintained messages',async()=>{ const html=await read('localization/services/index.html'),dom=new JSDOM(html),d=dom.window.document,config=JSON.parse(await read('localization/services/site.json'));assert.equal(d.documentElement.lang,'pt-BR');assert.equal(d.querySelector('[data-service-directory]').dir,'ltr');assert.equal(d.querySelectorAll('.nb-service-card').length,4);assert.ok([...d.querySelectorAll('details')].every(n=>!n.open));assert.equal(d.querySelectorAll('input,select,button,script:not([src])').length,0);assert.ok(d.querySelector('.demo-corner'));assert.ok(d.querySelector('link[href="/assets/example-theme.css"]'));assert.ok(d.querySelector('script[src="/assets/localized-services.mjs"]'));assert.equal(d.querySelector('h1').textContent,config.localization.serviceDirectory.title);for(const kind of ['privacy','cookies'])assert.ok(d.querySelector(`a[href="/localization/services/${kind}"]`));assert.ok(d.querySelector('a[href="/guide/services"]'));assert.ok(d.querySelector('a[href="/examples/privacy-information/"]'));assert.doesNotMatch(d.querySelector('[data-service-directory]').textContent,/Processing location|Purpose retention|Browser storage|declared service/);dom.window.close(); const topic=siteSections.flatMap(s=>s.topics).find(t=>t.id==='services');assert.ok(JSON.stringify(topic).includes('/localization/services/'));const guide=await read('guide/localization/index.html');assert.ok(guide.includes('/source/examples/localization/service-messages.json.txt'));assert.deepEqual(JSON.parse(await read('source/examples/localization/service-messages.json.txt')),config.localization.serviceDirectory); const env={ASSETS:{fetch:async()=>new Response(html,{headers:{'Content-Type':'text/html'}})},DEMO_WORKSPACES:{get(){throw Error('Directory must not allocate a demo workspace');}},WORKSPACE:{get(){throw Error('Directory must not read operational records');}}};const response=await serveHost(new Request('https://example.test/localization/services/'),env);assert.equal(response.status,200);assert.equal(response.headers.get('Set-Cookie'),null);assert.match(response.headers.get('Content-Security-Policy'),/script-src 'self'; connect-src 'self'/u);assert.doesNotMatch(response.headers.get('Content-Security-Policy'),/unsafe-inline|unsafe-eval/); }); test('actual bundled directory search filters locally, announces selected messages and never writes storage or contacts providers',async()=>{ const html=await read('localization/services/index.html'),script=await read('assets/localized-services.mjs'),config=JSON.parse(await read('localization/services/site.json')),dom=new JSDOM(html,{url:'https://example.test/localization/services/'}),original=Object.fromEntries(['window','document','fetch'].map(k=>[k,Object.getOwnPropertyDescriptor(globalThis,k)])),requests=[];Object.defineProperty(dom.window,'localStorage',{get(){throw Error('Directory must not read localStorage');}});Object.defineProperty(dom.window,'sessionStorage',{get(){throw Error('Directory must not read sessionStorage');}}); try{globalThis.window=dom.window;globalThis.document=dom.window.document;globalThis.fetch=async url=>{requests.push(url);assert.equal(url,'/localization/services/site.json');return {ok:true,json:async()=>structuredClone(config)};};const details=dom.window.document.querySelector('details');details.open=true;details.querySelector('summary').focus();const focused=dom.window.document.activeElement;await import('data:text/javascript;base64,'+Buffer.from(script).toString('base64'));await new Promise(r=>setImmediate(r));const d=dom.window.document,search=d.querySelector('input[type=search]'),purpose=d.querySelector('select'),count=d.querySelector('[role=status]');assert.ok(search);assert.equal(d.activeElement,focused);assert.equal(details.open,true);assert.equal(search.labels[0].textContent,'Buscar um serviço');search.value='Brookpost';search.dispatchEvent(new dom.window.Event('input'));assert.equal(count.textContent,'Exibindo 1 de 4 serviços declarados');assert.equal(d.querySelector('.nb-service-list > li:not([hidden])').dataset.serviceId,'mail');purpose.value='measurement';purpose.dispatchEvent(new dom.window.Event('change'));assert.equal(count.textContent,'Exibindo 0 de 4 serviços declarados');assert.equal(d.querySelector('[data-service-empty]').hidden,false);search.value='';search.dispatchEvent(new dom.window.Event('input'));assert.equal(d.querySelector('.nb-service-list > li:not([hidden])').dataset.serviceId,'measurement');assert.deepEqual(requests,['/localization/services/site.json']);assert.equal(d.cookie,'');} finally{dom.window.close();for(const [key,descriptor] of Object.entries(original)){if(descriptor)Object.defineProperty(globalThis,key,descriptor);else delete globalThis[key];}} });