import {test} from 'node:test'; import assert from 'node:assert/strict'; import {spawn} from 'node:child_process'; import net from 'node:net'; import fs from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import {defaults} from './config.mjs'; import {T,packet,Parser} from './protocol.mjs'; const sleep=ms=>new Promise(r=>setTimeout(r,ms)); test('admin accessible while disabled, listener toggle and persisted restart', {timeout:30000},async()=>{ const dir=await fs.mkdtemp(path.join(os.tmpdir(),'mars-lifecycle-'));let child; const socket=path.join(dir,'admin.sock'); const request=req=>new Promise((resolve,reject)=>{let b='';const s=net.createConnection(socket,()=>s.write(JSON.stringify(req)+'\n'));s.setTimeout(10000,()=>s.destroy(Error('timeout')));s.on('data',x=>b+=x);s.on('error',reject);s.on('end',()=>{try{resolve(JSON.parse(b));}catch(e){reject(e);}});}); const start=async()=>{child=spawn(process.execPath,[new URL('./server.mjs',import.meta.url).pathname],{env:{...process.env,STATE_DIRECTORY:dir,RUNTIME_DIRECTORY:dir,MARS_MANAGED_AUDIO:'1',PULSE_SERVER:''},stdio:'ignore'});for(let i=0;i<120;i++){try{return await request({op:'get'});}catch{}if(child.exitCode!==null)throw Error('worker exited');await sleep(100);}throw Error('worker not ready');}; const stop=async()=>{if(child&&child.exitCode===null&&child.signalCode===null){const done=new Promise(r=>child.once('exit',r));child.kill('SIGTERM');await done;}child=null;await fs.rm(socket,{force:true});}; const probe=net.createServer();await new Promise(r=>probe.listen(0,'127.0.0.1',r));const port=probe.address().port;await new Promise(r=>probe.close(r)); try{ let s=await start();assert.equal(s.listen,false);assert.equal(s.audioPid,0); const enabled={...defaults,enabled:true,port};s=await request({op:'save',revision:s.revision,config:enabled});assert.equal(s.listen,true,s.error||s.listenerError);assert.equal(s.audioPid,0); const original=s.config.token;assert.match(original,/^[a-f0-9]{64}$/); assert.equal(JSON.parse(await fs.readFile(path.join(dir,'config.json'),'utf8')).token,original); assert.equal((await fs.stat(path.join(dir,'config.json'))).mode&0o777,0o600); const auth=token=>new Promise((resolve,reject)=>{const c=net.createConnection(port,'127.0.0.1',()=>c.write(packet(T.AUTH,token)));let got=false;const parser=new Parser(t=>{got=true;resolve(t===T.HELLO);c.destroy();});c.on('data',b=>parser.feed(b));c.on('error',reject);c.on('close',()=>{if(!got)resolve(false);});c.setTimeout(3000,()=>c.destroy(Error('auth timeout')));}); assert.equal(await auth(original),true); const invalid=await request({op:'save',revision:s.revision,config:{...enabled,token:'bad'}});assert.match(invalid.error,/Hex-Token/); assert.equal((await request({op:'get'})).config.token,original); const failed=await request({op:'save',revision:s.revision,config:{...enabled,audio:true}});assert.match(failed.error,/PULSE_SERVER/);s=await request({op:'get'});assert.equal(s.listen,true);assert.equal(s.config.audio,false);assert.equal(s.audioPid,0);assert.equal(s.config.token,original); await stop();s=await start();assert.equal(s.listen,true);assert.equal(s.config.enabled,true);assert.equal(s.config.token,original); s=await request({op:'save',revision:s.revision,config:{...enabled,enabled:false,audio:true}});assert.equal(s.listen,false);assert.equal(s.audioPid,0);assert.equal(s.config.audio,true);assert.equal(s.config.token,original); await stop();s=await start();assert.equal(s.listen,false);assert.equal(s.audioPid,0);assert.equal(s.config.token,original); s=await request({op:'save',revision:s.revision,config:enabled});assert.equal(s.config.token,original);assert.equal(s.listen,true); const stale=await request({op:'rotate-token',revision:s.revision-1});assert.match(stale.error,/neu laden/); s=await request({op:'rotate-token',revision:s.revision});assert.match(s.config.token,/^[a-f0-9]{64}$/);assert.notEqual(s.config.token,original);assert.equal(s.listen,true); assert.equal(await auth(original),false);assert.equal(await auth(s.config.token),true); const rotated=s.config.token;await stop();s=await start();assert.equal(s.config.token,rotated); const manual='b'.repeat(64);s=await request({op:'save',revision:s.revision,config:{...s.config,token:manual}});assert.equal(s.config.token,manual);assert.equal(await auth(rotated),false);assert.equal(await auth(manual),true); }finally{await stop();await fs.rm(dir,{recursive:true,force:true});} });