mirror of
https://github.com/anomalyco/opencode.git
synced 2026-07-10 05:08:32 +00:00
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import { describe, expect, it } from "bun:test"
|
|
import { Message, SystemPart } from "@opencode-ai/llm"
|
|
import { Agent } from "@opencode-ai/schema/agent"
|
|
import { Model } from "@opencode-ai/schema/model"
|
|
import { Provider } from "@opencode-ai/schema/provider"
|
|
import { Session } from "@opencode-ai/schema/session"
|
|
import { Effect, Layer } from "effect"
|
|
import { PluginHooks } from "../src/plugin/hooks"
|
|
|
|
describe("PluginHooks", () => {
|
|
it("registers scoped domain hooks and triggers them sequentially", async () => {
|
|
const seen: string[] = []
|
|
const program = Effect.gen(function* () {
|
|
const hooks = yield* PluginHooks.Service
|
|
yield* hooks.register("session", "request", (event) =>
|
|
Effect.sync(() => {
|
|
seen.push("first")
|
|
event.system.push(SystemPart.make("second"))
|
|
}),
|
|
)
|
|
yield* hooks.register("session", "request", (event) =>
|
|
Effect.sync(() => {
|
|
seen.push(event.system[1]?.text ?? "missing")
|
|
event.messages = [Message.user("changed")]
|
|
}),
|
|
)
|
|
const event = {
|
|
sessionID: Session.ID.make("ses_hooks"),
|
|
agent: Agent.ID.make("build"),
|
|
model: Model.Ref.make({ providerID: Provider.ID.make("test"), id: Model.ID.make("model") }),
|
|
system: [SystemPart.make("first")],
|
|
messages: [Message.user("original")],
|
|
tools: {},
|
|
}
|
|
|
|
expect(yield* hooks.trigger("session", "request", event)).toBe(event)
|
|
expect(seen).toEqual(["first", "second"])
|
|
expect(event.messages).toEqual([Message.user("changed")])
|
|
})
|
|
|
|
await Effect.runPromise(
|
|
Effect.scoped(program).pipe(
|
|
Effect.provide(PluginHooks.node.implementation as Layer.Layer<PluginHooks.Service>),
|
|
),
|
|
)
|
|
})
|
|
})
|