provider-gitlab.test.ts 13 KB

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