models-dev.test.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import path from "path"
  2. import { describe, expect } from "bun:test"
  3. import { Effect, Layer, Stream } from "effect"
  4. import { Catalog } from "@opencode-ai/core/catalog"
  5. import { Integration } from "@opencode-ai/core/integration"
  6. import { Credential } from "@opencode-ai/core/credential"
  7. import { Database } from "@opencode-ai/core/database/database"
  8. import { EventV2 } from "@opencode-ai/core/event"
  9. import { Flag } from "@opencode-ai/core/flag/flag"
  10. import { Location } from "@opencode-ai/core/location"
  11. import { ModelsDev } from "@opencode-ai/core/models-dev"
  12. import { PluginV2 } from "@opencode-ai/core/plugin"
  13. import { ModelsDevPlugin } from "@opencode-ai/core/plugin/models-dev"
  14. import { Policy } from "@opencode-ai/core/policy"
  15. import { AbsolutePath } from "@opencode-ai/core/schema"
  16. import { location } from "../fixture/location"
  17. import { testEffect } from "../lib/effect"
  18. import { catalogHost, host, integrationHost } from "./host"
  19. const events = EventV2.defaultLayer
  20. const locationLayer = Layer.succeed(
  21. Location.Service,
  22. Location.Service.of(location({ directory: AbsolutePath.make(import.meta.dir) })),
  23. )
  24. const plugins = PluginV2.layer.pipe(Layer.provide(events))
  25. const policy = Policy.layer.pipe(Layer.provide(locationLayer))
  26. const connections = Credential.layer.pipe(
  27. Layer.fresh,
  28. Layer.provide(Database.layerFromPath(":memory:").pipe(Layer.fresh)),
  29. Layer.provide(events),
  30. )
  31. const integrations = Integration.locationLayer.pipe(Layer.provide(events), Layer.provide(connections))
  32. const catalog = Catalog.layer.pipe(
  33. Layer.provide(Layer.mergeAll(events, locationLayer, plugins, policy, connections, integrations)),
  34. )
  35. const layer = Layer.mergeAll(
  36. catalog.pipe(Layer.provide(connections)),
  37. integrations,
  38. connections,
  39. events,
  40. locationLayer,
  41. plugins,
  42. )
  43. const it = testEffect(layer)
  44. describe("ModelsDevPlugin", () => {
  45. it.effect("registers key methods for providers with environment variables", () =>
  46. Effect.acquireUseRelease(
  47. Effect.sync(() => {
  48. const previous = {
  49. path: Flag.OPENCODE_MODELS_PATH,
  50. disabled: Flag.OPENCODE_DISABLE_MODELS_FETCH,
  51. }
  52. Flag.OPENCODE_MODELS_PATH = path.join(import.meta.dir, "fixtures", "models-dev.json")
  53. Flag.OPENCODE_DISABLE_MODELS_FETCH = true
  54. return previous
  55. }),
  56. () =>
  57. Effect.gen(function* () {
  58. const integrations = yield* Integration.Service
  59. const catalog = yield* Catalog.Service
  60. yield* ModelsDevPlugin.effect(
  61. host({
  62. catalog: catalogHost(catalog),
  63. event: { subscribe: () => Stream.never },
  64. integration: integrationHost(integrations),
  65. }),
  66. )
  67. expect(yield* integrations.list()).toEqual([
  68. new Integration.Info({
  69. id: Integration.ID.make("acme"),
  70. name: "Acme",
  71. methods: [
  72. { type: "key" },
  73. {
  74. type: "env",
  75. names: ["ACME_API_KEY"],
  76. },
  77. ],
  78. connections: [],
  79. }),
  80. ])
  81. }).pipe(Effect.provide(ModelsDev.defaultLayer)),
  82. (previous) =>
  83. Effect.sync(() => {
  84. Flag.OPENCODE_MODELS_PATH = previous.path
  85. Flag.OPENCODE_DISABLE_MODELS_FETCH = previous.disabled
  86. }),
  87. ),
  88. )
  89. })