promise.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { describe, expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { AgentV2 } from "@opencode-ai/core/agent"
  4. import { PluginV2 } from "@opencode-ai/core/plugin"
  5. import { PluginHost } from "@opencode-ai/core/plugin/host"
  6. import { PluginPromise } from "@opencode-ai/core/plugin/promise"
  7. import { define } from "@opencode-ai/plugin/v2/promise"
  8. import { testEffect } from "../lib/effect"
  9. import { PluginTestLayer } from "./fixture"
  10. const it = testEffect(PluginTestLayer)
  11. describe("fromPromise", () => {
  12. it.effect("loads a promise plugin and registers a transform hook", () =>
  13. Effect.gen(function* () {
  14. const agents = yield* AgentV2.Service
  15. const plugin = yield* PluginV2.Service
  16. const host = yield* PluginHost.make(plugin)
  17. const promisePlugin = define({
  18. id: "promise-example",
  19. setup: async (ctx) => {
  20. expect(ctx.options.mode).toBe("strict")
  21. await ctx.agent.transform((draft) => {
  22. draft.update("reviewer", (item) => {
  23. item.description = "Reviews code"
  24. item.mode = "subagent"
  25. })
  26. })
  27. },
  28. })
  29. const adapted = PluginPromise.fromPromise(promisePlugin)
  30. yield* adapted.effect({ ...host, options: { mode: "strict" } })
  31. expect(yield* agents.get(AgentV2.ID.make("reviewer"))).toMatchObject({
  32. description: "Reviews code",
  33. mode: "subagent",
  34. })
  35. }),
  36. )
  37. it.effect("disposes a hook registration on request", () =>
  38. Effect.gen(function* () {
  39. const agents = yield* AgentV2.Service
  40. const plugin = yield* PluginV2.Service
  41. const host = yield* PluginHost.make(plugin)
  42. const promisePlugin = define({
  43. id: "promise-dispose",
  44. setup: async (ctx) => {
  45. const registration = await ctx.agent.transform((draft) => {
  46. draft.update("temp", (item) => {
  47. item.description = "temporary"
  48. })
  49. })
  50. await registration.dispose()
  51. },
  52. })
  53. const adapted = PluginPromise.fromPromise(promisePlugin)
  54. yield* adapted.effect(host)
  55. expect(yield* agents.get(AgentV2.ID.make("temp"))).toBeUndefined()
  56. }),
  57. )
  58. })