Skip to content

Commit 66387bc

Browse files
committed
test: add unit tests for OpenRouterEmbedder
1 parent c162cf6 commit 66387bc

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// npx vitest run src/services/code-index/embedders/__tests__/openrouter.spec.ts
2+
3+
import { describe, it, expect, vi, beforeEach } from "vitest"
4+
import { OpenRouterEmbedder } from "../openrouter"
5+
import { OpenAICompatibleEmbedder } from "../openai-compatible"
6+
import { t } from "../../../../i18n"
7+
8+
vi.mock("../../../../i18n", () => ({
9+
t: (key: string, params?: any) => {
10+
const translations: Record<string, string> = {
11+
"embeddings:validation.apiKeyRequired": "API key is required",
12+
}
13+
return translations[key] || key
14+
},
15+
}))
16+
17+
// Mock the parent class
18+
vi.mock("../openai-compatible")
19+
20+
describe("OpenRouterEmbedder", () => {
21+
beforeEach(() => {
22+
vi.clearAllMocks()
23+
// Mock the OpenAICompatibleEmbedder constructor
24+
vi.mocked(OpenAICompatibleEmbedder).mockImplementation(function (
25+
this: any,
26+
baseUrl: string,
27+
apiKey: string,
28+
modelId: string,
29+
maxTokens: number,
30+
) {
31+
// Store constructor arguments for verification
32+
this.baseUrl = baseUrl
33+
this.apiKey = apiKey
34+
this.modelId = modelId
35+
this.maxTokens = maxTokens
36+
37+
// Mock methods
38+
this.createEmbeddings = vi.fn()
39+
this.validateConfiguration = vi.fn()
40+
41+
// Return this for chaining
42+
return this
43+
} as any)
44+
})
45+
46+
describe("constructor", () => {
47+
it("should create an instance with valid API key", () => {
48+
const embedder = new OpenRouterEmbedder("test-api-key")
49+
expect(embedder).toBeDefined()
50+
expect(OpenAICompatibleEmbedder).toHaveBeenCalledWith(
51+
"https://openrouter.ai/api/v1",
52+
"test-api-key",
53+
"openai/text-embedding-3-small",
54+
8191,
55+
)
56+
})
57+
58+
it("should use custom model ID when provided", () => {
59+
const embedder = new OpenRouterEmbedder("test-api-key", "openai/text-embedding-3-large")
60+
expect(embedder).toBeDefined()
61+
expect(OpenAICompatibleEmbedder).toHaveBeenCalledWith(
62+
"https://openrouter.ai/api/v1",
63+
"test-api-key",
64+
"openai/text-embedding-3-large",
65+
8191,
66+
)
67+
})
68+
69+
it("should use custom base URL when provided", () => {
70+
const embedder = new OpenRouterEmbedder("test-api-key", undefined, "https://custom.openrouter.ai/api/v1")
71+
expect(embedder).toBeDefined()
72+
expect(OpenAICompatibleEmbedder).toHaveBeenCalledWith(
73+
"https://custom.openrouter.ai/api/v1",
74+
"test-api-key",
75+
"openai/text-embedding-3-small",
76+
8191,
77+
)
78+
})
79+
80+
it("should throw error when API key is not provided", () => {
81+
expect(() => new OpenRouterEmbedder(undefined as any)).toThrow("API key is required")
82+
})
83+
84+
it("should throw error when API key is empty string", () => {
85+
expect(() => new OpenRouterEmbedder("")).toThrow("API key is required")
86+
})
87+
})
88+
89+
describe("embedderInfo", () => {
90+
it("should return openrouter as the embedder name", () => {
91+
const embedder = new OpenRouterEmbedder("test-api-key")
92+
// The embedderInfo getter in OpenRouterEmbedder overrides the parent class
93+
expect(embedder.embedderInfo).toEqual({ name: "openrouter" })
94+
})
95+
})
96+
})

0 commit comments

Comments
 (0)