mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-04 04:50:23 +00:00
test(plugin-sdk): extract direct helper seams
This commit is contained in:
@@ -1,7 +1,3 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
// Narrow public testing surface for plugin authors.
|
||||
// Keep this list additive and limited to helpers we are willing to support.
|
||||
|
||||
@@ -55,83 +51,7 @@ export {
|
||||
createWhatsAppPollFixture,
|
||||
expectWhatsAppPollSent,
|
||||
} from "../test-helpers/whatsapp-outbound.js";
|
||||
export { createWindowsCmdShimFixture } from "../test-helpers/windows-cmd-shim.js";
|
||||
export { installCommonResolveTargetErrorCases } from "../test-helpers/resolve-target-error-cases.js";
|
||||
export { sanitizeTerminalText } from "../terminal/safe-text.js";
|
||||
export { withStateDirEnv } from "../test-helpers/state-dir-env.js";
|
||||
|
||||
/** Create a tiny Windows `.cmd` shim fixture for plugin tests that spawn CLIs. */
|
||||
export async function createWindowsCmdShimFixture(params: {
|
||||
shimPath: string;
|
||||
scriptPath: string;
|
||||
shimLine: string;
|
||||
}): Promise<void> {
|
||||
await fs.mkdir(path.dirname(params.scriptPath), { recursive: true });
|
||||
await fs.mkdir(path.dirname(params.shimPath), { recursive: true });
|
||||
await fs.writeFile(params.scriptPath, "module.exports = {};\n", "utf8");
|
||||
await fs.writeFile(params.shimPath, `@echo off\r\n${params.shimLine}\r\n`, "utf8");
|
||||
}
|
||||
|
||||
type ResolveTargetMode = "explicit" | "implicit" | "heartbeat";
|
||||
|
||||
type ResolveTargetResult = {
|
||||
ok: boolean;
|
||||
to?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
type ResolveTargetFn = (params: {
|
||||
to?: string;
|
||||
mode: ResolveTargetMode;
|
||||
allowFrom: string[];
|
||||
}) => ResolveTargetResult;
|
||||
|
||||
/** Install a shared test matrix for target-resolution error handling. */
|
||||
export function installCommonResolveTargetErrorCases(params: {
|
||||
resolveTarget: ResolveTargetFn;
|
||||
implicitAllowFrom: string[];
|
||||
}) {
|
||||
const { resolveTarget, implicitAllowFrom } = params;
|
||||
|
||||
it("should error on normalization failure with allowlist (implicit mode)", () => {
|
||||
const result = resolveTarget({
|
||||
to: "invalid-target",
|
||||
mode: "implicit",
|
||||
allowFrom: implicitAllowFrom,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it("should error when no target provided with allowlist", () => {
|
||||
const result = resolveTarget({
|
||||
to: undefined,
|
||||
mode: "implicit",
|
||||
allowFrom: implicitAllowFrom,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it("should error when no target and no allowlist", () => {
|
||||
const result = resolveTarget({
|
||||
to: undefined,
|
||||
mode: "explicit",
|
||||
allowFrom: [],
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
|
||||
it("should handle whitespace-only target", () => {
|
||||
const result = resolveTarget({
|
||||
to: " ",
|
||||
mode: "explicit",
|
||||
allowFrom: [],
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.error).toBeDefined();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user