provider-xai.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { describe, expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { Credential } from "@opencode-ai/core/credential"
  4. import { Integration } from "@opencode-ai/core/integration"
  5. import { Plugin } from "@opencode-ai/core/plugin"
  6. import { PluginHost } from "@opencode-ai/core/plugin/host"
  7. import { XAIPlugin } from "@opencode-ai/core/plugin/provider/xai"
  8. import { testEffect } from "../lib/effect"
  9. import { PluginTestLayer } from "./fixture"
  10. const it = testEffect(PluginTestLayer)
  11. const addPlugin = Effect.fn(function* () {
  12. const plugin = yield* Plugin.Service
  13. const host = yield* PluginHost.make(plugin)
  14. yield* XAIPlugin.effect(host)
  15. })
  16. describe("XAIPlugin", () => {
  17. it.effect("registers device OAuth and API key methods", () =>
  18. Effect.gen(function* () {
  19. yield* addPlugin()
  20. const integrations = yield* Integration.Service
  21. const integration = yield* integrations.get(Integration.ID.make("xai"))
  22. expect(integration?.name).toBe("xAI")
  23. expect(integration?.methods).toEqual([
  24. {
  25. id: Integration.MethodID.make("device"),
  26. type: "oauth",
  27. label: "SuperGrok Subscription",
  28. },
  29. { type: "key", label: "Manually enter API Key" },
  30. ])
  31. }),
  32. )
  33. it.effect("migrates browser OAuth credentials to the device method", () =>
  34. Effect.gen(function* () {
  35. const credentials = yield* Credential.Service
  36. const original = yield* credentials.create({
  37. integrationID: Integration.ID.make("xai"),
  38. label: "personal",
  39. value: Credential.OAuth.make({
  40. type: "oauth",
  41. methodID: Integration.MethodID.make("browser"),
  42. access: "access",
  43. refresh: "refresh",
  44. expires: 123,
  45. metadata: { account: "account" },
  46. }),
  47. })
  48. yield* addPlugin()
  49. expect(yield* credentials.get(original.id)).toEqual({
  50. ...original,
  51. value: Credential.OAuth.make({
  52. type: "oauth",
  53. methodID: Integration.MethodID.make("device"),
  54. access: "access",
  55. refresh: "refresh",
  56. expires: 123,
  57. metadata: { account: "account" },
  58. }),
  59. })
  60. }),
  61. )
  62. })