← 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
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: ' + key); }
export async function credentials() { return [] }
export const kody = {};
`
registerHooks({
resolve(spec, ctx, next) {
if (spec === 'kody:runtime') return { url: `data:text/javascript,${encodeURIComponent(STUB)}`, shortCircuit: true }
if (spec.startsWith('kody:@kody/discord/')) {
const code = `export default async function() { throw new Error('discord not mapped'); }`
return { url: `data:text/javascript,${encodeURIComponent(code)}`, shortCircuit: true }
}
return next(spec, ctx)
},
})
const { shouldNotify, resolveRules, defaultNotificationRules } = await import('./types.ts')
const { loadNotifications, saveNotification, markRead, markAllRead, countUnread, loadRules, saveRules } = await import('./store.ts')
const { createNotification } = await import('./create.ts')
test('shouldNotify: accepts when category and severity match', () => {
const rule: import('./types.ts').NotificationRule = { categories: [], minSeverity: 'info', discordEnabled: false, wakeEnabled: false }
assert.equal(shouldNotify(rule, 'stall', 'warning'), true)
})
test('shouldNotify: rejects when category not in list', () => {
const rule: import('./types.ts').NotificationRule = { categories: ['stall', 'error'], minSeverity: 'info', discordEnabled: false, wakeEnabled: false }
assert.equal(shouldNotify(rule, 'merge', 'info'), false)
})
test('shouldNotify: rejects when severity too low', () => {
const rule: import('./types.ts').NotificationRule = { categories: [], minSeverity: 'warning', discordEnabled: false, wakeEnabled: false }
assert.equal(shouldNotify(rule, 'stall', 'info'), false)
assert.equal(shouldNotify(rule, 'stall', 'warning'), true)
assert.equal(shouldNotify(rule, 'stall', 'error'), true)
})
test('resolveRules: uses defaults when no project override', () => {
const rules = defaultNotificationRules()
const resolved = resolveRules(rules, 'unknown-project')
assert.deepEqual(resolved, rules.defaults)
})
test('resolveRules: merges project override', () => {
const rules = defaultNotificationRules()
rules.projects['my-project'] = { minSeverity: 'error', discordEnabled: true }
const resolved = resolveRules(rules, 'my-project')
assert.equal(resolved.minSeverity, 'error')
assert.equal(resolved.discordEnabled, true)
assert.equal(resolved.wakeEnabled, false)
})
test('store: save and load notifications', async () => {
const n = { id: 'notif_test1', projectId: 'kody', title: 'Test', body: 'Body', severity: 'info' as const, category: 'stall' as const, status: 'unread' as const, itemId: null, source: 'test', createdAt: new Date().toISOString() }
await saveNotification(n)
const all = await loadNotifications()
assert.ok(all.some((x) => x.id === 'notif_test1'))
})
test('store: markRead marks specific notifications', async () => {
const count = await markRead(['notif_test1'])
assert.equal(count, 1)
const all = await loadNotifications()
assert.equal(all.find((x) => x.id === 'notif_test1')?.status, 'read')
})
test('store: countUnread', async () => {
const n2 = { id: 'notif_test2', projectId: 'kody', title: 'T2', body: '', severity: 'warning' as const, category: 'error' as const, status: 'unread' as const, itemId: null, source: 'test', createdAt: new Date().toISOString() }
await saveNotification(n2)
const count = await countUnread()
assert.ok(count >= 1)
})
test('store: markAllRead', async () => {
await markAllRead()
const count = await countUnread()
assert.equal(count, 0)
})
test('store: rules save and load', async () => {
const rules = defaultNotificationRules()
rules.projects['kody'] = { minSeverity: 'warning' }
await saveRules(rules)
const loaded = await loadRules()
assert.equal(loaded.projects['kody']?.minSeverity, 'warning')
})
test('createNotification: creates and stores when rules match', async () => {
const result = await createNotification({
projectId: 'kody',
title: 'Item stalled',
body: 'wi_123 has been in Running for 8 hours',
severity: 'warning',
category: 'stall',
source: 'sweep',
})
assert.equal(result.created, true)
assert.ok(result.notification)
assert.equal(result.notification!.status, 'unread')
})
test('createNotification: skips when rules filter it out', async () => {
const rules = defaultNotificationRules()
rules.defaults.minSeverity = 'error'
await saveRules(rules)
const result = await createNotification({
projectId: 'kody',
title: 'Info event',
body: 'Not important',
severity: 'info',
category: 'state-change',
})
assert.equal(result.created, false)
assert.ok(result.skippedReason)
await saveRules(defaultNotificationRules())
})
test('createNotification: dry run does not persist', async () => {
const before = (await loadNotifications()).length
await createNotification({
projectId: 'kody',
title: 'Dry',
body: '',
severity: 'info',
category: 'stall',
dryRun: true,
})
assert.equal((await loadNotifications()).length, before)
})
test('createNotification: Discord output fails closed when credential not mapped', async () => {
const rules = defaultNotificationRules()
rules.defaults.discordEnabled = true
await saveRules(rules)
const result = await createNotification({
projectId: 'kody',
title: 'Test',
body: '',
severity: 'warning',
category: 'error',
})
const discordOut = result.outputs.find((o) => o.output === 'discord')
assert.ok(discordOut)
assert.equal(discordOut!.sent, false)
assert.ok(discordOut!.error)
await saveRules(defaultNotificationRules())
})
test('createNotification: wake output fails closed when credential not mapped', async () => {
const rules = defaultNotificationRules()
rules.defaults.wakeEnabled = true
await saveRules(rules)
const result = await createNotification({
projectId: 'kody',
title: 'Test',
body: '',
severity: 'warning',
category: 'error',
})
const wakeOut = result.outputs.find((o) => o.output === 'wake')
assert.ok(wakeOut)
assert.equal(wakeOut!.sent, false)
assert.ok(wakeOut!.error)
await saveRules(defaultNotificationRules())
})