| 1 | 'use strict'; |
| 2 | const test = require('node:test'); |
| 3 | const assert = require('node:assert/strict'); |
| 4 | const fs = require('node:fs'); |
| 5 | const path = require('node:path'); |
| 6 | const os = require('node:os'); |
| 7 | const crypto = require('node:crypto'); |
| 8 | const cp = require('node:child_process'); |
| 9 | const http = require('node:http'); |
| 10 | const {once} = require('node:events'); |
| 11 | const bridge = require('./windows_ccs_launch.cjs'); |
| 12 | |
| 13 | function fixture(t) { |
| 14 | const folder = fs.mkdtempSync(path.join(os.tmpdir(), 'cage-ccs-spawn-')); |
| 15 | t.after(() => fs.rmSync(folder,{recursive:true,force:true})); |
| 16 | return {folder,id:crypto.randomUUID(),session_id:crypto.randomUUID(),profile:'qa',binary:path.join(folder,'trusted-claude.exe'),python:process.execPath,helper:path.join(folder,'actual-helper.py')}; |
| 17 | } |
| 18 | test('only the fixed native Claude launch is wrapped, with complete dynamic child environment', async t => { |
| 19 | const context = fixture(t), calls = []; |
| 20 | const original = (command,args,options) => { calls.push({command,args,options});return new (require('node:events').EventEmitter)(); }; |
| 21 | const restore = bridge.install(context,original);t.after(restore); |
| 22 | const unrelated = {env:{UNCHANGED:'yes'}}; |
| 23 | cp.spawn('unrelated.exe',['--session-id',context.session_id],unrelated); |
| 24 | assert.equal(calls[0].command,'unrelated.exe');assert.equal(calls[0].options,unrelated); |
| 25 | cp.spawn(context.binary,['--version'],{env:{CCS_PROFILE_TYPE:'settings'}}); |
| 26 | assert.equal(calls[1].command,context.binary); |
| 27 | const settings = path.join(context.folder,'source.settings.json'); |
| 28 | const env = {CCS_PROFILE_TYPE:'settings',CLAUDE_CONFIG_DIR:path.join(context.folder,'actual-config'),DYNAMIC_PROXY_TOKEN:'fixture-only',UNRECOGNIZED_SETTING:'keep'}; |
| 29 | cp.spawn(context.binary,['--settings',settings,'--session-id',context.session_id,'--model','fixture-model'],{env,stdio:'inherit'}); |
| 30 | const call = calls[2];assert.equal(call.command,context.python);assert.deepEqual(call.args.slice(0,5),['-X','utf8','-I',context.helper,'ccs-live']); |
| 31 | assert.equal(call.options.env.DYNAMIC_PROXY_TOKEN,env.DYNAMIC_PROXY_TOKEN);assert.equal(call.options.env.CLAUDE_CONFIG_DIR,env.CLAUDE_CONFIG_DIR);assert.equal(call.options.env.UNRECOGNIZED_SETTING,'keep'); |
| 32 | const note = JSON.parse(fs.readFileSync(call.args[5],'utf8'));assert.equal(note.settings_source,settings);assert.equal(note.actual_args.at(-1),'fixture-model'); |
| 33 | assert.equal(fs.readFileSync(call.args[5],'utf8').includes('fixture-only'),false,'temporary token was copied into binding metadata'); |
| 34 | assert.throws(() => cp.spawn(context.binary,['--session-id',context.session_id],{env}),/duplicate_claude/); |
| 35 | }); |
| 36 | test('the real parent proxy remains usable until the actual child exits', async t => { |
| 37 | const context=fixture(t), token=crypto.randomUUID();let accepted=0; |
| 38 | const server=http.createServer((req,res)=>{if(req.headers.authorization===token){accepted++;res.end('alive')}else{res.statusCode=403;res.end()}}); |
| 39 | server.listen(0,'127.0.0.1');await once(server,'listening');t.after(()=>server.close()); |
| 40 | const originalSpawn=cp.spawn; |
| 41 | const childCode=`const fs=require('node:fs'),http=require('node:http');const note=JSON.parse(fs.readFileSync(process.argv[1],'utf8'));if(!note.actual_args.includes('--model'))process.exit(2);http.get(process.env.PROXY_URL,{headers:{authorization:process.env.DYNAMIC_PROXY_TOKEN}},r=>{let text='';r.on('data',b=>text+=b);r.on('end',()=>{if(text!=='alive')process.exit(3);setTimeout(()=>process.exit(0),100)})});`; |
| 42 | const wrapped=(command,args,options)=>originalSpawn(process.execPath,['-e',childCode,args[5]],{...options,stdio:'ignore'}); |
| 43 | const restore=bridge.install(context,wrapped);t.after(restore); |
| 44 | const child=cp.spawn(context.binary,['--session-id',context.session_id,'--model','fixture-model'],{env:{...process.env,PROXY_URL:`http://127.0.0.1:${server.address().port}`,DYNAMIC_PROXY_TOKEN:token}}); |
| 45 | const [code]=await once(child,'exit');assert.equal(code,0);assert.equal(accepted,1);assert.equal(fs.readdirSync(context.folder).some(name=>name.startsWith('ccs-actual-')),false); |
| 46 | }); |