2026-03-16 21:55:10 +09:00
|
|
|
import { describe, expect, test, mock, spyOn } from "bun:test"
|
|
|
|
|
import {
|
|
|
|
|
interpolateInstruction,
|
2026-03-16 22:28:54 +09:00
|
|
|
resolveCommandTimeoutMs,
|
2026-03-16 21:55:10 +09:00
|
|
|
shellEscapeArg,
|
|
|
|
|
wakeGateway,
|
|
|
|
|
wakeCommandGateway,
|
|
|
|
|
} from "../dispatcher"
|
|
|
|
|
|
|
|
|
|
describe("OpenClaw Dispatcher", () => {
|
|
|
|
|
test("interpolateInstruction replaces variables", () => {
|
|
|
|
|
const template = "Hello {{name}}, welcome to {{place}}!"
|
|
|
|
|
const variables = { name: "World", place: "Bun" }
|
|
|
|
|
expect(interpolateInstruction(template, variables)).toBe(
|
|
|
|
|
"Hello World, welcome to Bun!",
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test("interpolateInstruction handles missing variables", () => {
|
|
|
|
|
const template = "Hello {{name}}!"
|
|
|
|
|
const variables = {}
|
|
|
|
|
expect(interpolateInstruction(template, variables)).toBe("Hello !")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test("shellEscapeArg escapes single quotes", () => {
|
|
|
|
|
expect(shellEscapeArg("foo'bar")).toBe("'foo'\\''bar'")
|
|
|
|
|
expect(shellEscapeArg("simple")).toBe("'simple'")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test("wakeGateway sends POST request", async () => {
|
|
|
|
|
const fetchSpy = spyOn(global, "fetch").mockResolvedValue(
|
|
|
|
|
new Response(JSON.stringify({ ok: true }), { status: 200 }),
|
|
|
|
|
)
|
2026-03-16 22:28:54 +09:00
|
|
|
try {
|
|
|
|
|
const result = await wakeGateway(
|
|
|
|
|
"test",
|
|
|
|
|
{ url: "https://example.com", method: "POST", timeout: 1000, type: "http" },
|
|
|
|
|
{ foo: "bar" },
|
|
|
|
|
)
|
2026-03-16 21:55:10 +09:00
|
|
|
|
2026-03-16 22:28:54 +09:00
|
|
|
expect(result.success).toBe(true)
|
|
|
|
|
expect(fetchSpy).toHaveBeenCalled()
|
|
|
|
|
const call = fetchSpy.mock.calls[0]
|
|
|
|
|
expect(call[0]).toBe("https://example.com")
|
|
|
|
|
expect(call[1]?.method).toBe("POST")
|
|
|
|
|
expect(call[1]?.body).toBe('{"foo":"bar"}')
|
|
|
|
|
} finally {
|
|
|
|
|
fetchSpy.mockRestore()
|
|
|
|
|
}
|
2026-03-16 21:55:10 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test("wakeGateway fails on invalid URL", async () => {
|
|
|
|
|
const result = await wakeGateway("test", { url: "http://example.com", method: "POST", timeout: 1000, type: "http" }, {})
|
|
|
|
|
expect(result.success).toBe(false)
|
|
|
|
|
expect(result.error).toContain("Invalid URL")
|
|
|
|
|
})
|
2026-03-16 22:28:54 +09:00
|
|
|
|
|
|
|
|
test("resolveCommandTimeoutMs reads OMO env fallback", () => {
|
|
|
|
|
const original = process.env.OMO_OPENCLAW_COMMAND_TIMEOUT_MS
|
|
|
|
|
process.env.OMO_OPENCLAW_COMMAND_TIMEOUT_MS = "4321"
|
|
|
|
|
|
|
|
|
|
try {
|
2026-03-17 17:00:18 +09:00
|
|
|
// Call without explicit envTimeoutRaw so the function reads from process.env itself
|
|
|
|
|
expect(resolveCommandTimeoutMs(undefined)).toBe(4321)
|
2026-03-16 22:28:54 +09:00
|
|
|
} finally {
|
|
|
|
|
if (original === undefined) delete process.env.OMO_OPENCLAW_COMMAND_TIMEOUT_MS
|
|
|
|
|
else process.env.OMO_OPENCLAW_COMMAND_TIMEOUT_MS = original
|
|
|
|
|
}
|
|
|
|
|
})
|
2026-03-16 21:55:10 +09:00
|
|
|
})
|