cloudflare.test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { expect, test } from "bun:test"
  2. import { CloudflareAIGatewayAuthPlugin } from "@/plugin/cloudflare"
  3. const pluginInput = {
  4. client: {} as never,
  5. project: {} as never,
  6. directory: "",
  7. worktree: "",
  8. experimental_workspace: {
  9. register() {},
  10. },
  11. serverUrl: new URL("https://example.com"),
  12. $: {} as never,
  13. }
  14. function makeHookInput(overrides: { providerID?: string; apiId?: string; reasoning?: boolean }) {
  15. return {
  16. sessionID: "s",
  17. agent: "a",
  18. provider: {} as never,
  19. message: {} as never,
  20. model: {
  21. providerID: overrides.providerID ?? "cloudflare-ai-gateway",
  22. api: { id: overrides.apiId ?? "openai/gpt-5.2-codex", url: "", npm: "ai-gateway-provider" },
  23. capabilities: {
  24. reasoning: overrides.reasoning ?? true,
  25. temperature: false,
  26. attachment: true,
  27. toolcall: true,
  28. input: { text: true, audio: false, image: false, video: false, pdf: false },
  29. output: { text: true, audio: false, image: false, video: false, pdf: false },
  30. interleaved: false,
  31. },
  32. } as never,
  33. }
  34. }
  35. function makeHookOutput() {
  36. return { temperature: 0, topP: 1, topK: 0, maxOutputTokens: 32_000 as number | undefined, options: {} }
  37. }
  38. test("omits maxOutputTokens for openai reasoning models on cloudflare-ai-gateway", async () => {
  39. const hooks = await CloudflareAIGatewayAuthPlugin(pluginInput)
  40. const out = makeHookOutput()
  41. await hooks["chat.params"]!(makeHookInput({ apiId: "openai/gpt-5.2-codex", reasoning: true }), out)
  42. expect(out.maxOutputTokens).toBeUndefined()
  43. })
  44. test("keeps maxOutputTokens for openai non-reasoning models", async () => {
  45. const hooks = await CloudflareAIGatewayAuthPlugin(pluginInput)
  46. const out = makeHookOutput()
  47. await hooks["chat.params"]!(makeHookInput({ apiId: "openai/gpt-4-turbo", reasoning: false }), out)
  48. expect(out.maxOutputTokens).toBe(32_000)
  49. })
  50. test("keeps maxOutputTokens for non-openai reasoning models on cloudflare-ai-gateway", async () => {
  51. const hooks = await CloudflareAIGatewayAuthPlugin(pluginInput)
  52. const out = makeHookOutput()
  53. await hooks["chat.params"]!(makeHookInput({ apiId: "anthropic/claude-sonnet-4-5", reasoning: true }), out)
  54. expect(out.maxOutputTokens).toBe(32_000)
  55. })
  56. test("ignores non-cloudflare-ai-gateway providers", async () => {
  57. const hooks = await CloudflareAIGatewayAuthPlugin(pluginInput)
  58. const out = makeHookOutput()
  59. await hooks["chat.params"]!(makeHookInput({ providerID: "openai", apiId: "gpt-5.2-codex", reasoning: true }), out)
  60. expect(out.maxOutputTokens).toBe(32_000)
  61. })