provider-gitlab.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import { AISDK } from "@opencode-ai/core/aisdk"
  2. import { describe, expect, mock } from "bun:test"
  3. import { Effect } from "effect"
  4. import { Catalog } from "@opencode-ai/core/catalog"
  5. import { ModelV2 } from "@opencode-ai/core/model"
  6. import { PluginV2 } from "@opencode-ai/core/plugin"
  7. import { PluginHost } from "@opencode-ai/core/plugin/host"
  8. import { GitLabPlugin } from "@opencode-ai/core/plugin/provider/gitlab"
  9. import { ProviderV2 } from "@opencode-ai/core/provider"
  10. import { testEffect } from "../lib/effect"
  11. import { PluginTestLayer } from "./fixture"
  12. const gitlabSDKOptions: Record<string, unknown>[] = []
  13. const it = testEffect(PluginTestLayer)
  14. const addPlugin = Effect.fn(function* () {
  15. const plugin = yield* PluginV2.Service
  16. const aisdk = yield* AISDK.Service
  17. const host = yield* PluginHost.make(plugin)
  18. yield* GitLabPlugin.effect(host)
  19. })
  20. function withEnv<A, E, R>(vars: Record<string, string | undefined>, effect: () => Effect.Effect<A, E, R>) {
  21. return Effect.acquireUseRelease(
  22. Effect.sync(() => {
  23. const previous = Object.fromEntries(Object.keys(vars).map((key) => [key, process.env[key]]))
  24. Object.entries(vars).forEach(([key, value]) => {
  25. if (value === undefined) delete process.env[key]
  26. else process.env[key] = value
  27. })
  28. return previous
  29. }),
  30. effect,
  31. (previous) =>
  32. Effect.sync(() =>
  33. Object.entries(previous).forEach(([key, value]) => {
  34. if (value === undefined) delete process.env[key]
  35. else process.env[key] = value
  36. }),
  37. ),
  38. )
  39. }
  40. void mock.module("gitlab-ai-provider", () => ({
  41. VERSION: "test-version",
  42. createGitLab: (options: Record<string, unknown>) => {
  43. gitlabSDKOptions.push(options)
  44. return {
  45. agenticChat: (id: string, options: unknown) => ({ id, options, type: "agentic" }),
  46. workflowChat: (id: string, options: unknown) => ({ id, options, type: "workflow" }),
  47. }
  48. },
  49. discoverWorkflowModels: async () => ({ models: [], project: undefined }),
  50. isWorkflowModel: (id: string) => id === "duo-workflow" || id === "duo-workflow-exact",
  51. }))
  52. describe("GitLabPlugin", () => {
  53. it.effect("creates SDKs with legacy default instance URL, token env, headers, and feature flags", () =>
  54. withEnv(
  55. {
  56. GITLAB_INSTANCE_URL: undefined,
  57. GITLAB_TOKEN: "env-token",
  58. },
  59. () =>
  60. Effect.gen(function* () {
  61. gitlabSDKOptions.length = 0
  62. const plugin = yield* PluginV2.Service
  63. const aisdk = yield* AISDK.Service
  64. yield* addPlugin()
  65. yield* aisdk.runSDK({
  66. model: ModelV2.Info.make({
  67. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("claude")),
  68. api: { id: ModelV2.ID.make("claude"), type: "aisdk", package: "test-provider" },
  69. }),
  70. package: "gitlab-ai-provider",
  71. options: { name: "gitlab" },
  72. })
  73. expect(gitlabSDKOptions).toHaveLength(1)
  74. expect(gitlabSDKOptions[0].instanceUrl).toBe("https://gitlab.com")
  75. expect(gitlabSDKOptions[0].apiKey).toBe("env-token")
  76. expect(gitlabSDKOptions[0].aiGatewayHeaders).toMatchObject({
  77. "anthropic-beta": "context-1m-2025-08-07",
  78. })
  79. expect(String((gitlabSDKOptions[0].aiGatewayHeaders as Record<string, string>)["User-Agent"])).toContain(
  80. "gitlab-ai-provider/test-version",
  81. )
  82. expect(gitlabSDKOptions[0].featureFlags).toEqual({
  83. duo_agent_platform_agentic_chat: true,
  84. duo_agent_platform: true,
  85. })
  86. }),
  87. ),
  88. )
  89. it.effect("uses GITLAB_INSTANCE_URL when instanceUrl is not configured", () =>
  90. withEnv(
  91. {
  92. GITLAB_INSTANCE_URL: "https://env.gitlab.example",
  93. GITLAB_TOKEN: undefined,
  94. },
  95. () =>
  96. Effect.gen(function* () {
  97. gitlabSDKOptions.length = 0
  98. const plugin = yield* PluginV2.Service
  99. const aisdk = yield* AISDK.Service
  100. yield* addPlugin()
  101. yield* aisdk.runSDK({
  102. model: ModelV2.Info.make({
  103. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("claude")),
  104. api: { id: ModelV2.ID.make("claude"), type: "aisdk", package: "test-provider" },
  105. }),
  106. package: "gitlab-ai-provider",
  107. options: { name: "gitlab" },
  108. })
  109. expect(gitlabSDKOptions[0].instanceUrl).toBe("https://env.gitlab.example")
  110. }),
  111. ),
  112. )
  113. it.effect("keeps configured instance URL, apiKey, aiGatewayHeaders, and featureFlags over env/defaults", () =>
  114. withEnv(
  115. {
  116. GITLAB_INSTANCE_URL: "https://env.gitlab.example",
  117. GITLAB_TOKEN: "env-token",
  118. },
  119. () =>
  120. Effect.gen(function* () {
  121. gitlabSDKOptions.length = 0
  122. const plugin = yield* PluginV2.Service
  123. const aisdk = yield* AISDK.Service
  124. yield* addPlugin()
  125. yield* aisdk.runSDK({
  126. model: ModelV2.Info.make({
  127. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("claude")),
  128. api: { id: ModelV2.ID.make("claude"), type: "aisdk", package: "test-provider" },
  129. }),
  130. package: "gitlab-ai-provider",
  131. options: {
  132. name: "gitlab",
  133. instanceUrl: "https://configured.gitlab.example",
  134. apiKey: "configured-token",
  135. aiGatewayHeaders: {
  136. "anthropic-beta": "configured-beta",
  137. "x-gitlab-test": "1",
  138. },
  139. featureFlags: {
  140. duo_agent_platform: false,
  141. custom_flag: true,
  142. },
  143. },
  144. })
  145. expect(gitlabSDKOptions[0].instanceUrl).toBe("https://configured.gitlab.example")
  146. expect(gitlabSDKOptions[0].apiKey).toBe("configured-token")
  147. expect(gitlabSDKOptions[0].aiGatewayHeaders).toMatchObject({
  148. "anthropic-beta": "configured-beta",
  149. "x-gitlab-test": "1",
  150. })
  151. expect(gitlabSDKOptions[0].featureFlags).toEqual({
  152. duo_agent_platform_agentic_chat: true,
  153. duo_agent_platform: false,
  154. custom_flag: true,
  155. })
  156. }),
  157. ),
  158. )
  159. it.effect("ignores non-GitLab SDK packages", () =>
  160. Effect.gen(function* () {
  161. gitlabSDKOptions.length = 0
  162. const plugin = yield* PluginV2.Service
  163. const aisdk = yield* AISDK.Service
  164. yield* addPlugin()
  165. const result = yield* aisdk.runSDK({
  166. model: ModelV2.Info.make({
  167. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("claude")),
  168. api: { id: ModelV2.ID.make("claude"), type: "aisdk", package: "test-provider" },
  169. }),
  170. package: "@ai-sdk/openai",
  171. options: { name: "gitlab" },
  172. })
  173. expect(result.sdk).toBeUndefined()
  174. expect(gitlabSDKOptions).toHaveLength(0)
  175. }),
  176. )
  177. it.effect("uses workflowChat for duo workflow models and preserves selectedModelRef", () =>
  178. Effect.gen(function* () {
  179. const plugin = yield* PluginV2.Service
  180. const aisdk = yield* AISDK.Service
  181. const calls: [string, unknown][] = []
  182. yield* addPlugin()
  183. const result = yield* aisdk.runLanguage({
  184. model: ModelV2.Info.make({
  185. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("duo-workflow-custom")),
  186. api: { id: ModelV2.ID.make("duo-workflow-custom"), type: "aisdk", package: "test-provider" },
  187. request: {
  188. headers: {},
  189. body: { workflowRef: "ref", workflowDefinition: "definition" },
  190. },
  191. }),
  192. sdk: {
  193. workflowChat: (id: string, options: unknown) => {
  194. calls.push([id, options])
  195. return { id, options }
  196. },
  197. agenticChat: () => undefined,
  198. },
  199. options: { featureFlags: { configured: true } },
  200. })
  201. expect(calls).toEqual([
  202. ["duo-workflow", { featureFlags: { configured: true }, workflowDefinition: "definition" }],
  203. ])
  204. expect(result.language as unknown).toEqual({
  205. id: "duo-workflow",
  206. options: calls[0]?.[1],
  207. selectedModelRef: "ref",
  208. })
  209. }),
  210. )
  211. it.effect("uses exact static workflow model ids when the provider recognizes them", () =>
  212. Effect.gen(function* () {
  213. const plugin = yield* PluginV2.Service
  214. const aisdk = yield* AISDK.Service
  215. const calls: [string, unknown][] = []
  216. yield* addPlugin()
  217. const result = yield* aisdk.runLanguage({
  218. model: ModelV2.Info.make({
  219. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("duo-workflow-exact")),
  220. api: { id: ModelV2.ID.make("duo-workflow-exact"), type: "aisdk", package: "test-provider" },
  221. }),
  222. sdk: {
  223. workflowChat: (id: string, options: unknown) => {
  224. calls.push([id, options])
  225. return { id, options }
  226. },
  227. agenticChat: () => undefined,
  228. },
  229. options: { featureFlags: { configured: true } },
  230. })
  231. expect(calls).toEqual([
  232. ["duo-workflow-exact", { featureFlags: { configured: true }, workflowDefinition: undefined }],
  233. ])
  234. expect(result.language as unknown).toEqual({ id: "duo-workflow-exact", options: calls[0]?.[1] })
  235. }),
  236. )
  237. it.effect("uses provider feature flags instead of request feature flags", () =>
  238. Effect.gen(function* () {
  239. const plugin = yield* PluginV2.Service
  240. const aisdk = yield* AISDK.Service
  241. const calls: [string, unknown][] = []
  242. yield* addPlugin()
  243. yield* aisdk.runLanguage({
  244. model: ModelV2.Info.make({
  245. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("duo-workflow-custom")),
  246. api: { id: ModelV2.ID.make("duo-workflow-custom"), type: "aisdk", package: "test-provider" },
  247. request: {
  248. headers: {},
  249. body: { featureFlags: { request_flag: true } },
  250. },
  251. }),
  252. sdk: {
  253. workflowChat: (id: string, options: unknown) => {
  254. calls.push([id, options])
  255. return { id, options }
  256. },
  257. agenticChat: () => undefined,
  258. },
  259. options: { featureFlags: { configured: true } },
  260. })
  261. expect(calls).toEqual([["duo-workflow", { featureFlags: { configured: true }, workflowDefinition: undefined }]])
  262. }),
  263. )
  264. it.effect("uses agenticChat with provider aiGatewayHeaders and feature flags for normal models", () =>
  265. Effect.gen(function* () {
  266. const plugin = yield* PluginV2.Service
  267. const aisdk = yield* AISDK.Service
  268. const calls: [string, unknown][] = []
  269. yield* addPlugin()
  270. yield* aisdk.runLanguage({
  271. model: ModelV2.Info.make({
  272. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("claude")),
  273. api: { id: ModelV2.ID.make("claude"), type: "aisdk", package: "test-provider" },
  274. request: { headers: { h: "v" }, body: {} },
  275. }),
  276. sdk: {
  277. workflowChat: () => undefined,
  278. agenticChat: (id: string, options: unknown) => {
  279. const selected = options as {
  280. aiGatewayHeaders?: Record<string, string>
  281. featureFlags?: Record<string, boolean>
  282. }
  283. calls.push([
  284. id,
  285. { aiGatewayHeaders: { ...selected.aiGatewayHeaders }, featureFlags: { ...selected.featureFlags } },
  286. ])
  287. },
  288. },
  289. options: { aiGatewayHeaders: { fallback: "header" }, featureFlags: { duo_agent_platform: true } },
  290. })
  291. expect(calls).toEqual([
  292. ["claude", { aiGatewayHeaders: { fallback: "header" }, featureFlags: { duo_agent_platform: true } }],
  293. ])
  294. }),
  295. )
  296. })