mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 20:40:43 +00:00
test: share lazy channel contract surface
This commit is contained in:
@@ -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;
|
||||
|
||||
18
test/helpers/channels/lazy-object-surface.ts
Normal file
18
test/helpers/channels/lazy-object-surface.ts
Normal 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);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user