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

events-self-test.ts

64 lines · 3.0 KB · TypeScript
import { TOPICS, emitFactoryEvent, eventKey } from '../events/emit.ts'
import { loadJson, saveJson } from '../lib/storage.ts'
import { requestWake } from '../events/wake-transport.ts'

const LIVE_GUARD_KEY = 'factory:events:self-test-live:v1'
const LIVE_COOLDOWN_MS = 24 * 60 * 60 * 1000

/**
 * Prove the wake path end to end.
 *
 * Default: emit one synthetic `@kody/factory.wake.requested` with
 * `dryRun: true, shadow: true`. The relay logs a would-deliver and never posts.
 *
 * `live: true`: emit one real request for one bot (default Patch) through
 * `@kody/wake-relay`, tracked as a pending wake. The factory never posts to a
 * bot itself, so the test wakes that bot at most once. Runs at most once per
 * 24 hours.
 *
 * @param input - `{ live?, bot?, role?, note? }`
 * @returns The request id and emit result. No secrets.
 *
 * @example
 * import selfTest from 'kody:@kody/kody-factory/events/self-test'
 * await selfTest({ role: 'package-triage' })
 */
export default async function eventsSelfTest(input: { live?: boolean; bot?: string; role?: string; note?: string } = {}) {
	const note = typeof input?.note === 'string' ? input.note.slice(0, 200) : ''
	if (input?.live === true) {
		const bot = typeof input.bot === 'string' && /^[a-z][a-z0-9_-]{0,31}$/.test(input.bot) ? input.bot : 'patch'
		const guard = await loadJson<{ at?: string; requestId?: string }>(LIVE_GUARD_KEY, {})
		if (guard.at && Date.now() - Date.parse(guard.at) < LIVE_COOLDOWN_MS) {
			return { ok: false, live: true, reason: 'a live self-test already ran in the last 24 hours', last: guard }
		}
		const at = new Date().toISOString()
		await saveJson(LIVE_GUARD_KEY, { at })
		const prompt = [
			'Factory events end-to-end self-test (approved by Kent). This wake came through @kody/wake-relay from @kody/kody-factory.',
			'No action needed, nothing to file, do not reply.',
			note ? `Note: ${note}` : '',
		].filter(Boolean).join('\n')
		const r = await requestWake(
			{ source: 'self-test', bot, prompt, reason: `factory events live self-test${note ? `: ${note}` : ''}`, ref: `self-test:${at}` },
			{ selfTest: true },
		)
		await saveJson(LIVE_GUARD_KEY, { at, requestId: r.requestId })
		return { ok: r.ok, live: true, bot, requestId: r.requestId, topic: TOPICS.wakeRequested, ...(r.detail ? { detail: r.detail } : {}) }
	}
	const roles = ['package-triage', 'platform', 'outage', 'dr', 'notification']
	const role = roles.includes(String(input?.role)) ? String(input.role) : 'package-triage'
	const at = new Date().toISOString()
	const requestId = eventKey('self-test', role, at)
	const result = await emitFactoryEvent(TOPICS.wakeRequested, `wake:${requestId}`, {
		requestId,
		role,
		...(typeof input?.bot === 'string' && /^[a-z][a-z0-9_-]{0,31}$/.test(input.bot) ? { bot: input.bot } : {}),
		source: 'self-test',
		reason: `factory events self-test${note ? `: ${note}` : ''}`,
		ref: requestId,
		prompt: 'Factory events self-test. This is a dry run; no wake should be posted.',
		dryRun: true,
		shadow: true,
	})
	return { requestId, topic: TOPICS.wakeRequested, dryRun: true, ...result }
}