import { test, expect } from 'bun:test'; import { mkdtemp, writeFile, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { eddyAgent, eddyReviewArgs, eddyDeviceSimArgs } from '../src/eddy_agent'; import { reviewEddyPackage } from '../src/eddy_safety'; import { obviousRisk } from '@bbbug/shared/eddy_safety'; test('public attribution IDs are distinct from actual bearer credentials',()=>{ expect(obviousRisk('{"author":"api-key:opk_0123456789ab"}')).toBeNull(); expect(obviousRisk('sk-sl-'+ 'a'.repeat(32))).not.toBeNull(); }); test('selection preserves the chosen CLI and keeps review distinct from source editing',()=>{ const agent=eddyAgent('review',{EDDY_AGENT:'codex',CODEX_BIN:'/explicit/codex'}); expect(agent).toEqual({name:'codex',binary:'/explicit/codex'}); const review=eddyReviewArgs(agent,'/tmp/review'); expect(review).toContain('read-only');expect(review).toContain('shell_tool');expect(review).toContain('--ignore-user-config'); expect(eddyDeviceSimArgs(agent,'/tmp/repo','inspect')).toContain('workspace-write'); expect(()=>eddyAgent('review',{EDDY_AGENT:'invalid'})).toThrow(); }); for(const name of ['codex','claude','failure'] as const)test(`review consumes ${name} CLI output without fallback`,async()=>{ const dir=await mkdtemp(path.join(tmpdir(),'eddy-agent-contract-')); const before={name:process.env.EDDY_REVIEW_AGENT,bin:process.env.EDDY_REVIEW_AGENT_BIN}; try{ const binary=path.join(dir,'cli'); const result=JSON.stringify({verdict:'allow',reason:'fixture: bounded read-only check'}); const code=name==='failure' ? 'process.exit(7)' : name==='codex' ? `const fs=require('node:fs'); const a=process.argv; fs.writeFileSync(a[a.indexOf('--output-last-message')+1], ${JSON.stringify(result)});` : `console.log(JSON.stringify({result:${JSON.stringify(result)},is_error:false}));`; await writeFile(binary,`#!${process.execPath}\n${code}\n`,{mode:0o700}); process.env.EDDY_REVIEW_AGENT=name==='claude'?'claude':'codex';process.env.EDDY_REVIEW_AGENT_BIN=binary; const receipt=await reviewEddyPackage({manifest:'{}',payload:'',payloadSha256:null}); expect(receipt.verdict).toBe(name==='failure'?'unknown':'allow'); if(name!=='failure')expect(receipt.reviewer).toBe(`satellite/${name}-tools-disabled`); }finally{ if(before.name===undefined)delete process.env.EDDY_REVIEW_AGENT;else process.env.EDDY_REVIEW_AGENT=before.name; if(before.bin===undefined)delete process.env.EDDY_REVIEW_AGENT_BIN;else process.env.EDDY_REVIEW_AGENT_BIN=before.bin; await rm(dir,{recursive:true,force:true}); } });