feat: add webhook hook mappings
# Conflicts: # src/gateway/server.ts
This commit is contained in:
89
src/gateway/hooks-mapping.test.ts
Normal file
89
src/gateway/hooks-mapping.test.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { applyHookMappings, resolveHookMappings } from "./hooks-mapping.js";
|
||||
|
||||
const baseUrl = new URL("http://127.0.0.1:18789/hooks/gmail");
|
||||
|
||||
describe("hooks mapping", () => {
|
||||
it("resolves gmail preset", () => {
|
||||
const mappings = resolveHookMappings({ presets: ["gmail"] });
|
||||
expect(mappings.length).toBeGreaterThan(0);
|
||||
expect(mappings[0]?.matchPath).toBe("gmail");
|
||||
});
|
||||
|
||||
it("renders template from payload", async () => {
|
||||
const mappings = resolveHookMappings({
|
||||
mappings: [
|
||||
{
|
||||
id: "demo",
|
||||
match: { path: "gmail" },
|
||||
action: "agent",
|
||||
messageTemplate: "Subject: {{messages[0].subject}}",
|
||||
},
|
||||
],
|
||||
});
|
||||
const result = await applyHookMappings(mappings, {
|
||||
payload: { messages: [{ subject: "Hello" }] },
|
||||
headers: {},
|
||||
url: baseUrl,
|
||||
path: "gmail",
|
||||
});
|
||||
expect(result?.ok).toBe(true);
|
||||
if (result?.ok) {
|
||||
expect(result.action.kind).toBe("agent");
|
||||
expect(result.action.message).toBe("Subject: Hello");
|
||||
}
|
||||
});
|
||||
|
||||
it("runs transform module", async () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "clawdis-hooks-"));
|
||||
const modPath = path.join(dir, "transform.mjs");
|
||||
const placeholder = "${" + "payload.name}";
|
||||
fs.writeFileSync(
|
||||
modPath,
|
||||
`export default ({ payload }) => ({ kind: "wake", text: \`Ping ${placeholder}\` });`,
|
||||
);
|
||||
|
||||
const mappings = resolveHookMappings({
|
||||
transformsDir: dir,
|
||||
mappings: [
|
||||
{
|
||||
match: { path: "custom" },
|
||||
action: "agent",
|
||||
transform: { module: "transform.mjs" },
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const result = await applyHookMappings(mappings, {
|
||||
payload: { name: "Ada" },
|
||||
headers: {},
|
||||
url: new URL("http://127.0.0.1:18789/hooks/custom"),
|
||||
path: "custom",
|
||||
});
|
||||
|
||||
expect(result?.ok).toBe(true);
|
||||
if (result?.ok) {
|
||||
expect(result.action.kind).toBe("wake");
|
||||
if (result.action.kind === "wake") {
|
||||
expect(result.action.text).toBe("Ping Ada");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects missing message", async () => {
|
||||
const mappings = resolveHookMappings({
|
||||
mappings: [{ match: { path: "noop" }, action: "agent" }],
|
||||
});
|
||||
const result = await applyHookMappings(mappings, {
|
||||
payload: {},
|
||||
headers: {},
|
||||
url: new URL("http://127.0.0.1:18789/hooks/noop"),
|
||||
path: "noop",
|
||||
});
|
||||
expect(result?.ok).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user