Skip to content
← Public packages

@kody/kody-factory

Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.

FollowFork

Files

push-branch-core.test.ts

47 lines · 2.5 KB · TypeScript
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { pushBranchWith, type Request } from './push-branch-core.ts'

function fakeGithub() {
	const calls: Array<Record<string, unknown>> = []
	const request: Request = async (params) => {
		calls.push(params)
		if ('account' in params) throw new Error('account was removed in @kody/github 1.0.0')
		const method = String(params.method ?? 'GET').toUpperCase()
		if (method !== 'GET' && params.confirm !== true && params.dryRun !== true) {
			throw new Error(`GitHub mutation "${method} ${params.path}" requires dryRun: true or confirm: true`)
		}
		const path = String(params.path)
		if (path.endsWith('/git/ref/heads/main')) return { ok: true, status: 200, data: { object: { sha: 'base1' } } }
		if (path.includes('/git/ref/heads/')) return { ok: false, status: 404, data: null }
		if (path.includes('/git/commits/') && method === 'GET') return { ok: true, status: 200, data: { tree: { sha: 'tree0' } } }
		if (path.endsWith('/git/blobs')) return { ok: true, status: 201, data: { sha: 'blob1' } }
		if (path.endsWith('/git/trees')) return { ok: true, status: 201, data: { sha: 'tree1' } }
		if (path.endsWith('/git/commits')) return { ok: true, status: 201, data: { sha: 'commit1' } }
		if (path.endsWith('/git/refs')) return { ok: true, status: 201, data: {} }
		if (path.endsWith('/pulls') && method === 'GET') return { ok: true, status: 200, data: [] }
		if (path.endsWith('/pulls')) return { ok: true, status: 201, data: { number: 7, html_url: 'https://github.com/kody-hq/kody/pull/7', user: { login: 'kody-bot' } } }
		return { ok: false, status: 500, data: { message: `unexpected ${method} ${path}` } }
	}
	return { request, calls }
}

test('push-branch confirms live writes and passes no account option', async () => {
	const { request, calls } = fakeGithub()
	const result = await pushBranchWith(request, {
		repo: 'kody-hq/kody',
		branch: 'friction-log/test',
		message: 'fix: test',
		files: [{ path: 'README.md', content: 'hi' }],
		pullRequest: { title: 'fix: test', body: 'body' },
	})
	assert.equal(result.ok, true)
	assert.equal(result.commit?.sha, 'commit1')
	assert.equal(result.pr?.number, 7)
	for (const call of calls) {
		assert.equal('account' in call, false)
		const method = String(call.method ?? 'GET').toUpperCase()
		assert.equal(call.confirm, method === 'GET' ? undefined : true, `${method} ${call.path}`)
	}
	assert.ok(calls.some((call) => call.method === 'POST' && String(call.path).endsWith('/git/refs')))
})