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 type { IncomingMessage } from "node:http";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { createMockServerResponse } from "../../src/test-utils/mock-http-response.js";
|
import { createMockServerResponse } from "../../src/test-utils/mock-http-response.js";
|
||||||
|
import { createTestPluginApi } from "../test-utils/plugin-api.js";
|
||||||
import plugin from "./index.js";
|
import plugin from "./index.js";
|
||||||
|
|
||||||
describe("diffs plugin registration", () => {
|
describe("diffs plugin registration", () => {
|
||||||
|
|
@ -9,33 +10,19 @@ describe("diffs plugin registration", () => {
|
||||||
const registerHttpRoute = vi.fn();
|
const registerHttpRoute = vi.fn();
|
||||||
const on = vi.fn();
|
const on = vi.fn();
|
||||||
|
|
||||||
plugin.register?.({
|
plugin.register?.(
|
||||||
id: "diffs",
|
createTestPluginApi({
|
||||||
name: "Diffs",
|
id: "diffs",
|
||||||
description: "Diffs",
|
name: "Diffs",
|
||||||
source: "test",
|
description: "Diffs",
|
||||||
config: {},
|
source: "test",
|
||||||
runtime: {} as never,
|
config: {},
|
||||||
logger: {
|
runtime: {} as never,
|
||||||
info() {},
|
registerTool,
|
||||||
warn() {},
|
registerHttpRoute,
|
||||||
error() {},
|
on,
|
||||||
},
|
}),
|
||||||
registerTool,
|
);
|
||||||
registerHook() {},
|
|
||||||
registerHttpRoute,
|
|
||||||
registerChannel() {},
|
|
||||||
registerGatewayMethod() {},
|
|
||||||
registerCli() {},
|
|
||||||
registerService() {},
|
|
||||||
registerProvider() {},
|
|
||||||
registerCommand() {},
|
|
||||||
registerContextEngine() {},
|
|
||||||
resolvePath(input: string) {
|
|
||||||
return input;
|
|
||||||
},
|
|
||||||
on,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(registerTool).toHaveBeenCalledTimes(1);
|
expect(registerTool).toHaveBeenCalledTimes(1);
|
||||||
expect(registerHttpRoute).toHaveBeenCalledTimes(1);
|
expect(registerHttpRoute).toHaveBeenCalledTimes(1);
|
||||||
|
|
@ -65,53 +52,38 @@ describe("diffs plugin registration", () => {
|
||||||
) => Promise<boolean>)
|
) => Promise<boolean>)
|
||||||
| undefined;
|
| undefined;
|
||||||
|
|
||||||
plugin.register?.({
|
plugin.register?.(
|
||||||
id: "diffs",
|
createTestPluginApi({
|
||||||
name: "Diffs",
|
id: "diffs",
|
||||||
description: "Diffs",
|
name: "Diffs",
|
||||||
source: "test",
|
description: "Diffs",
|
||||||
config: {
|
source: "test",
|
||||||
gateway: {
|
config: {
|
||||||
port: 18789,
|
gateway: {
|
||||||
bind: "loopback",
|
port: 18789,
|
||||||
|
bind: "loopback",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
pluginConfig: {
|
||||||
pluginConfig: {
|
defaults: {
|
||||||
defaults: {
|
mode: "view",
|
||||||
mode: "view",
|
theme: "light",
|
||||||
theme: "light",
|
background: false,
|
||||||
background: false,
|
layout: "split",
|
||||||
layout: "split",
|
showLineNumbers: false,
|
||||||
showLineNumbers: false,
|
diffIndicators: "classic",
|
||||||
diffIndicators: "classic",
|
lineSpacing: 2,
|
||||||
lineSpacing: 2,
|
},
|
||||||
},
|
},
|
||||||
},
|
runtime: {} as never,
|
||||||
runtime: {} as never,
|
registerTool(tool) {
|
||||||
logger: {
|
registeredTool = typeof tool === "function" ? undefined : tool;
|
||||||
info() {},
|
},
|
||||||
warn() {},
|
registerHttpRoute(params) {
|
||||||
error() {},
|
registeredHttpRouteHandler = params.handler as typeof registeredHttpRouteHandler;
|
||||||
},
|
},
|
||||||
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", {
|
const result = await registeredTool?.execute?.("tool-1", {
|
||||||
before: "one\n",
|
before: "one\n",
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import fs from "node:fs/promises";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/diffs";
|
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/diffs";
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { createTestPluginApi } from "../../test-utils/plugin-api.js";
|
||||||
import type { DiffScreenshotter } from "./browser.js";
|
import type { DiffScreenshotter } from "./browser.js";
|
||||||
import { DEFAULT_DIFFS_TOOL_DEFAULTS } from "./config.js";
|
import { DEFAULT_DIFFS_TOOL_DEFAULTS } from "./config.js";
|
||||||
import { DiffArtifactStore } from "./store.js";
|
import { DiffArtifactStore } from "./store.js";
|
||||||
|
|
@ -383,7 +384,7 @@ describe("diffs tool", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
function createApi(): OpenClawPluginApi {
|
function createApi(): OpenClawPluginApi {
|
||||||
return {
|
return createTestPluginApi({
|
||||||
id: "diffs",
|
id: "diffs",
|
||||||
name: "Diffs",
|
name: "Diffs",
|
||||||
description: "Diffs",
|
description: "Diffs",
|
||||||
|
|
@ -395,26 +396,7 @@ function createApi(): OpenClawPluginApi {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
runtime: {} as OpenClawPluginApi["runtime"],
|
runtime: {} as OpenClawPluginApi["runtime"],
|
||||||
logger: {
|
}) as OpenClawPluginApi;
|
||||||
info() {},
|
|
||||||
warn() {},
|
|
||||||
error() {},
|
|
||||||
},
|
|
||||||
registerTool() {},
|
|
||||||
registerHook() {},
|
|
||||||
registerHttpRoute() {},
|
|
||||||
registerChannel() {},
|
|
||||||
registerGatewayMethod() {},
|
|
||||||
registerCli() {},
|
|
||||||
registerService() {},
|
|
||||||
registerProvider() {},
|
|
||||||
registerCommand() {},
|
|
||||||
registerContextEngine() {},
|
|
||||||
resolvePath(input: string) {
|
|
||||||
return input;
|
|
||||||
},
|
|
||||||
on() {},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createToolWithScreenshotter(
|
function createToolWithScreenshotter(
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import type {
|
||||||
PluginCommandContext,
|
PluginCommandContext,
|
||||||
} from "openclaw/plugin-sdk/phone-control";
|
} from "openclaw/plugin-sdk/phone-control";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import { createTestPluginApi } from "../test-utils/plugin-api.js";
|
||||||
import registerPhoneControl from "./index.js";
|
import registerPhoneControl from "./index.js";
|
||||||
|
|
||||||
function createApi(params: {
|
function createApi(params: {
|
||||||
|
|
@ -15,7 +16,7 @@ function createApi(params: {
|
||||||
writeConfig: (next: Record<string, unknown>) => Promise<void>;
|
writeConfig: (next: Record<string, unknown>) => Promise<void>;
|
||||||
registerCommand: (command: OpenClawPluginCommandDefinition) => void;
|
registerCommand: (command: OpenClawPluginCommandDefinition) => void;
|
||||||
}): OpenClawPluginApi {
|
}): OpenClawPluginApi {
|
||||||
return {
|
return createTestPluginApi({
|
||||||
id: "phone-control",
|
id: "phone-control",
|
||||||
name: "phone-control",
|
name: "phone-control",
|
||||||
source: "test",
|
source: "test",
|
||||||
|
|
@ -30,22 +31,8 @@ function createApi(params: {
|
||||||
writeConfigFile: (next: Record<string, unknown>) => params.writeConfig(next),
|
writeConfigFile: (next: Record<string, unknown>) => params.writeConfig(next),
|
||||||
},
|
},
|
||||||
} as OpenClawPluginApi["runtime"],
|
} as OpenClawPluginApi["runtime"],
|
||||||
logger: { info() {}, warn() {}, error() {} },
|
|
||||||
registerTool() {},
|
|
||||||
registerHook() {},
|
|
||||||
registerHttpRoute() {},
|
|
||||||
registerChannel() {},
|
|
||||||
registerGatewayMethod() {},
|
|
||||||
registerCli() {},
|
|
||||||
registerService() {},
|
|
||||||
registerProvider() {},
|
|
||||||
registerContextEngine() {},
|
|
||||||
registerCommand: params.registerCommand,
|
registerCommand: params.registerCommand,
|
||||||
resolvePath(input: string) {
|
}) as OpenClawPluginApi;
|
||||||
return input;
|
|
||||||
},
|
|
||||||
on() {},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createCommandContext(args: string): PluginCommandContext {
|
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