Files
next-tjwater-agent/tests/frontendAction/callIdPlugin.test.ts
T
jiang 7d47a2df86
Generic Container CI/CD / test-build-publish (push) Failing after 8m25s
Agent CI/CD / build-test-publish-and-deploy (push) Failing after 8m25s
feat: add scoped history and artifacts
2026-10-22 18:40:20 +08:00

55 lines
3.0 KiB
TypeScript

import { afterAll, beforeAll, describe, expect, it } from "bun:test";
import plugin from "../../.opencode/plugins/tjwater-tools.js";
const originalInternalToken = process.env.AGENT_INTERNAL_TOKEN;
beforeAll(() => {
process.env.AGENT_INTERNAL_TOKEN = "test-internal-token";
});
afterAll(() => {
if (originalInternalToken === undefined) delete process.env.AGENT_INTERNAL_TOKEN;
else process.env.AGENT_INTERNAL_TOKEN = originalInternalToken;
});
describe("OpenCode 2 business tool registration", () => {
it("registers business tools for direct dispatch, forwards the native call ID and validates inputs", async () => {
const tools = new Map<string, any>();
const storage = new Map<string, unknown>();
let rpcList: (() => Promise<unknown>) | undefined;
let readTodos: ((input: { session_id: string }) => Promise<unknown>) | undefined;
await plugin.setup({
tool: { transform: async (fn: (editor: unknown) => void) => fn({ list: () => Array.from(tools, ([id, tool]) => ({ ...tool, id })), add: (tool: any) => tools.set(tool.name, tool), remove: (name: string) => tools.delete(name), update: () => {} }), list: async () => Array.from(tools, ([id, tool]) => ({ ...tool, id })) },
mcp: {
transform: async (fn: (editor: unknown) => void) =>
fn({ list: () => [], set: () => {} }),
list: async () => ({ data: [] }),
},
shell: { hook: async () => {} },
permission: { hook: async () => {} },
session: { hook: async () => {} },
rpc: { register: async (_definition: unknown, handlers: { list: () => Promise<unknown>; read_todos: (input: { session_id: string }) => Promise<unknown> }) => { rpcList = handlers.list; readTodos = handlers.read_todos; } },
storage: { set: async (key: string, value: unknown) => storage.set(key, value), get: async (key: string) => storage.get(key) },
} as never);
expect(tools.get("render_scada_analysis").options).toEqual({ codemode: false, permission: "render_scada_analysis" });
for (const name of ["publish_task_progress", "publish_task_presentation"]) {
expect(tools.get(name).options).toEqual({ codemode: false, permission: name });
}
const render = tools.get("render_scada_analysis");
await expect(render.execute({ reason: "test", items: [{ device_id: "x", level: "wrong" }] }, { sessionID: "s", id: "call-native" })).rejects.toThrow();
const todo = tools.get("todowrite");
const todos = [{ content: "check", status: "pending" }];
await todo.execute({ todos }, { sessionID: "s" });
expect(tools.has("todoread")).toBeFalse();
expect(await readTodos!({ session_id: "s" })).toEqual(todos);
expect(await readTodos!({ session_id: "another" })).toEqual([]);
const discovery = await rpcList!();
expect(JSON.parse(JSON.stringify(discovery))).toEqual(discovery);
});
it("exports a single native plugin entrypoint", async () => {
const module = await import("../../.opencode/plugins/tjwater-tools.js");
expect(Object.keys(module)).toEqual(["default"]);
expect(module.default.id).toBe("tjwater.tools");
});
});