import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import { registerHooks } from 'node:module' import { test } from 'node:test' type FetchCall = { url: string; init?: RequestInit } const STUB = ` const g = globalThis.__docsFreshness ??= { kv: new Map(), github: [], fetches: [], credentials: [] }; export const packageStorage = () => ({ async get(k) { return g.kv.get(k) ?? null }, async set(k, v) { g.kv.set(k, JSON.stringify(v)) }, }); export const packageContext = { appBasePath: '/apps/factory/' }; export async function credential(key) { g.credentials.push(key); if (key !== 'runner') { const e = new Error('unbound: ' + key); e.code = 'credential_unbound'; throw e; } return { name: 'runner', fetch: async (url, init) => { g.fetches.push({ url, init }); return new Response(JSON.stringify({ session_id: 'devin-session-docs', url: 'https://app.devin.ai/sessions/devin-session-docs', devin_mode: 'fusion', }), { status: 200, headers: { 'Content-Type': 'application/json' } }); }, }; } export async function credentials() { return [] } export const kody = {}; export const workflows = null; ` const GH_STUB = ` export default async function githubRequest({ path }) { const g = globalThis.__docsFreshness ??= { kv: new Map(), github: [], fetches: [], credentials: [] }; g.github.push(path); if (String(path).includes('/branches/')) { return { ok: true, status: 200, data: { commit: { sha: 'abc123deadbeefcafebabe' } } }; } if (String(path).includes('/commits?')) { return { ok: true, status: 200, data: [{ sha: 'abc123deadbeefcafebabe' }] }; } return { ok: true, status: 200, data: {} }; } ` const CURSOR_BAN = `throw new Error('kody:@kody/cursor must not load on the docs-freshness path')` registerHooks({ resolve(spec, ctx, next) { if (spec === 'kody:runtime') { return { url: `data:text/javascript,${encodeURIComponent(STUB)}`, shortCircuit: true } } if (spec === 'kody:@kody/github/request') { return { url: `data:text/javascript,${encodeURIComponent(GH_STUB)}`, shortCircuit: true } } if (spec === 'kody:@kody/cursor/agents' || spec.startsWith('kody:@kody/cursor')) { return { url: `data:text/javascript,${encodeURIComponent(CURSOR_BAN)}`, shortCircuit: true } } if (spec.startsWith('kody:')) { const generic = 'export default async function() { return { ok: true, answers: {} } }\nexport async function evaluate() { return { ok: true, answers: {} } }' return { url: `data:text/javascript,${encodeURIComponent(generic)}`, shortCircuit: true } } return next(spec, ctx) }, }) const { launcherOf, usesPrivateReportFooter } = await import('./shared.ts') const { resolvePrompt } = await import('./prompts.ts') const { set, get } = await import('./config.ts') const { setDispatchForTests } = await import('../events/emit.ts') const emitted: Array<{ topic: string; idempotencyKey: string; payload: Record }> = [] setDispatchForTests(async (i) => { emitted.push(i as never); return { status: 'ok' } }) const runNow = (await import('./run-devin.ts')).default const { launchOrPreviewDevin } = await import('./devin-launch.ts') type Harness = { kv: Map github: string[] fetches: FetchCall[] credentials: string[] } const state = () => (globalThis as unknown as { __docsFreshness: Harness }).__docsFreshness async function seedDocsFreshness() { state().kv.clear() state().github.length = 0 state().fetches.length = 0 state().credentials.length = 0 emitted.length = 0 await set({ automation: 'docs-freshness', enabled: true, repos: ['kody-hq/kody'] }) } test('docs-freshness launches Devin and keeps the shipping footer', () => { assert.equal(launcherOf('docs-freshness'), 'devin') assert.equal(usesPrivateReportFooter('docs-freshness'), false) assert.equal(usesPrivateReportFooter('general-audit'), true) const prompt = resolvePrompt({ automation: 'docs-freshness', repo: 'kody-hq/kody', branch: 'main', }) assert.match(prompt, /open or update the PR yourself/) assert.doesNotMatch(prompt, /Keep findings in the private report/) assert.doesNotMatch(prompt, /\u2014/) }) test('docs-freshness dryRun returns the Devin start request and never loads Cursor', async () => { await seedDocsFreshness() const result = await runNow({ automation: 'docs-freshness', dryRun: true, fullSweep: true, }) assert.equal(result.ok, true) assert.equal(result.automation, 'docs-freshness') assert.equal(result.dryRun, true) const launches = (result.results as Array<{ launches: Array> }>)[0] ?.launches assert.ok(Array.isArray(launches) && launches.length === 1) const launch = launches[0] assert.equal(launch.ok, true) assert.equal(launch.launcher, 'devin') assert.equal(launch.wouldSpawn, true) assert.equal(launch.dryRun, true) const request = launch.devinRequest as { topic: string; runner: string; repos: string[]; mode: string; prompt: string } assert.equal(request.topic, '@kody/factory.agent.start.requested') assert.equal(request.runner, 'devin') assert.deepEqual(request.repos, ['kody-hq/kody']) assert.equal(request.mode, 'fusion') assert.match(request.prompt, /open or update the PR yourself/) assert.equal(emitted.length, 0) assert.equal(state().fetches.length, 0) assert.equal(state().credentials.length, 0) }) test('docs-freshness live spawn asks the Devin runner by event and holds no Devin key', async () => { await seedDocsFreshness() const result = await launchOrPreviewDevin({ automation: 'docs-freshness', repo: 'kody-hq/kody', branch: 'main', model: { id: 'unused-on-devin' }, prompt: 'refresh stale docs and open a PR', dryRun: false, headSha: null, skipped: null, devinMode: 'fusion', maxAcuLimit: null, }) assert.equal(result.ok, true) assert.equal(result.launcher, 'devin') assert.equal(result.spawned, true) assert.match(String(result.agentId), /^pending_/) assert.equal(result.agentUrl, null) assert.deepEqual(state().credentials, []) assert.equal(state().fetches.length, 0) assert.equal(emitted.length, 1) assert.equal(emitted[0]!.topic, '@kody/factory.agent.start.requested') const p = emitted[0]!.payload as { runner: string; repos: string[]; prompt: string; mode: string; tags: string[] } assert.equal(p.runner, 'devin') assert.equal(p.mode, 'fusion') assert.ok(p.tags.includes('kody-factory')) assert.deepEqual(p.repos, ['kody-hq/kody']) assert.match(p.prompt, /refresh stale docs/) }) test('docs-freshness job graph has no static @kody/cursor import', () => { const root = dirname(fileURLToPath(import.meta.url)) const files = [ 'run-devin.ts', 'run-core.ts', 'devin-launch.ts', 'devin.ts', 'jobs/docs-freshness.ts', '../jobs/cleanup-docs-freshness.ts', ] for (const rel of files) { const source = readFileSync(join(root, rel), 'utf8') assert.doesNotMatch( source, /from ['"]kody:@kody\/cursor/, `${rel} must not import @kody/cursor`, ) } }) test('enabling docs-freshness via config does not enable sibling automations', async () => { await seedDocsFreshness() const audit = await get({ automation: 'general-audit' }) assert.equal(audit.enabled, false) assert.deepEqual(audit.repos, []) const tight = await get({ automation: 'keep-tests-tight' }) assert.equal(tight.enabled, false) })