reload.test.ts 4.7 KB

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