Files
next-tjwater-agent/tests/frontendAction/callIdPlugin.test.ts
T

43 lines
2.0 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { FRONTEND_ACTION_TOOLS, frontendActionCallIdPlugin, server } from "../../.opencode/plugins/frontend-action-call-id.js";
import { frontendActionRegistry } from "../../src/frontendAction/registry.js";
const frontendActionToolNames = () => readdirSync(".opencode/tools")
.filter((file) => file.endsWith(".ts"))
.filter((file) => readFileSync(join(".opencode/tools", file), "utf8").includes("executeFrontendAction("))
.map((file) => file.replace(/\.ts$/, ""))
.sort();
describe("frontend action call ID plugin", () => {
it("is registered in the OpenCode project config", () => {
const config = JSON.parse(readFileSync("opencode.json", "utf8")) as { plugin?: unknown[] };
expect(config.plugin).toContain("./.opencode/plugins/frontend-action-call-id.ts");
});
it("exposes the server export required by OpenCode", () => {
expect(server).toBe(frontendActionCallIdPlugin);
});
it("overwrites forged IDs only for allowlisted tools", async () => {
const hooks = await frontendActionCallIdPlugin({} as never);
const before = hooks["tool.execute.before"];
if (!before) throw new Error("hook missing");
const actionOutput = { args: { x: 1, __frontend_action_call_id: "forged" } };
await before({ tool: "zoom_to_map", sessionID: "session", callID: "real" }, actionOutput);
expect(actionOutput.args.__frontend_action_call_id).toBe("real");
const ordinaryOutput = { args: { query: "water" } };
await before({ tool: "web_search", sessionID: "session", callID: "ignored" }, ordinaryOutput);
expect(ordinaryOutput.args).toEqual({ query: "water" });
});
it("covers every tool that uses the frontend action bridge", () => {
const bridgeTools = frontendActionToolNames();
expect([...FRONTEND_ACTION_TOOLS].sort()).toEqual(bridgeTools);
expect(frontendActionRegistry.map((action) => action.id).sort()).toEqual(bridgeTools);
});
});