|
| 1 | +// npx vitest run api/providers/__tests__/umans.spec.ts |
| 2 | + |
| 3 | +vitest.mock("../utils/timeout-config", () => ({ |
| 4 | + getApiRequestTimeout: vitest.fn().mockReturnValue(300_000), |
| 5 | +})) |
| 6 | + |
| 7 | +const MOCK_TIMEOUT_MS = 300_000 |
| 8 | + |
| 9 | +import { Anthropic } from "@anthropic-ai/sdk" |
| 10 | +import OpenAI from "openai" |
| 11 | + |
| 12 | +import { UmansHandler } from "../umans" |
| 13 | +import type { ApiHandlerOptions } from "../../../shared/api" |
| 14 | +import { Package } from "../../../shared/package" |
| 15 | + |
| 16 | +const mockCreate = vitest.fn() |
| 17 | + |
| 18 | +vitest.mock("openai", () => ({ |
| 19 | + default: vitest.fn().mockImplementation(function () { |
| 20 | + return { |
| 21 | + chat: { |
| 22 | + completions: { |
| 23 | + create: mockCreate, |
| 24 | + }, |
| 25 | + }, |
| 26 | + } |
| 27 | + }), |
| 28 | +})) |
| 29 | + |
| 30 | +vitest.mock("../fetchers/modelCache", () => ({ |
| 31 | + getModels: vitest.fn().mockResolvedValue({ |
| 32 | + "umans-coder": { |
| 33 | + maxTokens: 32768, |
| 34 | + contextWindow: 262144, |
| 35 | + supportsImages: true, |
| 36 | + supportsPromptCache: false, |
| 37 | + supportsMaxTokens: true, |
| 38 | + inputPrice: 0.95, |
| 39 | + outputPrice: 4, |
| 40 | + description: "Umans Coder", |
| 41 | + }, |
| 42 | + "umans-glm-5.2": { |
| 43 | + maxTokens: 131071, |
| 44 | + contextWindow: 405504, |
| 45 | + supportsImages: true, |
| 46 | + supportsPromptCache: false, |
| 47 | + supportsMaxTokens: true, |
| 48 | + supportsReasoningEffort: ["none", "high", "max"], |
| 49 | + reasoningEffort: "high", |
| 50 | + inputPrice: 1.4, |
| 51 | + outputPrice: 4.4, |
| 52 | + description: "Umans GLM 5.2", |
| 53 | + }, |
| 54 | + }), |
| 55 | +})) |
| 56 | + |
| 57 | +describe("UmansHandler", () => { |
| 58 | + const mockOptions: ApiHandlerOptions = { |
| 59 | + umansApiKey: "test-key", |
| 60 | + umansModelId: "umans-coder", |
| 61 | + } |
| 62 | + |
| 63 | + beforeEach(() => vitest.clearAllMocks()) |
| 64 | + |
| 65 | + it("initializes with the Umans base URL and API key", () => { |
| 66 | + new UmansHandler(mockOptions) |
| 67 | + |
| 68 | + expect(OpenAI).toHaveBeenCalledWith({ |
| 69 | + baseURL: "https://api.code.umans.ai/v1", |
| 70 | + apiKey: "test-key", |
| 71 | + defaultHeaders: { |
| 72 | + "HTTP-Referer": "https://github.com/Zoo-Code-Org/Zoo-Code", |
| 73 | + "X-Title": "Zoo Code", |
| 74 | + "User-Agent": `ZooCode/${Package.version}`, |
| 75 | + }, |
| 76 | + timeout: MOCK_TIMEOUT_MS, |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + it("returns the default model when no options are provided", async () => { |
| 81 | + const handler = new UmansHandler({}) |
| 82 | + const result = await handler.fetchModel() |
| 83 | + |
| 84 | + expect(result.id).toBe("umans-coder") |
| 85 | + expect(result.info.description).toBe("Umans Coder") |
| 86 | + }) |
| 87 | + |
| 88 | + it("uses the provider's default OpenAI reasoning payload for Umans GLM models", async () => { |
| 89 | + const handler = new UmansHandler({ |
| 90 | + umansApiKey: "test-key", |
| 91 | + umansModelId: "umans-glm-5.2", |
| 92 | + reasoningEffort: "max", |
| 93 | + }) |
| 94 | + |
| 95 | + const mockStream = { |
| 96 | + async *[Symbol.asyncIterator]() { |
| 97 | + yield { |
| 98 | + choices: [{ delta: { content: "done" } }], |
| 99 | + } |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + mockCreate.mockResolvedValue(mockStream) |
| 104 | + |
| 105 | + const generator = handler.createMessage("system prompt", [{ role: "user" as const, content: "test" }]) |
| 106 | + await generator.next() |
| 107 | + |
| 108 | + expect(mockCreate).toHaveBeenCalledWith( |
| 109 | + expect.objectContaining({ |
| 110 | + model: "umans-glm-5.2", |
| 111 | + reasoning_effort: "max", |
| 112 | + stream: true, |
| 113 | + }), |
| 114 | + ) |
| 115 | + }) |
| 116 | +}) |
0 commit comments