← 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
/**
* Tests for the signals core: routing request building, template rendering,
* and type constants (pure functions only, no kody:runtime).
*/
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { buildRoutingRequestBody, parseRoutingChoice, ROUTING_MODEL, ROUTING_RULES, usesRoutingLadder } from './routing/request.ts'
import { DEFAULT_CONFIDENCE_THRESHOLD, DESTINATION_KINDS } from './routing/types.ts'
import { MAX_REDACTED_PAYLOAD_SIZE, MAX_RECORDS } from './records/types.ts'
import { SOURCE_KINDS } from './sources/types.ts'
const defaultCriteria = {
create_work_item: { label: 'Create a work item', destination: 'board-card' as const, promptTemplateId: null },
wake_grok: { label: 'Wake Grok Bot', destination: 'grok-wake' as const, promptTemplateId: null },
ignore: { label: 'No action needed', destination: 'none' as const, promptTemplateId: null },
}
function testConfig(projectId: string) {
return {
projectId,
questions: [{ key: 'actionability', label: 'Actionable', description: 'Is this signal actionable?' }],
criteria: defaultCriteria,
confidenceThreshold: DEFAULT_CONFIDENCE_THRESHOLD,
enabled: true,
}
}
function renderTemplate(template: string, ctx: Record<string, unknown>): string {
return template.replace(/\{\{(\w+)\}\}/g, (_, key: string) => {
const val = ctx[key]
if (Array.isArray(val)) return val.join(', ')
return String(val ?? '')
})
}
// --- Routing request tests ---
test('buildRoutingRequestBody asks one noul per ladder rule for default criteria', () => {
const config = testConfig('kody')
const signal = { title: 'Bug in login', body: 'Users cannot log in', source: 'sentry', labels: ['sentry'], severity: 'high' }
const body = buildRoutingRequestBody(signal, config)
assert.equal(body.model, ROUTING_MODEL)
assert.ok(usesRoutingLadder(config))
assert.deepEqual(Object.keys(body.input.questions), ROUTING_RULES.map((r) => r.key))
for (const q of Object.values(body.input.questions) as Array<Record<string, unknown>>) {
assert.equal(q.type, 'noul')
assert.match(String(q.instructions), /untrusted data/)
}
})
test('buildRoutingRequestBody keeps one choice question for custom criteria', () => {
const config = { ...testConfig('kody'), criteria: { page_oncall: { label: 'Page on-call', destination: 'grok-wake' as const, promptTemplateId: null }, file_it: { label: 'File it', destination: 'board-card' as const, promptTemplateId: null } } }
const body = buildRoutingRequestBody({ title: 'T', body: 'B', source: 'manual', labels: [] }, config)
const q = (body.input.questions as Record<string, Record<string, unknown>>).routing
assert.equal(q?.type, 'choice')
assert.deepEqual(Object.keys(q?.criteria as object).sort(), ['file_it', 'page_oncall'])
})
function routingNouls(yes: Record<string, number>) {
return Object.fromEntries(ROUTING_RULES.map((r) => [r.key, { type: 'noul', noul: yes[r.key] ?? 0.05 }]))
}
test('routing ladder: urgent facts win over noise facts', () => {
const parsed = parseRoutingChoice({ answers: routingNouls({ site_down: 0.8, spam_or_probe: 0.9 }) })
assert.deepEqual(parsed, { value: 'wake_grok', confidence: 0.8, decidedBy: 'site_down' })
})
test('routing ladder: noise fact routes to ignore', () => {
const parsed = parseRoutingChoice({ result: { result: { answers: routingNouls({ marked_no_action: 0.7 }) } } })
assert.deepEqual(parsed, { value: 'ignore', confidence: 0.7, decidedBy: 'marked_no_action' })
})
test('routing ladder: no yes defaults to create_work_item with lowest P(no) as confidence', () => {
const parsed = parseRoutingChoice({ answers: routingNouls({ no_problem: 0.3 }) })
assert.deepEqual(parsed, { value: 'create_work_item', confidence: 0.7, decidedBy: 'default' })
})
test('routing ladder: a missing noul answer returns null', () => {
const answers = routingNouls({})
delete (answers as Record<string, unknown>).site_down
assert.equal(parseRoutingChoice({ answers }), null)
})
test('buildRoutingRequestBody passes signal state correctly', () => {
const config = testConfig('kody')
const signal = { title: 'Test', body: 'Body text', source: 'manual', labels: ['test'], severity: 'low' }
const body = buildRoutingRequestBody(signal, config)
assert.equal(body.input.state.title, 'Test')
assert.equal(body.input.state.body, 'Body text')
assert.equal(body.input.state.source, 'manual')
assert.deepEqual(body.input.state.labels, ['test'])
assert.equal(body.input.state.severity, 'low')
assert.equal(body.input.state.projectId, 'kody')
})
test('buildRoutingRequestBody truncates body to 4000 chars', () => {
const config = testConfig('kody')
const longBody = 'x'.repeat(5000)
const body = buildRoutingRequestBody({ title: 'Test', body: longBody, source: 'manual', labels: [] }, config)
assert.ok(body.input.state.body.length <= 4000)
})
test('buildRoutingRequestBody defaults severity to medium', () => {
const config = testConfig('kody')
const body = buildRoutingRequestBody({ title: 'Test', body: '', source: 'manual', labels: [] }, config)
assert.equal(body.input.state.severity, 'medium')
})
test('parseRoutingChoice reads answers.routing.choice from direct shape', () => {
const parsed = parseRoutingChoice({
answers: { routing: { type: 'choice', choice: 'create_work_item', confidence: 0.92 } },
})
assert.deepEqual(parsed, { value: 'create_work_item', confidence: 0.92 })
})
test('parseRoutingChoice reads from async v4 envelope', () => {
const parsed = parseRoutingChoice({
result: {
state: 'Completed',
result: {
model: 'jev-1.13.0',
answers: { routing: { type: 'choice', choice: 'ignore', confidence: 0.85 } },
},
},
})
assert.deepEqual(parsed, { value: 'ignore', confidence: 0.85 })
})
test('parseRoutingChoice reads from response envelope', () => {
const parsed = parseRoutingChoice({
response: {
answers: { routing: { choice: 'wake_grok', confidence: 0.6 } },
},
})
assert.deepEqual(parsed, { value: 'wake_grok', confidence: 0.6 })
})
test('parseRoutingChoice returns null for missing answers', () => {
assert.equal(parseRoutingChoice({}), null)
assert.equal(parseRoutingChoice({ answers: {} }), null)
assert.equal(parseRoutingChoice(null), null)
assert.equal(parseRoutingChoice('string'), null)
assert.equal(parseRoutingChoice(42), null)
})
test('parseRoutingChoice defaults confidence to 0 when missing', () => {
const parsed = parseRoutingChoice({
answers: { routing: { choice: 'ignore' } },
})
assert.deepEqual(parsed, { value: 'ignore', confidence: 0 })
})
test('parseRoutingChoice reads value field as fallback', () => {
const parsed = parseRoutingChoice({
answers: { routing: { value: 'create_work_item', confidence: 0.75 } },
})
assert.deepEqual(parsed, { value: 'create_work_item', confidence: 0.75 })
})
// --- Config tests ---
test('default criteria map has three destinations', () => {
assert.equal(defaultCriteria.create_work_item.destination, 'board-card')
assert.equal(defaultCriteria.wake_grok.destination, 'grok-wake')
assert.equal(defaultCriteria.ignore.destination, 'none')
})
// --- Template tests ---
test('renderTemplate substitutes variables', () => {
const ctx = {
title: 'Bug report', body: 'Login is broken', source: 'sentry',
sourceId: 'my-sentry', projectId: 'kody', severity: 'high',
labels: ['bug', 'auth'], receivedAt: '2024-01-01T00:00:00Z', signalId: 'sig_abc123',
}
const result = renderTemplate('Title: {{title}}, Source: {{source}}, Labels: {{labels}}', ctx)
assert.equal(result, 'Title: Bug report, Source: sentry, Labels: bug, auth')
})
test('renderTemplate handles unknown variables gracefully', () => {
const ctx = { title: 'Test' }
const result = renderTemplate('{{title}} - {{unknown}}', ctx)
assert.equal(result, 'Test - ')
})
// --- Types tests ---
test('SOURCE_KINDS includes expected values', () => {
assert.ok(SOURCE_KINDS.includes('generic-webhook'))
assert.ok(SOURCE_KINDS.includes('sentry'))
assert.ok(SOURCE_KINDS.includes('github-issues'))
assert.ok(SOURCE_KINDS.includes('discord'))
assert.ok(SOURCE_KINDS.includes('manual'))
assert.ok(SOURCE_KINDS.includes('friction'))
assert.ok(SOURCE_KINDS.includes('email'))
assert.ok(SOURCE_KINDS.includes('x'))
})
test('DESTINATION_KINDS includes expected values', () => {
assert.ok(DESTINATION_KINDS.includes('board-card'))
assert.ok(DESTINATION_KINDS.includes('grok-wake'))
assert.ok(DESTINATION_KINDS.includes('none'))
assert.equal(DESTINATION_KINDS.length, 3)
})
test('MAX_REDACTED_PAYLOAD_SIZE is a reasonable limit', () => {
assert.equal(MAX_REDACTED_PAYLOAD_SIZE, 4096)
})
test('DEFAULT_CONFIDENCE_THRESHOLD is between 0 and 1', () => {
assert.ok(DEFAULT_CONFIDENCE_THRESHOLD > 0)
assert.ok(DEFAULT_CONFIDENCE_THRESHOLD < 1)
assert.equal(DEFAULT_CONFIDENCE_THRESHOLD, 0.7)
})
test('ROUTING_MODEL is typesafe/jev', () => {
assert.equal(ROUTING_MODEL, 'typesafe/jev')
})
// --- Storage cap tests ---
test('MAX_RECORDS cap is 500', () => {
assert.equal(MAX_RECORDS, 500)
})
test('MAX_REDACTED_PAYLOAD_SIZE is 4 KB', () => {
assert.equal(MAX_REDACTED_PAYLOAD_SIZE, 4096)
})