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

secret-resolution.node.test.ts

39 lines · 1.9 KB · TypeScript
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'

import { request } from './core.ts'
import { defaultSecretResolution } from './secret-resolution.ts'

describe('defaultSecretResolution', () => {
	it('defaults file-content uploads to headers-only', () => {
		assert.equal(defaultSecretResolution('POST', '/repos/o/r/git/blobs'), 'headers-only')
		assert.equal(defaultSecretResolution('post', 'repos/o/r/git/trees'), 'headers-only')
		assert.equal(defaultSecretResolution('PUT', '/repos/o/r/contents/src/a.test.ts'), 'headers-only')
		assert.equal(defaultSecretResolution('POST', 'https://api.github.com/repos/o/r/git/blobs?x=1'), 'headers-only')
	})

	it('leaves other calls alone', () => {
		assert.equal(defaultSecretResolution('GET', '/repos/o/r/git/blobs/abc'), undefined)
		assert.equal(defaultSecretResolution('POST', '/repos/o/r/git/commits'), undefined)
		assert.equal(defaultSecretResolution('POST', '/repos/o/r/issues/1/comments'), undefined)
		assert.equal(defaultSecretResolution('DELETE', '/repos/o/r/contents/a.ts'), undefined)
	})
})

describe('request secret-resolution header', () => {
	it('sets headers-only for blob uploads and respects a caller header', async () => {
		const seen: Array<string | null> = []
		;(globalThis as { __kodyFetch?: unknown }).__kodyFetch = async (_url: unknown, init?: RequestInit) => {
			seen.push(new Headers(init?.headers).get('x-kody-secret-resolution'))
			return new Response('{"sha":"x"}', { status: 201, headers: { 'content-type': 'application/json' } })
		}
		await request({ path: '/repos/o/r/git/blobs', body: { content: 'YQ==', encoding: 'base64' } })
		await request({
			path: '/repos/o/r/git/blobs',
			headers: { 'x-kody-secret-resolution': 'off' },
			body: { content: 'YQ==', encoding: 'base64' },
		})
		await request({ path: '/repos/o/r/issues/1/comments', body: { body: 'hi' } })
		assert.deepEqual(seen, ['headers-only', 'off', null])
	})
})