provider-azure.test.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Layer } from "effect"
  3. import { AuthV2 } from "@opencode-ai/core/auth"
  4. import { PluginV2 } from "@opencode-ai/core/plugin"
  5. import { AuthPlugin } from "@opencode-ai/core/plugin/auth"
  6. import { AzurePlugin } from "@opencode-ai/core/plugin/provider/azure"
  7. import { testEffect } from "../lib/effect"
  8. import { fakeSelectorSdk, it, model, npmLayer, provider, withEnv } from "./provider-helper"
  9. const itWithAuth = testEffect(Layer.mergeAll(PluginV2.defaultLayer, AuthV2.defaultLayer, npmLayer))
  10. describe("AzurePlugin", () => {
  11. it.effect("resolves resourceName from env", () =>
  12. withEnv({ AZURE_RESOURCE_NAME: "from-env" }, () =>
  13. Effect.gen(function* () {
  14. const plugin = yield* PluginV2.Service
  15. yield* plugin.add(AzurePlugin)
  16. const result = yield* plugin.trigger("provider.update", {}, { provider: provider("azure"), cancel: false })
  17. expect(result.provider.options.aisdk.provider.resourceName).toBe("from-env")
  18. }),
  19. ),
  20. )
  21. it.effect("keeps explicit resourceName over env and ignores other providers", () =>
  22. withEnv({ AZURE_RESOURCE_NAME: "from-env" }, () =>
  23. Effect.gen(function* () {
  24. const plugin = yield* PluginV2.Service
  25. yield* plugin.add(AzurePlugin)
  26. const azure = yield* plugin.trigger(
  27. "provider.update",
  28. {},
  29. {
  30. provider: provider("azure", {
  31. options: { headers: {}, body: {}, aisdk: { provider: { resourceName: "from-config" }, request: {} } },
  32. }),
  33. cancel: false,
  34. },
  35. )
  36. const other = yield* plugin.trigger("provider.update", {}, { provider: provider("openai"), cancel: false })
  37. expect(azure.provider.options.aisdk.provider.resourceName).toBe("from-config")
  38. expect(other.provider.options.aisdk.provider.resourceName).toBeUndefined()
  39. }),
  40. ),
  41. )
  42. itWithAuth.effect("prefers auth resourceName over env", () =>
  43. withEnv(
  44. {
  45. AZURE_RESOURCE_NAME: "from-env",
  46. },
  47. () =>
  48. Effect.gen(function* () {
  49. const plugin = yield* PluginV2.Service
  50. const auth = yield* AuthV2.Service
  51. yield* auth.create({
  52. serviceID: AuthV2.ServiceID.make("azure"),
  53. credential: new AuthV2.ApiKeyCredential({
  54. type: "api",
  55. key: "key",
  56. metadata: { resourceName: "from-auth" },
  57. }),
  58. active: true,
  59. })
  60. yield* plugin.add({
  61. ...AuthPlugin,
  62. effect: AuthPlugin.effect.pipe(Effect.provideService(AuthV2.Service, auth)),
  63. })
  64. yield* plugin.add(AzurePlugin)
  65. const result = yield* plugin.trigger("provider.update", {}, { provider: provider("azure"), cancel: false })
  66. expect(result.provider.options.aisdk.provider.resourceName).toBe("from-auth")
  67. }),
  68. ),
  69. )
  70. it.effect("falls back to env when configured resourceName is blank", () =>
  71. withEnv({ AZURE_RESOURCE_NAME: "from-env" }, () =>
  72. Effect.gen(function* () {
  73. const plugin = yield* PluginV2.Service
  74. yield* plugin.add(AzurePlugin)
  75. const result = yield* plugin.trigger(
  76. "provider.update",
  77. {},
  78. {
  79. provider: provider("azure", {
  80. options: { headers: {}, body: {}, aisdk: { provider: { resourceName: "" }, request: {} } },
  81. }),
  82. cancel: false,
  83. },
  84. )
  85. expect(result.provider.options.aisdk.provider.resourceName).toBe("from-env")
  86. }),
  87. ),
  88. )
  89. it.effect("falls back to env when configured resourceName is whitespace", () =>
  90. withEnv({ AZURE_RESOURCE_NAME: "from-env" }, () =>
  91. Effect.gen(function* () {
  92. const plugin = yield* PluginV2.Service
  93. yield* plugin.add(AzurePlugin)
  94. const result = yield* plugin.trigger(
  95. "provider.update",
  96. {},
  97. {
  98. provider: provider("azure", {
  99. options: { headers: {}, body: {}, aisdk: { provider: { resourceName: " " }, request: {} } },
  100. }),
  101. cancel: false,
  102. },
  103. )
  104. expect(result.provider.options.aisdk.provider.resourceName).toBe("from-env")
  105. }),
  106. ),
  107. )
  108. it.effect("allows configured baseURL without resourceName", () =>
  109. withEnv({ AZURE_RESOURCE_NAME: undefined }, () =>
  110. Effect.gen(function* () {
  111. const plugin = yield* PluginV2.Service
  112. yield* plugin.add(AzurePlugin)
  113. const result = yield* plugin.trigger(
  114. "aisdk.sdk",
  115. {
  116. model: model("azure", "deployment"),
  117. package: "@ai-sdk/azure",
  118. options: { name: "azure", baseURL: "https://proxy.example.com/openai" },
  119. },
  120. {},
  121. )
  122. expect(result.sdk).toBeDefined()
  123. }),
  124. ),
  125. )
  126. it.effect("rejects missing resourceName when baseURL is not configured", () =>
  127. withEnv({ AZURE_RESOURCE_NAME: undefined }, () =>
  128. Effect.gen(function* () {
  129. const plugin = yield* PluginV2.Service
  130. yield* plugin.add(AzurePlugin)
  131. const exit = yield* plugin
  132. .trigger(
  133. "aisdk.sdk",
  134. { model: model("azure", "deployment"), package: "@ai-sdk/azure", options: { name: "azure" } },
  135. {},
  136. )
  137. .pipe(Effect.exit)
  138. expect(exit._tag).toBe("Failure")
  139. }),
  140. ),
  141. )
  142. it.effect("selects chat only for completion URLs", () =>
  143. Effect.gen(function* () {
  144. const plugin = yield* PluginV2.Service
  145. const calls: string[] = []
  146. yield* plugin.add(AzurePlugin)
  147. yield* plugin.trigger(
  148. "aisdk.language",
  149. { model: model("azure", "deployment"), sdk: fakeSelectorSdk(calls), options: { useCompletionUrls: true } },
  150. {},
  151. )
  152. expect(calls).toEqual(["chat:deployment"])
  153. }),
  154. )
  155. it.effect("selects chat from per-call useCompletionUrls", () =>
  156. Effect.gen(function* () {
  157. const plugin = yield* PluginV2.Service
  158. const calls: string[] = []
  159. yield* plugin.add(AzurePlugin)
  160. yield* plugin.trigger(
  161. "aisdk.language",
  162. { model: model("azure", "deployment"), sdk: fakeSelectorSdk(calls), options: { useCompletionUrls: true } },
  163. {},
  164. )
  165. expect(calls).toEqual(["chat:deployment"])
  166. }),
  167. )
  168. it.effect("ignores model useCompletionUrls when per-call option is unset", () =>
  169. Effect.gen(function* () {
  170. const plugin = yield* PluginV2.Service
  171. const calls: string[] = []
  172. yield* plugin.add(AzurePlugin)
  173. yield* plugin.trigger(
  174. "aisdk.language",
  175. {
  176. model: model("azure", "deployment", {
  177. options: { headers: {}, body: {}, aisdk: { provider: {}, request: { useCompletionUrls: true } } },
  178. }),
  179. sdk: fakeSelectorSdk(calls),
  180. options: {},
  181. },
  182. {},
  183. )
  184. expect(calls).toEqual(["responses:deployment"])
  185. }),
  186. )
  187. it.effect("uses the legacy Azure selector order and provider guard", () =>
  188. Effect.gen(function* () {
  189. const plugin = yield* PluginV2.Service
  190. const calls: string[] = []
  191. yield* plugin.add(AzurePlugin)
  192. yield* plugin.trigger(
  193. "aisdk.language",
  194. { model: model("azure", "deployment"), sdk: fakeSelectorSdk(calls), options: {} },
  195. {},
  196. )
  197. const ignored = yield* plugin.trigger(
  198. "aisdk.language",
  199. { model: model("openai", "deployment"), sdk: fakeSelectorSdk(calls), options: {} },
  200. {},
  201. )
  202. expect(calls).toEqual(["responses:deployment"])
  203. expect(ignored.language).toBeUndefined()
  204. }),
  205. )
  206. it.effect("falls back through the legacy Azure selector order", () =>
  207. Effect.gen(function* () {
  208. const plugin = yield* PluginV2.Service
  209. const calls: string[] = []
  210. const make = (method: string) => (id: string) => {
  211. calls.push(`${method}:${id}`)
  212. return { modelId: id, provider: method, specificationVersion: "v3" }
  213. }
  214. yield* plugin.add(AzurePlugin)
  215. yield* plugin.trigger(
  216. "aisdk.language",
  217. {
  218. model: model("azure", "messages-deployment"),
  219. sdk: { messages: make("messages"), chat: make("chat"), languageModel: make("languageModel") },
  220. options: {},
  221. },
  222. {},
  223. )
  224. yield* plugin.trigger(
  225. "aisdk.language",
  226. { model: model("azure", "language-deployment"), sdk: { languageModel: make("languageModel") }, options: {} },
  227. {},
  228. )
  229. expect(calls).toEqual(["messages:messages-deployment", "languageModel:language-deployment"])
  230. }),
  231. )
  232. })