provider-snowflake-cortex.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { describe, expect, it as bun_it } from "bun:test"
  2. import { Effect } from "effect"
  3. import { PluginV2 } from "@opencode-ai/core/plugin"
  4. import { SnowflakeCortexPlugin, cortexFetch } from "@opencode-ai/core/plugin/provider/snowflake-cortex"
  5. import { ProviderPlugins } from "@opencode-ai/core/plugin/provider"
  6. import { expectPluginRegistered, it, model, withEnv } from "./provider-helper"
  7. describe("SnowflakeCortexPlugin", () => {
  8. it.effect("is registered in ProviderPlugins before OpenAICompatiblePlugin", () =>
  9. Effect.sync(() => {
  10. expectPluginRegistered(
  11. ProviderPlugins.map((item) => item.id),
  12. "snowflake-cortex",
  13. )
  14. const ids = ProviderPlugins.map((p) => p.id as string)
  15. expect(ids.indexOf("snowflake-cortex")).toBeLessThan(ids.indexOf("openai-compatible"))
  16. }),
  17. )
  18. it.effect("ignores non-snowflake-cortex providers", () =>
  19. Effect.gen(function* () {
  20. const plugin = yield* PluginV2.Service
  21. yield* plugin.add(SnowflakeCortexPlugin)
  22. const result = yield* plugin.trigger(
  23. "aisdk.sdk",
  24. { model: model("openai", "gpt-4"), package: "@ai-sdk/openai", options: { name: "openai" } },
  25. {},
  26. )
  27. expect(result.sdk).toBeUndefined()
  28. }),
  29. )
  30. it.effect("creates SDK for snowflake-cortex using SNOWFLAKE_CORTEX_PAT env var", () =>
  31. withEnv({ SNOWFLAKE_CORTEX_PAT: "test-pat" }, () =>
  32. Effect.gen(function* () {
  33. const plugin = yield* PluginV2.Service
  34. yield* plugin.add(SnowflakeCortexPlugin)
  35. const result = yield* plugin.trigger(
  36. "aisdk.sdk",
  37. {
  38. model: model("snowflake-cortex", "claude-sonnet-4-6"),
  39. package: "@ai-sdk/openai-compatible",
  40. options: { name: "snowflake-cortex", baseURL: "https://test.snowflakecomputing.com/api/v2/cortex/v1" },
  41. },
  42. {},
  43. )
  44. expect(result.sdk).toBeDefined()
  45. }),
  46. ),
  47. )
  48. it.effect("falls back to options.apiKey when SNOWFLAKE_CORTEX_PAT env var is absent", () =>
  49. withEnv({ SNOWFLAKE_CORTEX_PAT: undefined }, () =>
  50. Effect.gen(function* () {
  51. const plugin = yield* PluginV2.Service
  52. yield* plugin.add(SnowflakeCortexPlugin)
  53. const result = yield* plugin.trigger(
  54. "aisdk.sdk",
  55. {
  56. model: model("snowflake-cortex", "claude-sonnet-4-6"),
  57. package: "@ai-sdk/openai-compatible",
  58. options: {
  59. name: "snowflake-cortex",
  60. baseURL: "https://test.snowflakecomputing.com/api/v2/cortex/v1",
  61. apiKey: "options-pat",
  62. },
  63. },
  64. {},
  65. )
  66. expect(result.sdk).toBeDefined()
  67. }),
  68. ),
  69. )
  70. it.effect("sets includeUsage on the SDK options", () =>
  71. withEnv({ SNOWFLAKE_CORTEX_PAT: "test-pat" }, () =>
  72. Effect.gen(function* () {
  73. const plugin = yield* PluginV2.Service
  74. const captured: Record<string, unknown>[] = []
  75. yield* plugin.add(SnowflakeCortexPlugin)
  76. yield* plugin.add({
  77. id: PluginV2.ID.make("inspector"),
  78. effect: Effect.succeed({
  79. "aisdk.sdk": (evt) =>
  80. Effect.sync(() => {
  81. captured.push({ ...evt.options })
  82. }),
  83. }),
  84. })
  85. yield* plugin.trigger(
  86. "aisdk.sdk",
  87. {
  88. model: model("snowflake-cortex", "claude-sonnet-4-6"),
  89. package: "@ai-sdk/openai-compatible",
  90. options: { name: "snowflake-cortex", baseURL: "https://test.snowflakecomputing.com/api/v2/cortex/v1" },
  91. },
  92. {},
  93. )
  94. expect(captured[0]?.includeUsage).toBe(true)
  95. }),
  96. ),
  97. )
  98. })
  99. type FetchLike = (url: string | URL | Request, init?: RequestInit) => Promise<Response>
  100. describe("cortexFetch", () => {
  101. bun_it("rewrites max_tokens to max_completion_tokens", async () => {
  102. const captured: RequestInit[] = []
  103. const upstream: FetchLike = async (_url, init) => {
  104. captured.push(init ?? {})
  105. return new Response("{}", { status: 200 })
  106. }
  107. await cortexFetch(upstream)("https://test", {
  108. method: "POST",
  109. body: JSON.stringify({ model: "claude-sonnet-4-6", max_tokens: 1024 }),
  110. })
  111. const body = JSON.parse(captured[0].body as string)
  112. expect(body.max_completion_tokens).toBe(1024)
  113. expect(body.max_tokens).toBeUndefined()
  114. })
  115. bun_it("preserves body when max_tokens is absent", async () => {
  116. const captured: RequestInit[] = []
  117. const upstream: FetchLike = async (_url, init) => {
  118. captured.push(init ?? {})
  119. return new Response("{}", { status: 200 })
  120. }
  121. const original = JSON.stringify({ model: "claude-sonnet-4-6", temperature: 0.7 })
  122. await cortexFetch(upstream)("https://test", { method: "POST", body: original })
  123. expect(captured[0].body).toBe(original)
  124. })
  125. bun_it("treats 400 'conversation complete' as a stop response", async () => {
  126. const upstream: FetchLike = async () =>
  127. new Response(JSON.stringify({ message: "Conversation complete" }), {
  128. status: 400,
  129. headers: { "content-type": "application/json" },
  130. })
  131. const response = await cortexFetch(upstream)("https://test", {})
  132. expect(response.status).toBe(200)
  133. const data = (await response.json()) as { choices: { finish_reason: string }[] }
  134. expect(data.choices[0].finish_reason).toBe("stop")
  135. })
  136. bun_it("passes through other 400 errors unchanged", async () => {
  137. const upstream: FetchLike = async () =>
  138. new Response(JSON.stringify({ message: "Invalid model" }), {
  139. status: 400,
  140. headers: { "content-type": "application/json" },
  141. })
  142. const response = await cortexFetch(upstream)("https://test", {})
  143. expect(response.status).toBe(400)
  144. })
  145. bun_it("passes through non-400 errors unchanged", async () => {
  146. const upstream: FetchLike = async () => new Response("Unauthorized", { status: 401 })
  147. const response = await cortexFetch(upstream)("https://test", {})
  148. expect(response.status).toBe(401)
  149. })
  150. bun_it("handles invalid JSON body gracefully without throwing", async () => {
  151. const captured: RequestInit[] = []
  152. const upstream: FetchLike = async (_url, init) => {
  153. captured.push(init ?? {})
  154. return new Response("{}", { status: 200 })
  155. }
  156. const invalidBody = "{ not json }"
  157. await cortexFetch(upstream)("https://test", { method: "POST", body: invalidBody })
  158. expect(captured[0].body).toBe(invalidBody)
  159. })
  160. bun_it("rewrites role:'' to role:'assistant' in streaming SSE chunks", async () => {
  161. const chunk = `data: {"choices":[{"delta":{"role":"","content":"Hi"},"index":0}]}\n\n`
  162. const upstream: FetchLike = async () =>
  163. new Response(
  164. new ReadableStream({
  165. start: (ctrl) => {
  166. ctrl.enqueue(new TextEncoder().encode(chunk))
  167. ctrl.close()
  168. },
  169. }),
  170. {
  171. status: 200,
  172. headers: { "content-type": "text/event-stream" },
  173. },
  174. )
  175. const response = await cortexFetch(upstream)("https://test", {})
  176. const text = await response.text()
  177. expect(text).toContain('"role":"assistant"')
  178. expect(text).not.toContain('"role":""')
  179. })
  180. })