mirror of
https://github.com/anomalyco/opencode.git
synced 2026-07-24 04:01:10 -04:00
129 lines
4.4 KiB
TypeScript
129 lines
4.4 KiB
TypeScript
import { describe, expect } from "bun:test"
|
|
import { Effect } from "effect"
|
|
import { AgentV2 } from "@opencode-ai/core/agent"
|
|
import { Integration } from "@opencode-ai/core/integration"
|
|
import { PluginV2 } from "@opencode-ai/core/plugin"
|
|
import { PluginHost } from "@opencode-ai/core/plugin/host"
|
|
import { PluginPromise } from "@opencode-ai/core/plugin/promise"
|
|
import { Plugin } from "@opencode-ai/plugin/v2"
|
|
import { testEffect } from "../lib/effect"
|
|
import { PluginTestLayer } from "./fixture"
|
|
|
|
const it = testEffect(PluginTestLayer)
|
|
|
|
describe("fromPromise", () => {
|
|
it.effect("forwards standard client reads", () =>
|
|
Effect.gen(function* () {
|
|
const plugin = yield* PluginV2.Service
|
|
const host = yield* PluginHost.make(plugin)
|
|
const seen: string[] = []
|
|
const promisePlugin = Plugin.define({
|
|
id: "promise-client-reads",
|
|
setup: async (ctx) => {
|
|
const results = await Promise.all([
|
|
ctx.agent.list(),
|
|
ctx.catalog.provider.list(),
|
|
ctx.catalog.model.list(),
|
|
ctx.command.list(),
|
|
ctx.integration.list(),
|
|
ctx.plugin.list(),
|
|
ctx.reference.list(),
|
|
ctx.skill.list(),
|
|
])
|
|
seen.push(...results.map((result) => result.location.directory))
|
|
},
|
|
})
|
|
|
|
yield* PluginPromise.fromPromise(promisePlugin).effect(host)
|
|
|
|
expect(seen).toHaveLength(8)
|
|
expect(new Set(seen).size).toBe(1)
|
|
}),
|
|
)
|
|
|
|
it.effect("loads a promise plugin and registers a transform hook", () =>
|
|
Effect.gen(function* () {
|
|
const agents = yield* AgentV2.Service
|
|
const plugin = yield* PluginV2.Service
|
|
const host = yield* PluginHost.make(plugin)
|
|
|
|
const promisePlugin = Plugin.define({
|
|
id: "promise-example",
|
|
setup: async (ctx) => {
|
|
expect(ctx.options.mode).toBe("strict")
|
|
await ctx.agent.transform((draft) => {
|
|
draft.update("reviewer", (item) => {
|
|
item.description = "Reviews code"
|
|
item.mode = "subagent"
|
|
})
|
|
})
|
|
},
|
|
})
|
|
|
|
const adapted = PluginPromise.fromPromise(promisePlugin)
|
|
yield* adapted.effect({ ...host, options: { mode: "strict" } })
|
|
|
|
expect(yield* agents.get(AgentV2.ID.make("reviewer"))).toMatchObject({
|
|
description: "Reviews code",
|
|
mode: "subagent",
|
|
})
|
|
}),
|
|
)
|
|
|
|
it.effect("disposes a hook registration on request", () =>
|
|
Effect.gen(function* () {
|
|
const agents = yield* AgentV2.Service
|
|
const plugin = yield* PluginV2.Service
|
|
const host = yield* PluginHost.make(plugin)
|
|
|
|
const promisePlugin = Plugin.define({
|
|
id: "promise-dispose",
|
|
setup: async (ctx) => {
|
|
const registration = await ctx.agent.transform((draft) => {
|
|
draft.update("temp", (item) => {
|
|
item.description = "temporary"
|
|
})
|
|
})
|
|
await registration.dispose()
|
|
},
|
|
})
|
|
|
|
const adapted = PluginPromise.fromPromise(promisePlugin)
|
|
yield* adapted.effect(host)
|
|
|
|
expect(yield* agents.get(AgentV2.ID.make("temp"))).toBeUndefined()
|
|
}),
|
|
)
|
|
|
|
it.effect("adapts promise web search capability execution", () =>
|
|
Effect.gen(function* () {
|
|
const integrations = yield* Integration.Service
|
|
const plugin = yield* PluginV2.Service
|
|
const host = yield* PluginHost.make(plugin)
|
|
const promisePlugin = Plugin.define({
|
|
id: "promise-websearch",
|
|
setup: async (ctx) => {
|
|
await ctx.integration.register({
|
|
id: "promise-websearch",
|
|
name: "Promise Web Search",
|
|
methods: [{ type: "env", names: ["PROMISE_WEBSEARCH_KEY"] }],
|
|
websearch: {
|
|
connection: "optional",
|
|
execute: async (input) => ({ text: `promise: ${input.query}` }),
|
|
},
|
|
})
|
|
},
|
|
})
|
|
|
|
yield* PluginPromise.fromPromise(promisePlugin).effect(host)
|
|
expect(yield* integrations.get(Integration.ID.make("promise-websearch"))).toMatchObject({
|
|
name: "Promise Web Search",
|
|
methods: [{ type: "env", names: ["PROMISE_WEBSEARCH_KEY"] }],
|
|
})
|
|
const provider = yield* integrations.websearch.get(Integration.ID.make("promise-websearch"))
|
|
if (!provider) return yield* Effect.die("Expected promise web search provider")
|
|
expect(yield* provider.execute({ query: "effect" }, {})).toEqual({ text: "promise: effect" })
|
|
}),
|
|
)
|
|
})
|