test(FN-2712): add runtime e2e and integration coverage
- Add OpenClaw runtime end-to-end tests covering execution flow and runtime contract behavior - Add OpenClaw integration tests to validate plugin/runtime wiring in engine scenarios - Add Paperclip runtime end-to-end and integration suites for equivalent cross-runtime coverage - Update Hermes, OpenClaw, and Paperclip manifest descriptions for consistent runtime metadata
This commit is contained in:
200
packages/engine/src/__tests__/openclaw-runtime-e2e.test.ts
Normal file
200
packages/engine/src/__tests__/openclaw-runtime-e2e.test.ts
Normal file
@@ -0,0 +1,200 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { mkdtempSync } from "node:fs";
|
||||
import { rm } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { PluginLoader, PluginStore, type TaskStore } from "@fusion/core";
|
||||
import { PluginRunner } from "../plugin-runner.js";
|
||||
import { resolveRuntime } from "../runtime-resolution.js";
|
||||
import { createResolvedAgentSession } from "../agent-session-helpers.js";
|
||||
|
||||
const { mockCreateFnAgent, mockPromptWithFallback, mockDescribeModel } = vi.hoisted(() => ({
|
||||
mockCreateFnAgent: vi.fn(),
|
||||
mockPromptWithFallback: vi.fn(),
|
||||
mockDescribeModel: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../logger.js", () => ({
|
||||
createLogger: vi.fn(() => ({
|
||||
log: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
})),
|
||||
executorLog: {
|
||||
log: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("../pi.js", () => ({
|
||||
createFnAgent: mockCreateFnAgent,
|
||||
promptWithFallback: mockPromptWithFallback,
|
||||
describeModel: mockDescribeModel,
|
||||
}));
|
||||
|
||||
function createTaskStoreMock(rootDir: string): TaskStore {
|
||||
return {
|
||||
getRootDir: () => rootDir,
|
||||
on: vi.fn(),
|
||||
off: vi.fn(),
|
||||
} as unknown as TaskStore;
|
||||
}
|
||||
|
||||
function openClawPluginModulePath(): string {
|
||||
return fileURLToPath(
|
||||
new URL("../../../../plugins/fusion-plugin-openclaw-runtime/src/index.ts", import.meta.url),
|
||||
);
|
||||
}
|
||||
|
||||
async function preloadOpenClawPluginModule(): Promise<void> {
|
||||
await import(pathToFileURL(openClawPluginModulePath()).href);
|
||||
}
|
||||
|
||||
describe("OpenClaw runtime E2E pipeline", () => {
|
||||
const originalEnv = { ...process.env };
|
||||
let testRoot: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
testRoot = mkdtempSync(join(tmpdir(), "fn-openclaw-e2e-"));
|
||||
vi.clearAllMocks();
|
||||
|
||||
process.env = {
|
||||
...originalEnv,
|
||||
OPENCLAW_GATEWAY_URL: "http://127.0.0.1:18789",
|
||||
OPENCLAW_AGENT_ID: "openclaw-agent",
|
||||
};
|
||||
|
||||
mockCreateFnAgent.mockResolvedValue({
|
||||
session: { id: "fallback-session", dispose: vi.fn() },
|
||||
sessionFile: "/tmp/fallback.session.json",
|
||||
});
|
||||
mockPromptWithFallback.mockResolvedValue(undefined);
|
||||
mockDescribeModel.mockReturnValue("pi/default");
|
||||
|
||||
const fetchMock = vi.fn().mockImplementation(async (input: string | URL, init?: RequestInit) => {
|
||||
const url = typeof input === "string" ? input : input.toString();
|
||||
const method = (init?.method ?? "GET").toUpperCase();
|
||||
|
||||
if (method === "HEAD" && url === "http://127.0.0.1:18789") {
|
||||
return new Response(null, { status: 200 });
|
||||
}
|
||||
|
||||
if (method === "POST" && url === "http://127.0.0.1:18789/v1/chat/completions") {
|
||||
const ssePayload =
|
||||
'data: {"choices":[{"delta":{"content":"OpenClaw response"}}]}\n\n' +
|
||||
"data: [DONE]\\n\\n";
|
||||
return new Response(ssePayload, {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "text/event-stream" },
|
||||
});
|
||||
}
|
||||
|
||||
return new Response(`Unexpected request: ${method} ${url}`, { status: 500 });
|
||||
});
|
||||
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
await preloadOpenClawPluginModule();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
process.env = { ...originalEnv };
|
||||
vi.unstubAllGlobals();
|
||||
await rm(testRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("loads OpenClaw plugin and executes through OpenClaw runtime", async () => {
|
||||
const pluginStore = new PluginStore(testRoot, { inMemoryDb: true });
|
||||
await pluginStore.init();
|
||||
|
||||
await pluginStore.registerPlugin({
|
||||
manifest: {
|
||||
id: "fusion-plugin-openclaw-runtime",
|
||||
name: "OpenClaw Runtime Plugin",
|
||||
version: "0.1.0",
|
||||
description: "Provides OpenClaw runtime for Fusion AI agents",
|
||||
runtime: {
|
||||
runtimeId: "openclaw",
|
||||
name: "OpenClaw Runtime",
|
||||
description: "OpenClaw-backed AI session using the local OpenClaw gateway",
|
||||
version: "0.1.0",
|
||||
},
|
||||
},
|
||||
path: openClawPluginModulePath(),
|
||||
});
|
||||
|
||||
const taskStore = createTaskStoreMock(testRoot);
|
||||
const pluginLoader = new PluginLoader({ pluginStore, taskStore });
|
||||
const loadResult = await pluginLoader.loadAllPlugins();
|
||||
expect(loadResult).toEqual({ loaded: 1, errors: 0 });
|
||||
|
||||
const pluginRunner = new PluginRunner({
|
||||
pluginLoader,
|
||||
pluginStore,
|
||||
taskStore,
|
||||
rootDir: testRoot,
|
||||
});
|
||||
|
||||
const resolved = await resolveRuntime({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "openclaw",
|
||||
pluginRunner,
|
||||
});
|
||||
|
||||
expect(resolved.runtimeId).toBe("openclaw");
|
||||
expect(resolved.wasConfigured).toBe(true);
|
||||
expect(resolved.runtime.id).toBe("openclaw");
|
||||
expect(resolved.runtime.name).toBe("OpenClaw Runtime");
|
||||
|
||||
const created = await createResolvedAgentSession({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "openclaw",
|
||||
pluginRunner,
|
||||
cwd: testRoot,
|
||||
systemPrompt: "You are helpful",
|
||||
tools: "coding",
|
||||
skills: ["bash"],
|
||||
});
|
||||
|
||||
expect(created.runtimeId).toBe("openclaw");
|
||||
expect(created.wasConfigured).toBe(true);
|
||||
expect(created.session).toBeTruthy();
|
||||
|
||||
await expect(resolved.runtime.promptWithFallback(created.session, "Hello from e2e")).resolves.toBeUndefined();
|
||||
expect(resolved.runtime.describeModel(created.session)).toBe("openclaw/openclaw-agent");
|
||||
expect(mockCreateFnAgent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("falls back to default pi runtime when OpenClaw plugin is not installed", async () => {
|
||||
const pluginStore = new PluginStore(testRoot, { inMemoryDb: true });
|
||||
await pluginStore.init();
|
||||
|
||||
const taskStore = createTaskStoreMock(testRoot);
|
||||
const pluginLoader = new PluginLoader({ pluginStore, taskStore });
|
||||
await pluginLoader.loadAllPlugins();
|
||||
|
||||
const pluginRunner = new PluginRunner({
|
||||
pluginLoader,
|
||||
pluginStore,
|
||||
taskStore,
|
||||
rootDir: testRoot,
|
||||
});
|
||||
|
||||
const result = await createResolvedAgentSession({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "openclaw",
|
||||
pluginRunner,
|
||||
cwd: testRoot,
|
||||
systemPrompt: "fallback",
|
||||
});
|
||||
|
||||
expect(result.runtimeId).toBe("pi");
|
||||
expect(result.wasConfigured).toBe(false);
|
||||
expect(mockCreateFnAgent).toHaveBeenCalledWith({
|
||||
cwd: testRoot,
|
||||
systemPrompt: "fallback",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,192 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import type { AgentRuntime } from "../agent-runtime.js";
|
||||
import { resolveRuntime } from "../runtime-resolution.js";
|
||||
import { createResolvedAgentSession } from "../agent-session-helpers.js";
|
||||
import type { PluginRunner } from "../plugin-runner.js";
|
||||
import type { PluginRuntimeRegistration } from "@fusion/core";
|
||||
|
||||
const mockCreateFnAgent = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock("../logger.js", () => ({
|
||||
createLogger: vi.fn(() => ({
|
||||
log: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
vi.mock("../pi.js", () => ({
|
||||
createFnAgent: mockCreateFnAgent,
|
||||
promptWithFallback: vi.fn().mockResolvedValue(undefined),
|
||||
describeModel: vi.fn().mockReturnValue("pi/default"),
|
||||
}));
|
||||
|
||||
function isAgentRuntime(value: unknown): value is AgentRuntime {
|
||||
return (
|
||||
typeof value === "object" &&
|
||||
value !== null &&
|
||||
"id" in value &&
|
||||
"name" in value &&
|
||||
typeof (value as AgentRuntime).createSession === "function" &&
|
||||
typeof (value as AgentRuntime).promptWithFallback === "function" &&
|
||||
typeof (value as AgentRuntime).describeModel === "function"
|
||||
);
|
||||
}
|
||||
|
||||
function createMockPluginRunner(overrides: Partial<PluginRunner> = {}): PluginRunner {
|
||||
return {
|
||||
getPluginRuntimes: vi.fn().mockReturnValue([]),
|
||||
getRuntimeById: vi.fn().mockReturnValue(undefined),
|
||||
createRuntimeContext: vi.fn().mockResolvedValue({
|
||||
pluginId: "fusion-plugin-openclaw-runtime",
|
||||
taskStore: {},
|
||||
settings: {},
|
||||
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
|
||||
emitEvent: vi.fn(),
|
||||
}),
|
||||
...overrides,
|
||||
} as unknown as PluginRunner;
|
||||
}
|
||||
|
||||
function createOpenClawRegistration(factoryImpl?: () => unknown): {
|
||||
pluginId: string;
|
||||
runtime: PluginRuntimeRegistration;
|
||||
} {
|
||||
return {
|
||||
pluginId: "fusion-plugin-openclaw-runtime",
|
||||
runtime: {
|
||||
metadata: {
|
||||
runtimeId: "openclaw",
|
||||
name: "OpenClaw Runtime",
|
||||
description: "OpenClaw-backed AI session using the local OpenClaw gateway",
|
||||
version: "0.1.0",
|
||||
},
|
||||
factory: vi.fn().mockImplementation(async () =>
|
||||
factoryImpl
|
||||
? factoryImpl()
|
||||
: {
|
||||
id: "openclaw",
|
||||
name: "OpenClaw Runtime",
|
||||
createSession: vi.fn().mockResolvedValue({
|
||||
session: { runtime: "openclaw", prompt: vi.fn() },
|
||||
sessionFile: "/tmp/openclaw.session.json",
|
||||
}),
|
||||
promptWithFallback: vi.fn().mockResolvedValue(undefined),
|
||||
describeModel: vi.fn().mockReturnValue("openclaw/main"),
|
||||
},
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("OpenClaw runtime integration via engine resolution pipeline", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockCreateFnAgent.mockResolvedValue({
|
||||
session: { runtime: "pi", prompt: vi.fn() },
|
||||
sessionFile: "/tmp/pi.session.json",
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("resolves OpenClaw runtime through PluginRunner lookup when runtimeHint is openclaw", async () => {
|
||||
const registration = createOpenClawRegistration();
|
||||
const pluginRunner = createMockPluginRunner({
|
||||
getRuntimeById: vi.fn().mockReturnValue(registration),
|
||||
});
|
||||
|
||||
const resolved = await resolveRuntime({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "openclaw",
|
||||
pluginRunner,
|
||||
});
|
||||
|
||||
expect(resolved.runtimeId).toBe("openclaw");
|
||||
expect(resolved.wasConfigured).toBe(true);
|
||||
expect(resolved.runtime.id).toBe("openclaw");
|
||||
expect(resolved.runtime.name).toBe("OpenClaw Runtime");
|
||||
expect(pluginRunner.getRuntimeById).toHaveBeenCalledWith("openclaw");
|
||||
expect(pluginRunner.createRuntimeContext).toHaveBeenCalledWith("fusion-plugin-openclaw-runtime");
|
||||
});
|
||||
|
||||
it("returns a runtime object that conforms to AgentRuntime", async () => {
|
||||
const registration = createOpenClawRegistration();
|
||||
const pluginRunner = createMockPluginRunner({
|
||||
getRuntimeById: vi.fn().mockReturnValue(registration),
|
||||
});
|
||||
|
||||
const resolved = await resolveRuntime({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "openclaw",
|
||||
pluginRunner,
|
||||
});
|
||||
|
||||
expect(isAgentRuntime(resolved.runtime)).toBe(true);
|
||||
});
|
||||
|
||||
it("createResolvedAgentSession uses OpenClaw runtime and reports configured runtime metadata", async () => {
|
||||
const runtimeSession = { runtime: "openclaw", prompt: vi.fn() };
|
||||
const createSession = vi.fn().mockResolvedValue({
|
||||
session: runtimeSession,
|
||||
sessionFile: "/tmp/openclaw.session.json",
|
||||
});
|
||||
const registration = createOpenClawRegistration(() => ({
|
||||
id: "openclaw",
|
||||
name: "OpenClaw Runtime",
|
||||
createSession,
|
||||
promptWithFallback: vi.fn().mockResolvedValue(undefined),
|
||||
describeModel: vi.fn().mockReturnValue("openclaw/main"),
|
||||
}));
|
||||
|
||||
const pluginRunner = createMockPluginRunner({
|
||||
getRuntimeById: vi.fn().mockReturnValue(registration),
|
||||
});
|
||||
|
||||
const result = await createResolvedAgentSession({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "openclaw",
|
||||
pluginRunner,
|
||||
cwd: "/tmp/project",
|
||||
systemPrompt: "You are helpful",
|
||||
tools: "coding",
|
||||
});
|
||||
|
||||
expect(result.runtimeId).toBe("openclaw");
|
||||
expect(result.wasConfigured).toBe(true);
|
||||
expect(result.session).toBe(runtimeSession);
|
||||
expect(result.sessionFile).toBe("/tmp/openclaw.session.json");
|
||||
expect(createSession).toHaveBeenCalledWith({
|
||||
cwd: "/tmp/project",
|
||||
systemPrompt: "You are helpful",
|
||||
tools: "coding",
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back to default pi runtime when OpenClaw factory throws", async () => {
|
||||
const registration = createOpenClawRegistration(() => {
|
||||
throw new Error("factory exploded");
|
||||
});
|
||||
|
||||
const pluginRunner = createMockPluginRunner({
|
||||
getRuntimeById: vi.fn().mockReturnValue(registration),
|
||||
});
|
||||
|
||||
const result = await createResolvedAgentSession({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "openclaw",
|
||||
pluginRunner,
|
||||
cwd: "/tmp/project",
|
||||
systemPrompt: "Use fallback",
|
||||
});
|
||||
|
||||
expect(result.runtimeId).toBe("pi");
|
||||
expect(result.wasConfigured).toBe(false);
|
||||
expect(mockCreateFnAgent).toHaveBeenCalledWith({
|
||||
cwd: "/tmp/project",
|
||||
systemPrompt: "Use fallback",
|
||||
});
|
||||
});
|
||||
});
|
||||
219
packages/engine/src/__tests__/paperclip-runtime-e2e.test.ts
Normal file
219
packages/engine/src/__tests__/paperclip-runtime-e2e.test.ts
Normal file
@@ -0,0 +1,219 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { mkdtempSync } from "node:fs";
|
||||
import { rm } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { PluginLoader, PluginStore, type TaskStore } from "@fusion/core";
|
||||
import { PluginRunner } from "../plugin-runner.js";
|
||||
import { resolveRuntime } from "../runtime-resolution.js";
|
||||
import { createResolvedAgentSession } from "../agent-session-helpers.js";
|
||||
|
||||
const { mockCreateFnAgent, mockPromptWithFallback, mockDescribeModel } = vi.hoisted(() => ({
|
||||
mockCreateFnAgent: vi.fn(),
|
||||
mockPromptWithFallback: vi.fn(),
|
||||
mockDescribeModel: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../logger.js", () => ({
|
||||
createLogger: vi.fn(() => ({
|
||||
log: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
})),
|
||||
executorLog: {
|
||||
log: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("../pi.js", () => ({
|
||||
createFnAgent: mockCreateFnAgent,
|
||||
promptWithFallback: mockPromptWithFallback,
|
||||
describeModel: mockDescribeModel,
|
||||
}));
|
||||
|
||||
function createTaskStoreMock(rootDir: string): TaskStore {
|
||||
return {
|
||||
getRootDir: () => rootDir,
|
||||
on: vi.fn(),
|
||||
off: vi.fn(),
|
||||
} as unknown as TaskStore;
|
||||
}
|
||||
|
||||
function paperclipPluginModulePath(): string {
|
||||
return fileURLToPath(
|
||||
new URL("../../../../plugins/fusion-plugin-paperclip-runtime/src/index.ts", import.meta.url),
|
||||
);
|
||||
}
|
||||
|
||||
async function preloadPaperclipPluginModule(): Promise<void> {
|
||||
await import(pathToFileURL(paperclipPluginModulePath()).href);
|
||||
}
|
||||
|
||||
function jsonResponse(body: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
describe("Paperclip runtime E2E pipeline", () => {
|
||||
const originalEnv = { ...process.env };
|
||||
let testRoot: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
testRoot = mkdtempSync(join(tmpdir(), "fn-paperclip-e2e-"));
|
||||
vi.clearAllMocks();
|
||||
|
||||
process.env = {
|
||||
...originalEnv,
|
||||
PAPERCLIP_API_URL: "http://localhost:3100",
|
||||
PAPERCLIP_API_KEY: "test-key",
|
||||
PAPERCLIP_AGENT_ID: "paperclip-agent",
|
||||
PAPERCLIP_COMPANY_ID: "COMP-1",
|
||||
};
|
||||
|
||||
mockCreateFnAgent.mockResolvedValue({
|
||||
session: { id: "fallback-session", dispose: vi.fn() },
|
||||
sessionFile: "/tmp/fallback.session.json",
|
||||
});
|
||||
mockPromptWithFallback.mockResolvedValue(undefined);
|
||||
mockDescribeModel.mockReturnValue("pi/default");
|
||||
|
||||
const fetchMock = vi.fn().mockImplementation(async (input: string | URL, init?: RequestInit) => {
|
||||
const url = typeof input === "string" ? input : input.toString();
|
||||
const method = (init?.method ?? "GET").toUpperCase();
|
||||
|
||||
if (method === "GET" && url === "http://localhost:3100/api/health") {
|
||||
return jsonResponse({ status: "ok", deploymentMode: "local_trusted" });
|
||||
}
|
||||
|
||||
if (method === "POST" && url === "http://localhost:3100/api/companies/COMP-1/issues") {
|
||||
return jsonResponse({ id: "ISS-1", status: "backlog" });
|
||||
}
|
||||
|
||||
if (method === "POST" && url === "http://localhost:3100/api/issues/ISS-1/checkout") {
|
||||
return jsonResponse({ id: "ISS-1", status: "in_progress" });
|
||||
}
|
||||
|
||||
if (method === "POST" && url === "http://localhost:3100/api/agents/paperclip-agent/heartbeat/invoke") {
|
||||
return jsonResponse({ id: "RUN-1", status: "queued" });
|
||||
}
|
||||
|
||||
if (method === "GET" && url === "http://localhost:3100/api/issues/ISS-1") {
|
||||
return jsonResponse({ id: "ISS-1", status: "done" });
|
||||
}
|
||||
|
||||
if (method === "GET" && url === "http://localhost:3100/api/issues/ISS-1/comments") {
|
||||
return jsonResponse([{ id: "C1", body: "Paperclip result" }]);
|
||||
}
|
||||
|
||||
return jsonResponse({ error: `Unexpected request: ${method} ${url}` }, 500);
|
||||
});
|
||||
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
await preloadPaperclipPluginModule();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
process.env = { ...originalEnv };
|
||||
vi.unstubAllGlobals();
|
||||
await rm(testRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("loads Paperclip plugin and executes through Paperclip runtime", async () => {
|
||||
const pluginStore = new PluginStore(testRoot, { inMemoryDb: true });
|
||||
await pluginStore.init();
|
||||
|
||||
await pluginStore.registerPlugin({
|
||||
manifest: {
|
||||
id: "fusion-plugin-paperclip-runtime",
|
||||
name: "Paperclip Runtime Plugin",
|
||||
version: "1.0.0",
|
||||
description: "Provides Paperclip runtime for Fusion AI agents",
|
||||
runtime: {
|
||||
runtimeId: "paperclip",
|
||||
name: "Paperclip Runtime",
|
||||
description: "Paperclip-backed AI session via Paperclip REST API",
|
||||
version: "1.0.0",
|
||||
},
|
||||
},
|
||||
path: paperclipPluginModulePath(),
|
||||
});
|
||||
|
||||
const taskStore = createTaskStoreMock(testRoot);
|
||||
const pluginLoader = new PluginLoader({ pluginStore, taskStore });
|
||||
const loadResult = await pluginLoader.loadAllPlugins();
|
||||
expect(loadResult).toEqual({ loaded: 1, errors: 0 });
|
||||
|
||||
const pluginRunner = new PluginRunner({
|
||||
pluginLoader,
|
||||
pluginStore,
|
||||
taskStore,
|
||||
rootDir: testRoot,
|
||||
});
|
||||
|
||||
const resolved = await resolveRuntime({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "paperclip",
|
||||
pluginRunner,
|
||||
});
|
||||
|
||||
expect(resolved.runtimeId).toBe("paperclip");
|
||||
expect(resolved.wasConfigured).toBe(true);
|
||||
expect(resolved.runtime.id).toBe("paperclip");
|
||||
expect(resolved.runtime.name).toBe("Paperclip Runtime");
|
||||
|
||||
const created = await createResolvedAgentSession({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "paperclip",
|
||||
pluginRunner,
|
||||
cwd: testRoot,
|
||||
systemPrompt: "You are helpful",
|
||||
tools: "coding",
|
||||
skills: ["bash"],
|
||||
});
|
||||
|
||||
expect(created.runtimeId).toBe("paperclip");
|
||||
expect(created.wasConfigured).toBe(true);
|
||||
expect(created.session).toBeTruthy();
|
||||
|
||||
await expect(resolved.runtime.promptWithFallback(created.session, "Hello from e2e")).resolves.toBeUndefined();
|
||||
expect(resolved.runtime.describeModel(created.session)).toBe("paperclip/paperclip-agent");
|
||||
expect(mockCreateFnAgent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("falls back to default pi runtime when Paperclip plugin is not installed", async () => {
|
||||
const pluginStore = new PluginStore(testRoot, { inMemoryDb: true });
|
||||
await pluginStore.init();
|
||||
|
||||
const taskStore = createTaskStoreMock(testRoot);
|
||||
const pluginLoader = new PluginLoader({ pluginStore, taskStore });
|
||||
await pluginLoader.loadAllPlugins();
|
||||
|
||||
const pluginRunner = new PluginRunner({
|
||||
pluginLoader,
|
||||
pluginStore,
|
||||
taskStore,
|
||||
rootDir: testRoot,
|
||||
});
|
||||
|
||||
const result = await createResolvedAgentSession({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "paperclip",
|
||||
pluginRunner,
|
||||
cwd: testRoot,
|
||||
systemPrompt: "fallback",
|
||||
});
|
||||
|
||||
expect(result.runtimeId).toBe("pi");
|
||||
expect(result.wasConfigured).toBe(false);
|
||||
expect(mockCreateFnAgent).toHaveBeenCalledWith({
|
||||
cwd: testRoot,
|
||||
systemPrompt: "fallback",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,192 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import type { AgentRuntime } from "../agent-runtime.js";
|
||||
import { resolveRuntime } from "../runtime-resolution.js";
|
||||
import { createResolvedAgentSession } from "../agent-session-helpers.js";
|
||||
import type { PluginRunner } from "../plugin-runner.js";
|
||||
import type { PluginRuntimeRegistration } from "@fusion/core";
|
||||
|
||||
const mockCreateFnAgent = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock("../logger.js", () => ({
|
||||
createLogger: vi.fn(() => ({
|
||||
log: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
vi.mock("../pi.js", () => ({
|
||||
createFnAgent: mockCreateFnAgent,
|
||||
promptWithFallback: vi.fn().mockResolvedValue(undefined),
|
||||
describeModel: vi.fn().mockReturnValue("pi/default"),
|
||||
}));
|
||||
|
||||
function isAgentRuntime(value: unknown): value is AgentRuntime {
|
||||
return (
|
||||
typeof value === "object" &&
|
||||
value !== null &&
|
||||
"id" in value &&
|
||||
"name" in value &&
|
||||
typeof (value as AgentRuntime).createSession === "function" &&
|
||||
typeof (value as AgentRuntime).promptWithFallback === "function" &&
|
||||
typeof (value as AgentRuntime).describeModel === "function"
|
||||
);
|
||||
}
|
||||
|
||||
function createMockPluginRunner(overrides: Partial<PluginRunner> = {}): PluginRunner {
|
||||
return {
|
||||
getPluginRuntimes: vi.fn().mockReturnValue([]),
|
||||
getRuntimeById: vi.fn().mockReturnValue(undefined),
|
||||
createRuntimeContext: vi.fn().mockResolvedValue({
|
||||
pluginId: "fusion-plugin-paperclip-runtime",
|
||||
taskStore: {},
|
||||
settings: {},
|
||||
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
|
||||
emitEvent: vi.fn(),
|
||||
}),
|
||||
...overrides,
|
||||
} as unknown as PluginRunner;
|
||||
}
|
||||
|
||||
function createPaperclipRegistration(factoryImpl?: () => unknown): {
|
||||
pluginId: string;
|
||||
runtime: PluginRuntimeRegistration;
|
||||
} {
|
||||
return {
|
||||
pluginId: "fusion-plugin-paperclip-runtime",
|
||||
runtime: {
|
||||
metadata: {
|
||||
runtimeId: "paperclip",
|
||||
name: "Paperclip Runtime",
|
||||
description: "Paperclip-backed AI session via Paperclip REST API",
|
||||
version: "1.0.0",
|
||||
},
|
||||
factory: vi.fn().mockImplementation(async () =>
|
||||
factoryImpl
|
||||
? factoryImpl()
|
||||
: {
|
||||
id: "paperclip",
|
||||
name: "Paperclip Runtime",
|
||||
createSession: vi.fn().mockResolvedValue({
|
||||
session: { runtime: "paperclip", prompt: vi.fn() },
|
||||
sessionFile: "/tmp/paperclip.session.json",
|
||||
}),
|
||||
promptWithFallback: vi.fn().mockResolvedValue(undefined),
|
||||
describeModel: vi.fn().mockReturnValue("paperclip/main"),
|
||||
},
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("Paperclip runtime integration via engine resolution pipeline", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockCreateFnAgent.mockResolvedValue({
|
||||
session: { runtime: "pi", prompt: vi.fn() },
|
||||
sessionFile: "/tmp/pi.session.json",
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("resolves Paperclip runtime through PluginRunner lookup when runtimeHint is paperclip", async () => {
|
||||
const registration = createPaperclipRegistration();
|
||||
const pluginRunner = createMockPluginRunner({
|
||||
getRuntimeById: vi.fn().mockReturnValue(registration),
|
||||
});
|
||||
|
||||
const resolved = await resolveRuntime({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "paperclip",
|
||||
pluginRunner,
|
||||
});
|
||||
|
||||
expect(resolved.runtimeId).toBe("paperclip");
|
||||
expect(resolved.wasConfigured).toBe(true);
|
||||
expect(resolved.runtime.id).toBe("paperclip");
|
||||
expect(resolved.runtime.name).toBe("Paperclip Runtime");
|
||||
expect(pluginRunner.getRuntimeById).toHaveBeenCalledWith("paperclip");
|
||||
expect(pluginRunner.createRuntimeContext).toHaveBeenCalledWith("fusion-plugin-paperclip-runtime");
|
||||
});
|
||||
|
||||
it("returns a runtime object that conforms to AgentRuntime", async () => {
|
||||
const registration = createPaperclipRegistration();
|
||||
const pluginRunner = createMockPluginRunner({
|
||||
getRuntimeById: vi.fn().mockReturnValue(registration),
|
||||
});
|
||||
|
||||
const resolved = await resolveRuntime({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "paperclip",
|
||||
pluginRunner,
|
||||
});
|
||||
|
||||
expect(isAgentRuntime(resolved.runtime)).toBe(true);
|
||||
});
|
||||
|
||||
it("createResolvedAgentSession uses Paperclip runtime and reports configured runtime metadata", async () => {
|
||||
const runtimeSession = { runtime: "paperclip", prompt: vi.fn() };
|
||||
const createSession = vi.fn().mockResolvedValue({
|
||||
session: runtimeSession,
|
||||
sessionFile: "/tmp/paperclip.session.json",
|
||||
});
|
||||
const registration = createPaperclipRegistration(() => ({
|
||||
id: "paperclip",
|
||||
name: "Paperclip Runtime",
|
||||
createSession,
|
||||
promptWithFallback: vi.fn().mockResolvedValue(undefined),
|
||||
describeModel: vi.fn().mockReturnValue("paperclip/main"),
|
||||
}));
|
||||
|
||||
const pluginRunner = createMockPluginRunner({
|
||||
getRuntimeById: vi.fn().mockReturnValue(registration),
|
||||
});
|
||||
|
||||
const result = await createResolvedAgentSession({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "paperclip",
|
||||
pluginRunner,
|
||||
cwd: "/tmp/project",
|
||||
systemPrompt: "You are helpful",
|
||||
tools: "coding",
|
||||
});
|
||||
|
||||
expect(result.runtimeId).toBe("paperclip");
|
||||
expect(result.wasConfigured).toBe(true);
|
||||
expect(result.session).toBe(runtimeSession);
|
||||
expect(result.sessionFile).toBe("/tmp/paperclip.session.json");
|
||||
expect(createSession).toHaveBeenCalledWith({
|
||||
cwd: "/tmp/project",
|
||||
systemPrompt: "You are helpful",
|
||||
tools: "coding",
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back to default pi runtime when Paperclip factory throws", async () => {
|
||||
const registration = createPaperclipRegistration(() => {
|
||||
throw new Error("factory exploded");
|
||||
});
|
||||
|
||||
const pluginRunner = createMockPluginRunner({
|
||||
getRuntimeById: vi.fn().mockReturnValue(registration),
|
||||
});
|
||||
|
||||
const result = await createResolvedAgentSession({
|
||||
sessionPurpose: "executor",
|
||||
runtimeHint: "paperclip",
|
||||
pluginRunner,
|
||||
cwd: "/tmp/project",
|
||||
systemPrompt: "Use fallback",
|
||||
});
|
||||
|
||||
expect(result.runtimeId).toBe("pi");
|
||||
expect(result.wasConfigured).toBe(false);
|
||||
expect(mockCreateFnAgent).toHaveBeenCalledWith({
|
||||
cwd: "/tmp/project",
|
||||
systemPrompt: "Use fallback",
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user