|
| 1 | +import type { Model } from "@mariozechner/pi-ai"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { |
| 4 | + buildTransportAwareSimpleStreamFn, |
| 5 | + isTransportAwareApiSupported, |
| 6 | + parseTransportChunkUsage, |
| 7 | + prepareTransportAwareSimpleModel, |
| 8 | + resolveAzureOpenAIApiVersion, |
| 9 | + resolveTransportAwareSimpleApi, |
| 10 | + sanitizeTransportPayloadText, |
| 11 | +} from "./openai-transport-stream.js"; |
| 12 | +import { attachModelProviderRequestTransport } from "./provider-request-config.js"; |
| 13 | + |
| 14 | +describe("openai transport stream", () => { |
| 15 | + it("reports the supported transport-aware APIs", () => { |
| 16 | + expect(isTransportAwareApiSupported("openai-responses")).toBe(true); |
| 17 | + expect(isTransportAwareApiSupported("openai-completions")).toBe(true); |
| 18 | + expect(isTransportAwareApiSupported("azure-openai-responses")).toBe(true); |
| 19 | + expect(isTransportAwareApiSupported("anthropic-messages")).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it("prepares a custom simple-completion api alias when transport overrides are attached", () => { |
| 23 | + const model = attachModelProviderRequestTransport( |
| 24 | + { |
| 25 | + id: "gpt-5", |
| 26 | + name: "GPT-5", |
| 27 | + api: "openai-responses", |
| 28 | + provider: "openai", |
| 29 | + baseUrl: "https://api.openai.com/v1", |
| 30 | + reasoning: true, |
| 31 | + input: ["text"], |
| 32 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, |
| 33 | + contextWindow: 200000, |
| 34 | + maxTokens: 8192, |
| 35 | + } satisfies Model<"openai-responses">, |
| 36 | + { |
| 37 | + proxy: { |
| 38 | + mode: "explicit-proxy", |
| 39 | + url: "http://proxy.internal:8443", |
| 40 | + }, |
| 41 | + }, |
| 42 | + ); |
| 43 | + |
| 44 | + const prepared = prepareTransportAwareSimpleModel(model); |
| 45 | + |
| 46 | + expect(resolveTransportAwareSimpleApi(model.api)).toBe("openclaw-openai-responses-transport"); |
| 47 | + expect(prepared).toMatchObject({ |
| 48 | + api: "openclaw-openai-responses-transport", |
| 49 | + provider: "openai", |
| 50 | + id: "gpt-5", |
| 51 | + }); |
| 52 | + expect(buildTransportAwareSimpleStreamFn(model)).toBeTypeOf("function"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("removes unpaired surrogate code units but preserves valid surrogate pairs", () => { |
| 56 | + const high = String.fromCharCode(0xd83d); |
| 57 | + const low = String.fromCharCode(0xdc00); |
| 58 | + |
| 59 | + expect(sanitizeTransportPayloadText(`left${high}right`)).toBe("leftright"); |
| 60 | + expect(sanitizeTransportPayloadText(`left${low}right`)).toBe("leftright"); |
| 61 | + expect(sanitizeTransportPayloadText("emoji 🙈 ok")).toBe("emoji 🙈 ok"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("uses a valid Azure API version default when the environment is unset", () => { |
| 65 | + expect(resolveAzureOpenAIApiVersion({})).toBe("2024-12-01-preview"); |
| 66 | + expect(resolveAzureOpenAIApiVersion({ AZURE_OPENAI_API_VERSION: "2025-01-01-preview" })).toBe( |
| 67 | + "2025-01-01-preview", |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it("does not double-count reasoning tokens and clamps uncached prompt usage at zero", () => { |
| 72 | + const model = { |
| 73 | + id: "gpt-5", |
| 74 | + name: "GPT-5", |
| 75 | + api: "openai-completions", |
| 76 | + provider: "openai", |
| 77 | + baseUrl: "https://api.openai.com/v1", |
| 78 | + reasoning: true, |
| 79 | + input: ["text"], |
| 80 | + cost: { input: 1, output: 2, cacheRead: 0.5, cacheWrite: 0 }, |
| 81 | + contextWindow: 200000, |
| 82 | + maxTokens: 8192, |
| 83 | + } satisfies Model<"openai-completions">; |
| 84 | + |
| 85 | + expect( |
| 86 | + parseTransportChunkUsage( |
| 87 | + { |
| 88 | + prompt_tokens: 10, |
| 89 | + completion_tokens: 20, |
| 90 | + total_tokens: 30, |
| 91 | + prompt_tokens_details: { cached_tokens: 3 }, |
| 92 | + completion_tokens_details: { reasoning_tokens: 7 }, |
| 93 | + }, |
| 94 | + model, |
| 95 | + ), |
| 96 | + ).toMatchObject({ |
| 97 | + input: 7, |
| 98 | + output: 20, |
| 99 | + cacheRead: 3, |
| 100 | + totalTokens: 30, |
| 101 | + }); |
| 102 | + |
| 103 | + expect( |
| 104 | + parseTransportChunkUsage( |
| 105 | + { |
| 106 | + prompt_tokens: 2, |
| 107 | + completion_tokens: 5, |
| 108 | + total_tokens: 7, |
| 109 | + prompt_tokens_details: { cached_tokens: 4 }, |
| 110 | + }, |
| 111 | + model, |
| 112 | + ), |
| 113 | + ).toMatchObject({ |
| 114 | + input: 0, |
| 115 | + output: 5, |
| 116 | + cacheRead: 4, |
| 117 | + totalTokens: 9, |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments