|
| 1 | +import { |
| 2 | + expect, |
| 3 | + describe, |
| 4 | + test, |
| 5 | + beforeAll, |
| 6 | + beforeEach, |
| 7 | + afterAll, |
| 8 | + mock, |
| 9 | + spyOn, |
| 10 | +} from "bun:test"; |
| 11 | +import Database from "bun:sqlite"; |
| 12 | +import * as sqliteVec from "sqlite-vec"; |
| 13 | +import { createDocumentsTableSQL } from "../src/database/migrations"; |
| 14 | + |
| 15 | +// Set test environment variables |
| 16 | +process.env.DATABASE_PATH = "******"; // In-memory database for tests |
| 17 | +process.env.AI_API_KEY = "test-key"; |
| 18 | +process.env.AI_EMBEDDING_MODEL = "test-model"; |
| 19 | + |
| 20 | +// Mock for generateEmbeddings |
| 21 | +const mockEmbeddings = Array(1536).fill(0.1); |
| 22 | +const generateEmbeddingsMock = mock(async (text, config) => { |
| 23 | + return mockEmbeddings; |
| 24 | +}); |
| 25 | + |
| 26 | +// Mock the generateEmbeddings module |
| 27 | +mock.module("../src/shared/generateEmbeddings", () => { |
| 28 | + return { |
| 29 | + generateEmbeddings: generateEmbeddingsMock, |
| 30 | + }; |
| 31 | +}); |
| 32 | + |
| 33 | +// Override the database module before other modules are imported |
| 34 | +describe("Document Route", () => { |
| 35 | + let db: Database; |
| 36 | + let documentId: number; |
| 37 | + |
| 38 | + // Create a fresh test setup before tests |
| 39 | + beforeAll(async () => { |
| 40 | + // Create fresh database |
| 41 | + db = new Database("******"); |
| 42 | + |
| 43 | + // Configure database |
| 44 | + db.exec("PRAGMA journal_mode = WAL;"); |
| 45 | + sqliteVec.load(db); |
| 46 | + db.exec(createDocumentsTableSQL("1536")); |
| 47 | + |
| 48 | + // Spy on database module to return our test db |
| 49 | + mock.module("../src/database/database", () => ({ |
| 50 | + db: db, |
| 51 | + getDb: () => db |
| 52 | + })); |
| 53 | + }); |
| 54 | + |
| 55 | + // Insert test data before each test |
| 56 | + beforeEach(() => { |
| 57 | + // Clear any existing data |
| 58 | + db.exec("DELETE FROM documents"); |
| 59 | + |
| 60 | + // Insert test data |
| 61 | + const sampleText = "This is a test document for document endpoint"; |
| 62 | + const sampleSource = "test-source"; |
| 63 | + const sampleMetadata = JSON.stringify({ testKey: "testValue" }); |
| 64 | + const embeddingsStr = `[${mockEmbeddings.join(",")}]`; |
| 65 | + |
| 66 | + db.query(` |
| 67 | + INSERT INTO documents (text, metadata, embeddings, source) |
| 68 | + VALUES (?, ?, ?, ?) |
| 69 | + `).run(sampleText, sampleMetadata, embeddingsStr, sampleSource); |
| 70 | + |
| 71 | + // Get the document id |
| 72 | + const document = db.query("SELECT id FROM documents LIMIT 1").get() as { id: number } | null; |
| 73 | + documentId = document?.id || 0; |
| 74 | + }); |
| 75 | + |
| 76 | + // Clean up after all tests |
| 77 | + afterAll(() => { |
| 78 | + db.close(); |
| 79 | + }); |
| 80 | + |
| 81 | + test("should retrieve document by id", async () => { |
| 82 | + // Import the module only after our mock is set up |
| 83 | + const { document } = await import("../src/routes/document/document"); |
| 84 | + |
| 85 | + // Get document by ID using the handler function directly |
| 86 | + const result = await document({ id: documentId }); |
| 87 | + |
| 88 | + // Verify document properties |
| 89 | + expect(result).toHaveProperty("document"); |
| 90 | + |
| 91 | + // Type guard to ensure we're checking the success case |
| 92 | + if ("document" in result) { |
| 93 | + const doc = result.document; |
| 94 | + expect(doc).toHaveProperty("id", documentId); |
| 95 | + expect(doc).toHaveProperty("text", "This is a test document for document endpoint"); |
| 96 | + expect(doc).toHaveProperty("source", "test-source"); |
| 97 | + expect(doc).toHaveProperty("metadata"); |
| 98 | + expect(doc.metadata).toHaveProperty("testKey", "testValue"); |
| 99 | + } else { |
| 100 | + // This should not happen in this test, but helps TypeScript |
| 101 | + throw new Error("Expected document in result but got error"); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + test("should handle non-existent document id", async () => { |
| 106 | + // Import the module only after our mock is set up |
| 107 | + const { document } = await import("../src/routes/document/document"); |
| 108 | + |
| 109 | + const result = await document({ id: 9999 }); |
| 110 | + |
| 111 | + // Verify error response |
| 112 | + expect(result).toHaveProperty("error", "Document not found"); |
| 113 | + expect(result).not.toHaveProperty("document"); |
| 114 | + }); |
| 115 | + |
| 116 | + test("should handle validation errors", async () => { |
| 117 | + // Import the module only after our mock is set up |
| 118 | + const { documentRoute } = await import("../src/routes/document/document"); |
| 119 | + |
| 120 | + // Create request with missing ID |
| 121 | + const request = new Request("http://localhost/document", { |
| 122 | + method: "POST", |
| 123 | + headers: { |
| 124 | + "Content-Type": "application/json", |
| 125 | + }, |
| 126 | + body: JSON.stringify({}), |
| 127 | + }); |
| 128 | + |
| 129 | + // Process the request |
| 130 | + const response = await documentRoute(request); |
| 131 | + const responseData = await response.json(); |
| 132 | + |
| 133 | + // Verify validation error |
| 134 | + expect(response.status).toBe(400); |
| 135 | + expect(responseData).toHaveProperty("error", "Validation failed"); |
| 136 | + expect(responseData).toHaveProperty("issues"); |
| 137 | + }); |
| 138 | +}); |
0 commit comments