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

file.test.ts

92 lines · 4.4 KB · TypeScript
import assert from 'node:assert/strict'
import { registerHooks } from 'node:module'
import { test } from 'node:test'

const STUB = `
export const packageStorage = () => ({ async get() { return null }, async set() {} });
export const packageContext = { appBasePath: '/apps/factory/' };
export async function credential() { throw new Error('credential not mapped') }
export async function credentials() { return [] }
export const kody = {};
`
// Mirrors @kody/github's guard: live mutating REST calls need confirm: true.
const REQUEST = `
export default async function(input) {
  const method = String(input.method ?? 'GET').toUpperCase();
  const mutating = !['GET', 'HEAD', 'OPTIONS'].includes(method);
  if (mutating && input.dryRun !== true && input.confirm !== true) {
    throw new Error('GitHub mutation "' + method + ' ' + input.path + '" requires dryRun: true (preview, no GitHub write) or confirm: true (live write).');
  }
  globalThis.__githubCalls.push({ method, path: input.path, confirm: input.confirm });
  return globalThis.__githubRespond(method, input.path);
}
`
registerHooks({
	resolve(spec, ctx, next) {
		if (spec === 'kody:@kody/discord/post-message') return { url: `data:text/javascript,${encodeURIComponent('export default async () => ({ ok: true })')}`, shortCircuit: true }
		if (spec === 'kody:runtime') return { url: `data:text/javascript,${encodeURIComponent(STUB)}`, shortCircuit: true }
		if (spec === 'kody:@kody/github/request') return { url: `data:text/javascript,${encodeURIComponent(REQUEST)}`, shortCircuit: true }
		return next(spec, ctx)
	},
})

type Call = { method: string; path: string; confirm?: boolean }
const g = globalThis as unknown as {
	__githubCalls: Array<Call>
	__githubRespond: (method: string, path: string) => { ok: boolean; status: number; data?: unknown }
}

const item = {
	title: 'Example papercut',
	whatHappened: 'A command failed.',
	whatYouWanted: 'It to work.',
	howToReproduce: 'Run the command.',
	cost: 'Five minutes.',
	preliminaryInvestigation: 'Looked at the export list.',
	kind: 'wasteful-context' as const,
}
const target = { host: 'github' as const, repo: 'example-org/example' }

test('live filing passes confirm: true on every GitHub write (create, label)', async () => {
	const { default: fileFriction } = await import('./file.ts')
	g.__githubCalls = []
	g.__githubRespond = (method, path) => {
		if (path.startsWith('/search/issues')) return { ok: true, status: 200, data: { items: [] } }
		if (method === 'GET' && path.includes('/labels/')) return { ok: false, status: 404 }
		if (method === 'POST' && path.endsWith('/issues')) return { ok: true, status: 201, data: { number: 7, html_url: 'https://github.com/example-org/example/issues/7' } }
		return { ok: true, status: 201, data: {} }
	}
	const result = await fileFriction({ target, items: [item] })
	assert.equal(result.filed.length, 1)
	assert.equal(result.filed[0]?.number, 7)
	const writes = g.__githubCalls.filter((c) => c.method !== 'GET')
	assert.deepEqual(writes.map((c) => c.path), ['/repos/example-org/example/labels', '/repos/example-org/example/issues'])
	assert.ok(writes.every((c) => c.confirm === true))
})

test('live reuse passes confirm: true when labeling and commenting', async () => {
	const { default: fileFriction } = await import('./file.ts')
	g.__githubCalls = []
	g.__githubRespond = (method, path) => {
		if (path.startsWith('/search/issues')) {
			return { ok: true, status: 200, data: { items: [{ number: 9, title: item.title, body: 'older body', html_url: 'https://github.com/example-org/example/issues/9', labels: [] }] } }
		}
		if (method === 'GET' && path.includes('/labels/')) return { ok: true, status: 200, data: {} }
		return { ok: true, status: 201, data: {} }
	}
	const result = await fileFriction({ target, items: [item] })
	assert.equal(result.reused.length, 1)
	assert.equal(result.reused[0]?.commented, true)
	const writes = g.__githubCalls.filter((c) => c.method !== 'GET')
	assert.deepEqual(writes.map((c) => c.path), ['/repos/example-org/example/issues/9/labels', '/repos/example-org/example/issues/9/comments'])
	assert.ok(writes.every((c) => c.confirm === true))
})

test('dry run makes no GitHub writes', async () => {
	const { default: fileFriction } = await import('./file.ts')
	g.__githubCalls = []
	g.__githubRespond = () => ({ ok: true, status: 200, data: { items: [] } })
	const result = await fileFriction({ target, items: [item], dryRun: true })
	assert.equal(result.filed.length, 1)
	assert.deepEqual(g.__githubCalls.filter((c) => c.method !== 'GET'), [])
})