perf: fold slack http route test

This commit is contained in:
Peter Steinberger
2026-04-24 01:43:01 +01:00
parent 382952a9e1
commit df58839a59
2 changed files with 28 additions and 52 deletions

View File

@@ -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");
}
});
});

View File

@@ -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();
}
});
});