From 43a34e23b3168581d451932b640f19c0f23bc39c Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 20 Apr 2026 18:58:09 +0100 Subject: [PATCH] test: share lazy channel contract surface --- test/helpers/channels/command-contract.ts | 20 +------------------ test/helpers/channels/lazy-object-surface.ts | 18 +++++++++++++++++ .../helpers/channels/matrix-setup-contract.ts | 20 +------------------ 3 files changed, 20 insertions(+), 38 deletions(-) create mode 100644 test/helpers/channels/lazy-object-surface.ts diff --git a/test/helpers/channels/command-contract.ts b/test/helpers/channels/command-contract.ts index f77b3883449..db64bf4c202 100644 --- a/test/helpers/channels/command-contract.ts +++ b/test/helpers/channels/command-contract.ts @@ -2,6 +2,7 @@ import { loadBundledPluginApiSync, loadBundledPluginContractApiSync, } from "../../../src/test-utils/bundled-plugin-public-surface.js"; +import { createLazyObjectSurface } from "./lazy-object-surface.js"; type TelegramContractSurface = { buildTelegramModelsProviderChannelData: (...args: unknown[]) => unknown; @@ -15,25 +16,6 @@ type WhatsAppApiSurface = { let telegramContractSurface: TelegramContractSurface | undefined; let whatsappApiSurface: WhatsAppApiSurface | undefined; -function createLazyObjectSurface(loadSurface: () => T): T { - return new Proxy({} as T, { - get(_target, property) { - const surface = loadSurface(); - const value = Reflect.get(surface, property, surface); - return typeof value === "function" ? value.bind(surface) : value; - }, - has(_target, property) { - return property in loadSurface(); - }, - ownKeys() { - return Reflect.ownKeys(loadSurface()); - }, - getOwnPropertyDescriptor(_target, property) { - return Reflect.getOwnPropertyDescriptor(loadSurface(), property); - }, - }); -} - function getTelegramContractSurface(): TelegramContractSurface { telegramContractSurface ??= loadBundledPluginContractApiSync("telegram"); return telegramContractSurface; diff --git a/test/helpers/channels/lazy-object-surface.ts b/test/helpers/channels/lazy-object-surface.ts new file mode 100644 index 00000000000..b5b27147386 --- /dev/null +++ b/test/helpers/channels/lazy-object-surface.ts @@ -0,0 +1,18 @@ +export function createLazyObjectSurface(loadSurface: () => T): T { + return new Proxy({} as T, { + get(_target, property) { + const surface = loadSurface(); + const value = Reflect.get(surface, property, surface); + return typeof value === "function" ? value.bind(surface) : value; + }, + has(_target, property) { + return property in loadSurface(); + }, + ownKeys() { + return Reflect.ownKeys(loadSurface()); + }, + getOwnPropertyDescriptor(_target, property) { + return Reflect.getOwnPropertyDescriptor(loadSurface(), property); + }, + }); +} diff --git a/test/helpers/channels/matrix-setup-contract.ts b/test/helpers/channels/matrix-setup-contract.ts index c6c4c8b7d95..90cb63eca4b 100644 --- a/test/helpers/channels/matrix-setup-contract.ts +++ b/test/helpers/channels/matrix-setup-contract.ts @@ -1,4 +1,5 @@ import { loadBundledPluginContractApiSync } from "../../../src/test-utils/bundled-plugin-public-surface.js"; +import { createLazyObjectSurface } from "./lazy-object-surface.js"; type MatrixContractSurface = { matrixSetupAdapter: Record; @@ -7,25 +8,6 @@ type MatrixContractSurface = { let matrixContractSurface: MatrixContractSurface | undefined; -function createLazyObjectSurface(loadSurface: () => T): T { - return new Proxy({} as T, { - get(_target, property) { - const surface = loadSurface(); - const value = Reflect.get(surface, property, surface); - return typeof value === "function" ? value.bind(surface) : value; - }, - has(_target, property) { - return property in loadSurface(); - }, - ownKeys() { - return Reflect.ownKeys(loadSurface()); - }, - getOwnPropertyDescriptor(_target, property) { - return Reflect.getOwnPropertyDescriptor(loadSurface(), property); - }, - }); -} - function getMatrixContractSurface(): MatrixContractSurface { matrixContractSurface ??= loadBundledPluginContractApiSync("matrix"); return matrixContractSurface;