|
| 1 | +import type { ReactNode } from "react" |
| 2 | + |
| 3 | +import { providerIdentifiers, type ProviderSettings } from "@roo-code/types" |
| 4 | + |
| 5 | +import { render, screen } from "@/utils/test-utils" |
| 6 | + |
| 7 | +import { BedrockCustomArn } from "../BedrockCustomArn" |
| 8 | + |
| 9 | +type SelectMockProps = { |
| 10 | + children: ReactNode |
| 11 | + value: string |
| 12 | + onValueChange: (value: string) => void |
| 13 | + disabled?: boolean |
| 14 | +} |
| 15 | +type TextFieldMockProps = { |
| 16 | + children: ReactNode |
| 17 | + value: string |
| 18 | + onInput: (event: { target: { value: string } }) => void |
| 19 | + placeholder?: string |
| 20 | + className?: string |
| 21 | + "data-testid"?: string |
| 22 | +} |
| 23 | + |
| 24 | +vi.mock("@src/components/ui", () => ({ |
| 25 | + Select: ({ children, value, onValueChange, disabled }: SelectMockProps) => ( |
| 26 | + <select |
| 27 | + data-testid="base-model-select" |
| 28 | + value={value} |
| 29 | + disabled={disabled} |
| 30 | + onChange={(e) => onValueChange(e.target.value)}> |
| 31 | + {children} |
| 32 | + </select> |
| 33 | + ), |
| 34 | + SelectContent: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 35 | + SelectItem: ({ children, value }: { children: ReactNode; value: string }) => ( |
| 36 | + <option value={value}>{children}</option> |
| 37 | + ), |
| 38 | + SelectTrigger: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 39 | + SelectValue: () => null, |
| 40 | +})) |
| 41 | + |
| 42 | +vi.mock("@vscode/webview-ui-toolkit/react", () => ({ |
| 43 | + VSCodeTextField: ({ children, value, onInput, placeholder, "data-testid": testId }: TextFieldMockProps) => ( |
| 44 | + <label> |
| 45 | + <input |
| 46 | + data-testid={testId ?? "text-field"} |
| 47 | + value={value} |
| 48 | + placeholder={placeholder} |
| 49 | + onChange={(e) => onInput({ target: { value: e.target.value } })} |
| 50 | + /> |
| 51 | + {children} |
| 52 | + </label> |
| 53 | + ), |
| 54 | +})) |
| 55 | + |
| 56 | +vi.mock("@src/i18n/TranslationContext", () => ({ |
| 57 | + useAppTranslation: () => ({ |
| 58 | + t: (key: string, options?: Record<string, unknown>) => |
| 59 | + options ? Object.entries(options).reduce((acc: string, [k, v]) => `${acc} ${k}=${String(v)}`, key) : key, |
| 60 | + }), |
| 61 | +})) |
| 62 | + |
| 63 | +describe("BedrockCustomArn - the memoised ARN validation", () => { |
| 64 | + it("reports no problem for a session with no ARN typed yet", () => { |
| 65 | + const apiConfiguration: ProviderSettings = { |
| 66 | + apiProvider: providerIdentifiers.bedrock, |
| 67 | + awsRegion: "us-east-1", |
| 68 | + } |
| 69 | + const setApiConfigurationField = vi.fn() |
| 70 | + |
| 71 | + render( |
| 72 | + <BedrockCustomArn |
| 73 | + apiConfiguration={apiConfiguration} |
| 74 | + setApiConfigurationField={setApiConfigurationField} |
| 75 | + />, |
| 76 | + ) |
| 77 | + |
| 78 | + expect(screen.queryByText("settings:providers.invalidArnFormat")).toBeNull() |
| 79 | + expect(screen.queryByText(/regionMismatch/)).toBeNull() |
| 80 | + }) |
| 81 | + |
| 82 | + it("re-validates when a different ARN arrives, including a region that contradicts the setting", () => { |
| 83 | + const base: ProviderSettings = { apiProvider: providerIdentifiers.bedrock, awsRegion: "us-east-1" } |
| 84 | + const setApiConfigurationField = vi.fn() |
| 85 | + const mismatchedArn = |
| 86 | + "arn:aws:bedrock:eu-west-1:123456789012:inference-profile/eu.anthropic.claude-3-7-sonnet-20250219-v1:0" |
| 87 | + |
| 88 | + const { rerender } = render( |
| 89 | + <BedrockCustomArn apiConfiguration={base} setApiConfigurationField={setApiConfigurationField} />, |
| 90 | + ) |
| 91 | + expect(screen.queryByText(/regionMismatch/)).toBeNull() |
| 92 | + |
| 93 | + rerender( |
| 94 | + <BedrockCustomArn |
| 95 | + apiConfiguration={{ ...base, awsCustomArn: mismatchedArn }} |
| 96 | + setApiConfigurationField={setApiConfigurationField} |
| 97 | + />, |
| 98 | + ) |
| 99 | + |
| 100 | + expect( |
| 101 | + screen.getByText("settings:validation.arn.regionMismatch arnRegion=eu-west-1 region=us-east-1"), |
| 102 | + ).toBeDefined() |
| 103 | + }) |
| 104 | +}) |
0 commit comments