provider-sap-ai-core.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { describe, expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { PluginV2 } from "@opencode-ai/core/plugin"
  4. import { SapAICorePlugin } from "@opencode-ai/core/plugin/provider/sap-ai-core"
  5. import { fixtureProvider, it, model, npmLayer, withEnv } from "./provider-helper"
  6. const pluginWithNpm = { id: SapAICorePlugin.id, effect: SapAICorePlugin.effect.pipe(Effect.provide(npmLayer)) }
  7. describe("SapAICorePlugin", () => {
  8. it.effect("copies serviceKey option into AICORE_SERVICE_KEY but keeps SDK options to deployment metadata", () =>
  9. withEnv(
  10. { AICORE_SERVICE_KEY: undefined, AICORE_DEPLOYMENT_ID: "deployment", AICORE_RESOURCE_GROUP: "resource-group" },
  11. () =>
  12. Effect.gen(function* () {
  13. const plugin = yield* PluginV2.Service
  14. yield* plugin.add(pluginWithNpm)
  15. const sdk = yield* plugin.trigger(
  16. "aisdk.sdk",
  17. {
  18. model: model("sap-ai-core", "sap-model"),
  19. package: fixtureProvider,
  20. options: { name: "sap-ai-core", serviceKey: "service-key" },
  21. },
  22. {},
  23. )
  24. expect(process.env.AICORE_SERVICE_KEY).toBe("service-key")
  25. expect(sdk.sdk.options).toEqual({ deploymentId: "deployment", resourceGroup: "resource-group" })
  26. }),
  27. ),
  28. )
  29. it.effect("preserves existing AICORE_SERVICE_KEY over serviceKey option", () =>
  30. withEnv(
  31. {
  32. AICORE_SERVICE_KEY: "env-service-key",
  33. AICORE_DEPLOYMENT_ID: "deployment",
  34. AICORE_RESOURCE_GROUP: "resource-group",
  35. },
  36. () =>
  37. Effect.gen(function* () {
  38. const plugin = yield* PluginV2.Service
  39. yield* plugin.add(pluginWithNpm)
  40. const sdk = yield* plugin.trigger(
  41. "aisdk.sdk",
  42. {
  43. model: model("sap-ai-core", "sap-model"),
  44. package: fixtureProvider,
  45. options: { name: "sap-ai-core", serviceKey: "option-service-key" },
  46. },
  47. {},
  48. )
  49. expect(process.env.AICORE_SERVICE_KEY).toBe("env-service-key")
  50. expect(sdk.sdk.options).toEqual({ deploymentId: "deployment", resourceGroup: "resource-group" })
  51. }),
  52. ),
  53. )
  54. it.effect("omits deployment and resourceGroup SDK options when no service key is available", () =>
  55. withEnv(
  56. { AICORE_SERVICE_KEY: undefined, AICORE_DEPLOYMENT_ID: "deployment", AICORE_RESOURCE_GROUP: "resource-group" },
  57. () =>
  58. Effect.gen(function* () {
  59. const plugin = yield* PluginV2.Service
  60. yield* plugin.add(pluginWithNpm)
  61. const sdk = yield* plugin.trigger(
  62. "aisdk.sdk",
  63. { model: model("sap-ai-core", "sap-model"), package: fixtureProvider, options: { name: "sap-ai-core" } },
  64. {},
  65. )
  66. expect(process.env.AICORE_SERVICE_KEY).toBeUndefined()
  67. expect(sdk.sdk.options).toEqual({})
  68. }),
  69. ),
  70. )
  71. it.effect("uses the callable SDK for language selection", () =>
  72. Effect.gen(function* () {
  73. const plugin = yield* PluginV2.Service
  74. yield* plugin.add(pluginWithNpm)
  75. const sdk = Object.assign((modelID: string) => ({ modelID, provider: "callable" }), {
  76. languageModel() {
  77. throw new Error("SAP AI Core should call the SDK directly")
  78. },
  79. })
  80. const language = yield* plugin.trigger(
  81. "aisdk.language",
  82. { model: model("sap-ai-core", "sap-model"), sdk, options: {} },
  83. {},
  84. )
  85. expect(language.language as unknown).toEqual({ modelID: "sap-model", provider: "callable" })
  86. }),
  87. )
  88. it.effect("ignores non-SAP AI Core providers", () =>
  89. withEnv(
  90. { AICORE_SERVICE_KEY: undefined, AICORE_DEPLOYMENT_ID: "deployment", AICORE_RESOURCE_GROUP: "resource-group" },
  91. () =>
  92. Effect.gen(function* () {
  93. const plugin = yield* PluginV2.Service
  94. yield* plugin.add(pluginWithNpm)
  95. const sdk = yield* plugin.trigger(
  96. "aisdk.sdk",
  97. {
  98. model: model("openai", "sap-model"),
  99. package: fixtureProvider,
  100. options: { name: "openai", serviceKey: "service-key" },
  101. },
  102. {},
  103. )
  104. const language = yield* plugin.trigger(
  105. "aisdk.language",
  106. {
  107. model: model("openai", "sap-model"),
  108. sdk: () => {
  109. throw new Error("SAP AI Core should ignore other providers")
  110. },
  111. options: {},
  112. },
  113. {},
  114. )
  115. expect(process.env.AICORE_SERVICE_KEY).toBeUndefined()
  116. expect(sdk.sdk).toBeUndefined()
  117. expect(language.language).toBeUndefined()
  118. }),
  119. ),
  120. )
  121. })