provider-gitlab.test.ts 13 KB

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