Skip to content

Commit d0a77e1

Browse files
committed
fix
1 parent b0883fa commit d0a77e1

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

******

0 Bytes
Binary file not shown.

tests/index.test.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import {
1010
} from "bun:test";
1111
import Database from "bun:sqlite";
1212
import * as sqliteVec from "sqlite-vec";
13-
import { createDocumentsTableSQL, createDocumentChunksTableSQL } from "../src/database/migrations";
13+
import {
14+
createDocumentsTableSQL,
15+
createDocumentChunksTableSQL,
16+
} from "../src/database/migrations";
17+
import { indexRoute } from "../src/routes/index/index";
1418

1519
// Set test environment variables
1620
process.env.DATABASE_PATH = "******"; // In-memory database for tests
@@ -36,17 +40,17 @@ describe("Index and Search routes", () => {
3640
beforeAll(async () => {
3741
// Create fresh database
3842
db = new Database("******");
39-
43+
4044
// Configure database
4145
db.exec("PRAGMA journal_mode = WAL;");
4246
sqliteVec.load(db);
4347
db.exec(createDocumentsTableSQL("1536"));
4448
db.exec(createDocumentChunksTableSQL("1536"));
45-
49+
4650
// Spy on database module to return our test db
4751
mock.module("../src/database/database", () => ({
4852
db: db,
49-
getDb: () => db
53+
getDb: () => db,
5054
}));
5155
});
5256

@@ -62,10 +66,7 @@ describe("Index and Search routes", () => {
6266

6367
test("should store short text as a single document with one chunk", async () => {
6468
const shortText = "This is a short test document.";
65-
66-
// Import the module
67-
const { indexRoute } = await import("../src/routes/index/index");
68-
69+
6970
// Create a request for indexing
7071
const request = new Request("http://localhost/index", {
7172
method: "POST",
@@ -83,18 +84,22 @@ describe("Index and Search routes", () => {
8384
expect(response.status).toBe(200);
8485

8586
// Check that one document was stored
86-
const docCount = db.query("SELECT COUNT(*) as count FROM documents").get() as { count: number };
87+
const docCount = db
88+
.query("SELECT COUNT(*) as count FROM documents")
89+
.get() as { count: number };
8790
expect(docCount.count).toBe(1);
8891

8992
// For short text, we should have just one chunk
90-
const chunkCount = db.query("SELECT COUNT(*) as count FROM document_chunks").get() as { count: number };
93+
const chunkCount = db
94+
.query("SELECT COUNT(*) as count FROM document_chunks")
95+
.get() as { count: number };
9196
expect(chunkCount.count).toBe(1);
9297
});
9398

9499
test("should store long text as a document with multiple chunks", async () => {
95100
// Import the module
96101
const { indexRoute } = await import("../src/routes/index/index");
97-
102+
98103
// Create a long text that will be split into multiple chunks
99104
const longText = `
100105
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia, nunc eu tincidunt lobortis,
@@ -106,7 +111,7 @@ describe("Index and Search routes", () => {
106111
Fusce eget metus quis magna mollis rhoncus. Pellentesque habitant morbi tristique senectus et netus et
107112
malesuada fames ac turpis egestas. Proin at semper libero. Nullam non sollicitudin risus.
108113
`;
109-
114+
110115
// Create a request for indexing
111116
const request = new Request("http://localhost/index", {
112117
method: "POST",
@@ -124,22 +129,27 @@ describe("Index and Search routes", () => {
124129
expect(response.status).toBe(200);
125130

126131
// Check that one document was stored
127-
const docCount = db.query("SELECT COUNT(*) as count FROM documents").get() as { count: number };
132+
const docCount = db
133+
.query("SELECT COUNT(*) as count FROM documents")
134+
.get() as { count: number };
128135
expect(docCount.count).toBe(1);
129136

130137
// For long text, we should have multiple chunks
131-
const chunkCount = db.query("SELECT COUNT(*) as count FROM document_chunks").get() as { count: number };
138+
const chunkCount = db
139+
.query("SELECT COUNT(*) as count FROM document_chunks")
140+
.get() as { count: number };
132141
expect(chunkCount.count).toBeGreaterThan(1);
133142
});
134143

135144
test("should be able to search for indexed documents", async () => {
136145
// Import modules
137146
const { indexRoute } = await import("../src/routes/index/index");
138147
const { searchRoute } = await import("../src/routes/search/search");
139-
148+
140149
// First, index a document
141-
const text = "Here is some text about artificial intelligence and machine learning";
142-
150+
const text =
151+
"Here is some text about artificial intelligence and machine learning";
152+
143153
// Create a request for indexing
144154
const indexRequest = new Request("http://localhost/index", {
145155
method: "POST",
@@ -154,7 +164,7 @@ describe("Index and Search routes", () => {
154164

155165
// Process the index request
156166
await indexRoute(indexRequest);
157-
167+
158168
// Now search for it
159169
const searchRequest = new Request("http://localhost/search", {
160170
method: "POST",
@@ -165,18 +175,18 @@ describe("Index and Search routes", () => {
165175
text: "artificial intelligence",
166176
}),
167177
});
168-
178+
169179
// Process the search request
170180
const searchResponse = await searchRoute(searchRequest);
171181
const searchData = await searchResponse.json();
172-
182+
173183
// Verify search results
174184
expect(searchResponse.status).toBe(200);
175185
expect(searchData.results).toBeDefined();
176-
186+
177187
// Since our mock always returns the same embeddings, any search will match
178188
expect(searchData.results.length).toBeGreaterThan(0);
179-
189+
180190
// Check the first result
181191
if (searchData.results.length > 0) {
182192
const result = searchData.results[0];

0 commit comments

Comments
 (0)