also looking at this
fix(ci): give the repair loop the guard it already had
#1407
10 files
+229
-34
| @@ -0,0 +1,79 @@ | ||
| 1 | import { describe, expect, it } from 'bun:test' | |
| 2 | import { attemptFix } from '../src/ci/fix' | |
| 3 | import { parseFixAttempts, upsertFixAttempts } from '../src/ci/attempts' | |
| 4 | ||
| 5 | const TYPE_ERROR_LOG = `src/app.ts(12,5): error TS2322: Type 'string' is not assignable to type 'number'.` | |
| 6 | ||
| 7 | /** | |
| 8 | * `attemptFix` has always refused to keep trying past `maxAttempts`, but no | |
| 9 | * production caller passed `priorAttempts`, so every failure looked like the | |
| 10 | * first one and the guard never engaged. | |
| 11 | */ | |
| 12 | describe('fix attempt tracking', () => { | |
| 13 | describe('marker round-trip', () => { | |
| 14 | it('success case - a recorded attempt reads back', () => { | |
| 15 | const body = upsertFixAttempts('Original description.', 1) | |
| 16 | ||
| 17 | expect(parseFixAttempts(body)?.attempts).toBe(1) | |
| 18 | expect(body).toContain('Original description.') | |
| 19 | }) | |
| 20 | ||
| 21 | it('success case - recording again replaces rather than stacks', () => { | |
| 22 | const first = upsertFixAttempts('desc', 1) | |
| 23 | const second = upsertFixAttempts(first, 2) | |
| 24 | ||
| 25 | expect(second.match(/<!--\s*buddy:fix-ci/g)).toHaveLength(1) | |
| 26 | expect(parseFixAttempts(second)?.attempts).toBe(2) | |
| 27 | }) | |
| 28 | ||
| 29 | it('edge case - no marker means no attempts yet', () => { | |
| 30 | expect(parseFixAttempts('just a description')).toBeNull() | |
| 31 | expect(parseFixAttempts(null)).toBeNull() | |
| 32 | expect(parseFixAttempts('')).toBeNull() | |
| 33 | }) | |
| 34 | ||
| 35 | it('edge case - an unreadable marker reads as absent rather than throwing', () => { | |
| 36 | // Refusing to act because the bookkeeping is corrupt would be a worse | |
| 37 | // failure than one extra attempt. | |
| 38 | expect(() => parseFixAttempts('<!-- buddy:fix-ci v1\n{bad\n-->')).not.toThrow() | |
| 39 | expect(parseFixAttempts('<!-- buddy:fix-ci v1\n{bad\n-->')).toBeNull() | |
| 40 | }) | |
| 41 | ||
| 42 | it('edge case - a negative count is rejected', () => { | |
| 43 | expect(parseFixAttempts('<!-- buddy:fix-ci v1\n{"attempts":-3}\n-->')).toBeNull() | |
| 44 | }) | |
| 45 | ||
| 46 | it('success case - survives alongside the review marker', () => { | |
| 47 | // Both live in the pull request body; neither may eat the other. | |
| 48 | const withReview = 'desc\n\n<!-- buddy:review v1\n{"reviewedSha":"abc","fingerprints":[]}\n-->' | |
| 49 | const body = upsertFixAttempts(withReview, 1) | |
| 50 | ||
| 51 | expect(body).toContain('buddy:review') | |
| 52 | expect(parseFixAttempts(body)?.attempts).toBe(1) | |
| 53 | }) | |
| 54 | }) | |
| 55 | ||
| 56 | describe('the guard the count feeds', () => { | |
| 57 | const base = { workspace: process.cwd(), baseBranch: 'main' } | |
| 58 | ||
| 59 | it('failure case - stops once the attempts are spent', async () => { | |
| 60 | const outcome = await attemptFix({ ...base, log: TYPE_ERROR_LOG, priorAttempts: 3, maxAttempts: 3 }) | |
| 61 | ||
| 62 | expect(outcome.action).toBe('skipped') | |
| 63 | expect(outcome.report).toContain('stopping rather than looping') | |
| 64 | }) | |
| 65 | ||
| 66 | it('success case - a count below the ceiling still tries', async () => { | |
| 67 | const outcome = await attemptFix({ ...base, log: TYPE_ERROR_LOG, priorAttempts: 1, maxAttempts: 3 }) | |
| 68 | ||
| 69 | expect(outcome.action).not.toBe('skipped') | |
| 70 | }) | |
| 71 | ||
| 72 | it('failure case - a pre-existing failure on base is not this PR to fix', async () => { | |
| 73 | const outcome = await attemptFix({ ...base, log: TYPE_ERROR_LOG, failsOnBase: true }) | |
| 74 | ||
| 75 | expect(outcome.action).toBe('skipped') | |
| 76 | expect(outcome.report).toContain('base branch') | |
| 77 | }) | |
| 78 | }) | |
| 79 | }) | |