import { describe, it, expect, beforeEach } from 'vitest'; import { createInMemoryMemoryStore, PERSONAL_SCOPE, type MemoryStore } from '@waggle/memory-contract'; import { Orchestrator } from '../src/orchestrator.js'; describe('Agent Loop with Custom Tools', () => { let memory: MemoryStore; let orchestrator: Orchestrator; beforeEach(() => { memory = createInMemoryMemoryStore(); orchestrator = new Orchestrator({ memory }); }); describe('Tool definitions', () => { it('creates all required tool definitions', () => { const names = orchestrator.getTools().map(t => t.name); expect(names).toEqual(expect.arrayContaining([ 'search_memory', 'search_all_workspaces', 'save_memory', 'get_identity', 'get_awareness', 'add_task', ])); // The knowledge graph is not part of v1 (ADR-0002). expect(names).not.toContain('query_knowledge'); expect(names).not.toContain('correct_knowledge'); }); it('each tool has name, description, parameters, and execute function', () => { for (const tool of orchestrator.getTools()) { expect(tool.name).toBeTruthy(); expect(tool.description).toBeTruthy(); expect(tool.parameters).toBeDefined(); expect(typeof tool.execute).toBe('function'); } }); }); describe('Tool execution: get_identity', () => { it('returns identity context', async () => { orchestrator.getIdentity().create({ name: 'Waggle', role: 'Personal Assistant', department: '', personality: 'Helpful', capabilities: 'Memory, search', system_prompt: 'You are Waggle.', }); const result = await orchestrator.executeTool('get_identity', {}); expect(result).toContain('Waggle'); expect(result).toContain('Personal Assistant'); }); it('returns message when no identity configured', async () => { const result = await orchestrator.executeTool('get_identity', {}); expect(result).toContain('No identity configured'); }); }); describe('Tool execution: get_awareness', () => { it('returns awareness context', async () => { orchestrator.getAwareness().add('task', 'Review PR #42', 10); const result = await orchestrator.executeTool('get_awareness', {}); expect(result).toContain('Review PR #42'); }); it('returns empty message when no items', async () => { const result = await orchestrator.executeTool('get_awareness', {}); expect(result).toContain('No active awareness items'); }); }); describe('Tool execution: save_memory', () => { it('saves a memory', async () => { const result = await orchestrator.executeTool('save_memory', { content: 'User prefers dark mode', importance: 'normal', }); expect(result).toContain('Memory saved'); }); it('writes to personal memory as an agent fact when no workspace is active', async () => { await orchestrator.executeTool('save_memory', { content: 'First memory' }); const [record] = await memory.list({ scopes: [PERSONAL_SCOPE] }); expect(record).toMatchObject({ text: 'First memory', scope: PERSONAL_SCOPE, kind: 'fact', importance: 'normal', source: 'user_stated', origin: 'agent', }); }); it('saved memory is searchable', async () => { await orchestrator.executeTool('save_memory', { content: 'The meeting about quantum computing was postponed' }); const searchResult = await orchestrator.executeTool('search_memory', { query: 'quantum' }); expect(searchResult).toContain('quantum computing'); }); }); describe('Tool execution: search_memory', () => { it('returns no results for empty memory', async () => { const result = await orchestrator.executeTool('search_memory', { query: 'anything' }); expect(result).toContain('No relevant memories'); }); it('honours the limit argument', async () => { await orchestrator.executeTool('save_memory', { content: 'Important meeting notes from today' }); await orchestrator.executeTool('save_memory', { content: 'Meeting agenda for the planning review' }); const result = await orchestrator.executeTool('search_memory', { query: 'meeting', limit: 1 }); expect(result).toContain('[1]'); expect(result).not.toContain('[2]'); }); }); describe('Tool execution: add_task', () => { it('adds a task to awareness', async () => { const result = await orchestrator.executeTool('add_task', { content: 'Deploy v2.1', priority: 8, }); expect(result).toContain('Deploy v2.1'); expect(result).toContain('priority: 8'); const tasks = orchestrator.getAwareness().getByCategory('task'); expect(tasks).toHaveLength(1); }); }); describe('System prompt builder', () => { it('builds system prompt from identity + awareness', async () => { orchestrator.getIdentity().create({ name: 'Waggle', role: 'Assistant', department: '', personality: '', capabilities: '', system_prompt: '', }); orchestrator.getAwareness().add('task', 'Check emails', 5); const prompt = await orchestrator.buildSystemPrompt(); expect(prompt).toContain('Waggle'); expect(prompt).toContain('Check emails'); expect(prompt).toContain('# Self-Awareness'); }); it('builds prompt without identity', async () => { const prompt = await orchestrator.buildSystemPrompt(); expect(prompt).toContain('# Self-Awareness'); expect(prompt).not.toContain('# Identity'); }); it('reports the number of readable memories', async () => { await orchestrator.executeTool('save_memory', { content: 'First fact about the launch plan' }); await orchestrator.executeTool('save_memory', { content: 'Second fact about the hiring plan' }); const prompt = await orchestrator.buildSystemPrompt(); expect(prompt).toContain('Memory: 2 memories.'); }); }); describe('Unknown tool handling', () => { it('throws for unknown tool name', async () => { await expect(orchestrator.executeTool('nonexistent', {})).rejects.toThrow('Unknown tool: nonexistent'); }); }); describe('E4: Auto-save style detection', () => { it('saves explicit user preferences to personal memory', async () => { const saved = await orchestrator.autoSaveFromExchange( 'I prefer bullet-point summaries for everything', 'Got it! I will use bullet points going forward.' ); expect(saved.length).toBeGreaterThan(0); expect(saved[0]).toContain('User preference'); const searchResult = await orchestrator.executeTool('search_memory', { query: 'preference' }); expect(searchResult).toContain('bullet'); }); it('detects implicit style signals from user messages', async () => { // Phrasing that triggers implicit style detection, not explicit preference patterns const saved = await orchestrator.autoSaveFromExchange( 'Can you present this data in a table format with columns for each category?', 'Here is the table: ...' ); expect(saved.length).toBeGreaterThan(0); expect(saved.some(s => s.includes('Style note'))).toBe(true); }); it('does not duplicate style notes on repeated detection', async () => { await orchestrator.autoSaveFromExchange( 'Just show me the code examples please', 'Here is the code: ...' ); const saved2 = await orchestrator.autoSaveFromExchange( 'Can you just show me the code for the other function too?', 'Here is the other code: ...' ); // The second detection is neither reported nor written again. expect(saved2.filter(s => s.includes('Style note'))).toHaveLength(0); const notes = (await memory.list({ scopes: [PERSONAL_SCOPE] })).filter(r => r.text.startsWith('Style note')); expect(notes).toHaveLength(1); }); }); describe('E4: Personal preferences in context', () => { it('includes personal preferences in loadRecentContext without a workspace', async () => { await orchestrator.autoSaveFromExchange( 'I always want concise answers from now on', 'Understood, I will keep things concise.' ); const ctx = await orchestrator.loadRecentContext(); expect(ctx).toContain('## Recent Personal Memory'); expect(ctx).toContain('## Personal Preferences'); expect(ctx).toContain('I always want concise answers'); }); it('keeps personal preferences next to workspace memory when a workspace is active', async () => { await orchestrator.autoSaveFromExchange( 'I always want concise answers from now on', 'Understood, I will keep things concise.' ); orchestrator.setWorkspace('ws-1'); await orchestrator.executeTool('save_memory', { content: 'The launch review is on Thursday' }); const ctx = await orchestrator.loadRecentContext(); expect(ctx).toContain('## Recent Workspace Memory'); expect(ctx).toContain('The launch review is on Thursday'); expect(ctx).toContain('## Personal Preferences (across all workspaces)'); expect(ctx).toContain('I always want concise answers'); }); }); });