1
1
import { expect , describe , test , beforeAll , beforeEach , afterAll } from "bun:test" ;
2
2
import { indexRoute } from "../src/routes/index" ;
3
+ import { search } from "../src/routes/search" ;
3
4
4
5
// We need to set environment variables before importing the database module
5
6
process . env . DATABASE_PATH = ":memory:" ;
@@ -30,8 +31,9 @@ mock.module("openai", () => {
30
31
} ;
31
32
} ) ;
32
33
33
- // Now we can import the database
34
+ // Now we can import database and migrations
34
35
import { db } from "../src/database/database" ;
36
+ import { migrations } from "../src/database/migrations" ;
35
37
36
38
// Test short and long texts to ensure correct chunking behavior
37
39
const shortText = "This is a short text that should not be split into chunks." ;
@@ -43,30 +45,22 @@ Sed imperdiet eros at diam cursus, sed volutpat nibh accumsan. Integer vel tinci
43
45
Nulla facilisi. Cras eu dolor a neque lacinia tincidunt vel vitae mi. Pellentesque habitant morbi tristique.
44
46
` ;
45
47
46
- describe ( "Index route " , ( ) => {
48
+ describe ( "Index and Search routes " , ( ) => {
47
49
// Set up the test environment
48
50
beforeAll ( ( ) => {
49
- // Create the necessary tables for testing
51
+ // Create migrations table
50
52
db . exec ( `
51
- CREATE TABLE IF NOT EXISTS documents (
52
- id INTEGER PRIMARY KEY AUTOINCREMENT,
53
- external_id VARCHAR UNIQUE,
54
- text TEXT,
55
- metadata JSON,
56
- embeddings TEXT,
57
- source VARCHAR,
58
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
59
- );
60
-
61
- CREATE TABLE IF NOT EXISTS document_chunks (
62
- id INTEGER PRIMARY KEY AUTOINCREMENT,
63
- document_id INTEGER,
64
- text TEXT,
65
- embeddings TEXT,
66
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
67
- FOREIGN KEY (document_id) REFERENCES documents(id)
53
+ CREATE TABLE IF NOT EXISTS migrations (
54
+ id SERIAL PRIMARY KEY,
55
+ name TEXT UNIQUE NOT NULL,
56
+ applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
68
57
);
69
58
` ) ;
59
+
60
+ // Run the migrations to create database schema
61
+ for ( const migration of migrations ) {
62
+ migration . up ( ) ;
63
+ }
70
64
} ) ;
71
65
72
66
// Clean up before each test
@@ -144,4 +138,52 @@ describe("Index route", () => {
144
138
// Verify that all content is preserved
145
139
expect ( chunksText ) . toBe ( originalText ) ;
146
140
} ) ;
141
+
142
+ test ( "should be able to search for indexed documents" , async ( ) => {
143
+ // First, index a document
144
+ const indexRequest = new Request ( "http://localhost/index" , {
145
+ method : "POST" ,
146
+ headers : {
147
+ "Content-Type" : "application/json" ,
148
+ } ,
149
+ body : JSON . stringify ( {
150
+ source : "test" ,
151
+ text : shortText
152
+ } ) ,
153
+ } ) ;
154
+
155
+ await indexRoute ( indexRequest ) ;
156
+
157
+ // Now search for the document
158
+ const searchRequest = new Request ( "http://localhost/search" , {
159
+ method : "POST" ,
160
+ headers : {
161
+ "Content-Type" : "application/json" ,
162
+ } ,
163
+ body : JSON . stringify ( {
164
+ text : "chunks"
165
+ } ) ,
166
+ } ) ;
167
+
168
+ const response = await search ( searchRequest ) ;
169
+ const responseData = await response . json ( ) ;
170
+
171
+ // Verify we get results back
172
+ expect ( response . status ) . toBe ( 200 ) ;
173
+ expect ( responseData . results ) . toBeDefined ( ) ;
174
+ expect ( Array . isArray ( responseData . results ) ) . toBe ( true ) ;
175
+ expect ( responseData . results . length ) . toBeGreaterThan ( 0 ) ;
176
+
177
+ // Verify the first result has the expected properties
178
+ const firstResult = responseData . results [ 0 ] ;
179
+ expect ( firstResult ) . toHaveProperty ( "id" ) ;
180
+ expect ( firstResult ) . toHaveProperty ( "text" ) ;
181
+ expect ( firstResult ) . toHaveProperty ( "source" ) ;
182
+ expect ( firstResult ) . toHaveProperty ( "distance" ) ;
183
+ expect ( firstResult ) . toHaveProperty ( "metadata" ) ;
184
+
185
+ // The text should match what we indexed
186
+ expect ( firstResult . text ) . toBe ( shortText ) ;
187
+ expect ( firstResult . source ) . toBe ( "test" ) ;
188
+ } ) ;
147
189
} ) ;
0 commit comments