models-dev.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import path from "path"
  2. import { describe, expect } from "bun:test"
  3. import { Effect, Layer } 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. const events = EventV2.defaultLayer
  19. const locationLayer = Layer.succeed(
  20. Location.Service,
  21. Location.Service.of(location({ directory: AbsolutePath.make(import.meta.dir) })),
  22. )
  23. const plugins = PluginV2.layer.pipe(Layer.provide(events))
  24. const policy = Policy.layer.pipe(Layer.provide(locationLayer))
  25. const connections = Credential.layer.pipe(
  26. Layer.fresh,
  27. Layer.provide(Database.layerFromPath(":memory:").pipe(Layer.fresh)),
  28. Layer.provide(events),
  29. )
  30. const catalog = Catalog.layer.pipe(Layer.provide(Layer.mergeAll(events, locationLayer, plugins, policy, connections)))
  31. const integrations = Integration.locationLayer.pipe(Layer.provide(events), Layer.provide(connections))
  32. const layer = Layer.mergeAll(
  33. catalog.pipe(Layer.provide(connections)),
  34. integrations,
  35. connections,
  36. events,
  37. locationLayer,
  38. plugins,
  39. )
  40. const it = testEffect(layer)
  41. describe("ModelsDevPlugin", () => {
  42. it.effect("registers key methods for providers with environment variables", () =>
  43. Effect.acquireUseRelease(
  44. Effect.sync(() => {
  45. const previous = {
  46. path: Flag.OPENCODE_MODELS_PATH,
  47. disabled: Flag.OPENCODE_DISABLE_MODELS_FETCH,
  48. }
  49. Flag.OPENCODE_MODELS_PATH = path.join(import.meta.dir, "fixtures", "models-dev.json")
  50. Flag.OPENCODE_DISABLE_MODELS_FETCH = true
  51. return previous
  52. }),
  53. () =>
  54. Effect.gen(function* () {
  55. yield* ModelsDevPlugin.effect
  56. const integrations = yield* Integration.Service
  57. expect(yield* integrations.list()).toEqual([
  58. new Integration.Info({
  59. id: Integration.ID.make("acme"),
  60. name: "Acme",
  61. methods: [
  62. new Integration.KeyMethod({ type: "key" }),
  63. new Integration.EnvMethod({
  64. type: "env",
  65. names: ["ACME_API_KEY"],
  66. }),
  67. ],
  68. connections: [],
  69. }),
  70. ])
  71. }).pipe(Effect.provide(ModelsDev.defaultLayer)),
  72. (previous) =>
  73. Effect.sync(() => {
  74. Flag.OPENCODE_MODELS_PATH = previous.path
  75. Flag.OPENCODE_DISABLE_MODELS_FETCH = previous.disabled
  76. }),
  77. ),
  78. )
  79. })