models-dev.test.ts 2.6 KB

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