1
0

reload.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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((yield* skills.list()).some((skill) => skill.id === "first")).toBe(true)
  46. expect((yield* references.list()).map((reference) => reference.name)).toEqual(["first"])
  47. expect(yield* catalog.provider.get(Provider.ID.make("first"))).toBeDefined()
  48. yield* test.setEntries([config("second")])
  49. yield* Effect.yieldNow
  50. yield* bus.publish(Event.Updated, {})
  51. yield* waitUntil(
  52. Effect.gen(function* () {
  53. return (
  54. (yield* agents.get(Agent.ID.make("first"))) === undefined &&
  55. (yield* agents.get(Agent.ID.make("second")))?.description === "Second agent" &&
  56. (yield* commands.get("first")) === undefined &&
  57. (yield* commands.get("second"))?.description === "Second command" &&
  58. (yield* references.list()).some((reference) => reference.name === "second") &&
  59. (yield* catalog.provider.get(Provider.ID.make("first"))) === undefined &&
  60. (yield* catalog.provider.get(Provider.ID.make("second"))) !== undefined
  61. )
  62. }),
  63. )
  64. expect((yield* skills.list()).some((skill) => skill.id === "first")).toBe(false)
  65. expect((yield* skills.list()).some((skill) => skill.id === "second")).toBe(true)
  66. }).pipe(
  67. Effect.provide(Config.testLayer([config("first")])),
  68. Effect.provideService(Global.Service, Global.Service.of(Global.make())),
  69. ),
  70. )
  71. })
  72. function config(name: string) {
  73. return new Document({
  74. type: "document",
  75. path: document,
  76. info: decode({
  77. agents: { [name]: { description: `${title(name)} agent`, mode: "subagent" } },
  78. commands: { [name]: { template: `${title(name)} command`, description: `${title(name)} command` } },
  79. skills: [path.join(import.meta.dir, "fixture", "skills", `${name}-source`)],
  80. references: { [name]: `/references/${name}` },
  81. providers: { [name]: { models: { chat: { name: `${title(name)} model` } } } },
  82. }),
  83. })
  84. }
  85. function title(value: string) {
  86. return value.charAt(0).toUpperCase() + value.slice(1)
  87. }
  88. const waitUntil = Effect.fnUntraced(function* (condition: Effect.Effect<boolean>) {
  89. for (let attempt = 0; attempt < 100; attempt++) {
  90. if (yield* condition) return
  91. yield* Effect.sleep("10 millis")
  92. }
  93. return yield* Effect.die("Timed out waiting for config plugin reloads")
  94. })