From 40c9da1d571728087834c23b7d82b4202bb247bc Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 17 Apr 2026 16:06:26 +0100 Subject: [PATCH] perf: avoid Matrix entry full-channel load in tests --- extensions/matrix/index.test.ts | 11 ++-- extensions/matrix/index.ts | 89 +++++++++++++++++---------------- 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/extensions/matrix/index.test.ts b/extensions/matrix/index.test.ts index 60c42c1c257..5089d266ebd 100644 --- a/extensions/matrix/index.test.ts +++ b/extensions/matrix/index.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it, vi } from "vitest"; import { createTestPluginApi } from "../../test/helpers/plugins/plugin-api.js"; import { registerMatrixCliMetadata } from "./cli-metadata.js"; -import entry from "./index.js"; +import entry, { registerMatrixFullRuntime } from "./index.js"; const cliMocks = vi.hoisted(() => ({ registerMatrixCli: vi.fn(), @@ -68,9 +68,8 @@ describe("matrix plugin", () => { expect(entry.name).toBe("Matrix"); }); - it("registers subagent lifecycle hooks during full registration", () => { + it("registers subagent lifecycle hooks during full runtime registration", () => { const on = vi.fn(); - const registerChannel = vi.fn(); const registerGatewayMethod = vi.fn(); const api = createTestPluginApi({ id: "matrix", @@ -80,15 +79,11 @@ describe("matrix plugin", () => { runtime: {} as never, registrationMode: "full", on, - registerChannel, registerGatewayMethod, }); - entry.register(api); + registerMatrixFullRuntime(api); - expect(registerChannel).toHaveBeenCalledWith({ - plugin: expect.objectContaining({ id: "matrix" }), - }); expect(on.mock.calls.map(([hookName]) => hookName)).toEqual([ "subagent_spawning", "subagent_ended", diff --git a/extensions/matrix/index.ts b/extensions/matrix/index.ts index e46b307e1b6..85813c89d68 100644 --- a/extensions/matrix/index.ts +++ b/extensions/matrix/index.ts @@ -1,4 +1,7 @@ -import { defineBundledChannelEntry } from "openclaw/plugin-sdk/channel-entry-contract"; +import { + defineBundledChannelEntry, + type OpenClawPluginApi, +} from "openclaw/plugin-sdk/channel-entry-contract"; import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime"; import { registerMatrixCliMetadata } from "./cli-metadata.js"; @@ -11,6 +14,48 @@ function loadMatrixSubagentHooksModule() { return matrixSubagentHooksPromise; } +export function registerMatrixFullRuntime(api: OpenClawPluginApi): void { + void import("./plugin-entry.handlers.runtime.js") + .then(({ ensureMatrixCryptoRuntime }) => + ensureMatrixCryptoRuntime({ log: api.logger.info }).catch((err: unknown) => { + const message = formatErrorMessage(err); + api.logger.warn?.(`matrix: crypto runtime bootstrap failed: ${message}`); + }), + ) + .catch((err: unknown) => { + const message = formatErrorMessage(err); + api.logger.warn?.(`matrix: failed loading crypto bootstrap runtime: ${message}`); + }); + + api.registerGatewayMethod("matrix.verify.recoveryKey", async (ctx) => { + const { handleVerifyRecoveryKey } = await import("./plugin-entry.handlers.runtime.js"); + await handleVerifyRecoveryKey(ctx); + }); + + api.registerGatewayMethod("matrix.verify.bootstrap", async (ctx) => { + const { handleVerificationBootstrap } = await import("./plugin-entry.handlers.runtime.js"); + await handleVerificationBootstrap(ctx); + }); + + api.registerGatewayMethod("matrix.verify.status", async (ctx) => { + const { handleVerificationStatus } = await import("./plugin-entry.handlers.runtime.js"); + await handleVerificationStatus(ctx); + }); + + api.on("subagent_spawning", async (event) => { + const { handleMatrixSubagentSpawning } = await loadMatrixSubagentHooksModule(); + return await handleMatrixSubagentSpawning(api, event); + }); + api.on("subagent_ended", async (event) => { + const { handleMatrixSubagentEnded } = await loadMatrixSubagentHooksModule(); + await handleMatrixSubagentEnded(event); + }); + api.on("subagent_delivery_target", async (event) => { + const { handleMatrixSubagentDeliveryTarget } = await loadMatrixSubagentHooksModule(); + return handleMatrixSubagentDeliveryTarget(event); + }); +} + export default defineBundledChannelEntry({ id: "matrix", name: "Matrix", @@ -29,45 +74,5 @@ export default defineBundledChannelEntry({ exportName: "setMatrixRuntime", }, registerCliMetadata: registerMatrixCliMetadata, - registerFull(api) { - void import("./plugin-entry.handlers.runtime.js") - .then(({ ensureMatrixCryptoRuntime }) => - ensureMatrixCryptoRuntime({ log: api.logger.info }).catch((err: unknown) => { - const message = formatErrorMessage(err); - api.logger.warn?.(`matrix: crypto runtime bootstrap failed: ${message}`); - }), - ) - .catch((err: unknown) => { - const message = formatErrorMessage(err); - api.logger.warn?.(`matrix: failed loading crypto bootstrap runtime: ${message}`); - }); - - api.registerGatewayMethod("matrix.verify.recoveryKey", async (ctx) => { - const { handleVerifyRecoveryKey } = await import("./plugin-entry.handlers.runtime.js"); - await handleVerifyRecoveryKey(ctx); - }); - - api.registerGatewayMethod("matrix.verify.bootstrap", async (ctx) => { - const { handleVerificationBootstrap } = await import("./plugin-entry.handlers.runtime.js"); - await handleVerificationBootstrap(ctx); - }); - - api.registerGatewayMethod("matrix.verify.status", async (ctx) => { - const { handleVerificationStatus } = await import("./plugin-entry.handlers.runtime.js"); - await handleVerificationStatus(ctx); - }); - - api.on("subagent_spawning", async (event) => { - const { handleMatrixSubagentSpawning } = await loadMatrixSubagentHooksModule(); - return await handleMatrixSubagentSpawning(api, event); - }); - api.on("subagent_ended", async (event) => { - const { handleMatrixSubagentEnded } = await loadMatrixSubagentHooksModule(); - await handleMatrixSubagentEnded(event); - }); - api.on("subagent_delivery_target", async (event) => { - const { handleMatrixSubagentDeliveryTarget } = await loadMatrixSubagentHooksModule(); - return handleMatrixSubagentDeliveryTarget(event); - }); - }, + registerFull: registerMatrixFullRuntime, });