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

board.test.ts

322 lines · 10.1 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();
export const packageStorage = () => ({
  async get(k) { return store.get(k) ?? null },
  async set(k, v) { store.set(k, JSON.stringify(v)) },
});
export const packageContext = { appBasePath: '/apps/factory/' };
export async function credential(key) { throw new Error('credential not mapped'); }
export async function credentials() { return [] }
`
registerHooks({
	resolve(spec, ctx, next) {
		if (spec === 'kody:runtime') return { url: `data:text/javascript,${encodeURIComponent(STUB)}`, shortCircuit: true }
		return next(spec, ctx)
	},
})

const {
	boardColumnFor,
	reviewSubstateFor,
	columnEntryTime,
	stallInfo,
	validateMove,
	formatAge,
	toBoardCard,
	toPlanCard,
	defaultColumnTimeLimits,
	suggestAction,
} = await import('./board.ts')
const { createPlan } = await import('./plan.ts')
type WorkItem = import('./work-item.ts').WorkItem

function makeItem(overrides: Partial<WorkItem> = {}): WorkItem {
	const ts = overrides.createdAt ?? new Date().toISOString()
	return {
		id: 'wi_test1',
		projectId: 'kody',
		title: 'Test item',
		body: '',
		state: 'intake',
		priority: 2,
		needsKent: false,
		rank: 5,
		source: 'manual',
		externalId: null,
		fingerprint: 'test-fp',
		signalIds: [],
		labels: [],
		repoRef: 'kody-hq/kody',
		planCardId: null,
		runId: null,
		createdAt: ts,
		updatedAt: ts,
		events: [{ at: ts, from: null, to: 'intake' as const, reason: 'created', actor: 'system' as const }],
		shadow: false,
		...overrides,
	}
}

test('boardColumnFor: intake', () => {
	assert.equal(boardColumnFor(makeItem({ state: 'intake' })), 'intake')
})

test('boardColumnFor: triaged', () => {
	assert.equal(boardColumnFor(makeItem({ state: 'triaged' })), 'triaged')
})

test('boardColumnFor: planned', () => {
	assert.equal(boardColumnFor(makeItem({ state: 'planned' })), 'planned')
})

test('boardColumnFor: running without PR', () => {
	assert.equal(boardColumnFor(makeItem({ state: 'running', runId: 'run_123' })), 'running')
})

test('boardColumnFor: running with PR -> in-review', () => {
	assert.equal(
		boardColumnFor(makeItem({
			state: 'running',
			runId: 'run_123',
			pr: { url: 'https://github.com/kody-hq/kody/pull/1', number: 1, reviewState: 'pending', ready: false, updatedAt: new Date().toISOString() },
		})),
		'in-review',
	)
})

test('boardColumnFor: verifying -> in-review', () => {
	assert.equal(
		boardColumnFor(makeItem({
			state: 'verifying',
			pr: { url: 'https://github.com/kody-hq/kody/pull/1', number: 1, reviewState: 'approved', ready: true, updatedAt: new Date().toISOString() },
		})),
		'in-review',
	)
})

test('boardColumnFor: shipped with active post-merge watch -> watching-main', () => {
	assert.equal(
		boardColumnFor(makeItem({
			state: 'shipped',
			postMerge: { repoRef: 'kody-hq/kody', prUrl: 'https://github.com/kody-hq/kody/pull/1', mergeCommit: 'a'.repeat(40), startedAt: new Date().toISOString(), phase: 'watching', implementerRunId: null },
		})),
		'watching-main',
	)
})

test('boardColumnFor: shipped with implementer-notified -> watching-main', () => {
	assert.equal(
		boardColumnFor(makeItem({
			state: 'shipped',
			postMerge: { repoRef: 'kody-hq/kody', prUrl: 'https://github.com/kody-hq/kody/pull/1', mergeCommit: 'a'.repeat(40), startedAt: new Date().toISOString(), phase: 'implementer-notified', implementerRunId: 'run_x' },
		})),
		'watching-main',
	)
})

test('boardColumnFor: shipped without post-merge -> shipped', () => {
	assert.equal(boardColumnFor(makeItem({ state: 'shipped' })), 'shipped')
})

test('boardColumnFor: shipped with green post-merge -> shipped', () => {
	assert.equal(
		boardColumnFor(makeItem({
			state: 'shipped',
			postMerge: { repoRef: 'kody-hq/kody', prUrl: 'https://github.com/kody-hq/kody/pull/1', mergeCommit: 'a'.repeat(40), startedAt: new Date().toISOString(), phase: 'green', implementerRunId: null },
		})),
		'shipped',
	)
})

test('boardColumnFor: needs-kent', () => {
	assert.equal(boardColumnFor(makeItem({ state: 'needs-kent' })), 'needs-kent')
})

test('boardColumnFor: dropped', () => {
	assert.equal(boardColumnFor(makeItem({ state: 'dropped' })), 'dropped')
})

test('reviewSubstateFor: no PR returns null', () => {
	assert.equal(reviewSubstateFor(makeItem({ state: 'running' })), null)
})

test('reviewSubstateFor: CI failing', () => {
	assert.equal(
		reviewSubstateFor(makeItem({
			state: 'running',
			pr: {
				url: 'https://github.com/kody-hq/kody/pull/1', number: 1,
				reviewState: 'pending', ready: false, updatedAt: new Date().toISOString(),
				github: { state: 'open', merged: false, mergedAt: null, headSha: 'abc', checks: 'failure', readAt: new Date().toISOString() },
			},
		})),
		'needs_attention',
	)
})

test('reviewSubstateFor: CI green + approved + ready -> ready_to_merge', () => {
	assert.equal(
		reviewSubstateFor(makeItem({
			state: 'running',
			pr: {
				url: 'https://github.com/kody-hq/kody/pull/1', number: 1,
				reviewState: 'approved', ready: true, updatedAt: new Date().toISOString(),
				github: { state: 'open', merged: false, mergedAt: null, headSha: 'abc', checks: 'success', readAt: new Date().toISOString() },
			},
		})),
		'ready_to_merge',
	)
})

test('reviewSubstateFor: CI green + no review -> action_available', () => {
	assert.equal(
		reviewSubstateFor(makeItem({
			state: 'running',
			pr: {
				url: 'https://github.com/kody-hq/kody/pull/1', number: 1,
				reviewState: 'none', ready: false, updatedAt: new Date().toISOString(),
				github: { state: 'open', merged: false, mergedAt: null, headSha: 'abc', checks: 'success', readAt: new Date().toISOString() },
			},
		})),
		'action_available',
	)
})

test('reviewSubstateFor: changes requested -> needs_attention', () => {
	assert.equal(
		reviewSubstateFor(makeItem({
			state: 'running',
			pr: {
				url: 'https://github.com/kody-hq/kody/pull/1', number: 1,
				reviewState: 'changes_requested', ready: false, updatedAt: new Date().toISOString(),
			},
		})),
		'needs_attention',
	)
})

test('reviewSubstateFor: CI pending -> waiting', () => {
	assert.equal(
		reviewSubstateFor(makeItem({
			state: 'running',
			pr: {
				url: 'https://github.com/kody-hq/kody/pull/1', number: 1,
				reviewState: 'pending', ready: false, updatedAt: new Date().toISOString(),
				github: { state: 'open', merged: false, mergedAt: null, headSha: 'abc', checks: 'pending', readAt: new Date().toISOString() },
			},
		})),
		'waiting',
	)
})

test('stallInfo: not stalled', () => {
	const r = stallInfo(1000, 10000)
	assert.equal(r.stalled, false)
	assert.equal(r.stalledBy, 0)
	assert.equal(r.warm, false)
})

test('stallInfo: warm at 75%', () => {
	const r = stallInfo(7500, 10000)
	assert.equal(r.stalled, false)
	assert.equal(r.warm, true)
})

test('stallInfo: stalled at 100%', () => {
	const r = stallInfo(15000, 10000)
	assert.equal(r.stalled, true)
	assert.equal(r.stalledBy, 5000)
})

test('stallInfo: zero limit never stalls', () => {
	const r = stallInfo(99999, 0)
	assert.equal(r.stalled, false)
})

test('columnEntryTime: intake uses createdAt', () => {
	const item = makeItem({ createdAt: '2024-01-01T00:00:00Z' })
	assert.equal(columnEntryTime(item, 'intake'), '2024-01-01T00:00:00Z')
})

test('columnEntryTime: watching-main uses postMerge.startedAt', () => {
	const item = makeItem({
		state: 'shipped',
		postMerge: { repoRef: 'kody-hq/kody', prUrl: 'x', mergeCommit: 'a'.repeat(40), startedAt: '2024-06-15T10:00:00Z', phase: 'watching', implementerRunId: null },
	})
	assert.equal(columnEntryTime(item, 'watching-main'), '2024-06-15T10:00:00Z')
})

test('columnEntryTime: triaged uses event', () => {
	const item = makeItem({
		state: 'triaged',
		events: [
			{ at: '2024-01-01T00:00:00Z', from: null, to: 'intake', reason: 'created', actor: 'system' },
			{ at: '2024-01-02T00:00:00Z', from: 'intake', to: 'triaged', reason: 'triage', actor: 'sweep' },
		],
	})
	assert.equal(columnEntryTime(item, 'triaged'), '2024-01-02T00:00:00Z')
})

test('validateMove: valid moves', () => {
	assert.equal(validateMove('intake', 'triaged'), null)
	assert.equal(validateMove('intake', 'needs-kent'), null)
	assert.equal(validateMove('intake', 'dropped'), null)
	assert.equal(validateMove('needs-kent', 'intake'), null)
	assert.equal(validateMove('dropped', 'intake'), null)
})

test('validateMove: invalid moves', () => {
	assert.ok(validateMove('running', 'shipped'))
	assert.ok(validateMove('intake', 'running'))
	assert.ok(validateMove('shipped', 'intake'))
	assert.ok(validateMove('intake', 'intake'))
})

test('formatAge', () => {
	assert.equal(formatAge(30_000), 'just now')
	assert.equal(formatAge(5 * 60_000), '5m')
	assert.equal(formatAge(3 * 3600_000 + 12 * 60_000), '3h 12m')
	assert.equal(formatAge(48 * 3600_000), '2d')
	assert.equal(formatAge(50 * 3600_000), '2d 2h')
})

test('toBoardCard: stalled card', () => {
	const limits = defaultColumnTimeLimits()
	const old = new Date(Date.now() - 3 * 3600_000).toISOString()
	const item = makeItem({ createdAt: old, state: 'intake' })
	const card = toBoardCard(item, limits, Date.now(), null)
	assert.equal(card.column, 'intake')
	assert.equal(card.stalled, true)
	assert.ok(card.stalledBy > 0)
})

test('toBoardCard: not stalled card', () => {
	const limits = defaultColumnTimeLimits()
	const item = makeItem({ createdAt: new Date().toISOString(), state: 'intake' })
	const card = toBoardCard(item, limits, Date.now(), null)
	assert.equal(card.stalled, false)
})

test('toPlanCard: stalled plan', () => {
	const limits = defaultColumnTimeLimits()
	const old = new Date(Date.now() - 31 * 86400_000).toISOString()
	const plan = createPlan({ title: 'Old plan', createdAt: old })
	const card = toPlanCard(plan, limits, Date.now())
	assert.equal(card.column, 'exploring')
	assert.equal(card.stalled, true)
})

test('suggestAction: in-review ready_to_merge', () => {
	const limits = defaultColumnTimeLimits()
	const item = makeItem({
		state: 'running',
		pr: { url: 'https://github.com/kody-hq/kody/pull/1', number: 1, reviewState: 'approved', ready: true, updatedAt: new Date().toISOString() },
	})
	const card = toBoardCard(item, limits, Date.now(), null)
	const action = suggestAction(card)
	assert.ok(action.includes('merge'))
})