mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 08:40:44 +00:00
perf: fold slack http route test
This commit is contained in:
@@ -1,52 +0,0 @@
|
||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createTestPluginApi } from "../../../../test/helpers/plugins/plugin-api.js";
|
||||
import type { OpenClawConfig, OpenClawPluginApi } from "../runtime-api.js";
|
||||
|
||||
function createApi(config: OpenClawConfig, registerHttpRoute = vi.fn()): OpenClawPluginApi {
|
||||
return createTestPluginApi({
|
||||
id: "slack",
|
||||
config,
|
||||
registerHttpRoute,
|
||||
});
|
||||
}
|
||||
|
||||
describe("registerSlackPluginHttpRoutes dispatch", () => {
|
||||
it("uses the shared Slack HTTP handler registry", async () => {
|
||||
vi.resetModules();
|
||||
const staleRuntimeHandler = vi.fn(async () => false);
|
||||
vi.doMock("./handler.runtime.js", () => ({
|
||||
handleSlackHttpRequest: staleRuntimeHandler,
|
||||
}));
|
||||
|
||||
const [{ registerSlackPluginHttpRoutes }, { registerSlackHttpHandler }] = await Promise.all([
|
||||
import("./plugin-routes.js"),
|
||||
import("./registry.js"),
|
||||
]);
|
||||
const routeHandler = vi.fn();
|
||||
const unregister = registerSlackHttpHandler({
|
||||
path: "/slack/events",
|
||||
handler: routeHandler,
|
||||
});
|
||||
const registerHttpRoute = vi.fn();
|
||||
|
||||
try {
|
||||
registerSlackPluginHttpRoutes(createApi({}, registerHttpRoute));
|
||||
const route = registerHttpRoute.mock.calls[0]?.[0] as
|
||||
| {
|
||||
handler: (req: IncomingMessage, res: ServerResponse) => Promise<boolean>;
|
||||
}
|
||||
| undefined;
|
||||
const req = { url: "/slack/events" } as IncomingMessage;
|
||||
const res = {} as ServerResponse;
|
||||
|
||||
await expect(route?.handler(req, res)).resolves.toBe(true);
|
||||
|
||||
expect(routeHandler).toHaveBeenCalledWith(req, res);
|
||||
expect(staleRuntimeHandler).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
unregister();
|
||||
vi.doUnmock("./handler.runtime.js");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,9 @@
|
||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createTestPluginApi } from "../../../../test/helpers/plugins/plugin-api.js";
|
||||
import type { OpenClawConfig, OpenClawPluginApi } from "../runtime-api.js";
|
||||
import { registerSlackPluginHttpRoutes } from "./plugin-routes.js";
|
||||
import { registerSlackHttpHandler } from "./registry.js";
|
||||
|
||||
function createApi(config: OpenClawConfig, registerHttpRoute = vi.fn()): OpenClawPluginApi {
|
||||
return createTestPluginApi({
|
||||
@@ -59,4 +61,30 @@ describe("registerSlackPluginHttpRoutes", () => {
|
||||
.toSorted();
|
||||
expect(paths).toEqual(["/slack/events"]);
|
||||
});
|
||||
|
||||
it("dispatches through the shared Slack HTTP handler registry", async () => {
|
||||
const routeHandler = vi.fn();
|
||||
const unregister = registerSlackHttpHandler({
|
||||
path: "/slack/events",
|
||||
handler: routeHandler,
|
||||
});
|
||||
const registerHttpRoute = vi.fn();
|
||||
|
||||
try {
|
||||
registerSlackPluginHttpRoutes(createApi({}, registerHttpRoute));
|
||||
const route = registerHttpRoute.mock.calls[0]?.[0] as
|
||||
| {
|
||||
handler: (req: IncomingMessage, res: ServerResponse) => Promise<boolean>;
|
||||
}
|
||||
| undefined;
|
||||
const req = { url: "/slack/events" } as IncomingMessage;
|
||||
const res = {} as ServerResponse;
|
||||
|
||||
await expect(route?.handler(req, res)).resolves.toBe(true);
|
||||
|
||||
expect(routeHandler).toHaveBeenCalledWith(req, res);
|
||||
} finally {
|
||||
unregister();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user