provider-nvidia.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { describe, expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { PluginV2 } from "@opencode-ai/core/plugin"
  4. import { ProviderPlugins } from "@opencode-ai/core/plugin/provider"
  5. import { NvidiaPlugin } from "@opencode-ai/core/plugin/provider/nvidia"
  6. import { expectPluginRegistered, it, provider } from "./provider-helper"
  7. describe("NvidiaPlugin", () => {
  8. it.effect("is registered so legacy referer headers can be applied", () =>
  9. Effect.sync(() =>
  10. expectPluginRegistered(
  11. ProviderPlugins.map((item) => item.id),
  12. "nvidia",
  13. ),
  14. ),
  15. )
  16. it.effect("applies NVIDIA tracking headers only to nvidia", () =>
  17. Effect.gen(function* () {
  18. const plugin = yield* PluginV2.Service
  19. yield* plugin.add(NvidiaPlugin)
  20. const result = yield* plugin.trigger(
  21. "provider.update",
  22. {},
  23. {
  24. provider: provider("nvidia", {
  25. options: { headers: { Existing: "value" }, body: {}, aisdk: { provider: {}, request: {} } },
  26. }),
  27. cancel: false,
  28. },
  29. )
  30. const ignored = yield* plugin.trigger("provider.update", {}, { provider: provider("openrouter"), cancel: false })
  31. expect(result.provider.options.headers).toEqual({
  32. Existing: "value",
  33. "HTTP-Referer": "https://opencode.ai/",
  34. "X-Title": "opencode",
  35. "X-BILLING-INVOKE-ORIGIN": "OpenCode",
  36. })
  37. expect(ignored.provider.options.headers).toEqual({})
  38. }),
  39. )
  40. it.effect("adds billing origin for custom NVIDIA endpoints", () =>
  41. Effect.gen(function* () {
  42. const plugin = yield* PluginV2.Service
  43. yield* plugin.add(NvidiaPlugin)
  44. const result = yield* plugin.trigger(
  45. "provider.update",
  46. {},
  47. {
  48. provider: provider("nvidia", {
  49. endpoint: { type: "aisdk", package: "test-provider", url: "http://localhost:8000/v1" },
  50. options: { headers: {}, body: {}, aisdk: { provider: {}, request: {} } },
  51. }),
  52. cancel: false,
  53. },
  54. )
  55. expect(result.provider.options.headers).toEqual({
  56. "HTTP-Referer": "https://opencode.ai/",
  57. "X-Title": "opencode",
  58. "X-BILLING-INVOKE-ORIGIN": "OpenCode",
  59. })
  60. }),
  61. )
  62. it.effect("preserves an explicit NVIDIA billing origin header", () =>
  63. Effect.gen(function* () {
  64. const plugin = yield* PluginV2.Service
  65. yield* plugin.add(NvidiaPlugin)
  66. const result = yield* plugin.trigger(
  67. "provider.update",
  68. {},
  69. {
  70. provider: provider("nvidia", {
  71. options: {
  72. headers: { "X-BILLING-INVOKE-ORIGIN": "CustomOrigin" },
  73. body: {},
  74. aisdk: { provider: { baseURL: "https://integrate.api.nvidia.com/v1" }, request: {} },
  75. },
  76. }),
  77. cancel: false,
  78. },
  79. )
  80. expect(result.provider.options.headers).toEqual({
  81. "HTTP-Referer": "https://opencode.ai/",
  82. "X-Title": "opencode",
  83. "X-BILLING-INVOKE-ORIGIN": "CustomOrigin",
  84. })
  85. }),
  86. )
  87. })