provider-azure.test.ts 8.0 KB

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