test(zalo): broaden webhook monitor coverage

This commit is contained in:
Peter Steinberger
2026-02-22 11:28:27 +00:00
parent 081ab9c99d
commit 5c7ab8eae3

View File

@@ -21,37 +21,46 @@ async function withServer(handler: RequestListener, fn: (baseUrl: string) => Pro
} }
} }
describe("handleZaloWebhookRequest", () => { const DEFAULT_ACCOUNT: ResolvedZaloAccount = {
it("returns 400 for non-object payloads", async () => {
const core = {} as PluginRuntime;
const account: ResolvedZaloAccount = {
accountId: "default", accountId: "default",
enabled: true, enabled: true,
token: "tok", token: "tok",
tokenSource: "config", tokenSource: "config",
config: {}, config: {},
}; };
const unregister = registerZaloWebhookTarget({
token: "tok",
account,
config: {} as OpenClawConfig,
runtime: {},
core,
secret: "secret",
path: "/hook",
mediaMaxMb: 5,
});
try { const webhookRequestHandler: RequestListener = async (req, res) => {
await withServer(
async (req, res) => {
const handled = await handleZaloWebhookRequest(req, res); const handled = await handleZaloWebhookRequest(req, res);
if (!handled) { if (!handled) {
res.statusCode = 404; res.statusCode = 404;
res.end("not found"); res.end("not found");
} }
}, };
async (baseUrl) => {
function registerTarget(params: {
path: string;
secret?: string;
statusSink?: (patch: { lastInboundAt?: number; lastOutboundAt?: number }) => void;
}): () => void {
return registerZaloWebhookTarget({
token: "tok",
account: DEFAULT_ACCOUNT,
config: {} as OpenClawConfig,
runtime: {},
core: {} as PluginRuntime,
secret: params.secret ?? "secret",
path: params.path,
mediaMaxMb: 5,
statusSink: params.statusSink,
});
}
describe("handleZaloWebhookRequest", () => {
it("returns 400 for non-object payloads", async () => {
const unregister = registerTarget({ path: "/hook" });
try {
await withServer(webhookRequestHandler, async (baseUrl) => {
const response = await fetch(`${baseUrl}/hook`, { const response = await fetch(`${baseUrl}/hook`, {
method: "POST", method: "POST",
headers: { headers: {
@@ -63,57 +72,20 @@ describe("handleZaloWebhookRequest", () => {
expect(response.status).toBe(400); expect(response.status).toBe(400);
expect(await response.text()).toBe("Bad Request"); expect(await response.text()).toBe("Bad Request");
}, });
);
} finally { } finally {
unregister(); unregister();
} }
}); });
it("rejects ambiguous routing when multiple targets match the same secret", async () => { it("rejects ambiguous routing when multiple targets match the same secret", async () => {
const core = {} as PluginRuntime;
const account: ResolvedZaloAccount = {
accountId: "default",
enabled: true,
token: "tok",
tokenSource: "config",
config: {},
};
const sinkA = vi.fn(); const sinkA = vi.fn();
const sinkB = vi.fn(); const sinkB = vi.fn();
const unregisterA = registerZaloWebhookTarget({ const unregisterA = registerTarget({ path: "/hook", statusSink: sinkA });
token: "tok", const unregisterB = registerTarget({ path: "/hook", statusSink: sinkB });
account,
config: {} as OpenClawConfig,
runtime: {},
core,
secret: "secret",
path: "/hook",
mediaMaxMb: 5,
statusSink: sinkA,
});
const unregisterB = registerZaloWebhookTarget({
token: "tok",
account,
config: {} as OpenClawConfig,
runtime: {},
core,
secret: "secret",
path: "/hook",
mediaMaxMb: 5,
statusSink: sinkB,
});
try { try {
await withServer( await withServer(webhookRequestHandler, async (baseUrl) => {
async (req, res) => {
const handled = await handleZaloWebhookRequest(req, res);
if (!handled) {
res.statusCode = 404;
res.end("not found");
}
},
async (baseUrl) => {
const response = await fetch(`${baseUrl}/hook`, { const response = await fetch(`${baseUrl}/hook`, {
method: "POST", method: "POST",
headers: { headers: {
@@ -126,8 +98,7 @@ describe("handleZaloWebhookRequest", () => {
expect(response.status).toBe(401); expect(response.status).toBe(401);
expect(sinkA).not.toHaveBeenCalled(); expect(sinkA).not.toHaveBeenCalled();
expect(sinkB).not.toHaveBeenCalled(); expect(sinkB).not.toHaveBeenCalled();
}, });
);
} finally { } finally {
unregisterA(); unregisterA();
unregisterB(); unregisterB();
@@ -135,35 +106,10 @@ describe("handleZaloWebhookRequest", () => {
}); });
it("returns 415 for non-json content-type", async () => { it("returns 415 for non-json content-type", async () => {
const core = {} as PluginRuntime; const unregister = registerTarget({ path: "/hook-content-type" });
const account: ResolvedZaloAccount = {
accountId: "default",
enabled: true,
token: "tok",
tokenSource: "config",
config: {},
};
const unregister = registerZaloWebhookTarget({
token: "tok",
account,
config: {} as OpenClawConfig,
runtime: {},
core,
secret: "secret",
path: "/hook-content-type",
mediaMaxMb: 5,
});
try { try {
await withServer( await withServer(webhookRequestHandler, async (baseUrl) => {
async (req, res) => {
const handled = await handleZaloWebhookRequest(req, res);
if (!handled) {
res.statusCode = 404;
res.end("not found");
}
},
async (baseUrl) => {
const response = await fetch(`${baseUrl}/hook-content-type`, { const response = await fetch(`${baseUrl}/hook-content-type`, {
method: "POST", method: "POST",
headers: { headers: {
@@ -174,34 +120,15 @@ describe("handleZaloWebhookRequest", () => {
}); });
expect(response.status).toBe(415); expect(response.status).toBe(415);
}, });
);
} finally { } finally {
unregister(); unregister();
} }
}); });
it("deduplicates webhook replay by event_name + message_id", async () => { it("deduplicates webhook replay by event_name + message_id", async () => {
const core = {} as PluginRuntime;
const account: ResolvedZaloAccount = {
accountId: "default",
enabled: true,
token: "tok",
tokenSource: "config",
config: {},
};
const sink = vi.fn(); const sink = vi.fn();
const unregister = registerZaloWebhookTarget({ const unregister = registerTarget({ path: "/hook-replay", statusSink: sink });
token: "tok",
account,
config: {} as OpenClawConfig,
runtime: {},
core,
secret: "secret",
path: "/hook-replay",
mediaMaxMb: 5,
statusSink: sink,
});
const payload = { const payload = {
event_name: "message.text.received", event_name: "message.text.received",
@@ -215,15 +142,7 @@ describe("handleZaloWebhookRequest", () => {
}; };
try { try {
await withServer( await withServer(webhookRequestHandler, async (baseUrl) => {
async (req, res) => {
const handled = await handleZaloWebhookRequest(req, res);
if (!handled) {
res.statusCode = 404;
res.end("not found");
}
},
async (baseUrl) => {
const first = await fetch(`${baseUrl}/hook-replay`, { const first = await fetch(`${baseUrl}/hook-replay`, {
method: "POST", method: "POST",
headers: { headers: {
@@ -244,43 +163,17 @@ describe("handleZaloWebhookRequest", () => {
expect(first.status).toBe(200); expect(first.status).toBe(200);
expect(second.status).toBe(200); expect(second.status).toBe(200);
expect(sink).toHaveBeenCalledTimes(1); expect(sink).toHaveBeenCalledTimes(1);
}, });
);
} finally { } finally {
unregister(); unregister();
} }
}); });
it("returns 429 when per-path request rate exceeds threshold", async () => { it("returns 429 when per-path request rate exceeds threshold", async () => {
const core = {} as PluginRuntime; const unregister = registerTarget({ path: "/hook-rate" });
const account: ResolvedZaloAccount = {
accountId: "default",
enabled: true,
token: "tok",
tokenSource: "config",
config: {},
};
const unregister = registerZaloWebhookTarget({
token: "tok",
account,
config: {} as OpenClawConfig,
runtime: {},
core,
secret: "secret",
path: "/hook-rate",
mediaMaxMb: 5,
});
try { try {
await withServer( await withServer(webhookRequestHandler, async (baseUrl) => {
async (req, res) => {
const handled = await handleZaloWebhookRequest(req, res);
if (!handled) {
res.statusCode = 404;
res.end("not found");
}
},
async (baseUrl) => {
let saw429 = false; let saw429 = false;
for (let i = 0; i < 130; i += 1) { for (let i = 0; i < 130; i += 1) {
const response = await fetch(`${baseUrl}/hook-rate`, { const response = await fetch(`${baseUrl}/hook-rate`, {
@@ -298,8 +191,7 @@ describe("handleZaloWebhookRequest", () => {
} }
expect(saw429).toBe(true); expect(saw429).toBe(true);
}, });
);
} finally { } finally {
unregister(); unregister();
} }