mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 08:30:42 +00:00
Fix Slack HTTP route registry dispatch
This commit is contained in:
committed by
Peter Steinberger
parent
988fe85f2c
commit
7ecff96425
52
extensions/slack/src/http/plugin-routes.dispatch.test.ts
Normal file
52
extensions/slack/src/http/plugin-routes.dispatch.test.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
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");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -2,13 +2,7 @@ import { DEFAULT_ACCOUNT_ID } from "openclaw/plugin-sdk/account-id";
|
||||
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/channel-plugin-common";
|
||||
import { listSlackAccountIds, mergeSlackAccountConfig } from "../accounts.js";
|
||||
import { normalizeSlackWebhookPath } from "./paths.js";
|
||||
|
||||
let slackHttpHandlerRuntimePromise: Promise<typeof import("./handler.runtime.js")> | null = null;
|
||||
|
||||
async function loadSlackHttpHandlerRuntime() {
|
||||
slackHttpHandlerRuntimePromise ??= import("./handler.runtime.js");
|
||||
return await slackHttpHandlerRuntimePromise;
|
||||
}
|
||||
import { handleSlackHttpRequest } from "./registry.js";
|
||||
|
||||
export function registerSlackPluginHttpRoutes(api: OpenClawPluginApi): void {
|
||||
const accountIds = new Set<string>([DEFAULT_ACCOUNT_ID, ...listSlackAccountIds(api.config)]);
|
||||
@@ -25,8 +19,7 @@ export function registerSlackPluginHttpRoutes(api: OpenClawPluginApi): void {
|
||||
api.registerHttpRoute({
|
||||
path,
|
||||
auth: "plugin",
|
||||
handler: async (req, res) =>
|
||||
await (await loadSlackHttpHandlerRuntime()).handleSlackHttpRequest(req, res),
|
||||
handler: async (req, res) => await handleSlackHttpRequest(req, res),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user