|
| 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