mirror of
https://github.com/openclaw/openclaw.git
synced 2026-10-03 01:29:56 +00:00
test(telegram): prepare shared state before webhook deadlines (#159828)
Co-authored-by: Peter Steinberger <steipete@gmail.com>
This commit is contained in:
parent
1b5ac171f6
commit
272605d6d5
2 changed files with 32 additions and 37 deletions
|
|
@ -5,6 +5,8 @@ import {
|
|||
setActivePluginRegistry,
|
||||
} from "openclaw/plugin-sdk/plugin-test-runtime";
|
||||
import { canonicalizeWebhookRouteKey } from "openclaw/plugin-sdk/webhook-ingress";
|
||||
import { vi } from "vitest";
|
||||
import * as telegramIngressFactory from "../telegram-ingress-drain-factory.js";
|
||||
|
||||
type StartWebhook = typeof import("../webhook.js").startTelegramWebhook;
|
||||
type StartWebhookOptions = Omit<Parameters<StartWebhook>[0], "token" | "abortSignal">;
|
||||
|
|
@ -80,20 +82,36 @@ export function createTelegramWebhookTestGateway(options: {
|
|||
startWebhook,
|
||||
withWebhook: async <T>(
|
||||
params: StartWebhookOptions,
|
||||
run: (ctx: { server: Server; port: number }) => Promise<T>,
|
||||
run: (ctx: {
|
||||
server: Server;
|
||||
port: number;
|
||||
ingress: ReturnType<typeof telegramIngressFactory.createTelegramTransportIngressMonitor>;
|
||||
}) => Promise<T>,
|
||||
): Promise<T> => {
|
||||
const abort = new AbortController();
|
||||
const started = await startWebhook({
|
||||
token: options.token,
|
||||
abortSignal: abort.signal,
|
||||
...options.queueScope(),
|
||||
...params,
|
||||
});
|
||||
const createIngress = telegramIngressFactory.createTelegramTransportIngressMonitor;
|
||||
let ingress: ReturnType<typeof createIngress> | undefined;
|
||||
const ingressFactory = vi
|
||||
.spyOn(telegramIngressFactory, "createTelegramTransportIngressMonitor")
|
||||
.mockImplementation((ingressParams) => (ingress = createIngress(ingressParams)));
|
||||
try {
|
||||
return await run({ server, port: getServerPort(server) });
|
||||
const abort = new AbortController();
|
||||
const started = await startWebhook({
|
||||
token: options.token,
|
||||
abortSignal: abort.signal,
|
||||
...options.queueScope(),
|
||||
...params,
|
||||
});
|
||||
try {
|
||||
if (!ingress) {
|
||||
throw new Error("Expected the started webhook's ingress monitor");
|
||||
}
|
||||
return await run({ server, port: getServerPort(server), ingress });
|
||||
} finally {
|
||||
await started.stop();
|
||||
abort.abort();
|
||||
}
|
||||
} finally {
|
||||
await started.stop();
|
||||
abort.abort();
|
||||
ingressFactory.mockRestore();
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
|
|||
|
|
@ -38,7 +38,6 @@ import { installTelegramIngressQueueRuntime } from "./runtime-state.test-support
|
|||
import { setTelegramRuntime } from "./runtime.js";
|
||||
import { clearTelegramRuntimeForTest as clearTelegramRuntime } from "./runtime.test-support.js";
|
||||
import type { TelegramRuntime } from "./runtime.types.js";
|
||||
import * as telegramIngressFactory from "./telegram-ingress-drain-factory.js";
|
||||
import { openTelegramIngressQueue } from "./telegram-ingress-spool.js";
|
||||
import {
|
||||
writeTelegramSpooledUpdate,
|
||||
|
|
@ -161,6 +160,7 @@ const {
|
|||
startWebhook: startTelegramWebhook,
|
||||
server: gatewayServer,
|
||||
pendingRequests: pendingRouteRequests,
|
||||
withWebhook: withStartedWebhook,
|
||||
} = gateway;
|
||||
let webhookStateDir: string | undefined;
|
||||
|
||||
|
|
@ -199,6 +199,8 @@ beforeEach(async () => {
|
|||
resetTelegramWebhookMocks();
|
||||
webhookStateDir = await fs.mkdtemp(nodePath.join(os.tmpdir(), "openclaw-telegram-webhook-"));
|
||||
installTelegramIngressQueueRuntime(() => webhookStateDir ?? os.tmpdir());
|
||||
// The production monitor prepares shared state before starting the webhook.
|
||||
await openTelegramIngressQueue(requireWebhookQueueScope()).listPending();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
|
|
@ -214,31 +216,6 @@ afterEach(async () => {
|
|||
}
|
||||
});
|
||||
|
||||
async function withStartedWebhook<T>(
|
||||
options: Parameters<typeof gateway.withWebhook>[0],
|
||||
run: (ctx: {
|
||||
server: typeof gateway.server;
|
||||
port: number;
|
||||
ingress: ReturnType<typeof telegramIngressFactory.createTelegramTransportIngressMonitor>;
|
||||
}) => Promise<T>,
|
||||
): Promise<T> {
|
||||
const createIngress = telegramIngressFactory.createTelegramTransportIngressMonitor;
|
||||
let ingress: ReturnType<typeof createIngress> | undefined;
|
||||
const ingressFactory = vi
|
||||
.spyOn(telegramIngressFactory, "createTelegramTransportIngressMonitor")
|
||||
.mockImplementation((params) => (ingress = createIngress(params)));
|
||||
try {
|
||||
return await gateway.withWebhook(options, async (ctx) => {
|
||||
if (!ingress) {
|
||||
throw new Error("Expected the started webhook's ingress monitor");
|
||||
}
|
||||
return await run({ ...ctx, ingress });
|
||||
});
|
||||
} finally {
|
||||
ingressFactory.mockRestore();
|
||||
}
|
||||
}
|
||||
|
||||
function startWebhookStartupFixture(
|
||||
options: Partial<Parameters<typeof startTelegramWebhook>[0]> = {},
|
||||
) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue