Skip to content
← Public packages

@kody/github

Call GitHub REST, GraphQL, and pull requests with a GitHub OAuth App or a personal access token.

FollowFork

Files

check-rollup.test.ts

105 lines · 3.2 KB · TypeScript
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { readPrChecksPage, unreadableChecksNote } from '../src/pr/check-rollup.ts'

function prData(rollup: unknown) {
	return {
		repository: {
			pullRequest: {
				headRefOid: 'abc123',
				commits: { nodes: [{ commit: { oid: 'abc123', statusCheckRollup: rollup } }] },
			},
		},
	}
}

const checkRun = {
	__typename: 'CheckRun',
	name: 'Validate',
	status: 'COMPLETED',
	conclusion: 'SUCCESS',
	detailsUrl: 'https://github.com/example-org/example/actions/runs/1/job/2',
	startedAt: '2026-11-15T00:00:00Z',
	completedAt: '2026-11-15T00:05:00Z',
}

const failingStatus = {
	__typename: 'StatusContext',
	context: 'ci/legacy',
	state: 'FAILURE',
	targetUrl: 'https://ci.example.com/1',
	createdAt: '2026-11-15T00:01:00Z',
}

test('fine-grained token: forbidden check run nodes still yield the rollup verdict', () => {
	const errors = [0, 1].map((index) => ({
		type: 'FORBIDDEN',
		message: 'Resource not accessible by personal access token',
		path: ['repository', 'pullRequest', 'commits', 'nodes', 0, 'commit', 'statusCheckRollup', 'contexts', 'nodes', index],
	}))
	const page = readPrChecksPage(
		prData({
			state: 'FAILURE',
			contexts: { totalCount: 3, pageInfo: { hasNextPage: false, endCursor: 'Mw' }, nodes: [null, null, failingStatus] },
		}),
		errors,
		'example-org/example#1',
	)
	assert.equal(page.state, 'failing')
	assert.equal(page.totalCount, 3)
	assert.equal(page.unreadableCount, 2)
	assert.equal(page.endCursor, null)
	assert.deepEqual(page.checks, [
		{
			kind: 'status',
			name: 'ci/legacy',
			status: 'completed',
			conclusion: 'failure',
			detailsUrl: 'https://ci.example.com/1',
			startedAt: '2026-11-15T00:01:00Z',
			completedAt: null,
		},
	])
	assert.match(unreadableChecksNote(page.unreadableCount) ?? '', /Fine-grained personal access tokens have no Checks permission/)
})

test('readable check runs are named and the next page cursor is returned', () => {
	const page = readPrChecksPage(
		prData({
			state: 'PENDING',
			contexts: { totalCount: 101, pageInfo: { hasNextPage: true, endCursor: 'MTAw' }, nodes: [checkRun] },
		}),
		undefined,
		'example-org/example#1',
	)
	assert.equal(page.state, 'pending')
	assert.equal(page.unreadableCount, 0)
	assert.equal(page.endCursor, 'MTAw')
	assert.deepEqual(page.checks[0], {
		kind: 'check_run',
		name: 'Validate',
		status: 'completed',
		conclusion: 'success',
		detailsUrl: 'https://github.com/example-org/example/actions/runs/1/job/2',
		startedAt: '2026-11-15T00:00:00Z',
		completedAt: '2026-11-15T00:05:00Z',
	})
	assert.equal(unreadableChecksNote(0), null)
})

test('a head commit with no checks passes', () => {
	const page = readPrChecksPage(prData(null), undefined, 'example-org/example#1')
	assert.equal(page.state, 'passing')
	assert.equal(page.headSha, 'abc123')
	assert.equal(page.totalCount, 0)
})

test('errors outside the check nodes are not swallowed', () => {
	assert.throws(
		() =>
			readPrChecksPage({ repository: { pullRequest: null } }, [
				{ type: 'NOT_FOUND', message: 'Could not resolve to a PullRequest with the number of 9.', path: ['repository', 'pullRequest'] },
			], 'example-org/example#9'),
		/Could not resolve to a PullRequest/,
	)
})