|
1 | 1 | // npx vitest run api/providers/__tests__/openai.spec.ts
|
2 | 2 |
|
3 |
| -import { OpenAiHandler } from "../openai" |
| 3 | +import { OpenAiHandler, getOpenAiModels } from "../openai" |
4 | 4 | import { ApiHandlerOptions } from "../../../shared/api"
|
5 | 5 | import { Anthropic } from "@anthropic-ai/sdk"
|
6 | 6 | import OpenAI from "openai"
|
7 | 7 | import { openAiModelInfoSaneDefaults } from "@roo-code/types"
|
8 | 8 | import { Package } from "../../../shared/package"
|
| 9 | +import axios from "axios" |
9 | 10 |
|
10 | 11 | const mockCreate = vitest.fn()
|
11 | 12 |
|
@@ -68,6 +69,13 @@ vitest.mock("openai", () => {
|
68 | 69 | }
|
69 | 70 | })
|
70 | 71 |
|
| 72 | +// Mock axios for getOpenAiModels tests |
| 73 | +vitest.mock("axios", () => ({ |
| 74 | + default: { |
| 75 | + get: vitest.fn(), |
| 76 | + }, |
| 77 | +})) |
| 78 | + |
71 | 79 | describe("OpenAiHandler", () => {
|
72 | 80 | let handler: OpenAiHandler
|
73 | 81 | let mockOptions: ApiHandlerOptions
|
@@ -776,3 +784,143 @@ describe("OpenAiHandler", () => {
|
776 | 784 | })
|
777 | 785 | })
|
778 | 786 | })
|
| 787 | + |
| 788 | +describe("getOpenAiModels", () => { |
| 789 | + beforeEach(() => { |
| 790 | + vi.mocked(axios.get).mockClear() |
| 791 | + }) |
| 792 | + |
| 793 | + it("should return empty array when baseUrl is not provided", async () => { |
| 794 | + const result = await getOpenAiModels(undefined, "test-key") |
| 795 | + expect(result).toEqual([]) |
| 796 | + expect(axios.get).not.toHaveBeenCalled() |
| 797 | + }) |
| 798 | + |
| 799 | + it("should return empty array when baseUrl is empty string", async () => { |
| 800 | + const result = await getOpenAiModels("", "test-key") |
| 801 | + expect(result).toEqual([]) |
| 802 | + expect(axios.get).not.toHaveBeenCalled() |
| 803 | + }) |
| 804 | + |
| 805 | + it("should trim whitespace from baseUrl", async () => { |
| 806 | + const mockResponse = { |
| 807 | + data: { |
| 808 | + data: [{ id: "gpt-4" }, { id: "gpt-3.5-turbo" }], |
| 809 | + }, |
| 810 | + } |
| 811 | + vi.mocked(axios.get).mockResolvedValueOnce(mockResponse) |
| 812 | + |
| 813 | + const result = await getOpenAiModels(" https://api.openai.com/v1 ", "test-key") |
| 814 | + |
| 815 | + expect(axios.get).toHaveBeenCalledWith("https://api.openai.com/v1/models", expect.any(Object)) |
| 816 | + expect(result).toEqual(["gpt-4", "gpt-3.5-turbo"]) |
| 817 | + }) |
| 818 | + |
| 819 | + it("should handle baseUrl with trailing spaces", async () => { |
| 820 | + const mockResponse = { |
| 821 | + data: { |
| 822 | + data: [{ id: "model-1" }, { id: "model-2" }], |
| 823 | + }, |
| 824 | + } |
| 825 | + vi.mocked(axios.get).mockResolvedValueOnce(mockResponse) |
| 826 | + |
| 827 | + const result = await getOpenAiModels("https://api.example.com/v1 ", "test-key") |
| 828 | + |
| 829 | + expect(axios.get).toHaveBeenCalledWith("https://api.example.com/v1/models", expect.any(Object)) |
| 830 | + expect(result).toEqual(["model-1", "model-2"]) |
| 831 | + }) |
| 832 | + |
| 833 | + it("should handle baseUrl with leading spaces", async () => { |
| 834 | + const mockResponse = { |
| 835 | + data: { |
| 836 | + data: [{ id: "model-1" }], |
| 837 | + }, |
| 838 | + } |
| 839 | + vi.mocked(axios.get).mockResolvedValueOnce(mockResponse) |
| 840 | + |
| 841 | + const result = await getOpenAiModels(" https://api.example.com/v1", "test-key") |
| 842 | + |
| 843 | + expect(axios.get).toHaveBeenCalledWith("https://api.example.com/v1/models", expect.any(Object)) |
| 844 | + expect(result).toEqual(["model-1"]) |
| 845 | + }) |
| 846 | + |
| 847 | + it("should return empty array for invalid URL after trimming", async () => { |
| 848 | + const result = await getOpenAiModels(" not-a-valid-url ", "test-key") |
| 849 | + expect(result).toEqual([]) |
| 850 | + expect(axios.get).not.toHaveBeenCalled() |
| 851 | + }) |
| 852 | + |
| 853 | + it("should include authorization header when apiKey is provided", async () => { |
| 854 | + const mockResponse = { |
| 855 | + data: { |
| 856 | + data: [{ id: "model-1" }], |
| 857 | + }, |
| 858 | + } |
| 859 | + vi.mocked(axios.get).mockResolvedValueOnce(mockResponse) |
| 860 | + |
| 861 | + await getOpenAiModels("https://api.example.com/v1", "test-api-key") |
| 862 | + |
| 863 | + expect(axios.get).toHaveBeenCalledWith( |
| 864 | + "https://api.example.com/v1/models", |
| 865 | + expect.objectContaining({ |
| 866 | + headers: expect.objectContaining({ |
| 867 | + Authorization: "Bearer test-api-key", |
| 868 | + }), |
| 869 | + }), |
| 870 | + ) |
| 871 | + }) |
| 872 | + |
| 873 | + it("should include custom headers when provided", async () => { |
| 874 | + const mockResponse = { |
| 875 | + data: { |
| 876 | + data: [{ id: "model-1" }], |
| 877 | + }, |
| 878 | + } |
| 879 | + vi.mocked(axios.get).mockResolvedValueOnce(mockResponse) |
| 880 | + |
| 881 | + const customHeaders = { |
| 882 | + "X-Custom-Header": "custom-value", |
| 883 | + } |
| 884 | + |
| 885 | + await getOpenAiModels("https://api.example.com/v1", "test-key", customHeaders) |
| 886 | + |
| 887 | + expect(axios.get).toHaveBeenCalledWith( |
| 888 | + "https://api.example.com/v1/models", |
| 889 | + expect.objectContaining({ |
| 890 | + headers: expect.objectContaining({ |
| 891 | + "X-Custom-Header": "custom-value", |
| 892 | + Authorization: "Bearer test-key", |
| 893 | + }), |
| 894 | + }), |
| 895 | + ) |
| 896 | + }) |
| 897 | + |
| 898 | + it("should handle API errors gracefully", async () => { |
| 899 | + vi.mocked(axios.get).mockRejectedValueOnce(new Error("Network error")) |
| 900 | + |
| 901 | + const result = await getOpenAiModels("https://api.example.com/v1", "test-key") |
| 902 | + |
| 903 | + expect(result).toEqual([]) |
| 904 | + }) |
| 905 | + |
| 906 | + it("should handle malformed response data", async () => { |
| 907 | + vi.mocked(axios.get).mockResolvedValueOnce({ data: null }) |
| 908 | + |
| 909 | + const result = await getOpenAiModels("https://api.example.com/v1", "test-key") |
| 910 | + |
| 911 | + expect(result).toEqual([]) |
| 912 | + }) |
| 913 | + |
| 914 | + it("should deduplicate model IDs", async () => { |
| 915 | + const mockResponse = { |
| 916 | + data: { |
| 917 | + data: [{ id: "gpt-4" }, { id: "gpt-4" }, { id: "gpt-3.5-turbo" }, { id: "gpt-4" }], |
| 918 | + }, |
| 919 | + } |
| 920 | + vi.mocked(axios.get).mockResolvedValueOnce(mockResponse) |
| 921 | + |
| 922 | + const result = await getOpenAiModels("https://api.example.com/v1", "test-key") |
| 923 | + |
| 924 | + expect(result).toEqual(["gpt-4", "gpt-3.5-turbo"]) |
| 925 | + }) |
| 926 | +}) |
0 commit comments