import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { mkdtempSync, rmSync } from 'fs'; import { tmpdir } from 'os'; import { join } from 'path'; import { createHandoff, listHandoffs, getHandoff } from '../src/core/services/handoffService.js'; import { Index } from '../src/core/index.js'; describe('handoffService', () => { let cwd: string; beforeEach(() => { cwd = mkdtempSync(join(tmpdir(), 'ah-hof-')); }); afterEach(() => { rmSync(cwd, { recursive: true, force: true }); }); it('creates and reads a handoff', () => { const h = createHandoff(cwd, { fromRole: 'architect', toRole: 'implementer', summary: 's', context: 'c' }); expect(h.id).toBe('HOF-0001'); expect(getHandoff(cwd, h.id).handoff.summary).toBe('s'); expect(listHandoffs(cwd)).toHaveLength(1); }); it('listHandoffs carries fromRole and toRole in the index entry', () => { createHandoff(cwd, { fromRole: 'architect', toRole: 'implementer', summary: 'Hand over design', context: 'done', }); const items = listHandoffs(cwd); expect(items).toHaveLength(1); expect(items[0].fromRole).toBe('architect'); expect(items[0].toRole).toBe('implementer'); // agent names default to undefined when not supplied expect(items[0].fromAgent == null).toBe(true); expect(items[0].toAgent == null).toBe(true); }); it('listHandoffs carries fromAgent and toAgent when supplied', () => { createHandoff(cwd, { fromRole: 'reviewer', toRole: 'implementer', fromAgent: 'claude', toAgent: 'codex', summary: 'Review complete', context: 'lgtm', }); const items = listHandoffs(cwd); expect(items[0].fromAgent).toBe('claude'); expect(items[0].toAgent).toBe('codex'); }); it('listHandoffsByTask returns only handoffs linked to that taskId', () => { createHandoff(cwd, { fromRole: 'architect', toRole: 'implementer', taskId: 'TSK-0001', summary: 'For 0001' }); createHandoff(cwd, { fromRole: 'reviewer', toRole: 'implementer', taskId: 'TSK-0002', summary: 'For 0002' }); createHandoff(cwd, { fromRole: 'tester', toRole: 'implementer', summary: 'No task' }); const index = new Index(cwd); const results = index.listHandoffsByTask('TSK-0001'); index.close(); expect(results).toHaveLength(1); expect(results[0].title).toBe('For 0001'); }); it('listHandoffsByTask returns empty when no handoff links to the task', () => { createHandoff(cwd, { fromRole: 'architect', toRole: 'implementer', taskId: 'TSK-0002', summary: 'Other' }); const index = new Index(cwd); const results = index.listHandoffsByTask('TSK-0001'); index.close(); expect(results).toHaveLength(0); }); });