@@ -10,7 +10,11 @@ import {
10
10
} from "bun:test" ;
11
11
import Database from "bun:sqlite" ;
12
12
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" ;
14
18
15
19
// Set test environment variables
16
20
process . env . DATABASE_PATH = "******" ; // In-memory database for tests
@@ -36,17 +40,17 @@ describe("Index and Search routes", () => {
36
40
beforeAll ( async ( ) => {
37
41
// Create fresh database
38
42
db = new Database ( "******" ) ;
39
-
43
+
40
44
// Configure database
41
45
db . exec ( "PRAGMA journal_mode = WAL;" ) ;
42
46
sqliteVec . load ( db ) ;
43
47
db . exec ( createDocumentsTableSQL ( "1536" ) ) ;
44
48
db . exec ( createDocumentChunksTableSQL ( "1536" ) ) ;
45
-
49
+
46
50
// Spy on database module to return our test db
47
51
mock . module ( "../src/database/database" , ( ) => ( {
48
52
db : db ,
49
- getDb : ( ) => db
53
+ getDb : ( ) => db ,
50
54
} ) ) ;
51
55
} ) ;
52
56
@@ -62,10 +66,7 @@ describe("Index and Search routes", () => {
62
66
63
67
test ( "should store short text as a single document with one chunk" , async ( ) => {
64
68
const shortText = "This is a short test document." ;
65
-
66
- // Import the module
67
- const { indexRoute } = await import ( "../src/routes/index/index" ) ;
68
-
69
+
69
70
// Create a request for indexing
70
71
const request = new Request ( "http://localhost/index" , {
71
72
method : "POST" ,
@@ -83,18 +84,22 @@ describe("Index and Search routes", () => {
83
84
expect ( response . status ) . toBe ( 200 ) ;
84
85
85
86
// 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 } ;
87
90
expect ( docCount . count ) . toBe ( 1 ) ;
88
91
89
92
// 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 } ;
91
96
expect ( chunkCount . count ) . toBe ( 1 ) ;
92
97
} ) ;
93
98
94
99
test ( "should store long text as a document with multiple chunks" , async ( ) => {
95
100
// Import the module
96
101
const { indexRoute } = await import ( "../src/routes/index/index" ) ;
97
-
102
+
98
103
// Create a long text that will be split into multiple chunks
99
104
const longText = `
100
105
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia, nunc eu tincidunt lobortis,
@@ -106,7 +111,7 @@ describe("Index and Search routes", () => {
106
111
Fusce eget metus quis magna mollis rhoncus. Pellentesque habitant morbi tristique senectus et netus et
107
112
malesuada fames ac turpis egestas. Proin at semper libero. Nullam non sollicitudin risus.
108
113
` ;
109
-
114
+
110
115
// Create a request for indexing
111
116
const request = new Request ( "http://localhost/index" , {
112
117
method : "POST" ,
@@ -124,22 +129,27 @@ describe("Index and Search routes", () => {
124
129
expect ( response . status ) . toBe ( 200 ) ;
125
130
126
131
// 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 } ;
128
135
expect ( docCount . count ) . toBe ( 1 ) ;
129
136
130
137
// 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 } ;
132
141
expect ( chunkCount . count ) . toBeGreaterThan ( 1 ) ;
133
142
} ) ;
134
143
135
144
test ( "should be able to search for indexed documents" , async ( ) => {
136
145
// Import modules
137
146
const { indexRoute } = await import ( "../src/routes/index/index" ) ;
138
147
const { searchRoute } = await import ( "../src/routes/search/search" ) ;
139
-
148
+
140
149
// 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
+
143
153
// Create a request for indexing
144
154
const indexRequest = new Request ( "http://localhost/index" , {
145
155
method : "POST" ,
@@ -154,7 +164,7 @@ describe("Index and Search routes", () => {
154
164
155
165
// Process the index request
156
166
await indexRoute ( indexRequest ) ;
157
-
167
+
158
168
// Now search for it
159
169
const searchRequest = new Request ( "http://localhost/search" , {
160
170
method : "POST" ,
@@ -165,18 +175,18 @@ describe("Index and Search routes", () => {
165
175
text : "artificial intelligence" ,
166
176
} ) ,
167
177
} ) ;
168
-
178
+
169
179
// Process the search request
170
180
const searchResponse = await searchRoute ( searchRequest ) ;
171
181
const searchData = await searchResponse . json ( ) ;
172
-
182
+
173
183
// Verify search results
174
184
expect ( searchResponse . status ) . toBe ( 200 ) ;
175
185
expect ( searchData . results ) . toBeDefined ( ) ;
176
-
186
+
177
187
// Since our mock always returns the same embeddings, any search will match
178
188
expect ( searchData . results . length ) . toBeGreaterThan ( 0 ) ;
179
-
189
+
180
190
// Check the first result
181
191
if ( searchData . results . length > 0 ) {
182
192
const result = searchData . results [ 0 ] ;
0 commit comments