test: share lazy channel contract surface

This commit is contained in:
Peter Steinberger
2026-04-20 18:58:09 +01:00
parent 26c213031d
commit 43a34e23b3
3 changed files with 20 additions and 38 deletions

View File

@@ -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<T extends object>(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<TelegramContractSurface>("telegram");
return telegramContractSurface;

View File

@@ -0,0 +1,18 @@
export function createLazyObjectSurface<T extends object>(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);
},
});
}

View File

@@ -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<string, unknown>;
@@ -7,25 +8,6 @@ type MatrixContractSurface = {
let matrixContractSurface: MatrixContractSurface | undefined;
function createLazyObjectSurface<T extends object>(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<MatrixContractSurface>("matrix");
return matrixContractSurface;