import { describe, it, expect, beforeEach } from 'vitest'; import { createInMemoryMemoryStore, PERSONAL_SCOPE, type MemoryStore } from '@waggle/memory-contract'; import { Orchestrator, type ContextFrames } from '../src/orchestrator.js'; /** * Tests the PromptAssembler wiring points on Orchestrator: * - `loadRecentContextFrames()`: typed counterpart to loadRecentContext * - `recallMemory(query, limit, opts?)`: scoreFloor / tier options */ describe('Orchestrator PromptAssembler wiring', () => { let memory: MemoryStore; let orchestrator: Orchestrator; beforeEach(() => { memory = createInMemoryMemoryStore(); orchestrator = new Orchestrator({ memory }); }); describe('loadRecentContextFrames()', () => { it('returns well-formed ContextFrames on an empty memory', async () => { const frames: ContextFrames = await orchestrator.loadRecentContextFrames(); expect(frames.stateFrames).toEqual([]); expect(frames.recentChanges).toEqual([]); expect(frames.activeWork).toEqual([]); expect(frames.keyEntities).toEqual([]); expect(frames.personalPreferences).toEqual([]); }); it('populates activeWork from AwarenessLayer items', async () => { orchestrator.getAwareness().add('task', 'Ship PromptAssembler', 10); orchestrator.getAwareness().add('pending', 'Verify OpenRouter slugs', 5); const frames = await orchestrator.loadRecentContextFrames(); expect(frames.activeWork).toHaveLength(2); expect(frames.activeWork[0].content).toMatch(/Ship PromptAssembler|Verify OpenRouter/); expect(frames.activeWork[0].priority).toBeGreaterThanOrEqual(0); expect(['task', 'pending', 'action', 'flag']).toContain(frames.activeWork[0].category); }); it('pulls personal preferences even when no workspace is active', async () => { await orchestrator.executeTool('save_memory', { content: 'User preference: prefers concise replies', kind: 'preference', importance: 'normal', }); const frames = await orchestrator.loadRecentContextFrames(); expect(frames.personalPreferences).toEqual(['User preference: prefers concise replies']); }); it('pulls personal preferences while a workspace is active', async () => { await orchestrator.executeTool('save_memory', { content: 'User preference: prefers concise replies', kind: 'preference', }); orchestrator.setWorkspace('ws-1'); const frames = await orchestrator.loadRecentContextFrames(); expect(frames.personalPreferences).toEqual(['User preference: prefers concise replies']); // The personal preference is not workspace memory. expect([...frames.stateFrames, ...frames.recentChanges]).toEqual([]); }); it('splits by importance: critical and important → stateFrames, the rest → recentChanges', async () => { await memory.remember({ scope: PERSONAL_SCOPE, text: 'Launch date is fixed', importance: 'critical' }); await memory.remember({ scope: PERSONAL_SCOPE, text: 'Budget owner is Ana', importance: 'important' }); await memory.remember({ scope: PERSONAL_SCOPE, text: 'Standup moved to 10am', importance: 'normal' }); const ctx = await orchestrator.loadRecentContextFrames(); expect(ctx.stateFrames.map(f => f.content)).toEqual(['Launch date is fixed', 'Budget owner is Ana']); expect(ctx.recentChanges.map(f => f.content)).toEqual(['Standup moved to 10am']); }); it('respects the limit argument', async () => { for (let i = 0; i < 20; i++) { await memory.remember({ scope: PERSONAL_SCOPE, text: `State memory ${i}` }); } const ctx = await orchestrator.loadRecentContextFrames(3); expect(ctx.stateFrames.length + ctx.recentChanges.length).toBe(3); }); }); describe('recallMemory() without options', () => { it('returns empty result structure on empty memory', async () => { const result = await orchestrator.recallMemory('anything'); expect(result).toEqual({ text: '', count: 0, recalled: [], recalledFrames: [] }); }); it('renders a "# Recalled Memories" block when there are hits', async () => { await memory.remember({ scope: PERSONAL_SCOPE, text: 'The license boundary is non-negotiable', importance: 'important' }); const result = await orchestrator.recallMemory('license boundary'); expect(result.text.startsWith('# Recalled Memories\n')).toBe(true); expect(result.text).toContain('The license boundary is non-negotiable'); expect(result.count).toBe(1); expect(result.recalled).toEqual(['The license boundary is non-negotiable']); }); }); describe('recallMemory() with RecallOptions', () => { it('scoreFloor drops search hits below the floor', async () => { await memory.remember({ scope: PERSONAL_SCOPE, text: 'test memory about onboarding' }); // Word-overlap score of "test" against the memory is about 0.5. expect((await orchestrator.recallMemory('test', 5)).count).toBe(1); expect((await orchestrator.recallMemory('test', 5, { scoreFloor: 0.99 })).count).toBe(0); }); it('accepts tier hint (recorded, not acted upon in recallMemory)', async () => { const result = await orchestrator.recallMemory('test', 5, { tier: 'small' }); expect(result.count).toBe(0); }); }); });