models-dev.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  7. import { LayerNode } from "@opencode-ai/core/effect/layer-node"
  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 { ModelsDevPlugin } from "@opencode-ai/core/plugin/models-dev"
  13. import { AbsolutePath } from "@opencode-ai/core/schema"
  14. import { location } from "../fixture/location"
  15. import { testEffect } from "../lib/effect"
  16. import { catalogHost, host, integrationHost } from "./host"
  17. const locationLayer = Layer.succeed(
  18. Location.Service,
  19. Location.Service.of(location({ directory: AbsolutePath.make(import.meta.dir) })),
  20. )
  21. const layer = AppNodeBuilder.build(
  22. LayerNode.group([Catalog.node, Integration.node, EventV2.node]),
  23. [[Location.node, locationLayer]],
  24. )
  25. const it = testEffect(layer)
  26. describe("ModelsDevPlugin", () => {
  27. it.effect("registers key methods for providers with environment variables", () =>
  28. Effect.acquireUseRelease(
  29. Effect.sync(() => {
  30. const previous = {
  31. path: Flag.OPENCODE_MODELS_PATH,
  32. disabled: Flag.OPENCODE_DISABLE_MODELS_FETCH,
  33. }
  34. Flag.OPENCODE_MODELS_PATH = path.join(import.meta.dir, "fixtures", "models-dev.json")
  35. Flag.OPENCODE_DISABLE_MODELS_FETCH = true
  36. return previous
  37. }),
  38. () =>
  39. Effect.gen(function* () {
  40. const integrations = yield* Integration.Service
  41. const catalog = yield* Catalog.Service
  42. yield* ModelsDevPlugin.effect(
  43. host({
  44. catalog: catalogHost(catalog),
  45. integration: integrationHost(integrations),
  46. }),
  47. )
  48. expect(yield* integrations.list()).toEqual([
  49. new Integration.Info({
  50. id: Integration.ID.make("acme"),
  51. name: "Acme",
  52. methods: [
  53. { type: "key" },
  54. {
  55. type: "env",
  56. names: ["ACME_API_KEY"],
  57. },
  58. ],
  59. connections: [],
  60. }),
  61. ])
  62. }).pipe(Effect.provide(AppNodeBuilder.build(ModelsDev.node))),
  63. (previous) =>
  64. Effect.sync(() => {
  65. Flag.OPENCODE_MODELS_PATH = previous.path
  66. Flag.OPENCODE_DISABLE_MODELS_FETCH = previous.disabled
  67. }),
  68. ),
  69. )
  70. })