|
| 1 | +import { beforeAll, describe, expect, it } from "vitest"; |
| 2 | +import { OpenRouter } from "../../src/sdk/sdk.js"; |
| 3 | + |
| 4 | +describe("Chat E2E Tests", () => { |
| 5 | + let client: OpenRouter; |
| 6 | + |
| 7 | + beforeAll(() => { |
| 8 | + const apiKey = process.env.OPENROUTER_API_KEY; |
| 9 | + if (!apiKey) { |
| 10 | + throw new Error( |
| 11 | + "OPENROUTER_API_KEY environment variable is required for e2e tests" |
| 12 | + ); |
| 13 | + } |
| 14 | + |
| 15 | + client = new OpenRouter({ |
| 16 | + apiKey, |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + describe("chat.send() - Non-streaming", () => { |
| 21 | + it("should successfully send a chat request and get a response", async () => { |
| 22 | + const response = await client.chat.send({ |
| 23 | + model: "meta-llama/llama-3.2-1b-instruct", |
| 24 | + messages: [ |
| 25 | + { |
| 26 | + role: "user", |
| 27 | + content: "Say 'Hello, World!' and nothing else.", |
| 28 | + }, |
| 29 | + ], |
| 30 | + stream: false, |
| 31 | + }); |
| 32 | + |
| 33 | + expect(response).toBeDefined(); |
| 34 | + |
| 35 | + |
| 36 | + expect(Array.isArray(response.choices)).toBe(true); |
| 37 | + expect(response.choices.length).toBeGreaterThan(0); |
| 38 | + |
| 39 | + const firstChoice = response.choices[0]; |
| 40 | + expect(firstChoice).toBeDefined(); |
| 41 | + expect(firstChoice?.message).toBeDefined(); |
| 42 | + expect(firstChoice?.message?.content).toBeDefined(); |
| 43 | + expect(typeof firstChoice?.message?.content).toBe("string"); |
| 44 | + |
| 45 | + // Verify it has usage information |
| 46 | + expect(response.usage).toBeDefined(); |
| 47 | + expect(response.usage?.totalTokens).toBeGreaterThan(0); |
| 48 | + |
| 49 | + }); |
| 50 | + |
| 51 | + it("should handle multi-turn conversations", async () => { |
| 52 | + const response = await client.chat.send({ |
| 53 | + model: "meta-llama/llama-3.2-1b-instruct", |
| 54 | + messages: [ |
| 55 | + { |
| 56 | + role: "user", |
| 57 | + content: "My name is Alice.", |
| 58 | + }, |
| 59 | + { |
| 60 | + role: "assistant", |
| 61 | + content: "Hello Alice! How can I help you today?", |
| 62 | + }, |
| 63 | + { |
| 64 | + role: "user", |
| 65 | + content: "What is my name?", |
| 66 | + }, |
| 67 | + ], |
| 68 | + stream: false, |
| 69 | + }); |
| 70 | + |
| 71 | + expect(response).toBeDefined(); |
| 72 | + |
| 73 | + const content = typeof response.choices[0]?.message?.content === "string" ? response.choices[0]?.message?.content?.toLowerCase() : response.choices[0]?.message?.content?.map((item) => item.type === "text" ? item.text : "").join("").toLowerCase(); |
| 74 | + expect(content).toBeDefined(); |
| 75 | + expect(content).toContain("alice"); |
| 76 | + |
| 77 | + }); |
| 78 | + |
| 79 | + it("should respect max_tokens parameter", async () => { |
| 80 | + const response = await client.chat.send({ |
| 81 | + model: "meta-llama/llama-3.2-1b-instruct", |
| 82 | + messages: [ |
| 83 | + { |
| 84 | + role: "user", |
| 85 | + content: "Write a long story about a cat.", |
| 86 | + }, |
| 87 | + ], |
| 88 | + maxTokens: 10, |
| 89 | + stream: false, |
| 90 | + }); |
| 91 | + |
| 92 | + expect(response).toBeDefined(); |
| 93 | + |
| 94 | + expect(response.usage?.completionTokens).toBeLessThanOrEqual(10); |
| 95 | + |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe("chat.send() - Streaming", () => { |
| 100 | + it("should successfully stream chat responses", async () => { |
| 101 | + const response = await client.chat.send({ |
| 102 | + model: "meta-llama/llama-3.2-1b-instruct", |
| 103 | + messages: [ |
| 104 | + { |
| 105 | + role: "user", |
| 106 | + content: "Count from 1 to 5.", |
| 107 | + }, |
| 108 | + ], |
| 109 | + stream: true, |
| 110 | + }); |
| 111 | + |
| 112 | + expect(response).toBeDefined(); |
| 113 | + |
| 114 | + const chunks: any[] = []; |
| 115 | + |
| 116 | + for await (const chunk of response) { |
| 117 | + expect(chunk).toBeDefined(); |
| 118 | + chunks.push(chunk); |
| 119 | + } |
| 120 | + |
| 121 | + expect(chunks.length).toBeGreaterThan(0); |
| 122 | + |
| 123 | + // Verify chunks have expected structure |
| 124 | + const firstChunk = chunks[0]; |
| 125 | + expect(firstChunk?.choices).toBeDefined(); |
| 126 | + expect(Array.isArray(firstChunk?.choices)).toBe(true); |
| 127 | + |
| 128 | + }); |
| 129 | + |
| 130 | + it("should stream complete content progressively", async () => { |
| 131 | + const response = await client.chat.send({ |
| 132 | + model: "meta-llama/llama-3.2-1b-instruct", |
| 133 | + messages: [ |
| 134 | + { |
| 135 | + role: "user", |
| 136 | + content: "Say 'test'.", |
| 137 | + }, |
| 138 | + ], |
| 139 | + stream: true, |
| 140 | + }); |
| 141 | + |
| 142 | + expect(response).toBeDefined(); |
| 143 | + |
| 144 | + let fullContent = ""; |
| 145 | + let chunkCount = 0; |
| 146 | + |
| 147 | + for await (const chunk of response) { |
| 148 | + chunkCount++; |
| 149 | + const delta = chunk.choices?.[0]?.delta; |
| 150 | + if (delta?.content) { |
| 151 | + fullContent += delta.content; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + expect(chunkCount).toBeGreaterThan(0); |
| 156 | + expect(fullContent.length).toBeGreaterThan(0); |
| 157 | + } |
| 158 | + |
| 159 | + it("should include finish_reason in final chunk", async () => { |
| 160 | + const response = await client.chat.send({ |
| 161 | + model: "meta-llama/llama-3.2-1b-instruct", |
| 162 | + messages: [ |
| 163 | + { |
| 164 | + role: "user", |
| 165 | + content: "Say 'done'.", |
| 166 | + }, |
| 167 | + ], |
| 168 | + stream: true, |
| 169 | + }); |
| 170 | + |
| 171 | + expect(response).toBeDefined(); |
| 172 | + |
| 173 | + let foundFinishReason = false; |
| 174 | + |
| 175 | + for await (const chunk of response) { |
| 176 | + const finishReason = chunk.choices?.[0]?.finishReason; |
| 177 | + if (finishReason) { |
| 178 | + foundFinishReason = true; |
| 179 | + expect(typeof finishReason).toBe("string"); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + expect(foundFinishReason).toBe(true); |
| 184 | + } |
| 185 | + }); |
| 186 | +}); |
0 commit comments