// 부모 프로세스가 앱 종료 영수증과 소유 포트/PID 소멸을 별도로 확인한다. // 설명 문서: docs/specs/app-lifecycle.md v1.0.0 const fs=require('node:fs'),path=require('node:path'),cp=require('node:child_process'),assert=require('node:assert/strict'),net=require('node:net'); const root=path.resolve(__dirname,'..'),home=fs.mkdtempSync(path.join(root,'scratch/app-lifecycle-')),asar=process.argv.includes('--asar'); const prefix=path.join(root,'scratch/app-lifecycle-r41-'+(asar?'asar':'source')); async function available(port){const server=net.createServer();await new Promise((resolve,reject)=>{server.once('error',reject);server.listen(port,'127.0.0.1',resolve);});await new Promise(resolve=>server.close(resolve));} (async()=>{ fs.writeFileSync(prefix+'.json',JSON.stringify({complete:false,home,asar}));const fd=fs.openSync(prefix+'.log','w'); try{cp.execFileSync(require('electron'),[path.join(root,'test/app-lifecycle.electron.cjs')],{cwd:root,env:{...process.env,BADA_DB_BACKEND:'sqlite',BADA_APP_FIXTURE:home,BADA_APP_ASAR:asar?'1':'0'},stdio:['ignore',fd,fd],timeout:55000});}finally{fs.closeSync(fd);} const started=JSON.parse(fs.readFileSync(path.join(home,'started.json'))),ended=JSON.parse(fs.readFileSync(path.join(home,'exit.json'))); assert.equal(ended.kind,'quit','normal Electron quit events must run');assert.deepEqual(ended.events,['will-quit','quit']);assert.equal(ended.code,0);assert.equal(ended.state.natsStopped,true,'await async NATS cleanup');assert.equal(ended.state.shutdownCalls,1,'single cleanup'); assert.equal(ended.state.cwdListenersAfter,0);assert.equal(ended.state.hudListenersAfter,0);assert.equal(ended.state.scannerRunning,false); assert.equal(ended.state.bongStopped,true,'await actual Bong cleanup through Gateway');assert.equal(started.state.healthRead,true); for(const port of new Set([...Object.values(started.state.ports),started.state.openworkPort,started.state.natsPort]))await available(port); for(const pid of started.state.children){let alive=false;try{process.kill(pid,0);alive=true;}catch{/* 소유 자식이 이미 종료됨 */}assert.equal(alive,false,'owned child alive: '+pid);} fs.writeFileSync(prefix+'.json',JSON.stringify({complete:true,home,asar,started,ended},null,2));console.log('APP_LIFECYCLE_VERIFIED '+JSON.stringify({asar,ownedPortsClosed:true,childrenExited:true,asyncCleanupAwaited:true,quitEvents:true})); })().catch(error=>{ // 실패 시에도 방금 이 fixture가 만든 자식만 회수한다. 공유 서비스/PID 기록은 읽지 않는다. const owned=new Set(); try{for(const pid of JSON.parse(fs.readFileSync(path.join(home,'owned-pids.json'))))owned.add(pid);}catch{/* 아직 자식을 만들지 않음 */} try{for(const pid of fs.readFileSync(path.join(home,'engine-pids.jsonl'),'utf8').trim().split('\n').map(Number))owned.add(pid);}catch{/* 엔진을 만들기 전 실패 */} for(const pid of owned)if(Number.isInteger(pid)&&pid>0)require('../src/utils/process-helper').killProcessTree(pid,true); console.error('APP_LIFECYCLE_FAILED '+error.message+'; fixture='+home);process.exitCode=1; });