|
| 1 | +import { describe, it, expect, beforeEach, vi } from "vitest"; |
| 2 | +import * as vscode from "vscode"; |
| 3 | +import { loadStore } from "../src/ecl/docs/SimilarityModel"; |
| 4 | +import { MemoryVectorStore } from "@langchain/classic/vectorstores/memory"; |
| 5 | + |
| 6 | +// Mock vscode module |
| 7 | +vi.mock("vscode", () => ({ |
| 8 | + workspace: { |
| 9 | + fs: { |
| 10 | + readFile: vi.fn() |
| 11 | + } |
| 12 | + }, |
| 13 | + Uri: { |
| 14 | + file: (path: string) => ({ fsPath: path, path }) |
| 15 | + } |
| 16 | +})); |
| 17 | + |
| 18 | +// Mock @hpcc-js/wasm-llama |
| 19 | +vi.mock("@hpcc-js/wasm-llama", () => ({ |
| 20 | + Llama: { |
| 21 | + load: vi.fn(() => Promise.resolve({ |
| 22 | + embedding: vi.fn((text: string, modelData: Uint8Array) => { |
| 23 | + // Return mock embeddings based on text length |
| 24 | + return [[text.length / 100, (text.length * 2) / 100, (text.length * 3) / 100]]; |
| 25 | + }) |
| 26 | + })), |
| 27 | + unload: vi.fn() |
| 28 | + } |
| 29 | +})); |
| 30 | + |
| 31 | +// Mock @hpcc-js/wasm-zstd |
| 32 | +vi.mock("@hpcc-js/wasm-zstd", () => ({ |
| 33 | + Zstd: { |
| 34 | + load: vi.fn(() => Promise.resolve({ |
| 35 | + decompress: vi.fn((data: Uint8Array) => { |
| 36 | + // Mock decompression - just return a stringified JSON |
| 37 | + const mockData = { |
| 38 | + vectors: [ |
| 39 | + { |
| 40 | + content: "test document 1", |
| 41 | + embedding: [0.1, 0.2, 0.3], |
| 42 | + metadata: { source: "doc1" } |
| 43 | + }, |
| 44 | + { |
| 45 | + content: "test document 2", |
| 46 | + embedding: [0.4, 0.5, 0.6], |
| 47 | + metadata: { source: "doc2" } |
| 48 | + } |
| 49 | + ] |
| 50 | + }; |
| 51 | + const jsonStr = JSON.stringify(mockData); |
| 52 | + return new TextEncoder().encode(jsonStr); |
| 53 | + }) |
| 54 | + })) |
| 55 | + } |
| 56 | +})); |
| 57 | + |
| 58 | +describe("SimilarityModel loadStore", () => { |
| 59 | + beforeEach(() => { |
| 60 | + vi.clearAllMocks(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should load and return a MemoryVectorStore instance", async () => { |
| 64 | + // Mock file reading |
| 65 | + const mockModelData = new Uint8Array([1, 2, 3, 4]); |
| 66 | + const mockDocsData = new Uint8Array([5, 6, 7, 8]); |
| 67 | + |
| 68 | + vi.mocked(vscode.workspace.fs.readFile) |
| 69 | + .mockResolvedValueOnce(mockModelData) |
| 70 | + .mockResolvedValueOnce(mockDocsData); |
| 71 | + |
| 72 | + const modelPath = Promise.resolve(vscode.Uri.file("/path/to/model.bin")); |
| 73 | + const docsPath = vscode.Uri.file("/path/to/docs.vecdb"); |
| 74 | + |
| 75 | + const store = await loadStore(modelPath, docsPath); |
| 76 | + |
| 77 | + expect(store).toBeDefined(); |
| 78 | + expect(store).toBeInstanceOf(MemoryVectorStore); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should read the model and docs files", async () => { |
| 82 | + const mockModelData = new Uint8Array([1, 2, 3, 4]); |
| 83 | + const mockDocsData = new Uint8Array([5, 6, 7, 8]); |
| 84 | + |
| 85 | + vi.mocked(vscode.workspace.fs.readFile) |
| 86 | + .mockResolvedValueOnce(mockModelData) |
| 87 | + .mockResolvedValueOnce(mockDocsData); |
| 88 | + |
| 89 | + const modelPath = Promise.resolve(vscode.Uri.file("/path/to/model.bin")); |
| 90 | + const docsPath = vscode.Uri.file("/path/to/docs.vecdb"); |
| 91 | + |
| 92 | + await loadStore(modelPath, docsPath); |
| 93 | + |
| 94 | + expect(vscode.workspace.fs.readFile).toHaveBeenCalledTimes(2); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should decompress the docs file using Zstd", async () => { |
| 98 | + const { Zstd } = await import("@hpcc-js/wasm-zstd"); |
| 99 | + |
| 100 | + const mockModelData = new Uint8Array([1, 2, 3, 4]); |
| 101 | + const mockDocsData = new Uint8Array([5, 6, 7, 8]); |
| 102 | + |
| 103 | + vi.mocked(vscode.workspace.fs.readFile) |
| 104 | + .mockResolvedValueOnce(mockModelData) |
| 105 | + .mockResolvedValueOnce(mockDocsData); |
| 106 | + |
| 107 | + const modelPath = Promise.resolve(vscode.Uri.file("/path/to/model.bin")); |
| 108 | + const docsPath = vscode.Uri.file("/path/to/docs.vecdb"); |
| 109 | + |
| 110 | + await loadStore(modelPath, docsPath); |
| 111 | + |
| 112 | + expect(Zstd.load).toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should populate memoryVectors with decompressed data", async () => { |
| 116 | + const mockModelData = new Uint8Array([1, 2, 3, 4]); |
| 117 | + const mockDocsData = new Uint8Array([5, 6, 7, 8]); |
| 118 | + |
| 119 | + vi.mocked(vscode.workspace.fs.readFile) |
| 120 | + .mockResolvedValueOnce(mockModelData) |
| 121 | + .mockResolvedValueOnce(mockDocsData); |
| 122 | + |
| 123 | + const modelPath = Promise.resolve(vscode.Uri.file("/path/to/model.bin")); |
| 124 | + const docsPath = vscode.Uri.file("/path/to/docs.vecdb"); |
| 125 | + |
| 126 | + const store = await loadStore(modelPath, docsPath); |
| 127 | + |
| 128 | + expect(store.memoryVectors).toBeDefined(); |
| 129 | + expect(Array.isArray(store.memoryVectors)).toBe(true); |
| 130 | + expect(store.memoryVectors.length).toBe(2); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should return a store with the correct vector data structure", async () => { |
| 134 | + const mockModelData = new Uint8Array([1, 2, 3, 4]); |
| 135 | + const mockDocsData = new Uint8Array([5, 6, 7, 8]); |
| 136 | + |
| 137 | + vi.mocked(vscode.workspace.fs.readFile) |
| 138 | + .mockResolvedValueOnce(mockModelData) |
| 139 | + .mockResolvedValueOnce(mockDocsData); |
| 140 | + |
| 141 | + const modelPath = Promise.resolve(vscode.Uri.file("/path/to/model.bin")); |
| 142 | + const docsPath = vscode.Uri.file("/path/to/docs.vecdb"); |
| 143 | + |
| 144 | + const store = await loadStore(modelPath, docsPath); |
| 145 | + |
| 146 | + expect(store.memoryVectors.length).toBe(2); |
| 147 | + expect(store.memoryVectors[0]).toHaveProperty("content"); |
| 148 | + expect(store.memoryVectors[0]).toHaveProperty("embedding"); |
| 149 | + expect(store.memoryVectors[0]).toHaveProperty("metadata"); |
| 150 | + }); |
| 151 | + |
| 152 | + it("should handle errors when model file cannot be read", async () => { |
| 153 | + vi.mocked(vscode.workspace.fs.readFile) |
| 154 | + .mockRejectedValueOnce(new Error("Model file not found")); |
| 155 | + |
| 156 | + const modelPath = Promise.resolve(vscode.Uri.file("/invalid/model.bin")); |
| 157 | + const docsPath = vscode.Uri.file("/path/to/docs.vecdb"); |
| 158 | + |
| 159 | + await expect(loadStore(modelPath, docsPath)).rejects.toThrow("Model file not found"); |
| 160 | + }); |
| 161 | + |
| 162 | + it("should handle errors when docs file cannot be read", async () => { |
| 163 | + vi.mocked(vscode.workspace.fs.readFile) |
| 164 | + .mockRejectedValueOnce(new Error("Docs file not found")); |
| 165 | + |
| 166 | + const modelPath = Promise.resolve(vscode.Uri.file("/path/to/model.bin")); |
| 167 | + const docsPath = vscode.Uri.file("/invalid/docs.vecdb"); |
| 168 | + |
| 169 | + await expect(loadStore(modelPath, docsPath)).rejects.toThrow("Docs file not found"); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should handle invalid JSON in decompressed data", async () => { |
| 173 | + const { Zstd } = await import("@hpcc-js/wasm-zstd"); |
| 174 | + |
| 175 | + // Mock Zstd to return invalid JSON |
| 176 | + vi.mocked(Zstd.load).mockResolvedValueOnce({ |
| 177 | + decompress: vi.fn(() => new TextEncoder().encode("invalid json")) |
| 178 | + } as any); |
| 179 | + |
| 180 | + const mockModelData = new Uint8Array([1, 2, 3, 4]); |
| 181 | + const mockDocsData = new Uint8Array([5, 6, 7, 8]); |
| 182 | + |
| 183 | + vi.mocked(vscode.workspace.fs.readFile) |
| 184 | + .mockResolvedValueOnce(mockModelData) |
| 185 | + .mockResolvedValueOnce(mockDocsData); |
| 186 | + |
| 187 | + const modelPath = Promise.resolve(vscode.Uri.file("/path/to/model.bin")); |
| 188 | + const docsPath = vscode.Uri.file("/path/to/docs.vecdb"); |
| 189 | + |
| 190 | + await expect(loadStore(modelPath, docsPath)).rejects.toThrow(); |
| 191 | + }); |
| 192 | +}); |
0 commit comments