Skip to content

Commit 727752c

Browse files
committed
test: cover large line groups and recheck authorization on rotation
1 parent de060ad commit 727752c

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎src/worker/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ async function route(request: Request, env: Env): Promise<Response> {
229229
await store.stage(id, identity.runId, 'analyzing'); await notify(env, store, snapshot);
230230
return Response.json({ status: 'recorded' });
231231
}
232-
if (operation === 'prepare'||operation==='context') {
232+
if (['prepare','context','rotate','renew'].includes(operation)) {
233233
const hosted = await env.DB.prepare('SELECT state FROM web_connections WHERE installation_id=? AND repository_id=? AND actor_id=?').bind(snapshot.installationId, snapshot.repositoryId, snapshot.actorId).first<{ state: string }>();
234234
requireCondition(!hosted || hosted.state === 'ready', 'hosted_connection_requires_reconnect', 409);
235235
const { data: pr } = await github.rest.pulls.get({ ...splitRepo(snapshot.repository), pull_number: snapshot.pr });
@@ -241,6 +241,8 @@ async function route(request: Request, env: Env): Promise<Response> {
241241
const livePermission = await permission(github, snapshot.repository, snapshot.actor,snapshot.actorId);
242242
requireCondition(!snapshot.triggerActor || (livePermission==='admin' && config.credentials.automatic_reviewer?.toLowerCase()===snapshot.actor.toLowerCase()),'automatic_review_authorization_revoked',403);
243243
requireCondition(config.review.enabled && authorize(snapshot.actor, livePermission, config.authorization), 'review_authorization_revoked', 403);
244+
}
245+
if(operation==='prepare'||operation==='context'){
244246
if(operation==='prepare')requireCondition(await store.prepare(id, identity.runId), 'request_already_started', 409);
245247
else requireCondition(row.state==='running'&&row.run_id===identity.runId,'request_not_running',409);
246248
// Acquire before reading: reconnect must not replace the record between snapshot and lease.

‎tests/orchestration.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,20 @@ await test('synthesis reserve cannot authorize unfinished work or spend another
209209
t.mock.timers.enable({apis:['Date'],now:1700000000000});const started=Date.now();let calls=0;
210210
try{const result=await orchestrateReview(plan(2),async()=>'',async id=>{calls++;t.mock.timers.setTime(started+5*60000-1000);return response(id,false,[finding]);},started+5*60000);assert.equal(calls,2);assert.equal(result.synthesis,false);assert.equal(result.review.complete,false);assert.deepEqual(result.review.findings,[finding]);validateExecution(result,plan(2));}finally{t.mock.timers.reset();}
211211
});
212+
await test('large textual changes are grouped by changed-line work without splitting or omitting files',async()=>{
213+
const directory=await mkdtemp(join(tmpdir(),'cb-lines-'));try{
214+
await mkdir(join(directory,'source/head'),{recursive:true});await mkdir(join(directory,'source/base'),{recursive:true});
215+
const changed=[];
216+
for(let i=0;i<89;i++){
217+
const filename=`module-${String(i).padStart(2,'0')}.js`,lines=i===88?800:60;
218+
const before=Array.from({length:lines},(_,n)=>`export const before${i}_${n}=${n};`).join('\n')+'\n';
219+
const after=Array.from({length:lines},(_,n)=>`export const after${i}_${n}=${n+1};`).join('\n')+'\n';
220+
await writeFile(join(directory,'source/base',filename),before);await writeFile(join(directory,'source/head',filename),after);
221+
changed.push({filename,status:'modified',patch:`@@ -1,${lines} +1,${lines} @@\n`+before.trimEnd().split('\n').map(line=>'-'+line).join('\n')+'\n'+after.trimEnd().split('\n').map(line=>'+'+line).join('\n')});
222+
}
223+
await writeFile(join(directory,'diff.json'),JSON.stringify(changed));await writeFile(join(directory,'trusted-instructions.json'),'[]');
224+
const p=await buildReviewPlan(directory,HEAD,BASE,[]);assert.equal(p.units.length,9);assert.deepEqual(p.units.flatMap(u=>u.paths).sort(),changed.map(f=>f.filename).sort());
225+
for(const unit of p.units){const evidence=JSON.parse(await unitEvidence(directory,unit));const lines=evidence.changes.reduce((total:number,file:{patch:string})=>total+file.patch.split('\n').filter(line=>/^[+-]/.test(line)).length,0);assert(unit.paths.length===1||lines<=1500);}
226+
assert.deepEqual(p.units.at(-1)!.paths,['module-88.js']);
227+
}finally{await rm(directory,{recursive:true,force:true});}
228+
});

‎tests/worker.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ await test('long reviews renew only repository-read tokens through the same live
102102
assert.deepEqual(issued.at(-1)!.body,{repository_ids:[2],permissions:{contents:'read',pull_requests:'read'}});
103103
assert.equal((await post('context','codexbot-publish-1')).status,403);
104104
assert.equal((await post('context','codexbot-1-5',{run_id:'456'})).status,403);
105-
f.state.permission='none';assert.equal((await post('context')).status,403);f.state.permission='write';
106-
f.state.head='e'.repeat(40);assert.equal((await post('context')).status,409);f.state.head=HEAD;
105+
f.state.permission='none';for(const operation of ['context','rotate','renew'])assert.equal((await post(operation)).status,403);f.state.permission='write';
106+
f.state.head='e'.repeat(40);for(const operation of ['context','rotate','renew'])assert.equal((await post(operation)).status,409);f.state.head=HEAD;
107107
f.state.suspended=true;assert.equal((await post('context')).status,403);
108108
}));
109109
await test('delayed authorization-revocation webhooks do not delete newly authorized browser sessions by actor alone', async () => fixture(async env => {

0 commit comments

Comments
 (0)