// Tests the running local frontend proxy and real read-only HF library. // Creates and removes only its own temporary listener account. Never enables uploads. const fs=require('node:fs'),path=require('node:path'),crypto=require('node:crypto'); const {DatabaseSync}=require('node:sqlite'); require('dotenv').config({quiet:true}); const origin='http://127.0.0.1:3000',base=origin+'/api/v1'; (async()=>{ const username='qa_'+crypto.randomBytes(6).toString('hex'),password=crypto.randomBytes(24).toString('base64url'),email=username+'@example.test';let id; const call=async(p,options={})=>{const r=await fetch(base+p,{...options,signal:AbortSignal.timeout(60000)});const b=await r.json();if(!r.ok)throw Error(p+': '+r.status+' '+JSON.stringify(b));return b;}; const post=body=>({method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)}); try{ if((await fetch(base+'/tracks')).status!==401)throw Error('Anonymous library access'); if((await fetch(base+'/library/activity')).status!==401)throw Error('Anonymous backup activity access'); const signup=await call('/auth/signup',post({email,password,username,display_name:'Temporary verification'}));id=signup.user.id; const wrong=await fetch(base+'/auth/login',post({email,password:'incorrect'}));if(wrong.status!==401)throw Error('Incorrect password accepted'); const login=await call('/auth/login',post({email,password})),headers={Authorization:'Bearer '+login.access_token}; if((await call('/auth/me',{headers})).id!==id)throw Error('Session profile mismatch'); console.log('Login, wrong-password error, authenticated session and anonymous guard: PASS'); const sync=await call('/library/sync',{method:'POST',headers});const tracks=await call('/tracks',{headers}); const activity=await call('/library/activity',{headers}); if(activity.some(t=>t.asset||t.copies||t.wrappedKey))throw Error('Storage secrets leaked in activity'); if(activity.some(t=>!t.backup_status||!t.metadata_status))throw Error('Incomplete song activity'); if(activity.some(t=>!tracks.some(song=>song.id===t.id)))throw Error('Private activity leaked'); const song=tracks.find(t=>t.title==='Say Something'&&t.source==='huggingface');if(!song)throw Error('Dataset song missing'); const detail=await call('/tracks/'+song.id,{headers});if(!detail.has_artwork||detail.album_title!=='Is There Anybody Out There?')throw Error('Real metadata extraction failed: '+detail.metadata_error); const art=await fetch(base+'/tracks/'+song.id+'/artwork',{headers}),cover=Buffer.from(await art.arrayBuffer());if(art.status!==200||cover.subarray(0,2).toString('hex')!=='ffd8')throw Error('Artwork failed'); const {signedUrl}=await call('/tracks/'+song.id+'/stream',{headers}); for(const [from,to] of [[0,4095],[1048576,1049599]]){const response=await fetch(origin+signedUrl,{headers:{Range:`bytes=${from}-${to}`},signal:AbortSignal.timeout(60000)}),data=Buffer.from(await response.arrayBuffer());if(response.status!==206||data.length!==to-from+1||(from===0&&data.subarray(0,3).toString()!=='ID3'))throw Error('Remote playback or seeking failed');} await call('/auth/logout',{method:'POST',headers});if((await fetch(origin+signedUrl)).status!==401)throw Error('Playback ticket was not revoked'); const report={checked_at:new Date().toISOString(),anonymous_guard:true,login:true,wrong_password_error:true,dataset_tracks:sync.count,backup_activity:true,visible_local_tracks:activity.length,title:detail.title,artist:detail.artist_name,album:detail.album_title,artwork_bytes:cover.length,embedded_lyrics:detail.lyrics.length,remote_playback:true,range_seeking:true,logout_revocation:true}; fs.mkdirSync('../verification',{recursive:true});fs.writeFileSync('../verification/live-check.json',JSON.stringify(report,null,2));console.log(JSON.stringify(report,null,2)); }finally{ if(id){const db=new DatabaseSync(path.join(process.env.DATA_DIR||'./data','stillwave.sqlite'));try{const row=db.prepare("SELECT data FROM documents WHERE collection='users' AND id=?").get(id);if(row&&JSON.parse(row.data).username===username){db.prepare('DELETE FROM documents WHERE owner=?').run(id);db.prepare("DELETE FROM documents WHERE collection='users' AND id=?").run(id);}}finally{db.close();}console.log('Temporary verification account removed');} } })().catch(e=>{console.error(e.message);process.exitCode=1;});