Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration tests for Similarity Search API. #450

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/similarity_search/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": {
"@cloudflare/vitest-pool-workers": "^0.2.6",
"hono": "^4.3.2",
"vitest": "1.3.0"
"vitest": "1.5.0"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240403.0",
Expand Down
2 changes: 1 addition & 1 deletion tools/similarity_search/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Hono } from "hono"

type Env = {
export type Env = {
API_KEY_TOKEN_CHECK: string
AI: Ai
VECTORIZE_INDEX: VectorizeIndex
Expand Down
5 changes: 5 additions & 0 deletions tools/similarity_search/test/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Env } from "../src/index"

declare module "cloudflare:test" {
interface ProvidedEnv extends Env {}
}
86 changes: 81 additions & 5 deletions tools/similarity_search/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,98 @@
import { SELF } from "cloudflare:test"
import { SELF, env } from "cloudflare:test"
import { describe, it, expect } from "vitest"

import "../src/index"

interface SimilarityCheckResponse {
similarity_score: number
}

describe("Authentication", () => {
it("returns 401 Unauthorized when API key is missing or invalid", async () => {
it("returns 401 Unauthorized when API key is invalid", async () => {
const response = await SELF.fetch("https://example.com/", {
method: "POST",
headers: {
"Content-Type": "application/json"
"Content-Type": "application/json",
"X-API-Key": "invalid-api-key",
},
body: JSON.stringify({
text: "Sample text",
namespace: "test-namespace"
})
namespace: "test-namespace",
}),
})

expect(response.status).toBe(401)
expect(await response.text()).toBe("Unauthorized")
kol3x marked this conversation as resolved.
Show resolved Hide resolved
})
})

describe("Validation", () => {
it("returns 400 Invalid JSON format when text is missing", async () => {
const response = await SELF.fetch("https://example.com/", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-Key": "test-api-key",
},
body: JSON.stringify({
namespace: "test-namespace",
}),
})

expect(response.status).toBe(400)
expect(await response.text()).toBe("Invalid JSON format")
})

it("returns 400 Invalid JSON format when namespace is missing", async () => {
const response = await SELF.fetch("https://example.com/", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-Key": "test-api-key",
},
body: JSON.stringify({
text: "Sample text",
}),
})

expect(response.status).toBe(400)
expect(await response.text()).toBe("Invalid JSON format")
})
})
kol3x marked this conversation as resolved.
Show resolved Hide resolved

describe("Functionality", () => {
it("runs AI model and gets vectorized string back", async () => {
const response = await env.AI.run("@cf/baai/bge-base-en-v1.5", {
text: ["Sample text"],
})
expect(response).toHaveProperty("data")
})

it("queries vector database and gets proper response", async () => {
const response = await env.VECTORIZE_INDEX.query([1, 2, 3], {
namespace: "test-namespace",
topK: 1,
})
expect(response).toHaveProperty("matches")
expect(response.matches.length).toBeGreaterThan(0)
})

it("returns similarity score for valid requests", async () => {
const response = await SELF.fetch("https://example.com/", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-Key": "test-api-key",
},
body: JSON.stringify({
text: "Sample text",
namespace: "test-namespace",
}),
})

expect(response.status).toBe(200)
const jsonResponse: SimilarityCheckResponse = await response.json()
expect(jsonResponse).toHaveProperty("similarity_score")
expect(typeof jsonResponse.similarity_score).toBe("number")
})
})
Copy link
Contributor

@evgenydmitriev evgenydmitriev Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look like unit tests to me. For example, you can't trigger Worker AI yourself - this function is an internal component of the worker you are testing. Same for querying vectorize. The only trigger at your disposal is user's request. You can, however, assert all other worker interactions, including worker AI being called and vector DB being queried.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look like unit tests to me.

That's what I thought initially when you told me that these services behavior should be tested too. But now I see what you are talking about.

I changed it so now we are just looking at what AI and DB are called with and what they return during normal request to a service, instead of calling them directly. I don't know if this is sufficient, or the abstraction has to be even deeper.