import test from 'node:test'; import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import vm from 'node:vm'; function node(tag='div',text=''){ return {tag,textContent:text||'',children:[],events:{},value:'',hidden:false,disabled:false, append(...children){this.children.push(...children);},prepend(...children){this.children.unshift(...children);}, replaceChildren(...children){this.children=children;},insertBefore(child){this.children.push(child);}, setAttribute(name,value){this[name]=value;},addEventListener(name,fn){this.events[name]=fn;}, setCustomValidity(){},focus(){}}; } test('Citizen Passport has no password length constraints in either mode and submits an existing nine-character password',async()=>{ const nodes=[],surfaces=new Map(),writes=[];let reloads=0; const el=(tag,text)=>{const n=node(tag,text);nodes.push(n);return n;}; const document={body:node(),querySelector(selector){if(selector==='.command-banner')return null;if(!surfaces.has(selector))surfaces.set(selector,node());return surfaces.get(selector);}}; const source=readFileSync(new URL('../control-panel/public/cp-v2/cp-v2-auth.mjs',import.meta.url),'utf8').replace(/^import .*;\r?\n/gm,'').replaceAll('export ',''); const context=vm.createContext({el,document,URL,AbortSignal,mountSecurity(){},location:{href:'https://cp.dunerealms.com/'}}); vm.runInContext(source,context); await context.startCPAuthentication(()=>assert.fail('No authenticated session yet'),{locationTarget:{reload(){reloads++;}},fetcher:async(path,init)=>{ if(path==='/api/auth/session')return Response.json({authenticated:false}); writes.push({path,data:JSON.parse(init.body)});return Response.json({ok:true}); }}); const password=nodes.find(n=>n.name==='password'),name=nodes.find(n=>n.name==='username'),form=nodes.find(n=>n.tag==='form'); const login=nodes.find(n=>n.tag==='button'&&n.textContent==='Sign in'),register=nodes.find(n=>n.tag==='button'&&n.textContent==='Create account'); assert.equal(password.minLength,undefined);assert.equal(password.maxLength,undefined);assert.equal(password.autocomplete,'current-password'); register.onclick();assert.equal(password.minLength,undefined);assert.equal(password.autocomplete,'new-password'); login.onclick();assert.equal(password.minLength,undefined);assert.equal(password.autocomplete,'current-password'); name.value='legacy_test';password.value='oldpass12';await form.onsubmit({preventDefault(){}}); assert.deepEqual(writes,[{path:'/api/auth/login',data:{username:'legacy_test',password:'oldpass12'}}]);assert.equal(reloads,1); }); test('public sign-in starts with an existing-password constraint and has no registration length constraint',async()=>{ const html=readFileSync(new URL('../node/public/login.html',import.meta.url),'utf8'); assert.match(html,/autocomplete="current-password" required/); const form=node('form');form.elements={password:{}}; const nodes=new Map([['#account-form',form]]); const document={createElement:tag=>node(tag),querySelector(selector){if(!nodes.has(selector))nodes.set(selector,node());return nodes.get(selector);}}; const source=readFileSync(new URL('../node/public/login.js',import.meta.url),'utf8'); vm.runInNewContext(source,{document,URLSearchParams,location:{search:''},fetch:async()=>({json:async()=>({authenticated:false})})}); nodes.get('#register-tab').events.click();assert.equal(form.elements.password.minLength,undefined);assert.equal(form.elements.password.autocomplete,'new-password'); nodes.get('#login-tab').events.click();assert.equal(form.elements.password.minLength,undefined);assert.equal(form.elements.password.autocomplete,'current-password'); });