mirror of
https://github.com/Heretek-AI/opencode.git
synced 2026-11-22 00:45:12 -10:00
98 lines
3.9 KiB
TypeScript
98 lines
3.9 KiB
TypeScript
import { describe, expect } from "bun:test"
|
|
import { Duration, Effect, Layer, LayerMap } from "effect"
|
|
import { define } from "@opencode-ai/plugin/effect/plugin"
|
|
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
|
|
import { LayerNode } from "@opencode-ai/util/effect/layer-node"
|
|
import { Global } from "@opencode-ai/util/global"
|
|
import { Command } from "@opencode-ai/core/command"
|
|
import { Instance } from "@opencode-ai/core/instance"
|
|
import { LocationServiceMap } from "@opencode-ai/core/location-services"
|
|
import { Location } from "@opencode-ai/core/location"
|
|
import { Plugin } from "@opencode-ai/core/plugin"
|
|
import { SdkPlugins } from "@opencode-ai/core/plugin/sdk"
|
|
import { AbsolutePath } from "@opencode-ai/core/schema"
|
|
import { Database } from "../../src/database/database"
|
|
import { Bus } from "../../src/bus"
|
|
import { tempGlobalLayer } from "../fixture/global"
|
|
import { tmpdirScoped } from "../fixture/tmpdir"
|
|
import { testEffect } from "../lib/effect"
|
|
|
|
const id = Plugin.ID.make("account-prompts")
|
|
|
|
// Host and instance plugins share one ID; each registers a distinct command so the winner is observable.
|
|
const greeter = (command: string) =>
|
|
define({
|
|
id,
|
|
effect: (ctx) =>
|
|
ctx.command.transform((draft) => draft.add({ name: command, execute: () => Effect.void })).pipe(Effect.asVoid),
|
|
})
|
|
|
|
const instances = Layer.effect(
|
|
LocationServiceMap.Service,
|
|
Effect.gen(function* () {
|
|
const map = yield* LayerMap.make(
|
|
(ref: Location.Ref) =>
|
|
Instance.layer(ref, { discovery: false, plugins: [greeter("instance-greet")], replacements: bindings }),
|
|
{ idleTimeToLive: Duration.infinity },
|
|
)
|
|
const bindings: LayerNode.Replacements = [
|
|
Global.node.replace(tempGlobalLayer),
|
|
LocationServiceMap.node.replace(Layer.succeed(LocationServiceMap.Service, map)),
|
|
Instance.node.replace(
|
|
Layer.succeed(Instance.Service, {
|
|
provide: (session) => Effect.provide(map.get(session.location)),
|
|
}),
|
|
),
|
|
]
|
|
return map
|
|
}),
|
|
)
|
|
|
|
const it = testEffect(
|
|
AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node, SdkPlugins.node, LocationServiceMap.node]), [
|
|
Global.node.replace(tempGlobalLayer),
|
|
LocationServiceMap.node.replace(instances),
|
|
]),
|
|
)
|
|
|
|
describe("PluginSupervisor", () => {
|
|
it.live("reports a duplicate plugin ID as a failure without dropping the generation", () =>
|
|
Effect.gen(function* () {
|
|
const sdk = yield* SdkPlugins.Service
|
|
yield* sdk.register(greeter("host-greet"))
|
|
const directory = yield* tmpdirScoped()
|
|
const locations = yield* LocationServiceMap.Service
|
|
const state = yield* Effect.gen(function* () {
|
|
const plugins = yield* Plugin.Service
|
|
yield* plugins.awaitActivation
|
|
const commands = yield* Command.Service
|
|
return {
|
|
inventory: yield* plugins.list(),
|
|
host: yield* commands.get("host-greet"),
|
|
instance: yield* commands.get("instance-greet"),
|
|
}
|
|
}).pipe(
|
|
Effect.scoped,
|
|
Effect.provide(locations.get(Location.Ref.make({ directory: AbsolutePath.make(directory.path) }))),
|
|
)
|
|
|
|
// Earlier boot order wins: the host SDK plugin activates and the instance copy is reported.
|
|
expect(state.inventory.filter((plugin) => plugin.id === id)).toEqual([
|
|
{ id, source: { type: "sdk" }, state: { status: "active" }, features: { server: true } },
|
|
{
|
|
id,
|
|
source: { type: "sdk" },
|
|
state: { status: "failed", error: `Duplicate plugin ID: ${id}` },
|
|
features: { server: true },
|
|
},
|
|
])
|
|
expect(state.host).toBeDefined()
|
|
expect(state.instance).toBeUndefined()
|
|
// Builtins stay active: the duplicate is a plugin failure, not a generation defect.
|
|
expect(
|
|
state.inventory.some((plugin) => plugin.id?.startsWith("opencode.") && plugin.state.status === "active"),
|
|
).toBe(true)
|
|
}),
|
|
)
|
|
})
|