|
| 1 | +// npx vitest run api/providers/__tests__/perplexity.spec.ts |
| 2 | + |
| 3 | +import { Anthropic } from "@anthropic-ai/sdk" |
| 4 | +import OpenAI from "openai" |
| 5 | + |
| 6 | +import { type PerplexityModelId, perplexityDefaultModelId, perplexityModels } from "@roo-code/types" |
| 7 | + |
| 8 | +import { PerplexityHandler, resolvePerplexityApiKey } from "../perplexity" |
| 9 | + |
| 10 | +const mockCreate = vi.fn() |
| 11 | + |
| 12 | +vi.mock("openai", () => ({ |
| 13 | + default: vi.fn(() => ({ |
| 14 | + chat: { |
| 15 | + completions: { |
| 16 | + create: mockCreate, |
| 17 | + }, |
| 18 | + }, |
| 19 | + })), |
| 20 | +})) |
| 21 | + |
| 22 | +describe("PerplexityHandler", () => { |
| 23 | + let handler: PerplexityHandler |
| 24 | + const originalEnv = { ...process.env } |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + vi.clearAllMocks() |
| 28 | + mockCreate.mockImplementation(async () => ({ |
| 29 | + [Symbol.asyncIterator]: async function* () { |
| 30 | + yield { |
| 31 | + choices: [{ delta: { content: "Test response" }, index: 0 }], |
| 32 | + usage: null, |
| 33 | + } |
| 34 | + yield { |
| 35 | + choices: [{ delta: {}, index: 0 }], |
| 36 | + usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 }, |
| 37 | + } |
| 38 | + }, |
| 39 | + })) |
| 40 | + handler = new PerplexityHandler({ perplexityApiKey: "test-key" }) |
| 41 | + }) |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + vi.restoreAllMocks() |
| 45 | + process.env = { ...originalEnv } |
| 46 | + }) |
| 47 | + |
| 48 | + it("should use the correct Perplexity base URL", () => { |
| 49 | + new PerplexityHandler({ perplexityApiKey: "test-perplexity-api-key" }) |
| 50 | + expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ baseURL: "https://api.perplexity.ai" })) |
| 51 | + }) |
| 52 | + |
| 53 | + it("should use the provided API key from settings", () => { |
| 54 | + const perplexityApiKey = "test-perplexity-api-key" |
| 55 | + new PerplexityHandler({ perplexityApiKey }) |
| 56 | + expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: perplexityApiKey })) |
| 57 | + }) |
| 58 | + |
| 59 | + it("should fall back to PERPLEXITY_API_KEY env var when no settings key is provided", () => { |
| 60 | + delete process.env.PPLX_API_KEY |
| 61 | + process.env.PERPLEXITY_API_KEY = "env-perplexity-key" |
| 62 | + new PerplexityHandler({}) |
| 63 | + expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: "env-perplexity-key" })) |
| 64 | + }) |
| 65 | + |
| 66 | + it("should fall back to PPLX_API_KEY env var as a secondary fallback", () => { |
| 67 | + delete process.env.PERPLEXITY_API_KEY |
| 68 | + process.env.PPLX_API_KEY = "pplx-fallback-key" |
| 69 | + new PerplexityHandler({}) |
| 70 | + expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: "pplx-fallback-key" })) |
| 71 | + }) |
| 72 | + |
| 73 | + it("should prefer explicit settings API key over env vars", () => { |
| 74 | + process.env.PERPLEXITY_API_KEY = "env-key" |
| 75 | + process.env.PPLX_API_KEY = "pplx-key" |
| 76 | + new PerplexityHandler({ perplexityApiKey: "explicit-key" }) |
| 77 | + expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: "explicit-key" })) |
| 78 | + }) |
| 79 | + |
| 80 | + it("should throw when no API key is configured (settings or env vars)", () => { |
| 81 | + delete process.env.PERPLEXITY_API_KEY |
| 82 | + delete process.env.PPLX_API_KEY |
| 83 | + expect(() => new PerplexityHandler({})).toThrow("API key is required") |
| 84 | + }) |
| 85 | + |
| 86 | + it("resolvePerplexityApiKey should return undefined when nothing is set", () => { |
| 87 | + delete process.env.PERPLEXITY_API_KEY |
| 88 | + delete process.env.PPLX_API_KEY |
| 89 | + expect(resolvePerplexityApiKey()).toBeUndefined() |
| 90 | + expect(resolvePerplexityApiKey("")).toBeUndefined() |
| 91 | + }) |
| 92 | + |
| 93 | + it("should return default sonar-pro model when no model is specified", () => { |
| 94 | + const model = handler.getModel() |
| 95 | + expect(model.id).toBe(perplexityDefaultModelId) |
| 96 | + expect(model.id).toBe("sonar-pro") |
| 97 | + expect(model.info).toEqual(expect.objectContaining(perplexityModels[perplexityDefaultModelId])) |
| 98 | + }) |
| 99 | + |
| 100 | + it("should return sonar-reasoning-pro model when configured", () => { |
| 101 | + const testModelId: PerplexityModelId = "sonar-reasoning-pro" |
| 102 | + const handlerWithModel = new PerplexityHandler({ |
| 103 | + apiModelId: testModelId, |
| 104 | + perplexityApiKey: "test-key", |
| 105 | + }) |
| 106 | + const model = handlerWithModel.getModel() |
| 107 | + expect(model.id).toBe(testModelId) |
| 108 | + expect(model.info).toEqual( |
| 109 | + expect.objectContaining({ |
| 110 | + maxTokens: 8192, |
| 111 | + contextWindow: 128_000, |
| 112 | + supportsImages: false, |
| 113 | + supportsPromptCache: false, |
| 114 | + inputPrice: 2.0, |
| 115 | + outputPrice: 8.0, |
| 116 | + }), |
| 117 | + ) |
| 118 | + }) |
| 119 | + |
| 120 | + it("should fall back to default model when an unknown model id is provided", () => { |
| 121 | + const handlerWithModel = new PerplexityHandler({ |
| 122 | + apiModelId: "not-a-real-model", |
| 123 | + perplexityApiKey: "test-key", |
| 124 | + }) |
| 125 | + const model = handlerWithModel.getModel() |
| 126 | + expect(model.id).toBe(perplexityDefaultModelId) |
| 127 | + }) |
| 128 | + |
| 129 | + it("should expose all four Sonar models with 128k context", () => { |
| 130 | + const expectedIds: PerplexityModelId[] = ["sonar", "sonar-pro", "sonar-reasoning", "sonar-reasoning-pro"] |
| 131 | + for (const id of expectedIds) { |
| 132 | + expect(perplexityModels[id]).toBeDefined() |
| 133 | + expect(perplexityModels[id].contextWindow).toBe(128_000) |
| 134 | + } |
| 135 | + }) |
| 136 | + |
| 137 | + it("createMessage should yield text content from stream", async () => { |
| 138 | + const testContent = "Streamed content from Perplexity" |
| 139 | + mockCreate.mockImplementationOnce(() => ({ |
| 140 | + [Symbol.asyncIterator]: () => ({ |
| 141 | + next: vi |
| 142 | + .fn() |
| 143 | + .mockResolvedValueOnce({ |
| 144 | + done: false, |
| 145 | + value: { choices: [{ delta: { content: testContent } }] }, |
| 146 | + }) |
| 147 | + .mockResolvedValueOnce({ done: true }), |
| 148 | + }), |
| 149 | + })) |
| 150 | + |
| 151 | + const stream = handler.createMessage("system prompt", []) |
| 152 | + const firstChunk = await stream.next() |
| 153 | + expect(firstChunk.done).toBe(false) |
| 154 | + expect(firstChunk.value).toEqual({ type: "text", text: testContent }) |
| 155 | + }) |
| 156 | + |
| 157 | + it("createMessage should pass the configured model id to the upstream client", async () => { |
| 158 | + const modelId: PerplexityModelId = "sonar-reasoning" |
| 159 | + const handlerWithModel = new PerplexityHandler({ |
| 160 | + apiModelId: modelId, |
| 161 | + perplexityApiKey: "test-key", |
| 162 | + }) |
| 163 | + |
| 164 | + mockCreate.mockImplementationOnce(() => ({ |
| 165 | + [Symbol.asyncIterator]: () => ({ |
| 166 | + async next() { |
| 167 | + return { done: true } |
| 168 | + }, |
| 169 | + }), |
| 170 | + })) |
| 171 | + |
| 172 | + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "hi" }] |
| 173 | + const generator = handlerWithModel.createMessage("system", messages) |
| 174 | + await generator.next() |
| 175 | + |
| 176 | + expect(mockCreate).toHaveBeenCalledWith( |
| 177 | + expect.objectContaining({ |
| 178 | + model: modelId, |
| 179 | + stream: true, |
| 180 | + stream_options: { include_usage: true }, |
| 181 | + messages: expect.arrayContaining([{ role: "system", content: "system" }]), |
| 182 | + }), |
| 183 | + undefined, |
| 184 | + ) |
| 185 | + }) |
| 186 | + |
| 187 | + it("createMessage should propagate upstream errors", async () => { |
| 188 | + mockCreate.mockImplementationOnce(() => { |
| 189 | + throw new Error("upstream 401") |
| 190 | + }) |
| 191 | + |
| 192 | + const generator = handler.createMessage("system", [{ role: "user", content: "hi" }]) |
| 193 | + await expect(generator.next()).rejects.toThrow(/upstream 401/) |
| 194 | + }) |
| 195 | +}) |
0 commit comments