|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { IndexingPipeline } from '../indexing/pipeline.js'; |
| 3 | +import type { ContentItem } from '../indexing/providers/types.js'; |
| 4 | +import type { SourceConfig } from '../types.js'; |
| 5 | + |
| 6 | +// Mock the dependencies |
| 7 | +vi.mock('../indexing/chunking/index.js', () => ({ |
| 8 | + getChunker: vi.fn().mockReturnValue( |
| 9 | + (content: string, _filePath: string, _config: unknown) => [{ |
| 10 | + content, |
| 11 | + title: 'Test Title', |
| 12 | + chunkIndex: 0, |
| 13 | + }], |
| 14 | + ), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('../indexing/embeddings.js', () => { |
| 18 | + const MockEmbeddingClient = vi.fn().mockImplementation(function (this: Record<string, unknown>) { |
| 19 | + this.embedBatch = vi.fn().mockResolvedValue([[0.1, 0.2, 0.3]]); |
| 20 | + }); |
| 21 | + return { EmbeddingClient: MockEmbeddingClient }; |
| 22 | +}); |
| 23 | + |
| 24 | +vi.mock('../db/queries.js', () => ({ |
| 25 | + upsertChunks: vi.fn().mockResolvedValue(undefined), |
| 26 | + deleteChunksByFile: vi.fn().mockResolvedValue(undefined), |
| 27 | +})); |
| 28 | + |
| 29 | +vi.mock('../indexing/url-derivation.js', () => ({ |
| 30 | + deriveUrl: () => 'https://example.com/test', |
| 31 | +})); |
| 32 | + |
| 33 | +const { upsertChunks, deleteChunksByFile } = await import('../db/queries.js'); |
| 34 | +const { EmbeddingClient } = await import('../indexing/embeddings.js'); |
| 35 | + |
| 36 | +const testConfig: SourceConfig = { |
| 37 | + name: 'test-source', |
| 38 | + type: 'markdown', |
| 39 | + path: 'docs/', |
| 40 | + file_patterns: ['**/*.md'], |
| 41 | + chunk: { target_tokens: 600, overlap_tokens: 50 }, |
| 42 | +}; |
| 43 | + |
| 44 | +describe('IndexingPipeline', () => { |
| 45 | + it('indexes items: chunk → embed → delete old → upsert', async () => { |
| 46 | + const embeddingClient = new EmbeddingClient('key', 'model', 1536); |
| 47 | + const pipeline = new IndexingPipeline(embeddingClient, testConfig); |
| 48 | + |
| 49 | + const items: ContentItem[] = [{ |
| 50 | + id: 'docs/test.md', |
| 51 | + content: '# Hello\nSome content here', |
| 52 | + }]; |
| 53 | + |
| 54 | + await pipeline.indexItems(items, 'abc123'); |
| 55 | + |
| 56 | + expect(deleteChunksByFile).toHaveBeenCalledWith('test-source', 'docs/test.md'); |
| 57 | + expect(upsertChunks).toHaveBeenCalledWith( |
| 58 | + expect.arrayContaining([ |
| 59 | + expect.objectContaining({ |
| 60 | + source_name: 'test-source', |
| 61 | + file_path: 'docs/test.md', |
| 62 | + commit_sha: 'abc123', |
| 63 | + }), |
| 64 | + ]), |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('skips items that produce zero chunks', async () => { |
| 69 | + const { getChunker } = await import('../indexing/chunking/index.js'); |
| 70 | + vi.mocked(getChunker).mockReturnValueOnce(() => []); |
| 71 | + |
| 72 | + const embeddingClient = new EmbeddingClient('key', 'model', 1536); |
| 73 | + const pipeline = new IndexingPipeline(embeddingClient, testConfig); |
| 74 | + |
| 75 | + vi.mocked(upsertChunks).mockClear(); |
| 76 | + await pipeline.indexItems([{ id: 'empty.md', content: '' }], 'abc'); |
| 77 | + expect(upsertChunks).not.toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('removes items by ID', async () => { |
| 81 | + const embeddingClient = new EmbeddingClient('key', 'model', 1536); |
| 82 | + const pipeline = new IndexingPipeline(embeddingClient, testConfig); |
| 83 | + |
| 84 | + vi.mocked(deleteChunksByFile).mockClear(); |
| 85 | + await pipeline.removeItems(['docs/old.md', 'docs/deleted.md']); |
| 86 | + |
| 87 | + expect(deleteChunksByFile).toHaveBeenCalledTimes(2); |
| 88 | + expect(deleteChunksByFile).toHaveBeenCalledWith('test-source', 'docs/old.md'); |
| 89 | + expect(deleteChunksByFile).toHaveBeenCalledWith('test-source', 'docs/deleted.md'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('passes sourceUrl from ContentItem when provided', async () => { |
| 93 | + const embeddingClient = new EmbeddingClient('key', 'model', 1536); |
| 94 | + const pipeline = new IndexingPipeline(embeddingClient, testConfig); |
| 95 | + |
| 96 | + vi.mocked(upsertChunks).mockClear(); |
| 97 | + await pipeline.indexItems([{ |
| 98 | + id: 'docs/test.md', |
| 99 | + content: 'Content', |
| 100 | + sourceUrl: 'https://custom.url/test', |
| 101 | + }], 'abc'); |
| 102 | + |
| 103 | + expect(upsertChunks).toHaveBeenCalledWith( |
| 104 | + expect.arrayContaining([ |
| 105 | + expect.objectContaining({ |
| 106 | + source_url: 'https://custom.url/test', |
| 107 | + }), |
| 108 | + ]), |
| 109 | + ); |
| 110 | + }); |
| 111 | +}); |
0 commit comments