-
Notifications
You must be signed in to change notification settings - Fork 15.9k
test: add regression coverage for sync plugin hooks #19589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import { afterAll, afterEach, describe, expect, test } from "bun:test" | ||
| import path from "path" | ||
| import { pathToFileURL } from "url" | ||
| import { tmpdir } from "../fixture/fixture" | ||
|
|
||
| const disableDefault = process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS | ||
| process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS = "1" | ||
|
|
||
| const { Plugin } = await import("../../src/plugin/index") | ||
| const { Instance } = await import("../../src/project/instance") | ||
|
|
||
| afterEach(async () => { | ||
| await Instance.disposeAll() | ||
| }) | ||
|
|
||
| afterAll(() => { | ||
| if (disableDefault === undefined) { | ||
| delete process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS | ||
| return | ||
| } | ||
| process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS = disableDefault | ||
| }) | ||
|
|
||
| async function project(source: string) { | ||
| return tmpdir({ | ||
| init: async (dir) => { | ||
| const file = path.join(dir, "plugin.ts") | ||
| await Bun.write(file, source) | ||
| await Bun.write( | ||
| path.join(dir, "opencode.json"), | ||
| JSON.stringify( | ||
| { | ||
| $schema: "https://opencode.ai/config.json", | ||
| plugin: [pathToFileURL(file).href], | ||
| }, | ||
| null, | ||
| 2, | ||
| ), | ||
| ) | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| describe("plugin.trigger", () => { | ||
| test("runs synchronous hooks without crashing", async () => { | ||
| await using tmp = await project( | ||
| [ | ||
| "export default async () => ({", | ||
| ' "experimental.chat.system.transform": (_input, output) => {', | ||
| ' output.system.unshift("sync")', | ||
| " },", | ||
| "})", | ||
| "", | ||
| ].join("\n"), | ||
| ) | ||
|
|
||
| const out = await Instance.provide({ | ||
| directory: tmp.path, | ||
| fn: async () => { | ||
| const out = { system: [] as string[] } | ||
| await Plugin.trigger( | ||
| "experimental.chat.system.transform", | ||
| { | ||
| model: { | ||
| providerID: "anthropic", | ||
| modelID: "claude-sonnet-4-6", | ||
| } as any, | ||
| }, | ||
| out, | ||
| ) | ||
| return out | ||
| }, | ||
| }) | ||
|
|
||
| expect(out.system).toEqual(["sync"]) | ||
| }) | ||
|
|
||
| test("awaits asynchronous hooks", async () => { | ||
| await using tmp = await project( | ||
| [ | ||
| "export default async () => ({", | ||
| ' "experimental.chat.system.transform": async (_input, output) => {', | ||
| " await Bun.sleep(1)", | ||
| ' output.system.unshift("async")', | ||
| " },", | ||
| "})", | ||
| "", | ||
| ].join("\n"), | ||
| ) | ||
|
|
||
| const out = await Instance.provide({ | ||
| directory: tmp.path, | ||
| fn: async () => { | ||
| const out = { system: [] as string[] } | ||
| await Plugin.trigger( | ||
| "experimental.chat.system.transform", | ||
| { | ||
| model: { | ||
| providerID: "anthropic", | ||
| modelID: "claude-sonnet-4-6", | ||
| } as any, | ||
| }, | ||
|
Comment on lines
+97
to
+102
|
||
| out, | ||
| ) | ||
| return out | ||
| }, | ||
| }) | ||
|
|
||
| expect(out.system).toEqual(["async"]) | ||
| }) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hook type for "experimental.chat.system.transform" expects input.model to be a full SDK Model (per the Hooks definition in packages/plugin/src/index.ts), but the test passes { providerID, modelID } cast as any. This makes the regression less representative and could hide failures in plugins that legitimately read fields like model.id; consider passing a more realistic Model shape (or resolving one via the same provider/model helper used in runtime) so the test matches real inputs.