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

kill-switch.test.ts

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

const STUB = `
const store = globalThis.__factoryStore ??= new Map();
globalThis.__factoryWrites ??= { n: 0 };
export const packageStorage = () => ({
  async get(k) { return store.get(k) ?? null },
  async set(k, v) { globalThis.__factoryWrites.n++; store.set(k, JSON.stringify(v)) },
});
export const packageContext = { appBasePath: '/apps/factory/' };
export async function credential() { throw new Error('no credentials in tests') }
export async function credentials() { return [] }
export const kody = {};
export const workflows = null;
`
registerHooks({
	resolve(spec, ctx, next) {
		if (spec === 'kody:runtime') {
			return { url: `data:text/javascript,${encodeURIComponent(STUB)}`, shortCircuit: true }
		}
		return next(spec, ctx)
	},
})

const { sweep } = await import('./orchestrator.ts')
const { patchSettings, loadSettings } = await import('../lib/settings.ts')
const { seedKentProjects } = await import('../projects/config.ts')

await seedKentProjects()

test('sweep with killSwitch off behaves normally (scans items, holds lock)', async () => {
	await patchSettings({ killSwitch: false })
	const result = await sweep({ dryRun: true })
	assert.ok(result.lockHeld, 'Lock should be acquired when killSwitch is off')
	assert.ok(result.errors.length === 0 || !result.errors.some((e) => e.includes('kill switch')))
})

test('sweep with killSwitch on returns immediately with logged reason', async () => {
	await patchSettings({ killSwitch: true })
	const result = await sweep({ dryRun: true })
	assert.ok(!result.lockHeld, 'Lock should not be acquired when killSwitch is on')
	assert.ok(result.errors.some((e) => e.includes('kill switch')), 'Error message should mention kill switch')
	assert.equal(result.advanced.length, 0, 'No items should be advanced')
	assert.equal(result.started.length, 0, 'No items should be started')
	assert.equal(result.polled.length, 0, 'No items should be polled')
	await patchSettings({ killSwitch: false })
})

test('sweep with killSwitch off: existing sweep result shape is unchanged', async () => {
	await patchSettings({ killSwitch: false })
	const result = await sweep({ dryRun: true })
	assert.ok('scanned' in result, 'result has scanned')
	assert.ok('advanced' in result, 'result has advanced')
	assert.ok('started' in result, 'result has started')
	assert.ok('polled' in result, 'result has polled')
	assert.ok('needsKent' in result, 'result has needsKent')
	assert.ok('needsReview' in result, 'result has needsReview')
	assert.ok('skipped' in result, 'result has skipped')
	assert.ok('outOfScope' in result, 'result has outOfScope')
	assert.ok('suppressed' in result, 'result has suppressed')
	assert.ok('postMerge' in result, 'result has postMerge')
	assert.ok('shadowMode' in result, 'result has shadowMode')
	assert.ok('lockHeld' in result, 'result has lockHeld')
	assert.ok('errors' in result, 'result has errors')
})

test('sweep with killSwitch on preserves scanned count', async () => {
	await patchSettings({ killSwitch: true })
	const result = await sweep({ dryRun: true })
	assert.ok(typeof result.scanned === 'number')
	await patchSettings({ killSwitch: false })
})

test('per-project paused flag defaults to undefined/false', async () => {
	const { loadProjects } = await import('../projects/config.ts')
	const projects = await loadProjects()
	const kody = projects.projects['kody']
	assert.ok(kody, 'kody project should exist')
	assert.ok(kody.paused === undefined || kody.paused === false, 'paused should default to false/undefined')
})