Skip to content

Commit ec5522c

Browse files
Copilotzackify
andcommitted
Refactor test to use migrations and add search test
Co-authored-by: zackify <[email protected]>
1 parent 396176d commit ec5522c

File tree

2 files changed

+63
-21
lines changed

2 files changed

+63
-21
lines changed

src/database/migrations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function getAppliedMigrations(): Promise<string[]> {
2525
}
2626

2727
// Example migrations array
28-
const migrations: Migration[] = [
28+
export const migrations: Migration[] = [
2929
{
3030
name: "create_documents_table",
3131
up: () => {

tests/index.test.ts

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect, describe, test, beforeAll, beforeEach, afterAll } from "bun:test";
22
import { indexRoute } from "../src/routes/index";
3+
import { search } from "../src/routes/search";
34

45
// We need to set environment variables before importing the database module
56
process.env.DATABASE_PATH = ":memory:";
@@ -30,8 +31,9 @@ mock.module("openai", () => {
3031
};
3132
});
3233

33-
// Now we can import the database
34+
// Now we can import database and migrations
3435
import { db } from "../src/database/database";
36+
import { migrations } from "../src/database/migrations";
3537

3638
// Test short and long texts to ensure correct chunking behavior
3739
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
4345
Nulla facilisi. Cras eu dolor a neque lacinia tincidunt vel vitae mi. Pellentesque habitant morbi tristique.
4446
`;
4547

46-
describe("Index route", () => {
48+
describe("Index and Search routes", () => {
4749
// Set up the test environment
4850
beforeAll(() => {
49-
// Create the necessary tables for testing
51+
// Create migrations table
5052
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
6857
);
6958
`);
59+
60+
// Run the migrations to create database schema
61+
for (const migration of migrations) {
62+
migration.up();
63+
}
7064
});
7165

7266
// Clean up before each test
@@ -144,4 +138,52 @@ describe("Index route", () => {
144138
// Verify that all content is preserved
145139
expect(chunksText).toBe(originalText);
146140
});
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+
});
147189
});

0 commit comments

Comments
 (0)