Files
oh-my-opencode/src/openclaw/__tests__/dispatcher.test.ts
T

79 lines
2.4 KiB
TypeScript
Raw Normal View History

2026-03-16 17:21:56 +09:00
import { describe, it, expect } from "bun:test";
import {
interpolateInstruction,
resolveCommandTimeoutMs,
shellEscapeArg,
validateGatewayUrl,
wakeCommandGateway,
} from "../dispatcher";
import { type OpenClawCommandGatewayConfig } from "../types";
describe("OpenClaw Dispatcher", () => {
describe("validateGatewayUrl", () => {
it("accepts valid https URLs", () => {
expect(validateGatewayUrl("https://example.com")).toBe(true);
});
it("rejects http URLs (remote)", () => {
expect(validateGatewayUrl("http://example.com")).toBe(false);
});
it("accepts http URLs for localhost", () => {
expect(validateGatewayUrl("http://localhost:3000")).toBe(true);
expect(validateGatewayUrl("http://127.0.0.1:8080")).toBe(true);
});
});
describe("interpolateInstruction", () => {
it("interpolates variables correctly", () => {
const result = interpolateInstruction("Hello {{name}}!", { name: "World" });
expect(result).toBe("Hello World!");
});
it("handles missing variables", () => {
const result = interpolateInstruction("Hello {{name}}!", {});
expect(result).toBe("Hello !");
});
});
describe("shellEscapeArg", () => {
it("escapes simple string", () => {
expect(shellEscapeArg("foo")).toBe("'foo'");
});
it("escapes string with single quotes", () => {
expect(shellEscapeArg("it's")).toBe("'it'\\''s'");
});
});
describe("resolveCommandTimeoutMs", () => {
it("uses default timeout", () => {
expect(resolveCommandTimeoutMs(undefined, undefined)).toBe(5000);
});
it("uses provided timeout", () => {
expect(resolveCommandTimeoutMs(1000, undefined)).toBe(1000);
});
it("clamps timeout", () => {
expect(resolveCommandTimeoutMs(10, undefined)).toBe(100);
expect(resolveCommandTimeoutMs(1000000, undefined)).toBe(300000);
});
});
describe("wakeCommandGateway", () => {
it("rejects if disabled via env", async () => {
const oldEnv = process.env.OMO_OPENCLAW_COMMAND;
process.env.OMO_OPENCLAW_COMMAND = "0";
2026-03-16 17:21:56 +09:00
const config: OpenClawCommandGatewayConfig = {
type: "command",
command: "echo hi",
};
const result = await wakeCommandGateway("test", config, {});
expect(result.success).toBe(false);
expect(result.error).toContain("disabled");
process.env.OMO_OPENCLAW_COMMAND = oldEnv;
2026-03-16 17:21:56 +09:00
});
});
});