/** * Combined Retrieval — merge engine tests. * * Tests the merge engine in isolation. MemorySearchLike (workspace + personal * memory search) is mocked. No server, no vault, no DB, no wiring. */ import { describe, it, expect, vi } from 'vitest'; import { CombinedRetrieval, mapMemoryResult, type MemorySearchLike, type MemorySearchResultLike, } from '../src/combined-retrieval.js'; // ── Factories ───────────────────────────────────────────────────────────── function makeMemoryResult( overrides: { id?: number; content?: string; score?: number; frameType?: string; importance?: string } = {}, ): MemorySearchResultLike { return { frame: { id: overrides.id ?? 1, content: overrides.content ?? 'Some memory content', frame_type: overrides.frameType ?? 'P', importance: overrides.importance ?? 'normal', }, finalScore: overrides.score ?? 0.5, }; } function makeSearchMock(results: MemorySearchResultLike[] = []): MemorySearchLike { return { search: vi.fn(async () => results) }; } // ── Helper unit tests ───────────────────────────────────────────────────── describe('mapMemoryResult', () => { it('maps workspace memory result with correct attribution', () => { const input = makeMemoryResult({ id: 7, content: 'decision made', score: 0.85, frameType: 'I', importance: 'important' }); const result = mapMemoryResult(input, 'workspace'); expect(result.source).toBe('workspace'); expect(result.attribution).toBe('[workspace memory]'); expect(result.content).toBe('decision made'); expect(result.score).toBe(0.85); expect(result.metadata.frameId).toBe(7); expect(result.metadata.frameType).toBe('I'); expect(result.metadata.importance).toBe('important'); }); it('maps personal memory result with correct attribution', () => { const input = makeMemoryResult({ id: 3, content: 'user prefers bullets' }); const result = mapMemoryResult(input, 'personal'); expect(result.source).toBe('personal'); expect(result.attribution).toBe('[personal memory]'); expect(result.content).toBe('user prefers bullets'); }); }); // ── CombinedRetrieval class tests ───────────────────────────────────────── describe('CombinedRetrieval', () => { it('returns workspace + personal results', async () => { const wsResults = [makeMemoryResult({ id: 1, content: 'ws fact', score: 0.9 })]; const pResults = [makeMemoryResult({ id: 2, content: 'personal fact', score: 0.8 })]; const cr = new CombinedRetrieval({ workspaceSearch: makeSearchMock(wsResults), personalSearch: makeSearchMock(pResults), }); const result = await cr.search('test'); expect(result.query).toBe('test'); expect(result.workspaceResults.map(r => r.content)).toEqual(['ws fact']); expect(result.personalResults.map(r => r.content)).toEqual(['personal fact']); }); it('searches only personal memory when scope=personal', async () => { const wsMock = makeSearchMock([makeMemoryResult({ id: 1 })]); const pMock = makeSearchMock([makeMemoryResult({ id: 2, content: 'personal only' })]); const cr = new CombinedRetrieval({ workspaceSearch: wsMock, personalSearch: pMock }); const result = await cr.search('test', { scope: 'personal' }); expect(result.workspaceResults).toHaveLength(0); expect(result.personalResults).toHaveLength(1); expect(wsMock.search).not.toHaveBeenCalled(); }); it('searches only workspace memory when scope=workspace', async () => { const wsMock = makeSearchMock([makeMemoryResult({ id: 1, content: 'ws only' })]); const pMock = makeSearchMock([makeMemoryResult({ id: 2 })]); const cr = new CombinedRetrieval({ workspaceSearch: wsMock, personalSearch: pMock }); const result = await cr.search('test', { scope: 'workspace' }); expect(result.workspaceResults).toHaveLength(1); expect(result.personalResults).toHaveLength(0); expect(pMock.search).not.toHaveBeenCalled(); }); it('works correctly when no workspace search is available', async () => { const cr = new CombinedRetrieval({ workspaceSearch: null, personalSearch: makeSearchMock([makeMemoryResult({ id: 1, content: 'personal fact', score: 0.6 })]), }); const result = await cr.search('test'); expect(result.workspaceResults).toHaveLength(0); expect(result.personalResults).toHaveLength(1); }); it('passes limit and profile through to memory search', async () => { const wsMock = makeSearchMock([]); const pMock = makeSearchMock([]); const cr = new CombinedRetrieval({ workspaceSearch: wsMock, personalSearch: pMock }); await cr.search('test', { limit: 5, profile: 'recent' }); expect(wsMock.search).toHaveBeenCalledWith('test', { limit: 5, profile: 'recent' }); expect(pMock.search).toHaveBeenCalledWith('test', { limit: 5, profile: 'recent' }); }); it('uses sensible defaults (limit=10, profile=balanced, scope=all)', async () => { const wsMock = makeSearchMock([]); const pMock = makeSearchMock([]); const cr = new CombinedRetrieval({ workspaceSearch: wsMock, personalSearch: pMock }); await cr.search('test'); expect(wsMock.search).toHaveBeenCalledWith('test', { limit: 10, profile: 'balanced' }); expect(pMock.search).toHaveBeenCalledWith('test', { limit: 10, profile: 'balanced' }); }); it('returns empty lists when nothing matches', async () => { const cr = new CombinedRetrieval({ workspaceSearch: makeSearchMock([]), personalSearch: makeSearchMock([]), }); const result = await cr.search('nothing'); expect(result.workspaceResults).toHaveLength(0); expect(result.personalResults).toHaveLength(0); }); });