|
| 1 | +import "dotenv/config"; |
| 2 | +import { beforeAll, beforeEach, describe, expect, test } from "vitest"; |
| 3 | + |
| 4 | +import { |
| 5 | + beforeAllHook, |
| 6 | + beforeEachHook, |
| 7 | + getConcurrentTestOptions, |
| 8 | + getTestOptions, |
| 9 | + logMode, |
| 10 | + testModels, |
| 11 | + validateLogByRequestId, |
| 12 | + validateResponse, |
| 13 | +} from "@/chat-helpers.e2e.js"; |
| 14 | + |
| 15 | +import { models } from "@llmgateway/models"; |
| 16 | + |
| 17 | +import { app } from "./app.js"; |
| 18 | + |
| 19 | +import type { ProviderModelMapping } from "@llmgateway/models"; |
| 20 | + |
| 21 | +// Helper function to generate unique request IDs for tests |
| 22 | +export function generateTestRequestId(): string { |
| 23 | + return `test-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; |
| 24 | +} |
| 25 | + |
| 26 | +describe("e2e", getConcurrentTestOptions(), () => { |
| 27 | + beforeAll(beforeAllHook); |
| 28 | + |
| 29 | + beforeEach(beforeEachHook); |
| 30 | + |
| 31 | + test("empty", () => { |
| 32 | + expect(true).toBe(true); |
| 33 | + }); |
| 34 | + |
| 35 | + if (process.env.CACHE_MODE === "true") { |
| 36 | + test.each(testModels)( |
| 37 | + "completions with cache checks $model", |
| 38 | + getTestOptions(), |
| 39 | + async ({ model, originalModel }) => { |
| 40 | + // Use a long prompt to trigger caching mechanism (1024+ tokens) for models that support it |
| 41 | + const longPrompt = `You are a helpful assistant. Please analyze the following long text carefully and provide insights. ${"This is a very detailed example text that needs to be quite long to trigger caching mechanisms which require at least 1024 tokens. ".repeat(50)} Just reply 'OK' after processing this text.`; |
| 42 | + |
| 43 | + const requestId = generateTestRequestId(); |
| 44 | + const res = await app.request("/v1/chat/completions", { |
| 45 | + method: "POST", |
| 46 | + headers: { |
| 47 | + "Content-Type": "application/json", |
| 48 | + "x-request-id": requestId, |
| 49 | + Authorization: `Bearer real-token`, |
| 50 | + }, |
| 51 | + body: JSON.stringify({ |
| 52 | + model: model, |
| 53 | + messages: [ |
| 54 | + { |
| 55 | + role: "system", |
| 56 | + content: longPrompt, |
| 57 | + }, |
| 58 | + { |
| 59 | + role: "user", |
| 60 | + content: "Hello, just reply 'OK'!", |
| 61 | + }, |
| 62 | + ], |
| 63 | + }), |
| 64 | + }); |
| 65 | + |
| 66 | + const json = await res.json(); |
| 67 | + if (logMode) { |
| 68 | + console.log("response:", JSON.stringify(json, null, 2)); |
| 69 | + } |
| 70 | + |
| 71 | + expect(res.status).toBe(200); |
| 72 | + validateResponse(json); |
| 73 | + |
| 74 | + const log = await validateLogByRequestId(requestId); |
| 75 | + expect(log.streamed).toBe(false); |
| 76 | + |
| 77 | + expect(json).toHaveProperty("usage"); |
| 78 | + expect(json.usage).toHaveProperty("prompt_tokens"); |
| 79 | + expect(json.usage).toHaveProperty("completion_tokens"); |
| 80 | + expect(json.usage).toHaveProperty("total_tokens"); |
| 81 | + expect(typeof json.usage.prompt_tokens).toBe("number"); |
| 82 | + expect(typeof json.usage.completion_tokens).toBe("number"); |
| 83 | + expect(typeof json.usage.total_tokens).toBe("number"); |
| 84 | + expect(json.usage.prompt_tokens).toBeGreaterThan(0); |
| 85 | + expect(json.usage.completion_tokens).toBeGreaterThan(0); |
| 86 | + expect(json.usage.total_tokens).toBeGreaterThan(0); |
| 87 | + |
| 88 | + // expect(log.inputCost).not.toBeNull(); |
| 89 | + // expect(log.outputCost).not.toBeNull(); |
| 90 | + // expect(log.cost).not.toBeNull(); |
| 91 | + |
| 92 | + const originalModelProviderMapping = models |
| 93 | + .find((m) => m.id === originalModel) |
| 94 | + ?.providers.find( |
| 95 | + (p) => p.providerId === log.usedProvider, |
| 96 | + ) as ProviderModelMapping; |
| 97 | + if (originalModelProviderMapping.cachedInputPrice) { |
| 98 | + // for models that support cached input cost, make the same request twice with a long prompt (1024+ tokens) to trigger caching |
| 99 | + const secondRequestId = generateTestRequestId(); |
| 100 | + const secondRes = await app.request("/v1/chat/completions", { |
| 101 | + method: "POST", |
| 102 | + headers: { |
| 103 | + "Content-Type": "application/json", |
| 104 | + "x-request-id": secondRequestId, |
| 105 | + Authorization: `Bearer real-token`, |
| 106 | + }, |
| 107 | + body: JSON.stringify({ |
| 108 | + model: model, |
| 109 | + messages: [ |
| 110 | + { |
| 111 | + role: "system", |
| 112 | + content: longPrompt, |
| 113 | + }, |
| 114 | + { |
| 115 | + role: "user", |
| 116 | + content: "Hello, just reply 'OK'!", |
| 117 | + }, |
| 118 | + ], |
| 119 | + }), |
| 120 | + }); |
| 121 | + const secondJson = await secondRes.json(); |
| 122 | + if (logMode) { |
| 123 | + console.log( |
| 124 | + "second response:", |
| 125 | + JSON.stringify(secondJson, null, 2), |
| 126 | + ); |
| 127 | + } |
| 128 | + const secondLog = await validateLogByRequestId(secondRequestId); |
| 129 | + console.log("Second request log for caching test:", { |
| 130 | + cachedInputCost: secondLog.cachedInputCost, |
| 131 | + cachedTokens: secondLog.cachedTokens, |
| 132 | + provider: log.usedProvider, |
| 133 | + inputCost: secondLog.inputCost, |
| 134 | + totalCost: secondLog.cost, |
| 135 | + }); |
| 136 | + |
| 137 | + expect(secondLog.cachedInputCost).toBeGreaterThan(0); |
| 138 | + } |
| 139 | + }, |
| 140 | + ); |
| 141 | + } |
| 142 | +}); |
0 commit comments