← 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.
Files
import assert from 'node:assert/strict'
import { registerHooks } from 'node:module'
import { test } from 'node:test'
const STUB = `
const state = globalThis.__ownerGithub ??= { keys: [], fetchCalls: [], client: null, error: null };
export const packageStorage = () => ({ async get() { return null }, async set() {} });
export async function credential(key) {
state.keys.push(key);
if (state.error) throw state.error;
if (state.client) return state.client;
throw new Error('credential_unbound: ' + key);
}
export async function credentials() { return [] }
`
registerHooks({
resolve(s, c, n) {
return s === 'kody:runtime' ? { url: `data:text/javascript,${encodeURIComponent(STUB)}`, shortCircuit: true } : n(s, c)
},
})
const g = globalThis as unknown as {
__ownerGithub: {
keys: string[]
fetchCalls: Array<{ url: string; method?: string }>
client: { name: string; fetch: (url: string, init?: RequestInit) => Promise<Response> } | null
error: Error | null
}
}
g.__ownerGithub = { keys: [], fetchCalls: [], client: null, error: null }
const {
ownerGithubRequest,
openGithubOwnerCredential,
GITHUB_OWNER_CREDENTIAL_KEY,
GITHUB_OWNER_UNMAPPED_MESSAGE,
GithubOwnerSetupError,
} = await import('./owner-github-request.ts')
function reset() {
g.__ownerGithub.keys = []
g.__ownerGithub.fetchCalls = []
g.__ownerGithub.client = null
g.__ownerGithub.error = null
}
function mappedClient(login = 'kentcdodds') {
return {
name: 'github-kentcdodds',
fetch: async (url: string, init?: RequestInit) => {
g.__ownerGithub.fetchCalls.push({ url: String(url), method: init?.method })
return new Response(JSON.stringify({ login, ok: true }), {
status: 200,
headers: { 'content-type': 'application/json' },
})
},
}
}
test('opens only the githubOwner slot, never the bot github slot', async () => {
reset()
g.__ownerGithub.client = mappedClient()
const client = await openGithubOwnerCredential()
assert.equal(typeof client.fetch, 'function')
assert.deepEqual(g.__ownerGithub.keys, [GITHUB_OWNER_CREDENTIAL_KEY])
assert.equal(g.__ownerGithub.keys.includes('github'), false)
})
test('unmapped githubOwner fails clearly and does not fall back to github', async () => {
reset()
await assert.rejects(
() => ownerGithubRequest({ method: 'GET', path: '/user' }),
(err: unknown) => {
assert.ok(err instanceof GithubOwnerSetupError)
assert.equal((err as GithubOwnerSetupError).code, 'github-owner-credential-missing')
assert.equal((err as Error).message, GITHUB_OWNER_UNMAPPED_MESSAGE)
return true
},
)
assert.deepEqual(g.__ownerGithub.keys, [GITHUB_OWNER_CREDENTIAL_KEY])
assert.equal(g.__ownerGithub.keys.includes('github'), false)
assert.equal(g.__ownerGithub.fetchCalls.length, 0)
})
test('GET uses githubOwner and sends no confirm flag', async () => {
reset()
g.__ownerGithub.client = mappedClient()
const result = await ownerGithubRequest({ method: 'GET', path: '/user' })
assert.equal(result.ok, true)
assert.equal((result.data as { login: string }).login, 'kentcdodds')
assert.deepEqual(g.__ownerGithub.keys, [GITHUB_OWNER_CREDENTIAL_KEY])
assert.equal(g.__ownerGithub.fetchCalls.length, 1)
assert.ok(g.__ownerGithub.fetchCalls[0].url.startsWith('https://api.github.com/user'))
})
test('mutations require dryRun or confirm, and dryRun does not call GitHub', async () => {
reset()
g.__ownerGithub.client = mappedClient()
await assert.rejects(
() => ownerGithubRequest({ method: 'POST', path: '/repos/kody-hq/kody/git/blobs', body: { content: 'x' } }),
/requires dryRun: true or confirm: true/,
)
assert.equal(g.__ownerGithub.fetchCalls.length, 0)
assert.deepEqual(g.__ownerGithub.keys, [])
const preview = await ownerGithubRequest({
method: 'POST',
path: '/repos/kody-hq/kody/git/blobs',
body: { content: 'x' },
dryRun: true,
})
assert.equal(preview.ok, true)
assert.equal((preview.data as { dryRun: boolean }).dryRun, true)
assert.equal(g.__ownerGithub.fetchCalls.length, 0)
assert.deepEqual(g.__ownerGithub.keys, [])
const live = await ownerGithubRequest({
method: 'POST',
path: '/repos/kody-hq/kody/git/blobs',
body: { content: 'x' },
confirm: true,
})
assert.equal(live.ok, true)
assert.equal(g.__ownerGithub.fetchCalls.length, 1)
assert.deepEqual(g.__ownerGithub.keys, [GITHUB_OWNER_CREDENTIAL_KEY])
})
test('refuses account and never requests a non-GitHub host', async () => {
reset()
g.__ownerGithub.client = mappedClient()
await assert.rejects(
() => ownerGithubRequest({ method: 'GET', path: '/user', account: 'kent' }),
/account/,
)
assert.equal(g.__ownerGithub.keys.length, 0)
await assert.rejects(
() => ownerGithubRequest({ method: 'GET', path: 'https://example.com/user' }),
/non-GitHub API host/,
)
assert.equal(g.__ownerGithub.fetchCalls.length, 0)
})