2026-02-19 11:30:36 +00:00
|
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
2026-03-16 15:51:08 -05:00
|
|
|
import { DEFAULT_MODEL, DEFAULT_PROVIDER } from "../../agents/defaults.js";
|
2026-03-03 01:37:37 +00:00
|
|
|
import { onAgentEvent } from "../../infra/agent-events.js";
|
2026-03-03 01:40:11 +00:00
|
|
|
import { requestHeartbeatNow } from "../../infra/heartbeat-wake.js";
|
2026-03-03 01:37:37 +00:00
|
|
|
import { onSessionTranscriptUpdate } from "../../sessions/transcript-events.js";
|
2026-02-19 11:30:36 +00:00
|
|
|
|
|
|
|
|
const runCommandWithTimeoutMock = vi.hoisted(() => vi.fn());
|
|
|
|
|
|
|
|
|
|
vi.mock("../../process/exec.js", () => ({
|
|
|
|
|
runCommandWithTimeout: (...args: unknown[]) => runCommandWithTimeoutMock(...args),
|
|
|
|
|
}));
|
|
|
|
|
|
2026-03-16 14:27:54 -07:00
|
|
|
import {
|
|
|
|
|
clearGatewaySubagentRuntime,
|
|
|
|
|
createPluginRuntime,
|
|
|
|
|
setGatewaySubagentRuntime,
|
|
|
|
|
} from "./index.js";
|
2026-02-19 10:17:29 +00:00
|
|
|
|
2026-02-19 16:01:22 +01:00
|
|
|
describe("plugin runtime command execution", () => {
|
2026-02-19 11:30:36 +00:00
|
|
|
beforeEach(() => {
|
2026-02-22 08:09:14 +00:00
|
|
|
runCommandWithTimeoutMock.mockClear();
|
2026-03-16 14:27:54 -07:00
|
|
|
clearGatewaySubagentRuntime();
|
2026-02-19 11:30:36 +00:00
|
|
|
});
|
|
|
|
|
|
2026-02-19 16:01:22 +01:00
|
|
|
it("exposes runtime.system.runCommandWithTimeout by default", async () => {
|
2026-02-19 11:30:36 +00:00
|
|
|
const commandResult = {
|
|
|
|
|
stdout: "hello\n",
|
|
|
|
|
stderr: "",
|
|
|
|
|
code: 0,
|
|
|
|
|
signal: null,
|
|
|
|
|
killed: false,
|
|
|
|
|
termination: "exit" as const,
|
|
|
|
|
};
|
|
|
|
|
runCommandWithTimeoutMock.mockResolvedValue(commandResult);
|
|
|
|
|
|
|
|
|
|
const runtime = createPluginRuntime();
|
|
|
|
|
await expect(
|
|
|
|
|
runtime.system.runCommandWithTimeout(["echo", "hello"], { timeoutMs: 1000 }),
|
|
|
|
|
).resolves.toEqual(commandResult);
|
|
|
|
|
expect(runCommandWithTimeoutMock).toHaveBeenCalledWith(["echo", "hello"], { timeoutMs: 1000 });
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-19 16:01:22 +01:00
|
|
|
it("forwards runtime.system.runCommandWithTimeout errors", async () => {
|
|
|
|
|
runCommandWithTimeoutMock.mockRejectedValue(new Error("boom"));
|
2026-02-19 10:17:29 +00:00
|
|
|
const runtime = createPluginRuntime();
|
|
|
|
|
await expect(
|
|
|
|
|
runtime.system.runCommandWithTimeout(["echo", "hello"], { timeoutMs: 1000 }),
|
2026-02-19 16:01:22 +01:00
|
|
|
).rejects.toThrow("boom");
|
|
|
|
|
expect(runCommandWithTimeoutMock).toHaveBeenCalledWith(["echo", "hello"], { timeoutMs: 1000 });
|
2026-02-19 10:17:29 +00:00
|
|
|
});
|
2026-03-03 01:37:37 +00:00
|
|
|
|
|
|
|
|
it("exposes runtime.events listener registration helpers", () => {
|
|
|
|
|
const runtime = createPluginRuntime();
|
|
|
|
|
expect(runtime.events.onAgentEvent).toBe(onAgentEvent);
|
|
|
|
|
expect(runtime.events.onSessionTranscriptUpdate).toBe(onSessionTranscriptUpdate);
|
|
|
|
|
});
|
2026-03-03 01:40:11 +00:00
|
|
|
|
|
|
|
|
it("exposes runtime.system.requestHeartbeatNow", () => {
|
|
|
|
|
const runtime = createPluginRuntime();
|
|
|
|
|
expect(runtime.system.requestHeartbeatNow).toBe(requestHeartbeatNow);
|
|
|
|
|
});
|
2026-03-10 00:07:26 +01:00
|
|
|
|
2026-03-16 15:51:08 -05:00
|
|
|
it("exposes runtime.agent host helpers", () => {
|
|
|
|
|
const runtime = createPluginRuntime();
|
|
|
|
|
expect(runtime.agent.defaults).toEqual({
|
|
|
|
|
model: DEFAULT_MODEL,
|
|
|
|
|
provider: DEFAULT_PROVIDER,
|
|
|
|
|
});
|
|
|
|
|
expect(typeof runtime.agent.runEmbeddedPiAgent).toBe("function");
|
|
|
|
|
expect(typeof runtime.agent.resolveAgentDir).toBe("function");
|
|
|
|
|
expect(typeof runtime.agent.session.resolveSessionFilePath).toBe("function");
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-10 00:07:26 +01:00
|
|
|
it("exposes runtime.modelAuth with getApiKeyForModel and resolveApiKeyForProvider", () => {
|
|
|
|
|
const runtime = createPluginRuntime();
|
|
|
|
|
expect(runtime.modelAuth).toBeDefined();
|
|
|
|
|
expect(typeof runtime.modelAuth.getApiKeyForModel).toBe("function");
|
|
|
|
|
expect(typeof runtime.modelAuth.resolveApiKeyForProvider).toBe("function");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("modelAuth wrappers strip agentDir and store to prevent credential steering", async () => {
|
|
|
|
|
// The wrappers should not forward agentDir or store from plugin callers.
|
|
|
|
|
// We verify this by checking the wrapper functions exist and are not the
|
|
|
|
|
// raw implementations (they are wrapped, not direct references).
|
|
|
|
|
const { getApiKeyForModel: rawGetApiKey } = await import("../../agents/model-auth.js");
|
|
|
|
|
const runtime = createPluginRuntime();
|
|
|
|
|
// Wrappers should NOT be the same reference as the raw functions
|
|
|
|
|
expect(runtime.modelAuth.getApiKeyForModel).not.toBe(rawGetApiKey);
|
|
|
|
|
});
|
2026-03-16 14:27:54 -07:00
|
|
|
|
|
|
|
|
it("keeps subagent unavailable by default even after gateway initialization", async () => {
|
|
|
|
|
const runtime = createPluginRuntime();
|
|
|
|
|
setGatewaySubagentRuntime({
|
|
|
|
|
run: vi.fn(),
|
|
|
|
|
waitForRun: vi.fn(),
|
|
|
|
|
getSessionMessages: vi.fn(),
|
|
|
|
|
getSession: vi.fn(),
|
|
|
|
|
deleteSession: vi.fn(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(() => runtime.subagent.run({ sessionKey: "s-1", message: "hello" })).toThrow(
|
|
|
|
|
"Plugin runtime subagent methods are only available during a gateway request.",
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("late-binds to the gateway subagent when explicitly enabled", async () => {
|
|
|
|
|
const run = vi.fn().mockResolvedValue({ runId: "run-1" });
|
|
|
|
|
const runtime = createPluginRuntime({ allowGatewaySubagentBinding: true });
|
|
|
|
|
|
|
|
|
|
setGatewaySubagentRuntime({
|
|
|
|
|
run,
|
|
|
|
|
waitForRun: vi.fn(),
|
|
|
|
|
getSessionMessages: vi.fn(),
|
|
|
|
|
getSession: vi.fn(),
|
|
|
|
|
deleteSession: vi.fn(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(runtime.subagent.run({ sessionKey: "s-2", message: "hello" })).resolves.toEqual({
|
|
|
|
|
runId: "run-1",
|
|
|
|
|
});
|
|
|
|
|
expect(run).toHaveBeenCalledWith({ sessionKey: "s-2", message: "hello" });
|
|
|
|
|
});
|
2026-02-19 10:17:29 +00:00
|
|
|
});
|