|
3 | 3 | import { Anthropic } from "@anthropic-ai/sdk" |
4 | 4 | import OpenAI from "openai" |
5 | 5 |
|
6 | | -import { friendliDefaultModelId, friendliModels } from "@roo-code/types" |
| 6 | +import { friendliDefaultModelId, friendliModels, openAiModelInfoSaneDefaults } from "@roo-code/types" |
7 | 7 |
|
8 | 8 | import { buildApiHandler } from "../../index" |
9 | 9 | import { getModelMaxOutputTokens } from "../../../shared/api" |
@@ -87,13 +87,51 @@ describe("FriendliHandler", () => { |
87 | 87 | expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: friendliApiKey })) |
88 | 88 | }) |
89 | 89 |
|
90 | | - it("should use fetched model info, keeping a model id missing from the static list", async () => { |
| 90 | + it("should construct without an API key and without fetching the catalog", () => { |
| 91 | + expect(() => new FriendliHandler({})).not.toThrow() |
| 92 | + expect(mockGetModels).not.toHaveBeenCalled() |
| 93 | + }) |
| 94 | + |
| 95 | + it("should load the catalog once before building requests for fetched-only models", async () => { |
| 96 | + const info = { maxTokens: 8192, contextWindow: 100_000, supportsPromptCache: false } |
| 97 | + mockGetModels.mockResolvedValueOnce({ "vendor/new-model": info }) |
| 98 | + mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] }) |
| 99 | + const handlerWithModel = new FriendliHandler({ apiModelId: "vendor/new-model", friendliApiKey: "test-key" }) |
| 100 | + |
| 101 | + await handlerWithModel.completePrompt("a") |
| 102 | + await handlerWithModel.completePrompt("b") |
| 103 | + |
| 104 | + expect(mockGetModels).toHaveBeenCalledTimes(1) |
| 105 | + expect(handlerWithModel.getModel()).toEqual(expect.objectContaining({ id: "vendor/new-model", info })) |
| 106 | + }) |
| 107 | + |
| 108 | + it("should wait for the catalog before streaming", async () => { |
91 | 109 | const info = { maxTokens: 8192, contextWindow: 100_000, supportsPromptCache: false } |
92 | 110 | mockGetModels.mockResolvedValueOnce({ "vendor/new-model": info }) |
93 | 111 | const handlerWithModel = new FriendliHandler({ apiModelId: "vendor/new-model", friendliApiKey: "test-key" }) |
94 | 112 |
|
95 | | - await vi.waitFor(() => expect(handlerWithModel.getModel().info).toEqual(info)) |
96 | | - expect(handlerWithModel.getModel().id).toBe("vendor/new-model") |
| 113 | + await handlerWithModel.createMessage("system", []).next() |
| 114 | + |
| 115 | + expect(mockCreate.mock.calls[0][0]).toEqual(expect.objectContaining({ max_tokens: 8192 })) |
| 116 | + }) |
| 117 | + |
| 118 | + it("should fall back to static models when the catalog is empty", async () => { |
| 119 | + mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] }) |
| 120 | + await handler.completePrompt("a") |
| 121 | + |
| 122 | + expect(handler.getModel().info).toEqual(friendliModels[friendliDefaultModelId]) |
| 123 | + }) |
| 124 | + |
| 125 | + it("should keep an unknown model id with default metadata when the catalog fails", async () => { |
| 126 | + mockGetModels.mockRejectedValueOnce(new Error("Network error")) |
| 127 | + mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] }) |
| 128 | + const handlerWithModel = new FriendliHandler({ apiModelId: "vendor/unknown", friendliApiKey: "test-key" }) |
| 129 | + |
| 130 | + await handlerWithModel.completePrompt("a") |
| 131 | + |
| 132 | + expect(handlerWithModel.getModel()).toEqual( |
| 133 | + expect.objectContaining({ id: "vendor/unknown", info: openAiModelInfoSaneDefaults }), |
| 134 | + ) |
97 | 135 | }) |
98 | 136 |
|
99 | 137 | it("fetchModels should load the public /v1/models catalog", async () => { |
|
0 commit comments