feat: add SCADA frontend action bridge
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
|
||||
import { frontendActionCallIdPlugin } from "../../.opencode/plugins/frontend-action-call-id.js";
|
||||
|
||||
describe("frontend action call ID plugin", () => {
|
||||
it("injects call IDs only for SCADA frontend actions", async () => {
|
||||
const hooks = await frontendActionCallIdPlugin({} as never);
|
||||
const before = hooks["tool.execute.before"];
|
||||
if (!before) throw new Error("before hook missing");
|
||||
const actionArgs: Record<string, unknown> = { items: [] };
|
||||
const actionOutput: { args: Record<string, unknown> } = { args: actionArgs };
|
||||
await before(
|
||||
{ tool: "render_scada_analysis", callID: "call-1", sessionID: "s" },
|
||||
actionOutput,
|
||||
);
|
||||
expect(actionOutput.args).toBe(actionArgs);
|
||||
expect(actionArgs).toEqual({
|
||||
items: [],
|
||||
__frontend_action_call_id: "call-1",
|
||||
});
|
||||
const regularArgs = { query: "SCADA" };
|
||||
const regularOutput = { args: regularArgs };
|
||||
await before(
|
||||
{ tool: "session_search", callID: "call-2", sessionID: "s" },
|
||||
regularOutput,
|
||||
);
|
||||
expect(regularOutput.args).toBe(regularArgs);
|
||||
expect(regularArgs).toEqual({ query: "SCADA" });
|
||||
});
|
||||
|
||||
it("exports only plugin functions so OpenCode can load the module", async () => {
|
||||
const pluginModule = await import("../../.opencode/plugins/frontend-action-call-id.js");
|
||||
expect(Object.keys(pluginModule)).toEqual(["frontendActionCallIdPlugin"]);
|
||||
expect(Object.values(pluginModule).every((value) => typeof value === "function")).toBeTrue();
|
||||
});
|
||||
});
|
||||
@@ -1,12 +1,143 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
|
||||
import { FrontendActionCoordinator, FrontendActionError } from "../../src/frontendAction/coordinator.js";
|
||||
import {
|
||||
FrontendActionCoordinator,
|
||||
FrontendActionError,
|
||||
} from "../../src/frontendAction/coordinator.js";
|
||||
import { frontendActionRegistry } from "../../src/frontendAction/registry.js";
|
||||
import type { FrontendActionRequest } from "../../src/frontendAction/types.js";
|
||||
|
||||
const items = [
|
||||
{ sensor_id: "MP01", level: "high" },
|
||||
{ sensor_id: "MP02", level: "medium" },
|
||||
] as const;
|
||||
|
||||
const start = (
|
||||
coordinator: FrontendActionCoordinator,
|
||||
name = "render_scada_analysis",
|
||||
params: unknown = { items },
|
||||
) => {
|
||||
let emitted: FrontendActionRequest | undefined;
|
||||
coordinator.registerSink("session-1", (request) => {
|
||||
emitted = request;
|
||||
});
|
||||
const pending = coordinator.request({
|
||||
sessionId: "session-1",
|
||||
toolCallId: "call-1",
|
||||
name,
|
||||
params,
|
||||
});
|
||||
if (!emitted) throw new Error("request was not emitted");
|
||||
return { emitted, pending };
|
||||
};
|
||||
|
||||
describe("FrontendActionCoordinator", () => {
|
||||
it("rejects frontend actions when the registry is empty", () => {
|
||||
it("registers only the two view-risk SCADA actions with 15 second expiry", () => {
|
||||
expect(frontendActionRegistry).toEqual([
|
||||
expect.objectContaining({ id: "render_scada_analysis", risk: "view", timeoutMs: 15_000 }),
|
||||
expect.objectContaining({ id: "clear_scada_analysis", risk: "view", timeoutMs: 15_000 }),
|
||||
]);
|
||||
});
|
||||
|
||||
it("emits once and resolves with validated browser output", async () => {
|
||||
const coordinator = new FrontendActionCoordinator();
|
||||
const { emitted, pending } = start(coordinator);
|
||||
expect(emitted).toMatchObject({
|
||||
name: "render_scada_analysis",
|
||||
risk: "view",
|
||||
replayPolicy: "never",
|
||||
params: { items },
|
||||
});
|
||||
expect(emitted.expiresAt - emitted.issuedAt).toBe(15_000);
|
||||
const result = {
|
||||
version: "frontend-action-result@1" as const,
|
||||
actionId: emitted.actionId,
|
||||
status: "succeeded" as const,
|
||||
output: {
|
||||
rendered_ids: ["MP01", "MP02"],
|
||||
missing_ids: [],
|
||||
level_counts: { high: 1, medium: 1, low: 0, unrated: 0 },
|
||||
fitted: true as const,
|
||||
},
|
||||
completedAt: Date.now(),
|
||||
};
|
||||
expect(coordinator.submitResult("session-1", result).duplicate).toBe(false);
|
||||
expect((await pending).output).toEqual(result.output);
|
||||
expect(coordinator.submitResult("session-1", result).duplicate).toBe(true);
|
||||
});
|
||||
|
||||
it("does not replay never-policy actions when the browser sink reconnects", async () => {
|
||||
const coordinator = new FrontendActionCoordinator();
|
||||
const emitted: FrontendActionRequest[] = [];
|
||||
const unregister = coordinator.registerSink("session-1", (request) => emitted.push(request));
|
||||
const pending = coordinator.request({
|
||||
sessionId: "session-1",
|
||||
toolCallId: "call-no-replay",
|
||||
name: "clear_scada_analysis",
|
||||
params: {},
|
||||
});
|
||||
unregister();
|
||||
coordinator.registerSink("session-1", (request) => emitted.push(request));
|
||||
expect(emitted).toHaveLength(1);
|
||||
coordinator.submitResult("session-1", {
|
||||
version: "frontend-action-result@1",
|
||||
actionId: emitted[0].actionId,
|
||||
status: "succeeded",
|
||||
output: { cleared: true },
|
||||
completedAt: Date.now(),
|
||||
});
|
||||
await pending;
|
||||
});
|
||||
|
||||
it("accepts clear output and rejects invalid successful output", async () => {
|
||||
const coordinator = new FrontendActionCoordinator();
|
||||
const { emitted, pending } = start(coordinator, "clear_scada_analysis", {});
|
||||
expect(() => coordinator.submitResult("session-1", {
|
||||
version: "frontend-action-result@1",
|
||||
actionId: emitted.actionId,
|
||||
status: "succeeded",
|
||||
output: { cleared: false },
|
||||
completedAt: Date.now(),
|
||||
})).toThrow("invalid frontend action output");
|
||||
coordinator.submitResult("session-1", {
|
||||
version: "frontend-action-result@1",
|
||||
actionId: emitted.actionId,
|
||||
status: "succeeded",
|
||||
output: { cleared: true },
|
||||
completedAt: Date.now(),
|
||||
});
|
||||
expect((await pending).output).toEqual({ cleared: true });
|
||||
});
|
||||
|
||||
it("validates item count, unique IDs, ID length, and levels", () => {
|
||||
const coordinator = new FrontendActionCoordinator();
|
||||
coordinator.registerSink("s", () => undefined);
|
||||
expect(() => coordinator.request({ sessionId: "s", toolCallId: "c", name: "zoom_to_map", params: {} })).toThrow(FrontendActionError);
|
||||
expect(() => coordinator.request({ sessionId: "s", toolCallId: "c", name: "zoom_to_map", params: {} })).toThrow("unknown frontend action");
|
||||
const request = (value: unknown) => coordinator.request({
|
||||
sessionId: "s",
|
||||
toolCallId: "call-validation",
|
||||
name: "render_scada_analysis",
|
||||
params: value,
|
||||
});
|
||||
expect(() => request({ items: [] })).toThrow(FrontendActionError);
|
||||
expect(() => request({ items: Array.from({ length: 101 }, (_, index) => ({ sensor_id: `MP${index}`, level: "low" })) })).toThrow("invalid frontend action params");
|
||||
expect(() => request({ items: [{ sensor_id: "MP01", level: "high" }, { sensor_id: "MP01", level: "low" }] })).toThrow("invalid frontend action params");
|
||||
expect(() => request({ items: [{ sensor_id: "x".repeat(129), level: "low" }] })).toThrow("invalid frontend action params");
|
||||
expect(() => request({ items: [{ sensor_id: "MP01", level: "critical" }] })).toThrow("invalid frontend action params");
|
||||
});
|
||||
|
||||
it("keeps terminal results idempotent and rejects conflicts", async () => {
|
||||
const coordinator = new FrontendActionCoordinator();
|
||||
const { emitted, pending } = start(coordinator, "clear_scada_analysis", {});
|
||||
const result = {
|
||||
version: "frontend-action-result@1" as const,
|
||||
actionId: emitted.actionId,
|
||||
status: "succeeded" as const,
|
||||
output: { cleared: true as const },
|
||||
completedAt: Date.now(),
|
||||
};
|
||||
expect(() => coordinator.submitResult("other", result)).toThrow("session mismatch");
|
||||
coordinator.submitResult("session-1", result);
|
||||
await pending;
|
||||
expect(() => coordinator.submitResult("session-1", { ...result, completedAt: result.completedAt + 1 })).toThrow("conflicts");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
|
||||
import { unwrapFrontendActionResult } from "../../.opencode/tools/frontend_action.js";
|
||||
|
||||
describe("frontend action tool bridge", () => {
|
||||
it("returns only validated browser output for successful actions", () => {
|
||||
expect(unwrapFrontendActionResult(JSON.stringify({
|
||||
version: "frontend-action-result@1",
|
||||
actionId: "action-1",
|
||||
status: "succeeded",
|
||||
output: { cleared: true },
|
||||
completedAt: Date.now(),
|
||||
}))).toBe('{"cleared":true}');
|
||||
});
|
||||
|
||||
it("throws the browser error for failed and expired actions", () => {
|
||||
expect(() => unwrapFrontendActionResult(JSON.stringify({
|
||||
version: "frontend-action-result@1",
|
||||
actionId: "action-1",
|
||||
status: "failed",
|
||||
error: { code: "WFS_UNAVAILABLE", message: "SCADA geometry query failed" },
|
||||
completedAt: Date.now(),
|
||||
}))).toThrow("WFS_UNAVAILABLE: SCADA geometry query failed");
|
||||
expect(() => unwrapFrontendActionResult(JSON.stringify({
|
||||
version: "frontend-action-result@1",
|
||||
actionId: "action-2",
|
||||
status: "expired",
|
||||
error: { code: "ACTION_TIMEOUT", message: "browser action timed out" },
|
||||
completedAt: Date.now(),
|
||||
}))).toThrow("ACTION_TIMEOUT: browser action timed out");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user