reload.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import path from "path"
  2. import { describe, expect } from "bun:test"
  3. import { Document, Event, Info } from "@opencode-ai/schema/config"
  4. import { Agent } from "@opencode-ai/core/agent"
  5. import { Catalog } from "@opencode-ai/core/catalog"
  6. import { Command } from "@opencode-ai/core/command"
  7. import { Config } from "@opencode-ai/core/config"
  8. import { ConfigAgentPlugin } from "@opencode-ai/core/config/plugin/agent"
  9. import { ConfigCommandPlugin } from "@opencode-ai/core/config/plugin/command"
  10. import { ConfigProviderPlugin } from "@opencode-ai/core/config/plugin/provider"
  11. import { ConfigReferencePlugin } from "@opencode-ai/core/config/plugin/reference"
  12. import { ConfigSkillPlugin } from "@opencode-ai/core/config/plugin/skill"
  13. import { Bus } from "@opencode-ai/core/bus"
  14. import { Global } from "@opencode-ai/util/global"
  15. import { Plugin } from "@opencode-ai/core/plugin"
  16. import { PluginHost } from "@opencode-ai/core/plugin/host"
  17. import { Provider } from "@opencode-ai/core/provider"
  18. import { Reference } from "@opencode-ai/core/reference"
  19. import { Skill } from "@opencode-ai/core/skill"
  20. import { Effect, Schema } from "effect"
  21. import { AbsolutePath } from "@opencode-ai/core/schema"
  22. import { testEffect } from "../lib/effect"
  23. import { PluginTestLayer } from "../plugin/fixture"
  24. const it = testEffect(PluginTestLayer)
  25. const decode = Schema.decodeUnknownSync(Info)
  26. const document = path.join(import.meta.dir, "opencode.json")
  27. describe("config plugin reloads", () => {
  28. it.live("reloads config-backed domains without reloading external plugins", () =>
  29. Effect.gen(function* () {
  30. const agents = yield* Agent.Service
  31. const catalog = yield* Catalog.Service
  32. const commands = yield* Command.Service
  33. const bus = yield* Bus.Service
  34. const plugins = yield* Plugin.Service
  35. const references = yield* Reference.Service
  36. const skills = yield* Skill.Service
  37. const host = yield* PluginHost.make(plugins)
  38. const test = yield* Config.Test
  39. yield* ConfigAgentPlugin.Plugin.effect(host)
  40. yield* ConfigCommandPlugin.Plugin.effect(host)
  41. yield* ConfigSkillPlugin.Plugin.effect(host)
  42. yield* ConfigReferencePlugin.Plugin.effect(host)
  43. yield* ConfigProviderPlugin.Plugin.effect(host)
  44. expect((yield* agents.get(Agent.ID.make("first")))?.description).toBe("First agent")
  45. expect((yield* commands.get("first"))?.description).toBe("First command")
  46. expect((yield* skills.list()).some((skill) => skill.id === "first")).toBe(true)
  47. expect((yield* references.list()).map((reference) => reference.name)).toEqual(["first"])
  48. expect(yield* catalog.provider.get(Provider.ID.make("first"))).toBeDefined()
  49. yield* test.setEntries([config("second")])
  50. yield* Effect.yieldNow
  51. yield* bus.publish(Event.Updated, {})
  52. yield* waitUntil(
  53. Effect.gen(function* () {
  54. return (
  55. (yield* agents.get(Agent.ID.make("first"))) === undefined &&
  56. (yield* agents.get(Agent.ID.make("second")))?.description === "Second agent" &&
  57. (yield* commands.get("first")) === undefined &&
  58. (yield* commands.get("second"))?.description === "Second command" &&
  59. (yield* references.list()).some((reference) => reference.name === "second") &&
  60. (yield* catalog.provider.get(Provider.ID.make("first"))) === undefined &&
  61. (yield* catalog.provider.get(Provider.ID.make("second"))) !== undefined
  62. )
  63. }),
  64. )
  65. expect((yield* skills.list()).some((skill) => skill.id === "first")).toBe(false)
  66. expect((yield* skills.list()).some((skill) => skill.id === "second")).toBe(true)
  67. }).pipe(
  68. Effect.provide(Config.testLayer([config("first")])),
  69. Effect.provideService(Global.Service, Global.Service.of(Global.make())),
  70. ),
  71. )
  72. })
  73. function config(name: string) {
  74. return new Document({
  75. type: "document",
  76. path: AbsolutePath.make(document),
  77. info: decode({
  78. agents: { [name]: { description: `${title(name)} agent`, mode: "subagent" } },
  79. commands: { [name]: { template: `${title(name)} command`, description: `${title(name)} command` } },
  80. skills: [path.join(import.meta.dir, "fixture", "skills", `${name}-source`)],
  81. references: { [name]: `/references/${name}` },
  82. providers: { [name]: { models: { chat: { name: `${title(name)} model` } } } },
  83. }),
  84. })
  85. }
  86. function title(value: string) {
  87. return value.charAt(0).toUpperCase() + value.slice(1)
  88. }
  89. const waitUntil = Effect.fnUntraced(function* (condition: Effect.Effect<boolean>) {
  90. for (let attempt = 0; attempt < 100; attempt++) {
  91. if (yield* condition) return
  92. yield* Effect.sleep("10 millis")
  93. }
  94. return yield* Effect.die("Timed out waiting for config plugin reloads")
  95. })