|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import { configureAiTransportHost } from "../host.js"; |
| 3 | +import { buildOpenAIResponsesReplayContext } from "../transports/openai-responses-compaction-replay.js"; |
3 | 4 | import type { Context, Model } from "../types.js"; |
4 | 5 | import { |
5 | 6 | streamAzureOpenAIResponses, |
@@ -158,4 +159,77 @@ describe("azure-openai-responses", () => { |
158 | 159 | configureAiTransportHost({}); |
159 | 160 | } |
160 | 161 | }); |
| 162 | + |
| 163 | + it("fences compaction replay by the resolved Azure endpoint", async () => { |
| 164 | + const routeA = "https://route-a.openai.azure.com/openai/v1"; |
| 165 | + const routeB = "https://route-b.openai.azure.com/openai/v1"; |
| 166 | + const sessionId = "azure-replay-session"; |
| 167 | + const replayContext = buildOpenAIResponsesReplayContext( |
| 168 | + { ...azureResponsesModel, baseUrl: routeA }, |
| 169 | + { sessionId }, |
| 170 | + ); |
| 171 | + if (!replayContext.baseUrlHash) { |
| 172 | + throw new Error("expected Azure replay route hash"); |
| 173 | + } |
| 174 | + const replayMessages = { |
| 175 | + messages: [ |
| 176 | + { |
| 177 | + role: "assistant", |
| 178 | + content: [{ type: "text", text: "earlier" }], |
| 179 | + api: azureResponsesModel.api, |
| 180 | + provider: azureResponsesModel.provider, |
| 181 | + model: azureResponsesModel.id, |
| 182 | + providerReplay: { |
| 183 | + v: 1, |
| 184 | + type: "openai-responses-compaction", |
| 185 | + data: "opaque-compaction-route-a", |
| 186 | + ...replayContext, |
| 187 | + baseUrlHash: replayContext.baseUrlHash, |
| 188 | + }, |
| 189 | + usage: { |
| 190 | + input: 1, |
| 191 | + output: 1, |
| 192 | + cacheRead: 0, |
| 193 | + cacheWrite: 0, |
| 194 | + totalTokens: 2, |
| 195 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, |
| 196 | + }, |
| 197 | + stopReason: "stop", |
| 198 | + timestamp: 1, |
| 199 | + }, |
| 200 | + { role: "user", content: "continue", timestamp: 2 }, |
| 201 | + ], |
| 202 | + } satisfies Context; |
| 203 | + const inputs: unknown[][] = []; |
| 204 | + configureAiTransportHost({ |
| 205 | + buildModelFetch: () => async (input, init) => { |
| 206 | + const body = (await new Request(input, init).json()) as { input?: unknown[] }; |
| 207 | + inputs.push(body.input ?? []); |
| 208 | + return Response.json({ error: { message: "captured" } }, { status: 400 }); |
| 209 | + }, |
| 210 | + }); |
| 211 | + try { |
| 212 | + for (const azureBaseUrl of [routeA, routeB]) { |
| 213 | + await streamAzureOpenAIResponses(azureResponsesModel, replayMessages, { |
| 214 | + apiKey: "test-api-key", |
| 215 | + azureBaseUrl, |
| 216 | + sessionId, |
| 217 | + }).result(); |
| 218 | + } |
| 219 | + } finally { |
| 220 | + configureAiTransportHost({}); |
| 221 | + } |
| 222 | + |
| 223 | + expect(inputs[0]).toEqual( |
| 224 | + expect.arrayContaining([ |
| 225 | + expect.objectContaining({ |
| 226 | + type: "compaction", |
| 227 | + encrypted_content: "opaque-compaction-route-a", |
| 228 | + }), |
| 229 | + ]), |
| 230 | + ); |
| 231 | + expect(inputs[1]).not.toEqual( |
| 232 | + expect.arrayContaining([expect.objectContaining({ type: "compaction" })]), |
| 233 | + ); |
| 234 | + }); |
161 | 235 | }); |
0 commit comments