Skip to content

Commit 270105b

Browse files
pugazhendhi-mvigneshsubbiah16
authored andcommitted
Adds test file
1 parent 698ae65 commit 270105b

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
"eslint": "^8.57.0",
249249
"husky": "^9.1.7",
250250
"jest": "^29.7.0",
251+
"jest-fetch-mock": "^3.0.3",
251252
"jest-simple-dot-reporter": "^1.0.5",
252253
"lint-staged": "^15.2.11",
253254
"npm-run-all": "^4.1.5",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { UnboundHandler } from "../unbound"
2+
import { ApiHandlerOptions } from "../../../shared/api"
3+
import fetchMock from "jest-fetch-mock"
4+
5+
fetchMock.enableMocks()
6+
7+
describe("UnboundHandler", () => {
8+
const mockOptions: ApiHandlerOptions = {
9+
unboundApiKey: "test-api-key",
10+
apiModelId: "test-model-id",
11+
}
12+
13+
beforeEach(() => {
14+
fetchMock.resetMocks()
15+
})
16+
17+
it("should initialize with options", () => {
18+
const handler = new UnboundHandler(mockOptions)
19+
expect(handler).toBeDefined()
20+
})
21+
22+
it("should create a message successfully", async () => {
23+
const handler = new UnboundHandler(mockOptions)
24+
const mockResponse = {
25+
choices: [{ message: { content: "Hello, world!" } }],
26+
usage: { prompt_tokens: 5, completion_tokens: 7 },
27+
}
28+
29+
fetchMock.mockResponseOnce(JSON.stringify(mockResponse))
30+
31+
const generator = handler.createMessage("system prompt", [])
32+
const textResult = await generator.next()
33+
const usageResult = await generator.next()
34+
35+
expect(textResult.value).toEqual({ type: "text", text: "Hello, world!" })
36+
expect(usageResult.value).toEqual({
37+
type: "usage",
38+
inputTokens: 5,
39+
outputTokens: 7,
40+
})
41+
})
42+
43+
it("should handle API errors", async () => {
44+
const handler = new UnboundHandler(mockOptions)
45+
fetchMock.mockResponseOnce(JSON.stringify({ error: "API error" }), { status: 400 })
46+
47+
const generator = handler.createMessage("system prompt", [])
48+
await expect(generator.next()).rejects.toThrow("Unbound Gateway completion error: API error")
49+
})
50+
51+
it("should handle network errors", async () => {
52+
const handler = new UnboundHandler(mockOptions)
53+
fetchMock.mockRejectOnce(new Error("Network error"))
54+
55+
const generator = handler.createMessage("system prompt", [])
56+
await expect(generator.next()).rejects.toThrow("Unbound Gateway completion error: Network error")
57+
})
58+
59+
it("should return the correct model", () => {
60+
const handler = new UnboundHandler(mockOptions)
61+
const model = handler.getModel()
62+
expect(model.id).toBe("gpt-4o")
63+
})
64+
})

0 commit comments

Comments
 (0)