models-dev.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 { Connector } from "@opencode-ai/core/connector"
  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 credentials = Credential.layer.pipe(
  26. Layer.provide(Database.layerFromPath(":memory:")),
  27. Layer.provide(events),
  28. )
  29. const catalog = Catalog.layer.pipe(
  30. Layer.provide(Layer.mergeAll(events, locationLayer, plugins, policy, credentials)),
  31. )
  32. const connectors = Connector.locationLayer.pipe(Layer.provide(credentials), Layer.provide(events))
  33. const layer = Layer.mergeAll(catalog, connectors, credentials, events, locationLayer, plugins)
  34. const it = testEffect(layer)
  35. describe("ModelsDevPlugin", () => {
  36. it.effect("registers key connectors for providers with environment variables", () =>
  37. Effect.acquireUseRelease(
  38. Effect.sync(() => {
  39. const previous = {
  40. path: Flag.OPENCODE_MODELS_PATH,
  41. disabled: Flag.OPENCODE_DISABLE_MODELS_FETCH,
  42. }
  43. Flag.OPENCODE_MODELS_PATH = path.join(import.meta.dir, "fixtures", "models-dev.json")
  44. Flag.OPENCODE_DISABLE_MODELS_FETCH = true
  45. return previous
  46. }),
  47. () =>
  48. Effect.gen(function* () {
  49. yield* ModelsDevPlugin.effect
  50. const connectors = yield* Connector.Service
  51. expect(yield* connectors.list()).toEqual([
  52. new Connector.Info({
  53. id: Connector.ID.make("acme"),
  54. name: "Acme",
  55. methods: [
  56. new Connector.KeyMethod({ id: Connector.MethodID.make("api-key"), type: "key", label: "API Key" }),
  57. ],
  58. }),
  59. ])
  60. }).pipe(Effect.provide(ModelsDev.defaultLayer)),
  61. (previous) =>
  62. Effect.sync(() => {
  63. Flag.OPENCODE_MODELS_PATH = previous.path
  64. Flag.OPENCODE_DISABLE_MODELS_FETCH = previous.disabled
  65. }),
  66. ),
  67. )
  68. })