File size: 2,671 Bytes
914f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { afterEach, describe, expect, test } from 'bun:test';
import { aggregateCycle } from '../src/aggregate.js';
import { resetConfigCache } from '../src/config.js';
import {
  ciFailedChecks,
  ciGreenChecks,
  makeState,
  repoRoot,
} from './helpers.js';

afterEach(() => {
  delete process.env.CI_TRIGGER_FAILED;
  resetConfigCache();
});

describe('aggregateCycle cycle-cap vs CI-red fixer budget', () => {
  test('does not hand off when cycle>=max, CI failed, and fix rounds remain', () => {
    const state = makeState({
      cycle: 8,
      fixRound: 4,
      status: 'in_progress',
      consecutiveNoNewReviews: 0,
    });

    const result = aggregateCycle(
      repoRoot,
      state,
      {},
      ciFailedChecks(),
      [],
    );

    expect(result.ciFailed).toBe(true);
    expect(result.shouldHandoff).toBe(false);
    expect(result.shouldFix).toBe(true);
    expect(result.state.status).toBe('in_progress');
    expect(result.handoffReason).toBeUndefined();
  });

  test('hands off budget_exhausted when cycle>=max, CI failed, but fix rounds exhausted', () => {
    const state = makeState({
      cycle: 8,
      fixRound: 5,
      status: 'in_progress',
    });

    const result = aggregateCycle(
      repoRoot,
      state,
      {},
      ciFailedChecks(),
      [],
    );

    expect(result.ciFailed).toBe(true);
    expect(result.shouldHandoff).toBe(true);
    expect(result.shouldFix).toBe(false);
    expect(result.handoffReason).toBe('budget_exhausted');
    expect(result.state.status).toBe('budget_exhausted');
  });

  test('still hands off budget_exhausted when cycle>=max and CI is green', () => {
    const state = makeState({
      cycle: 8,
      fixRound: 1,
      status: 'in_progress',
      consecutiveNoNewReviews: 0,
    });

    const result = aggregateCycle(
      repoRoot,
      state,
      {},
      ciGreenChecks(),
      [],
    );

    expect(result.ciFailed).toBe(false);
    expect(result.shouldHandoff).toBe(true);
    expect(result.shouldFix).toBe(false);
    expect(result.handoffReason).toBe('budget_exhausted');
  });

  test('honors CI_TRIGGER_FAILED env even when check list looks green', () => {
    process.env.CI_TRIGGER_FAILED = 'true';
    const state = makeState({
      cycle: 8,
      fixRound: 2,
      status: 'in_progress',
    });

    const result = aggregateCycle(
      repoRoot,
      state,
      {},
      ciGreenChecks(),
      [],
    );

    expect(result.ciFailed).toBe(true);
    expect(result.shouldHandoff).toBe(false);
    expect(result.shouldFix).toBe(true);
  });
});