|
| 1 | +// Openai tests cover realtime session secret creation behavior. |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { |
| 4 | + createOpenAIRealtimeClientSecret, |
| 5 | + createOpenAIRealtimeTranscriptionClientSecret, |
| 6 | +} from "./realtime-provider-shared.js"; |
| 7 | + |
| 8 | +const { fetchWithSsrFGuardMock } = vi.hoisted(() => ({ |
| 9 | + fetchWithSsrFGuardMock: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("openclaw/plugin-sdk/ssrf-runtime", () => ({ |
| 13 | + fetchWithSsrFGuard: fetchWithSsrFGuardMock, |
| 14 | +})); |
| 15 | + |
| 16 | +function makeStreamingResponse(params: { chunkCount: number; chunkSize: number }): { |
| 17 | + response: Response; |
| 18 | + getReadCount: () => number; |
| 19 | + wasCanceled: () => boolean; |
| 20 | +} { |
| 21 | + let readCount = 0; |
| 22 | + let canceled = false; |
| 23 | + const chunk = new Uint8Array(params.chunkSize); |
| 24 | + const response = new Response( |
| 25 | + new ReadableStream<Uint8Array>({ |
| 26 | + pull(controller) { |
| 27 | + if (readCount >= params.chunkCount) { |
| 28 | + controller.close(); |
| 29 | + return; |
| 30 | + } |
| 31 | + readCount += 1; |
| 32 | + controller.enqueue(chunk); |
| 33 | + }, |
| 34 | + cancel() { |
| 35 | + canceled = true; |
| 36 | + }, |
| 37 | + }), |
| 38 | + { status: 200, headers: { "Content-Type": "application/json" } }, |
| 39 | + ); |
| 40 | + return { response, getReadCount: () => readCount, wasCanceled: () => canceled }; |
| 41 | +} |
| 42 | + |
| 43 | +function guardedFetch(response: Response): void { |
| 44 | + fetchWithSsrFGuardMock.mockResolvedValue({ response, release: vi.fn() }); |
| 45 | +} |
| 46 | + |
| 47 | +describe("createOpenAIRealtimeClientSecret", () => { |
| 48 | + it("returns client secret from a well-formed response", async () => { |
| 49 | + guardedFetch( |
| 50 | + new Response( |
| 51 | + JSON.stringify({ |
| 52 | + client_secret: { value: "eph-secret-abc" }, |
| 53 | + expires_at: Math.floor(Date.now() / 1000) + 60, |
| 54 | + }), |
| 55 | + { status: 200, headers: { "Content-Type": "application/json" } }, |
| 56 | + ), |
| 57 | + ); |
| 58 | + |
| 59 | + const result = await createOpenAIRealtimeClientSecret({ |
| 60 | + authToken: "sk-test", |
| 61 | + auditContext: "test", |
| 62 | + session: { model: "gpt-4o-realtime-preview" }, |
| 63 | + }); |
| 64 | + |
| 65 | + expect(result.value).toBe("eph-secret-abc"); |
| 66 | + expect(typeof result.expiresAt).toBe("number"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("bounds oversized success response and cancels the stream", async () => { |
| 70 | + // 20 MiB in 1 MiB chunks — well over the 16 MiB cap |
| 71 | + const streamed = makeStreamingResponse({ chunkCount: 20, chunkSize: 1024 * 1024 }); |
| 72 | + guardedFetch(streamed.response); |
| 73 | + |
| 74 | + await expect( |
| 75 | + createOpenAIRealtimeClientSecret({ |
| 76 | + authToken: "sk-test", |
| 77 | + auditContext: "test", |
| 78 | + session: { model: "gpt-4o-realtime-preview" }, |
| 79 | + }), |
| 80 | + ).rejects.toThrow(/openai\.realtime-session/); |
| 81 | + |
| 82 | + expect(streamed.wasCanceled()).toBe(true); |
| 83 | + expect(streamed.getReadCount()).toBeLessThan(20); |
| 84 | + }); |
| 85 | + |
| 86 | + it("throws the provider error label on oversized body", async () => { |
| 87 | + const streamed = makeStreamingResponse({ chunkCount: 20, chunkSize: 1024 * 1024 }); |
| 88 | + guardedFetch(streamed.response); |
| 89 | + |
| 90 | + await expect( |
| 91 | + createOpenAIRealtimeTranscriptionClientSecret({ |
| 92 | + authToken: "sk-test", |
| 93 | + auditContext: "test", |
| 94 | + session: { model: "gpt-4o-transcribe" }, |
| 95 | + }), |
| 96 | + ).rejects.toThrow(/openai\.realtime-session/); |
| 97 | + |
| 98 | + expect(streamed.wasCanceled()).toBe(true); |
| 99 | + }); |
| 100 | +}); |
0 commit comments