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 { default: importPlans } = await import('../exports/plans/import.ts') const { default: listPlans } = await import('../exports/plans/list.ts') const { default: getPlan } = await import('../exports/plans/get.ts') const { default: upsertPlan } = await import('../exports/plans/upsert.ts') const { default: addProgressNote } = await import('../exports/plans/add-progress-note.ts') const { default: moveCard } = await import('../exports/move-card.ts') const { default: claim } = await import('../exports/claim.ts') const { release } = await import('../exports/claim.ts') const PROJECT = { org: 'kody-hq', number: 1, title: 'Kody plans', url: 'https://github.com/orgs/kody-hq/projects/1' } function card(id: string, overrides: Record = {}) { return { id, type: 'DRAFT_ISSUE', title: `Card ${id}`, status: 'Exploring', priority: 'P2', updatedAt: '2024-06-01T00:00:00Z', createdAt: '2024-05-01T00:00:00Z', url: `https://github.com/orgs/kody-hq/projects/1/items/${id}`, linked: null, isArchived: false, body: 'Plan body', contentId: `content_${id}`, fields: { Status: 'Exploring' }, ...overrides, } } test('import: creates plans from cards', async () => { const result = await importPlans({ project: PROJECT, cards: [card('PVTI_1'), card('PVTI_2', { status: 'Decided', priority: 'P0' })], dryRun: false, }) assert.equal(result.created, 2) assert.equal(result.updated, 0) assert.equal(result.unchanged, 0) assert.equal(result.dryRun, false) const { plans } = await listPlans() assert.equal(plans.length, 2) const decided = plans.find((p) => p.status === 'decided') assert.ok(decided) assert.equal(decided!.priority, 0) }) test('import: idempotent re-import does not duplicate', async () => { const before = (await listPlans()).plans.length const result = await importPlans({ project: PROJECT, cards: [card('PVTI_1'), card('PVTI_2', { status: 'Decided', priority: 'P0' })], dryRun: false, }) assert.equal(result.created, 0) assert.equal(result.unchanged, 2) assert.equal((await listPlans()).plans.length, before) }) test('import: updates changed fields', async () => { const result = await importPlans({ project: PROJECT, cards: [card('PVTI_1', { title: 'Updated title', status: 'Proposed' })], dryRun: false, }) assert.equal(result.updated, 1) const { plans } = await listPlans({ status: 'proposed' }) assert.ok(plans.some((p) => p.title === 'Updated title')) }) test('import: dry run does not persist', async () => { const before = (await listPlans()).plans.length const result = await importPlans({ project: PROJECT, cards: [card('PVTI_NEW_DRY')], dryRun: true, }) assert.equal(result.created, 1) assert.equal(result.dryRun, true) assert.equal((await listPlans()).plans.length, before) }) test('import: returns counts', async () => { const result = await importPlans({ project: PROJECT, cards: [card('PVTI_3'), card('PVTI_BAD', { title: '' })], dryRun: false, }) assert.equal(result.received, 2) assert.equal(result.created, 1) assert.equal(result.skipped, 1) assert.ok(result.totalPlansAfter >= 3) }) test('upsert: creates a new plan', async () => { const { plan, created } = await upsertPlan({ title: 'New plan', status: 'decided', priority: 1 }) assert.equal(created, true) assert.equal(plan.status, 'decided') assert.equal(plan.priority, 1) }) test('upsert: updates an existing plan', async () => { const { plan: first } = await upsertPlan({ title: 'To update' }) const { plan: second, created } = await upsertPlan({ id: first.id, title: 'Updated title', status: 'proposed', reason: 'reviewed' }) assert.equal(created, false) assert.equal(second.title, 'Updated title') assert.equal(second.status, 'proposed') assert.ok(second.events.length > first.events.length) }) test('get: returns a plan', async () => { const { plan: p } = await upsertPlan({ title: 'Get me' }) const { plan, found } = await getPlan({ id: p.id }) assert.equal(found, true) assert.equal(plan!.title, 'Get me') }) test('get: returns null for missing', async () => { const { found } = await getPlan({ id: 'plan_nonexistent' }) assert.equal(found, false) }) test('add-progress-note: adds a note', async () => { const { plan: p } = await upsertPlan({ title: 'Note test' }) const { plan } = await addProgressNote({ id: p.id, text: 'Made progress', date: '2024-06-15' }) assert.equal(plan.progressNotes.length, 1) assert.equal(plan.progressNotes[0]!.text, 'Made progress') assert.equal(plan.progressNotes[0]!.date, '2024-06-15') }) test('move-card: valid plan move', async () => { const { plan: p } = await upsertPlan({ title: 'Move me' }) const result = await moveCard({ kind: 'plan', id: p.id, to: 'proposed', reason: 'reviewed', dryRun: false }) assert.equal(result.moved, true) assert.equal(result.to, 'proposed') }) test('move-card: invalid plan move', async () => { const { plan: p } = await upsertPlan({ title: 'Ship attempt', status: 'shipped' }) const result = await moveCard({ kind: 'plan', id: p.id, to: 'exploring', reason: 'oops', dryRun: false }) assert.equal(result.moved, false) }) test('claim: acquires and expires', async () => { const r1 = await claim({ id: 'wi_claimtest', kind: 'work', agent: 'patch', ttlMinutes: 0, dryRun: false }) assert.equal(r1.claimed, true) // TTL 0 means it has already expired; another agent can take it const r2 = await claim({ id: 'wi_claimtest', kind: 'work', agent: 'other', ttlMinutes: 60, dryRun: false }) assert.equal(r2.claimed, true) }) test('claim: same agent re-claims', async () => { const r1 = await claim({ id: 'wi_reclaim', kind: 'work', agent: 'patch', ttlMinutes: 60, dryRun: false }) assert.equal(r1.claimed, true) const r2 = await claim({ id: 'wi_reclaim', kind: 'work', agent: 'patch', ttlMinutes: 60, dryRun: false }) assert.equal(r2.claimed, true) }) test('release: releases a claim', async () => { await claim({ id: 'wi_reltest', kind: 'work', agent: 'patch', ttlMinutes: 60, dryRun: false }) const r = await release({ id: 'wi_reltest', agent: 'patch', dryRun: false }) assert.equal(r.released, true) assert.ok(r.was) }) test('release: wrong agent cannot release', async () => { await claim({ id: 'wi_wrongrel', kind: 'work', agent: 'patch', ttlMinutes: 60, dryRun: false }) const r = await release({ id: 'wi_wrongrel', agent: 'other' }) assert.equal(r.released, false) }) test('list: filters by status and query', async () => { await upsertPlan({ title: 'Search target exploring' }) const { plans } = await listPlans({ query: 'Search target', status: 'exploring' }) assert.ok(plans.length >= 1) assert.ok(plans.every((p) => p.status === 'exploring')) })