provider-gitlab.test.ts 12 KB

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