|
| 1 | +import React from "react" |
| 2 | + |
| 3 | +import { OpenAiServiceTier, providerIdentifiers, type ModelInfo, type ProviderSettings } from "@roo-code/types" |
| 4 | + |
| 5 | +import { fireEvent, render, screen } from "@/utils/test-utils" |
| 6 | + |
| 7 | +import { OpenAI } from "../OpenAI" |
| 8 | + |
| 9 | +vi.mock("@src/i18n/TranslationContext", () => ({ |
| 10 | + useAppTranslation: () => ({ t: (key: string) => key }), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock("vscrui", () => ({ |
| 14 | + Checkbox: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock("@src/components/ui", () => ({ |
| 18 | + Select: ({ children, value, onValueChange }: any) => ( |
| 19 | + <select aria-label="Service tier" value={value} onChange={(event) => onValueChange(event.target.value)}> |
| 20 | + {children} |
| 21 | + </select> |
| 22 | + ), |
| 23 | + SelectContent: ({ children }: any) => <>{children}</>, |
| 24 | + SelectItem: ({ children, value }: any) => <option value={value}>{children}</option>, |
| 25 | + SelectTrigger: () => null, |
| 26 | + SelectValue: () => null, |
| 27 | + StandardTooltip: ({ children, content }: any) => <span title={content}>{children}</span>, |
| 28 | +})) |
| 29 | + |
| 30 | +const baseModelInfo: ModelInfo = { |
| 31 | + contextWindow: 128_000, |
| 32 | + supportsPromptCache: true, |
| 33 | +} |
| 34 | + |
| 35 | +describe("OpenAI service tier selector", () => { |
| 36 | + it("shows supported service tiers and persists the selected tier", () => { |
| 37 | + const setApiConfigurationField = vi.fn() |
| 38 | + const selectedModelInfo: ModelInfo = { |
| 39 | + ...baseModelInfo, |
| 40 | + tiers: [ |
| 41 | + { name: OpenAiServiceTier.Default, contextWindow: 128_000 }, |
| 42 | + { contextWindow: 128_000 }, |
| 43 | + { name: OpenAiServiceTier.Flex, contextWindow: 128_000 }, |
| 44 | + { name: OpenAiServiceTier.Priority, contextWindow: 128_000 }, |
| 45 | + ], |
| 46 | + } |
| 47 | + const apiConfiguration: ProviderSettings = { |
| 48 | + apiProvider: providerIdentifiers.openaiNative, |
| 49 | + openAiNativeApiKey: "test-api-key", |
| 50 | + } |
| 51 | + |
| 52 | + render( |
| 53 | + <OpenAI |
| 54 | + apiConfiguration={apiConfiguration} |
| 55 | + setApiConfigurationField={setApiConfigurationField} |
| 56 | + selectedModelInfo={selectedModelInfo} |
| 57 | + />, |
| 58 | + ) |
| 59 | + |
| 60 | + const selector = screen.getByRole("combobox", { name: "Service tier" }) |
| 61 | + expect(selector).toHaveValue(OpenAiServiceTier.Default) |
| 62 | + expect(screen.getAllByRole("option").map((option) => option.textContent)).toEqual([ |
| 63 | + "Standard", |
| 64 | + "Flex", |
| 65 | + "Priority", |
| 66 | + ]) |
| 67 | + |
| 68 | + fireEvent.change(selector, { target: { value: OpenAiServiceTier.Flex } }) |
| 69 | + expect(setApiConfigurationField).toHaveBeenLastCalledWith("openAiNativeServiceTier", OpenAiServiceTier.Flex) |
| 70 | + |
| 71 | + fireEvent.change(selector, { target: { value: OpenAiServiceTier.Priority } }) |
| 72 | + expect(setApiConfigurationField).toHaveBeenLastCalledWith("openAiNativeServiceTier", OpenAiServiceTier.Priority) |
| 73 | + }) |
| 74 | + |
| 75 | + it("hides the selector when the model only exposes the default tier", () => { |
| 76 | + render( |
| 77 | + <OpenAI |
| 78 | + apiConfiguration={{ |
| 79 | + apiProvider: providerIdentifiers.openaiNative, |
| 80 | + openAiNativeApiKey: "test-api-key", |
| 81 | + }} |
| 82 | + setApiConfigurationField={vi.fn()} |
| 83 | + selectedModelInfo={{ |
| 84 | + ...baseModelInfo, |
| 85 | + tiers: [{ name: OpenAiServiceTier.Default, contextWindow: 128_000 }], |
| 86 | + }} |
| 87 | + />, |
| 88 | + ) |
| 89 | + |
| 90 | + expect(screen.queryByTestId("openai-service-tier")).not.toBeInTheDocument() |
| 91 | + }) |
| 92 | +}) |
0 commit comments