Skip to content

src/ingest-shas.node.test.ts

36 lines · 1.1 KB · TypeScript
Raw
import assert from 'node:assert/strict'
import test from 'node:test'
import { ingestShaList } from './ingest-shas.ts'

test('one failed commit fetch defers that SHA instead of throwing', async () => {
	const warnings: string[] = []
	const result = await ingestShaList(['stored', 'boom', 'fresh'], {
		deadline: Number.POSITIVE_INFINITY,
		hasCommit: async (sha) => sha === 'stored',
		ingest: async (sha) => {
			if (sha === 'boom') throw new Error('GitHub request failed with 502')
		},
		warn: (message) => warnings.push(message),
	})
	assert.deepEqual(result.alreadyStored, ['stored'])
	assert.deepEqual(result.ingested, ['fresh'])
	assert.deepEqual(result.errors, [
		{ sha: 'boom', error: 'GitHub request failed with 502' },
	])
	assert.equal(result.remaining, 1)
	assert.equal(warnings.length, 1)
})

test('stops at the deadline and counts unvisited SHAs as remaining', async () => {
	let clock = 0
	const result = await ingestShaList(['a', 'b', 'c'], {
		deadline: 1,
		now: () => clock,
		hasCommit: async () => false,
		ingest: async () => {
			clock += 1
		},
	})
	assert.deepEqual(result.ingested, ['a'])
	assert.equal(result.remaining, 2)
})