Skip to content

src/github-paging.node.test.ts

27 lines · 1.0 KB · TypeScript
Raw
import assert from 'node:assert/strict'
import test from 'node:test'
import { nextLinkFrom, requestTimeoutWithin } from './github-paging.ts'

test('caps each GitHub request at 15s when plenty of budget remains', () => {
	assert.equal(requestTimeoutWithin(100_000, 0), 15_000)
	assert.equal(requestTimeoutWithin(undefined, 0), 15_000)
})

test('shrinks the request timeout to the time left before the deadline', () => {
	assert.equal(requestTimeoutWithin(10_000, 4_000), 6_000)
})

test('refuses to start a request with under 2s left', () => {
	assert.equal(requestTimeoutWithin(10_000, 8_500), null)
	assert.equal(requestTimeoutWithin(10_000, 12_000), null)
})

test('follows the rel="next" GitHub Link cursor', () => {
	const next = 'https://api.github.com/repositories/1/commits?sha=main&per_page=100&after=abc+99'
	assert.equal(
		nextLinkFrom(`<${next}>; rel="next", <https://api.github.com/x?page=1>; rel="first"`),
		next,
	)
	assert.equal(nextLinkFrom('<https://api.github.com/x?page=1>; rel="prev"'), null)
	assert.equal(nextLinkFrom(undefined), null)
})