mirror of https://github.com/openclaw/openclaw.git
test: share plugin api test harness
This commit is contained in:
parent
c3e78908c7
commit
6decaebcf2
|
|
@ -1,6 +1,7 @@
|
|||
import type { IncomingMessage } from "node:http";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createMockServerResponse } from "../../src/test-utils/mock-http-response.js";
|
||||
import { createTestPluginApi } from "../test-utils/plugin-api.js";
|
||||
import plugin from "./index.js";
|
||||
|
||||
describe("diffs plugin registration", () => {
|
||||
|
|
@ -9,33 +10,19 @@ describe("diffs plugin registration", () => {
|
|||
const registerHttpRoute = vi.fn();
|
||||
const on = vi.fn();
|
||||
|
||||
plugin.register?.({
|
||||
plugin.register?.(
|
||||
createTestPluginApi({
|
||||
id: "diffs",
|
||||
name: "Diffs",
|
||||
description: "Diffs",
|
||||
source: "test",
|
||||
config: {},
|
||||
runtime: {} as never,
|
||||
logger: {
|
||||
info() {},
|
||||
warn() {},
|
||||
error() {},
|
||||
},
|
||||
registerTool,
|
||||
registerHook() {},
|
||||
registerHttpRoute,
|
||||
registerChannel() {},
|
||||
registerGatewayMethod() {},
|
||||
registerCli() {},
|
||||
registerService() {},
|
||||
registerProvider() {},
|
||||
registerCommand() {},
|
||||
registerContextEngine() {},
|
||||
resolvePath(input: string) {
|
||||
return input;
|
||||
},
|
||||
on,
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
expect(registerTool).toHaveBeenCalledTimes(1);
|
||||
expect(registerHttpRoute).toHaveBeenCalledTimes(1);
|
||||
|
|
@ -65,7 +52,8 @@ describe("diffs plugin registration", () => {
|
|||
) => Promise<boolean>)
|
||||
| undefined;
|
||||
|
||||
plugin.register?.({
|
||||
plugin.register?.(
|
||||
createTestPluginApi({
|
||||
id: "diffs",
|
||||
name: "Diffs",
|
||||
description: "Diffs",
|
||||
|
|
@ -88,30 +76,14 @@ describe("diffs plugin registration", () => {
|
|||
},
|
||||
},
|
||||
runtime: {} as never,
|
||||
logger: {
|
||||
info() {},
|
||||
warn() {},
|
||||
error() {},
|
||||
},
|
||||
registerTool(tool) {
|
||||
registeredTool = typeof tool === "function" ? undefined : tool;
|
||||
},
|
||||
registerHook() {},
|
||||
registerHttpRoute(params) {
|
||||
registeredHttpRouteHandler = params.handler as typeof registeredHttpRouteHandler;
|
||||
},
|
||||
registerChannel() {},
|
||||
registerGatewayMethod() {},
|
||||
registerCli() {},
|
||||
registerService() {},
|
||||
registerProvider() {},
|
||||
registerCommand() {},
|
||||
registerContextEngine() {},
|
||||
resolvePath(input: string) {
|
||||
return input;
|
||||
},
|
||||
on() {},
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
const result = await registeredTool?.execute?.("tool-1", {
|
||||
before: "one\n",
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import fs from "node:fs/promises";
|
|||
import path from "node:path";
|
||||
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/diffs";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createTestPluginApi } from "../../test-utils/plugin-api.js";
|
||||
import type { DiffScreenshotter } from "./browser.js";
|
||||
import { DEFAULT_DIFFS_TOOL_DEFAULTS } from "./config.js";
|
||||
import { DiffArtifactStore } from "./store.js";
|
||||
|
|
@ -383,7 +384,7 @@ describe("diffs tool", () => {
|
|||
});
|
||||
|
||||
function createApi(): OpenClawPluginApi {
|
||||
return {
|
||||
return createTestPluginApi({
|
||||
id: "diffs",
|
||||
name: "Diffs",
|
||||
description: "Diffs",
|
||||
|
|
@ -395,26 +396,7 @@ function createApi(): OpenClawPluginApi {
|
|||
},
|
||||
},
|
||||
runtime: {} as OpenClawPluginApi["runtime"],
|
||||
logger: {
|
||||
info() {},
|
||||
warn() {},
|
||||
error() {},
|
||||
},
|
||||
registerTool() {},
|
||||
registerHook() {},
|
||||
registerHttpRoute() {},
|
||||
registerChannel() {},
|
||||
registerGatewayMethod() {},
|
||||
registerCli() {},
|
||||
registerService() {},
|
||||
registerProvider() {},
|
||||
registerCommand() {},
|
||||
registerContextEngine() {},
|
||||
resolvePath(input: string) {
|
||||
return input;
|
||||
},
|
||||
on() {},
|
||||
};
|
||||
}) as OpenClawPluginApi;
|
||||
}
|
||||
|
||||
function createToolWithScreenshotter(
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import type {
|
|||
PluginCommandContext,
|
||||
} from "openclaw/plugin-sdk/phone-control";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createTestPluginApi } from "../test-utils/plugin-api.js";
|
||||
import registerPhoneControl from "./index.js";
|
||||
|
||||
function createApi(params: {
|
||||
|
|
@ -15,7 +16,7 @@ function createApi(params: {
|
|||
writeConfig: (next: Record<string, unknown>) => Promise<void>;
|
||||
registerCommand: (command: OpenClawPluginCommandDefinition) => void;
|
||||
}): OpenClawPluginApi {
|
||||
return {
|
||||
return createTestPluginApi({
|
||||
id: "phone-control",
|
||||
name: "phone-control",
|
||||
source: "test",
|
||||
|
|
@ -30,22 +31,8 @@ function createApi(params: {
|
|||
writeConfigFile: (next: Record<string, unknown>) => params.writeConfig(next),
|
||||
},
|
||||
} as OpenClawPluginApi["runtime"],
|
||||
logger: { info() {}, warn() {}, error() {} },
|
||||
registerTool() {},
|
||||
registerHook() {},
|
||||
registerHttpRoute() {},
|
||||
registerChannel() {},
|
||||
registerGatewayMethod() {},
|
||||
registerCli() {},
|
||||
registerService() {},
|
||||
registerProvider() {},
|
||||
registerContextEngine() {},
|
||||
registerCommand: params.registerCommand,
|
||||
resolvePath(input: string) {
|
||||
return input;
|
||||
},
|
||||
on() {},
|
||||
};
|
||||
}) as OpenClawPluginApi;
|
||||
}
|
||||
|
||||
function createCommandContext(args: string): PluginCommandContext {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
type TestLogger = {
|
||||
info: () => void;
|
||||
warn: () => void;
|
||||
error: () => void;
|
||||
debug?: () => void;
|
||||
};
|
||||
|
||||
type TestPluginApiDefaults = {
|
||||
logger: TestLogger;
|
||||
registerTool: () => void;
|
||||
registerHook: () => void;
|
||||
registerHttpRoute: () => void;
|
||||
registerChannel: () => void;
|
||||
registerGatewayMethod: () => void;
|
||||
registerCli: () => void;
|
||||
registerService: () => void;
|
||||
registerProvider: () => void;
|
||||
registerCommand: () => void;
|
||||
registerContextEngine: () => void;
|
||||
resolvePath: (input: string) => string;
|
||||
on: () => void;
|
||||
};
|
||||
|
||||
export function createTestPluginApi<T extends object>(api: T): T & TestPluginApiDefaults {
|
||||
return {
|
||||
logger: { info() {}, warn() {}, error() {} },
|
||||
registerTool() {},
|
||||
registerHook() {},
|
||||
registerHttpRoute() {},
|
||||
registerChannel() {},
|
||||
registerGatewayMethod() {},
|
||||
registerCli() {},
|
||||
registerService() {},
|
||||
registerProvider() {},
|
||||
registerCommand() {},
|
||||
registerContextEngine() {},
|
||||
resolvePath(input: string) {
|
||||
return input;
|
||||
},
|
||||
on() {},
|
||||
...api,
|
||||
};
|
||||
}
|
||||
Loading…
Reference in New Issue