models-dev.test.ts 2.9 KB

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