import assert from 'node:assert/strict' import { describe, it } from 'node:test' import { BASE_MODIFIED_MAX_BACKOFF_MS, baseModifiedBackoffMs, isBaseBranchModifiedError, isMergeInFlightError, } from './merge-shared.ts' function fakeError(status: number, message?: string) { return { name: 'GitHubRequestError', message: message ? `GitHub request failed with ${status}: ${message}` : `GitHub request failed with ${status}`, response: { status, statusText: 'Error', ok: false, data: message ? { message } : null, text: message ? JSON.stringify({ message }) : '', account: 'bot', url: 'https://api.github.com/repos/o/r/pulls/1/merge', headers: {}, }, } } const BASE_MODIFIED = 'Base branch was modified. Review and try the merge again.' describe('isBaseBranchModifiedError', () => { it('detects GitHub 405 Base branch was modified', () => { assert.equal(isBaseBranchModifiedError(fakeError(405, BASE_MODIFIED)), true) }) it('is not an in-flight signal (handled separately)', () => { assert.equal(isMergeInFlightError(fakeError(405, BASE_MODIFIED)), false) }) it('falls back to the error message when data is missing', () => { const error = Object.assign( new Error(`GitHub request failed with 405: ${BASE_MODIFIED}`), { response: { status: 405, data: null, text: '' } }, ) assert.equal(isBaseBranchModifiedError(error), true) }) it('ignores other 405s, other statuses, and non-response errors', () => { assert.equal(isBaseBranchModifiedError(fakeError(405, 'Not Allowed')), false) assert.equal( isBaseBranchModifiedError(fakeError(405, 'Merge already in progress')), false, ) assert.equal(isBaseBranchModifiedError(fakeError(422, BASE_MODIFIED)), false) assert.equal(isBaseBranchModifiedError(new Error(BASE_MODIFIED)), false) assert.equal(isBaseBranchModifiedError(null), false) }) }) describe('baseModifiedBackoffMs', () => { it('grows per attempt and caps', () => { assert.ok(baseModifiedBackoffMs(2) > baseModifiedBackoffMs(1)) assert.equal(baseModifiedBackoffMs(99), BASE_MODIFIED_MAX_BACKOFF_MS) }) })