provider-gitlab.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import { describe, expect, mock } 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 { GitLabPlugin } from "@opencode-ai/core/plugin/provider/gitlab"
  7. import { testEffect } from "../lib/effect"
  8. import { it, model, npmLayer, provider, withEnv } from "./provider-helper"
  9. const gitlabSDKOptions: Record<string, unknown>[] = []
  10. void mock.module("gitlab-ai-provider", () => ({
  11. VERSION: "test-version",
  12. createGitLab: (options: Record<string, unknown>) => {
  13. gitlabSDKOptions.push(options)
  14. return {
  15. agenticChat: (id: string, options: unknown) => ({ id, options, type: "agentic" }),
  16. workflowChat: (id: string, options: unknown) => ({ id, options, type: "workflow" }),
  17. }
  18. },
  19. discoverWorkflowModels: async () => ({ models: [], project: undefined }),
  20. isWorkflowModel: (id: string) => id === "duo-workflow" || id === "duo-workflow-exact",
  21. }))
  22. const itWithAuth = testEffect(Layer.mergeAll(PluginV2.defaultLayer, AuthV2.defaultLayer, npmLayer))
  23. describe("GitLabPlugin", () => {
  24. it.effect("creates SDKs with legacy default instance URL, token env, headers, and feature flags", () =>
  25. withEnv(
  26. {
  27. GITLAB_INSTANCE_URL: undefined,
  28. GITLAB_TOKEN: "env-token",
  29. },
  30. () =>
  31. Effect.gen(function* () {
  32. gitlabSDKOptions.length = 0
  33. const plugin = yield* PluginV2.Service
  34. yield* plugin.add(GitLabPlugin)
  35. yield* plugin.trigger(
  36. "aisdk.sdk",
  37. { model: model("gitlab", "claude"), package: "gitlab-ai-provider", options: { name: "gitlab" } },
  38. {},
  39. )
  40. expect(gitlabSDKOptions).toHaveLength(1)
  41. expect(gitlabSDKOptions[0].instanceUrl).toBe("https://gitlab.com")
  42. expect(gitlabSDKOptions[0].apiKey).toBe("env-token")
  43. expect(gitlabSDKOptions[0].aiGatewayHeaders).toMatchObject({
  44. "anthropic-beta": "context-1m-2025-08-07",
  45. })
  46. expect(String((gitlabSDKOptions[0].aiGatewayHeaders as Record<string, string>)["User-Agent"])).toContain(
  47. "gitlab-ai-provider/test-version",
  48. )
  49. expect(gitlabSDKOptions[0].featureFlags).toEqual({
  50. duo_agent_platform_agentic_chat: true,
  51. duo_agent_platform: true,
  52. })
  53. }),
  54. ),
  55. )
  56. it.effect("uses GITLAB_INSTANCE_URL when instanceUrl is not configured", () =>
  57. withEnv(
  58. {
  59. GITLAB_INSTANCE_URL: "https://env.gitlab.example",
  60. GITLAB_TOKEN: undefined,
  61. },
  62. () =>
  63. Effect.gen(function* () {
  64. gitlabSDKOptions.length = 0
  65. const plugin = yield* PluginV2.Service
  66. yield* plugin.add(GitLabPlugin)
  67. yield* plugin.trigger(
  68. "aisdk.sdk",
  69. { model: model("gitlab", "claude"), package: "gitlab-ai-provider", options: { name: "gitlab" } },
  70. {},
  71. )
  72. expect(gitlabSDKOptions[0].instanceUrl).toBe("https://env.gitlab.example")
  73. }),
  74. ),
  75. )
  76. it.effect("keeps configured instance URL, apiKey, aiGatewayHeaders, and featureFlags over env/defaults", () =>
  77. withEnv(
  78. {
  79. GITLAB_INSTANCE_URL: "https://env.gitlab.example",
  80. GITLAB_TOKEN: "env-token",
  81. },
  82. () =>
  83. Effect.gen(function* () {
  84. gitlabSDKOptions.length = 0
  85. const plugin = yield* PluginV2.Service
  86. yield* plugin.add(GitLabPlugin)
  87. yield* plugin.trigger(
  88. "aisdk.sdk",
  89. {
  90. model: model("gitlab", "claude"),
  91. package: "gitlab-ai-provider",
  92. options: {
  93. name: "gitlab",
  94. instanceUrl: "https://configured.gitlab.example",
  95. apiKey: "configured-token",
  96. aiGatewayHeaders: {
  97. "anthropic-beta": "configured-beta",
  98. "x-gitlab-test": "1",
  99. },
  100. featureFlags: {
  101. duo_agent_platform: false,
  102. custom_flag: true,
  103. },
  104. },
  105. },
  106. {},
  107. )
  108. expect(gitlabSDKOptions[0].instanceUrl).toBe("https://configured.gitlab.example")
  109. expect(gitlabSDKOptions[0].apiKey).toBe("configured-token")
  110. expect(gitlabSDKOptions[0].aiGatewayHeaders).toMatchObject({
  111. "anthropic-beta": "configured-beta",
  112. "x-gitlab-test": "1",
  113. })
  114. expect(gitlabSDKOptions[0].featureFlags).toEqual({
  115. duo_agent_platform_agentic_chat: true,
  116. duo_agent_platform: false,
  117. custom_flag: true,
  118. })
  119. }),
  120. ),
  121. )
  122. it.effect("ignores non-GitLab SDK packages", () =>
  123. Effect.gen(function* () {
  124. gitlabSDKOptions.length = 0
  125. const plugin = yield* PluginV2.Service
  126. yield* plugin.add(GitLabPlugin)
  127. const result = yield* plugin.trigger(
  128. "aisdk.sdk",
  129. { model: model("gitlab", "claude"), package: "@ai-sdk/openai", options: { name: "gitlab" } },
  130. {},
  131. )
  132. expect(result.sdk).toBeUndefined()
  133. expect(gitlabSDKOptions).toHaveLength(0)
  134. }),
  135. )
  136. itWithAuth.effect("uses active API auth token over GITLAB_TOKEN", () =>
  137. withEnv(
  138. {
  139. GITLAB_TOKEN: "env-token",
  140. },
  141. () =>
  142. Effect.gen(function* () {
  143. gitlabSDKOptions.length = 0
  144. const plugin = yield* PluginV2.Service
  145. const auth = yield* AuthV2.Service
  146. yield* auth.create({
  147. serviceID: AuthV2.ServiceID.make("gitlab"),
  148. credential: new AuthV2.ApiKeyCredential({ type: "api", key: "auth-token" }),
  149. active: true,
  150. })
  151. yield* plugin.add({
  152. ...AuthPlugin,
  153. effect: AuthPlugin.effect.pipe(Effect.provideService(AuthV2.Service, auth)),
  154. })
  155. yield* plugin.add(GitLabPlugin)
  156. const updated = yield* plugin.trigger("provider.update", {}, { provider: provider("gitlab"), cancel: false })
  157. yield* plugin.trigger(
  158. "aisdk.sdk",
  159. {
  160. model: model("gitlab", "claude"),
  161. package: "gitlab-ai-provider",
  162. options: updated.provider.options.aisdk.provider,
  163. },
  164. {},
  165. )
  166. expect(gitlabSDKOptions[0].apiKey).toBe("auth-token")
  167. }),
  168. ),
  169. )
  170. itWithAuth.effect("uses active OAuth access token when no API auth exists", () =>
  171. withEnv(
  172. {
  173. GITLAB_TOKEN: undefined,
  174. },
  175. () =>
  176. Effect.gen(function* () {
  177. gitlabSDKOptions.length = 0
  178. const plugin = yield* PluginV2.Service
  179. const auth = yield* AuthV2.Service
  180. yield* auth.create({
  181. serviceID: AuthV2.ServiceID.make("gitlab"),
  182. credential: new AuthV2.OAuthCredential({
  183. type: "oauth",
  184. refresh: "refresh-token",
  185. access: "oauth-token",
  186. expires: 9999999999999,
  187. }),
  188. active: true,
  189. })
  190. yield* plugin.add({
  191. ...AuthPlugin,
  192. effect: AuthPlugin.effect.pipe(Effect.provideService(AuthV2.Service, auth)),
  193. })
  194. yield* plugin.add(GitLabPlugin)
  195. const updated = yield* plugin.trigger("provider.update", {}, { provider: provider("gitlab"), cancel: false })
  196. yield* plugin.trigger(
  197. "aisdk.sdk",
  198. {
  199. model: model("gitlab", "claude"),
  200. package: "gitlab-ai-provider",
  201. options: updated.provider.options.aisdk.provider,
  202. },
  203. {},
  204. )
  205. expect(gitlabSDKOptions[0].apiKey).toBe("oauth-token")
  206. }),
  207. ),
  208. )
  209. it.effect("uses workflowChat for duo workflow models and preserves selectedModelRef", () =>
  210. Effect.gen(function* () {
  211. const plugin = yield* PluginV2.Service
  212. const calls: [string, unknown][] = []
  213. yield* plugin.add(GitLabPlugin)
  214. const result = yield* plugin.trigger(
  215. "aisdk.language",
  216. {
  217. model: model("gitlab", "duo-workflow-custom", {
  218. options: {
  219. headers: {},
  220. body: {},
  221. aisdk: { provider: {}, request: { workflowRef: "ref", workflowDefinition: "definition" } },
  222. },
  223. }),
  224. sdk: {
  225. workflowChat: (id: string, options: unknown) => {
  226. calls.push([id, options])
  227. return { id, options }
  228. },
  229. agenticChat: () => undefined,
  230. },
  231. options: { featureFlags: { configured: true } },
  232. },
  233. {},
  234. )
  235. expect(calls).toEqual([
  236. ["duo-workflow", { featureFlags: { configured: true }, workflowDefinition: "definition" }],
  237. ])
  238. expect(result.language as unknown).toEqual({
  239. id: "duo-workflow",
  240. options: calls[0]?.[1],
  241. selectedModelRef: "ref",
  242. })
  243. }),
  244. )
  245. it.effect("uses exact static workflow model ids when the provider recognizes them", () =>
  246. Effect.gen(function* () {
  247. const plugin = yield* PluginV2.Service
  248. const calls: [string, unknown][] = []
  249. yield* plugin.add(GitLabPlugin)
  250. const result = yield* plugin.trigger(
  251. "aisdk.language",
  252. {
  253. model: model("gitlab", "duo-workflow-exact"),
  254. sdk: {
  255. workflowChat: (id: string, options: unknown) => {
  256. calls.push([id, options])
  257. return { id, options }
  258. },
  259. agenticChat: () => undefined,
  260. },
  261. options: { featureFlags: { configured: true } },
  262. },
  263. {},
  264. )
  265. expect(calls).toEqual([
  266. ["duo-workflow-exact", { featureFlags: { configured: true }, workflowDefinition: undefined }],
  267. ])
  268. expect(result.language as unknown).toEqual({ id: "duo-workflow-exact", options: calls[0]?.[1] })
  269. }),
  270. )
  271. it.effect("uses provider feature flags instead of request feature flags", () =>
  272. Effect.gen(function* () {
  273. const plugin = yield* PluginV2.Service
  274. const calls: [string, unknown][] = []
  275. yield* plugin.add(GitLabPlugin)
  276. yield* plugin.trigger(
  277. "aisdk.language",
  278. {
  279. model: model("gitlab", "duo-workflow-custom", {
  280. options: {
  281. headers: {},
  282. body: {},
  283. aisdk: { provider: {}, request: { featureFlags: { request_flag: true } } },
  284. },
  285. }),
  286. sdk: {
  287. workflowChat: (id: string, options: unknown) => {
  288. calls.push([id, options])
  289. return { id, options }
  290. },
  291. agenticChat: () => undefined,
  292. },
  293. options: { featureFlags: { configured: true } },
  294. },
  295. {},
  296. )
  297. expect(calls).toEqual([["duo-workflow", { featureFlags: { configured: true }, workflowDefinition: undefined }]])
  298. }),
  299. )
  300. it.effect("uses agenticChat with provider aiGatewayHeaders and feature flags for normal models", () =>
  301. Effect.gen(function* () {
  302. const plugin = yield* PluginV2.Service
  303. const calls: [string, unknown][] = []
  304. yield* plugin.add(GitLabPlugin)
  305. yield* plugin.trigger(
  306. "aisdk.language",
  307. {
  308. model: model("gitlab", "claude", {
  309. options: { headers: { h: "v" }, body: {}, aisdk: { provider: {}, request: {} } },
  310. }),
  311. sdk: {
  312. workflowChat: () => undefined,
  313. agenticChat: (id: string, options: unknown) => {
  314. const selected = options as {
  315. aiGatewayHeaders?: Record<string, string>
  316. featureFlags?: Record<string, boolean>
  317. }
  318. calls.push([
  319. id,
  320. { aiGatewayHeaders: { ...selected.aiGatewayHeaders }, featureFlags: { ...selected.featureFlags } },
  321. ])
  322. },
  323. },
  324. options: { aiGatewayHeaders: { fallback: "header" }, featureFlags: { duo_agent_platform: true } },
  325. },
  326. {},
  327. )
  328. expect(calls).toEqual([
  329. ["claude", { aiGatewayHeaders: { fallback: "header" }, featureFlags: { duo_agent_platform: true } }],
  330. ])
  331. }),
  332. )
  333. })