Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/github

GitHub REST, GraphQL, and pull request helpers for Kody agents.

FollowFork

Files

merge-queue.node.test.ts

73 lines · 3.2 KB · TypeScript
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import {
	describeQueued,
	enqueueIfMergeQueue,
	isMergeQueueRequiredError,
	type MergeQueueGraphql,
} from './merge-queue.ts'

function fakeGraphql(responses: Record<string, unknown>) {
	const calls: Array<{ query: string; variables: Record<string, unknown> }> = []
	const graphql: MergeQueueGraphql = async ({ query, variables }) => {
		calls.push({ query, variables })
		const name = /(?:query|mutation) (\w+)/.exec(query)![1]!
		return { data: responses[name] ?? null }
	}
	return { graphql, calls }
}

const base = { account: 'bot' as const, owner: 'o', repo: 'r', prNumber: 7, headSha: 'abc' }

describe('enqueueIfMergeQueue', () => {
	it('enqueues with the pinned head SHA when the base branch has a queue', async () => {
		const { graphql, calls } = fakeGraphql({
			MergeQueueLookup: { repository: { pullRequest: { id: 'PR_1', baseRefName: 'main', mergeQueueEntry: null } } },
			MergeQueueForBranch: { repository: { mergeQueue: { id: 'MQ_1' } } },
			EnqueuePullRequest: { enqueuePullRequest: { mergeQueueEntry: { state: 'QUEUED', position: 2 } } },
		})
		const result = await enqueueIfMergeQueue({ graphql, ...base })
		assert.deepEqual(result, { outcome: 'queued', alreadyQueued: false, state: 'QUEUED', position: 2 })
		assert.deepEqual(calls[1]!.variables, { owner: 'o', repo: 'r', branch: 'main' })
		assert.deepEqual(calls[2]!.variables, { pullRequestId: 'PR_1', expectedHeadOid: 'abc' })
	})

	it('reports an existing entry without enqueueing twice', async () => {
		const { graphql, calls } = fakeGraphql({
			MergeQueueLookup: {
				repository: { pullRequest: { id: 'PR_1', baseRefName: 'main', mergeQueueEntry: { state: 'AWAITING_CHECKS', position: 1 } } },
			},
		})
		const result = await enqueueIfMergeQueue({ graphql, ...base })
		assert.deepEqual(result, { outcome: 'queued', alreadyQueued: true, state: 'AWAITING_CHECKS', position: 1 })
		assert.equal(calls.length, 1)
	})

	it('returns no_queue when the base branch has no merge queue', async () => {
		const { graphql, calls } = fakeGraphql({
			MergeQueueLookup: { repository: { pullRequest: { id: 'PR_1', baseRefName: 'main', mergeQueueEntry: null } } },
			MergeQueueForBranch: { repository: { mergeQueue: null } },
		})
		assert.deepEqual(await enqueueIfMergeQueue({ graphql, ...base }), { outcome: 'no_queue' })
		assert.equal(calls.length, 2)
	})
})

describe('isMergeQueueRequiredError', () => {
	it('matches the 405 merge queue refusal only', () => {
		const err = (status: number, message: string) => ({ response: { status, data: { message }, text: '' } })
		assert.equal(isMergeQueueRequiredError(err(405, 'Changes must be made through the merge queue')), true)
		assert.equal(isMergeQueueRequiredError(err(405, 'Merge already in progress')), false)
		assert.equal(isMergeQueueRequiredError(err(422, 'merge queue')), false)
		assert.equal(isMergeQueueRequiredError(null), false)
	})
})

describe('describeQueued', () => {
	it('names the position and state', () => {
		assert.match(
			describeQueued(7, { outcome: 'queued', alreadyQueued: false, state: 'QUEUED', position: 2 }).message,
			/PR #7 was added to the merge queue at position 2 \(state QUEUED\)/,
		)
	})
})