import { expect } from "bun:test" import path from "path" import { Clock, Effect } from "effect" import { TestClock } from "effect/testing" import { Command } from "@opencode-ai/core/command" import { Credential } from "@opencode-ai/core/credential" import { Integration } from "@opencode-ai/core/integration" import { Plugin } from "@opencode-ai/core/plugin" import { PluginModule } from "@opencode-ai/core/plugin/module" import { Session } from "@opencode-ai/schema/session" import { testEffect } from "./lib/effect" import { PluginTestLayer } from "./plugin/fixture" const it = testEffect(PluginTestLayer) it.live("loads a local plugin with its configured options", () => Effect.gen(function* () { const plugins = yield* Plugin.Service const commands = yield* Command.Service yield* plugins.awaitActivation const definition = yield* PluginModule.load({ type: "add", target: path.join(import.meta.dir, "plugin/fixtures/greeting.ts"), options: { description: "Configured greeting" }, }) if ("pending" in definition) return yield* Effect.die("Local plugin was not loaded") yield* plugins.activate([definition]) expect(yield* commands.get("greet")).toMatchObject({ description: "Configured greeting" }) }), ) it.effect("unloading a plugin removes its commands and runs cleanup", () => Effect.gen(function* () { const plugins = yield* Plugin.Service const commands = yield* Command.Service let cleaned = false yield* plugins.activate([ { id: "greeting", revision: "1", effect: (ctx) => Effect.gen(function* () { yield* Effect.addFinalizer(() => Effect.sync(() => { cleaned = true }), ) yield* ctx.command.transform((draft) => draft.add({ name: "greet", execute: () => Effect.void })) }), }, ]) expect(yield* commands.get("greet")).toBeDefined() expect(cleaned).toBe(false) yield* plugins.activate([]) expect(yield* commands.get("greet")).toBeUndefined() expect(cleaned).toBe(true) }), ) it.effect("reports a failed plugin without blocking a healthy plugin", () => Effect.gen(function* () { const plugins = yield* Plugin.Service const commands = yield* Command.Service yield* plugins.activate([ { id: "broken", revision: "1", effect: () => Effect.die(new Error("Setup failed")) }, { id: "greeting", revision: "1", effect: (ctx) => ctx.command .transform((draft) => draft.add({ name: "greet", execute: () => Effect.void })) .pipe(Effect.asVoid), }, ]) expect((yield* plugins.list()).find((plugin) => plugin.id === "broken")?.state).toMatchObject({ status: "failed", error: expect.stringContaining("Setup failed"), }) expect(yield* commands.get("greet")).toBeDefined() }), ) it.effect("reloading a plugin replaces its command implementation", () => Effect.gen(function* () { const plugins = yield* Plugin.Service const commands = yield* Command.Service const output: string[] = [] const load = (revision: string, text: string) => plugins.activate([ { id: "greeting", revision, effect: (ctx) => ctx.command .transform((draft) => draft.add({ name: "greet", execute: () => Effect.sync(() => { output.push(text) }), }), ) .pipe(Effect.asVoid), }, ]) const request = { name: "greet", invocation: { sessionID: Session.ID.make("ses_plugin"), prompt: { text: "" }, delivery: "steer" as const }, } yield* load("1", "before") yield* commands.execute(request) expect(output).toEqual(["before"]) yield* load("2", "after") yield* commands.execute(request) expect(output).toEqual(["before", "after"]) }), ) it.effect("refreshes expired OAuth credentials through the context during activation", () => Effect.gen(function* () { const plugins = yield* Plugin.Service const credentials = yield* Credential.Service const integrations = yield* Integration.Service const clock = yield* Clock.Clock const integrationID = Integration.ID.make("refresh-fixture") const methodID = Integration.MethodID.make("oauth") const expired = Credential.OAuth.make({ type: "oauth", methodID, access: "expired-access", refresh: "fixture-refresh", expires: (yield* Clock.currentTimeMillis) + 60_000, }) const stored = yield* credentials.create({ integrationID, label: "Fixture", value: expired }) yield* TestClock.adjust("2 minutes") const refreshed = Credential.OAuth.make({ ...expired, access: "fresh-access", refresh: "rotated-refresh", expires: (yield* Clock.currentTimeMillis) + 3_600_000, }) const refreshes: Credential.OAuth[] = [] const resolved: Array = [] yield* plugins.activate([ { id: "oauth-refresh", revision: "1", effect: (ctx) => Effect.gen(function* () { yield* ctx.integration.transform((draft) => draft.method.update({ integrationID, method: { id: methodID, type: "oauth", label: "Fixture" }, authorize: () => Effect.die("unexpected authorization"), refresh: (value) => Effect.sync(() => { refreshes.push(value) return refreshed }), }), ) // The method registered above must be readable before the activation batch ends. const connection = yield* ctx.integration.connection.active(integrationID) if (!connection) return yield* Effect.die("fixture connection not found") resolved.push(yield* ctx.integration.connection.resolve(connection).pipe(Effect.orDie)) }).pipe( // Plugin activation isolates ambient services, including the test clock. Effect.provideService(Clock.Clock, clock), ), }, ]) expect(yield* plugins.list()).toMatchObject([{ id: "oauth-refresh", state: { status: "active" } }]) expect(resolved).toEqual([refreshed]) expect((yield* credentials.get(stored.id))?.value).toEqual(refreshed) expect(yield* integrations.connection.resolve({ type: "credential", id: stored.id, label: stored.label })).toEqual( refreshed, ) expect(refreshes).toEqual([expired]) }), )