provider-gitlab.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. modelID: ModelV2.ID.make("claude"),
  69. package: "aisdk:test-provider",
  70. }),
  71. package: "gitlab-ai-provider",
  72. options: { name: "gitlab" },
  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. const aisdk = yield* AISDK.Service
  101. yield* addPlugin()
  102. yield* aisdk.runSDK({
  103. model: ModelV2.Info.make({
  104. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("claude")),
  105. modelID: ModelV2.ID.make("claude"),
  106. package: "aisdk:test-provider",
  107. }),
  108. package: "gitlab-ai-provider",
  109. options: { name: "gitlab" },
  110. })
  111. expect(gitlabSDKOptions[0].instanceUrl).toBe("https://env.gitlab.example")
  112. }),
  113. ),
  114. )
  115. it.effect("keeps configured instance URL, apiKey, aiGatewayHeaders, and featureFlags over env/defaults", () =>
  116. withEnv(
  117. {
  118. GITLAB_INSTANCE_URL: "https://env.gitlab.example",
  119. GITLAB_TOKEN: "env-token",
  120. },
  121. () =>
  122. Effect.gen(function* () {
  123. gitlabSDKOptions.length = 0
  124. const plugin = yield* PluginV2.Service
  125. const aisdk = yield* AISDK.Service
  126. yield* addPlugin()
  127. yield* aisdk.runSDK({
  128. model: ModelV2.Info.make({
  129. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("claude")),
  130. modelID: ModelV2.ID.make("claude"),
  131. package: "aisdk:test-provider",
  132. }),
  133. package: "gitlab-ai-provider",
  134. options: {
  135. name: "gitlab",
  136. instanceUrl: "https://configured.gitlab.example",
  137. apiKey: "configured-token",
  138. aiGatewayHeaders: {
  139. "anthropic-beta": "configured-beta",
  140. "x-gitlab-test": "1",
  141. },
  142. featureFlags: {
  143. duo_agent_platform: false,
  144. custom_flag: true,
  145. },
  146. },
  147. })
  148. expect(gitlabSDKOptions[0].instanceUrl).toBe("https://configured.gitlab.example")
  149. expect(gitlabSDKOptions[0].apiKey).toBe("configured-token")
  150. expect(gitlabSDKOptions[0].aiGatewayHeaders).toMatchObject({
  151. "anthropic-beta": "configured-beta",
  152. "x-gitlab-test": "1",
  153. })
  154. expect(gitlabSDKOptions[0].featureFlags).toEqual({
  155. duo_agent_platform_agentic_chat: true,
  156. duo_agent_platform: false,
  157. custom_flag: true,
  158. })
  159. }),
  160. ),
  161. )
  162. it.effect("ignores non-GitLab SDK packages", () =>
  163. Effect.gen(function* () {
  164. gitlabSDKOptions.length = 0
  165. const plugin = yield* PluginV2.Service
  166. const aisdk = yield* AISDK.Service
  167. yield* addPlugin()
  168. const result = yield* aisdk.runSDK({
  169. model: ModelV2.Info.make({
  170. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("claude")),
  171. modelID: ModelV2.ID.make("claude"),
  172. package: "aisdk:test-provider",
  173. }),
  174. package: "@ai-sdk/openai",
  175. options: { name: "gitlab" },
  176. })
  177. expect(result.sdk).toBeUndefined()
  178. expect(gitlabSDKOptions).toHaveLength(0)
  179. }),
  180. )
  181. it.effect("uses workflowChat for duo workflow models and preserves selectedModelRef", () =>
  182. Effect.gen(function* () {
  183. const plugin = yield* PluginV2.Service
  184. const aisdk = yield* AISDK.Service
  185. const calls: [string, unknown][] = []
  186. yield* addPlugin()
  187. const result = yield* aisdk.runLanguage({
  188. model: ModelV2.Info.make({
  189. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("duo-workflow-custom")),
  190. modelID: ModelV2.ID.make("duo-workflow-custom"),
  191. package: "aisdk:test-provider",
  192. headers: {},
  193. settings: { workflowRef: "ref", workflowDefinition: "definition" },
  194. }),
  195. sdk: {
  196. workflowChat: (id: string, options: unknown) => {
  197. calls.push([id, options])
  198. return { id, options }
  199. },
  200. agenticChat: () => undefined,
  201. },
  202. options: { featureFlags: { configured: true } },
  203. })
  204. expect(calls).toEqual([
  205. ["duo-workflow", { featureFlags: { configured: true }, workflowDefinition: "definition" }],
  206. ])
  207. expect(result.language as unknown).toEqual({
  208. id: "duo-workflow",
  209. options: calls[0]?.[1],
  210. selectedModelRef: "ref",
  211. })
  212. }),
  213. )
  214. it.effect("uses exact static workflow model ids when the provider recognizes them", () =>
  215. Effect.gen(function* () {
  216. const plugin = yield* PluginV2.Service
  217. const aisdk = yield* AISDK.Service
  218. const calls: [string, unknown][] = []
  219. yield* addPlugin()
  220. const result = yield* aisdk.runLanguage({
  221. model: ModelV2.Info.make({
  222. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("duo-workflow-exact")),
  223. modelID: ModelV2.ID.make("duo-workflow-exact"),
  224. package: "aisdk:test-provider",
  225. }),
  226. sdk: {
  227. workflowChat: (id: string, options: unknown) => {
  228. calls.push([id, options])
  229. return { id, options }
  230. },
  231. agenticChat: () => undefined,
  232. },
  233. options: { featureFlags: { configured: true } },
  234. })
  235. expect(calls).toEqual([
  236. ["duo-workflow-exact", { featureFlags: { configured: true }, workflowDefinition: undefined }],
  237. ])
  238. expect(result.language as unknown).toEqual({ id: "duo-workflow-exact", options: calls[0]?.[1] })
  239. }),
  240. )
  241. it.effect("uses provider feature flags instead of model settings feature flags", () =>
  242. Effect.gen(function* () {
  243. const plugin = yield* PluginV2.Service
  244. const aisdk = yield* AISDK.Service
  245. const calls: [string, unknown][] = []
  246. yield* addPlugin()
  247. yield* aisdk.runLanguage({
  248. model: ModelV2.Info.make({
  249. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("duo-workflow-custom")),
  250. modelID: ModelV2.ID.make("duo-workflow-custom"),
  251. package: "aisdk:test-provider",
  252. headers: {},
  253. settings: { featureFlags: { request_flag: true } },
  254. }),
  255. sdk: {
  256. workflowChat: (id: string, options: unknown) => {
  257. calls.push([id, options])
  258. return { id, options }
  259. },
  260. agenticChat: () => undefined,
  261. },
  262. options: { featureFlags: { configured: true } },
  263. })
  264. expect(calls).toEqual([["duo-workflow", { featureFlags: { configured: true }, workflowDefinition: undefined }]])
  265. }),
  266. )
  267. it.effect("uses agenticChat with provider aiGatewayHeaders and feature flags for normal models", () =>
  268. Effect.gen(function* () {
  269. const plugin = yield* PluginV2.Service
  270. const aisdk = yield* AISDK.Service
  271. const calls: [string, unknown][] = []
  272. yield* addPlugin()
  273. yield* aisdk.runLanguage({
  274. model: ModelV2.Info.make({
  275. ...ModelV2.Info.empty(ProviderV2.ID.make("gitlab"), ModelV2.ID.make("claude")),
  276. modelID: ModelV2.ID.make("claude"),
  277. package: "aisdk:test-provider",
  278. headers: { h: "v" },
  279. settings: {},
  280. }),
  281. sdk: {
  282. workflowChat: () => undefined,
  283. agenticChat: (id: string, options: unknown) => {
  284. const selected = options as {
  285. aiGatewayHeaders?: Record<string, string>
  286. featureFlags?: Record<string, boolean>
  287. }
  288. calls.push([
  289. id,
  290. { aiGatewayHeaders: { ...selected.aiGatewayHeaders }, featureFlags: { ...selected.featureFlags } },
  291. ])
  292. },
  293. },
  294. options: { aiGatewayHeaders: { fallback: "header" }, featureFlags: { duo_agent_platform: true } },
  295. })
  296. expect(calls).toEqual([
  297. ["claude", { aiGatewayHeaders: { fallback: "header" }, featureFlags: { duo_agent_platform: true } }],
  298. ])
  299. }),
  300. )
  301. })