|
| 1 | +/** |
| 2 | + * Mock VectorStorage for tests that don't need a real antfly server. |
| 3 | + * |
| 4 | + * Used by indexer, subagent, and CLI tests that test their own logic, |
| 5 | + * not vector storage behavior. The real antfly tests are in antfly-store.test.ts. |
| 6 | + */ |
| 7 | + |
| 8 | +import { vi } from 'vitest'; |
| 9 | + |
| 10 | +export { type AntflyStoreConfig, AntflyVectorStore } from '../antfly-store.js'; |
| 11 | +// Re-export real types |
| 12 | +export * from '../types.js'; |
| 13 | + |
| 14 | +// In-memory document store for mock |
| 15 | +const docs = new Map<string, { text: string; metadata: Record<string, unknown> }>(); |
| 16 | + |
| 17 | +export class VectorStorage { |
| 18 | + private initialized = false; |
| 19 | + |
| 20 | + constructor(_config: { storePath: string; embeddingModel?: string; dimension?: number }) { |
| 21 | + // No-op — mock doesn't connect to anything |
| 22 | + } |
| 23 | + |
| 24 | + async initialize(_options?: { skipEmbedder?: boolean }): Promise<void> { |
| 25 | + this.initialized = true; |
| 26 | + } |
| 27 | + |
| 28 | + async addDocuments( |
| 29 | + documents: Array<{ id: string; text: string; metadata: Record<string, unknown> }> |
| 30 | + ): Promise<void> { |
| 31 | + for (const doc of documents) { |
| 32 | + docs.set(doc.id, { text: doc.text, metadata: doc.metadata }); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + async search( |
| 37 | + _query: string, |
| 38 | + options?: { limit?: number; scoreThreshold?: number } |
| 39 | + ): Promise<Array<{ id: string; score: number; metadata: Record<string, unknown> }>> { |
| 40 | + const limit = options?.limit ?? 10; |
| 41 | + const results = Array.from(docs.entries()) |
| 42 | + .slice(0, limit) |
| 43 | + .map(([id, doc]) => ({ |
| 44 | + id, |
| 45 | + score: 0.85, |
| 46 | + metadata: doc.metadata, |
| 47 | + })); |
| 48 | + return results; |
| 49 | + } |
| 50 | + |
| 51 | + async searchByDocumentId( |
| 52 | + documentId: string, |
| 53 | + options?: { limit?: number } |
| 54 | + ): Promise<Array<{ id: string; score: number; metadata: Record<string, unknown> }>> { |
| 55 | + return this.search(documentId, options); |
| 56 | + } |
| 57 | + |
| 58 | + async getAll(options?: { |
| 59 | + limit?: number; |
| 60 | + }): Promise<Array<{ id: string; score: number; metadata: Record<string, unknown> }>> { |
| 61 | + const limit = options?.limit ?? 10000; |
| 62 | + return Array.from(docs.entries()) |
| 63 | + .slice(0, limit) |
| 64 | + .map(([id, doc]) => ({ |
| 65 | + id, |
| 66 | + score: 1, |
| 67 | + metadata: doc.metadata, |
| 68 | + })); |
| 69 | + } |
| 70 | + |
| 71 | + async getDocument( |
| 72 | + id: string |
| 73 | + ): Promise<{ id: string; text: string; metadata: Record<string, unknown> } | null> { |
| 74 | + const doc = docs.get(id); |
| 75 | + if (!doc) return null; |
| 76 | + return { id, ...doc }; |
| 77 | + } |
| 78 | + |
| 79 | + async deleteDocuments(ids: string[]): Promise<void> { |
| 80 | + for (const id of ids) { |
| 81 | + docs.delete(id); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + async clear(): Promise<void> { |
| 86 | + docs.clear(); |
| 87 | + } |
| 88 | + |
| 89 | + async getStats(): Promise<{ |
| 90 | + totalDocuments: number; |
| 91 | + storageSize: number; |
| 92 | + dimension: number; |
| 93 | + modelName: string; |
| 94 | + }> { |
| 95 | + return { |
| 96 | + totalDocuments: docs.size, |
| 97 | + storageSize: 0, |
| 98 | + dimension: 384, |
| 99 | + modelName: 'BAAI/bge-small-en-v1.5', |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + async optimize(): Promise<void> { |
| 104 | + // no-op |
| 105 | + } |
| 106 | + |
| 107 | + async close(): Promise<void> { |
| 108 | + this.initialized = false; |
| 109 | + } |
| 110 | +} |
0 commit comments