Skip to content

Commit eeabc2c

Browse files
authored
test(telegram): remove probe cache reset (#124219)
1 parent 2925065 commit eeabc2c

6 files changed

Lines changed: 27 additions & 53 deletions

File tree

‎extensions/telegram/api.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,7 @@ export {
139139
parseTelegramReplyToMessageId,
140140
parseTelegramThreadId,
141141
} from "./src/outbound-params.js";
142-
export {
143-
probeTelegram,
144-
resetTelegramProbeFetcherCacheForTests,
145-
type TelegramProbe,
146-
type TelegramProbeOptions,
147-
} from "./src/probe.js";
142+
export { probeTelegram, type TelegramProbe, type TelegramProbeOptions } from "./src/probe.js";
148143
export {
149144
type ResolvedReactionLevel,
150145
resolveTelegramReactionLevel,

‎extensions/telegram/src/probe.abort-signal.integration.test.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { createServer, type IncomingMessage, type Server } from "node:http";
44
import type { AddressInfo, Socket } from "node:net";
55
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
6-
import { probeTelegram, resetTelegramProbeFetcherCacheForTests } from "./probe.js";
6+
import { probeTelegram } from "./probe.js";
77

88
describe("probeTelegram startup retry loop honors abortSignal", () => {
99
let server: Server;
@@ -60,7 +60,6 @@ describe("probeTelegram startup retry loop honors abortSignal", () => {
6060
});
6161

6262
afterAll(async () => {
63-
resetTelegramProbeFetcherCacheForTests();
6463
vi.unstubAllEnvs();
6564
for (const socket of liveSockets) {
6665
socket.destroy();

‎extensions/telegram/src/probe.response-body-timeout.integration.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { createServer, type Server } from "node:http";
44
import type { AddressInfo, Socket } from "node:net";
55
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
6-
import { probeTelegram, resetTelegramProbeFetcherCacheForTests } from "./probe.js";
6+
import { probeTelegram } from "./probe.js";
77

88
type ResponseMode = "stall" | "trickle";
99

@@ -62,7 +62,6 @@ describe("probeTelegram response body deadlines over real sockets", () => {
6262
});
6363

6464
afterAll(async () => {
65-
resetTelegramProbeFetcherCacheForTests();
6665
vi.unstubAllEnvs();
6766
for (const interval of activeIntervals) {
6867
clearInterval(interval);
@@ -75,12 +74,14 @@ describe("probeTelegram response body deadlines over real sockets", () => {
7574
});
7675
});
7776

77+
let probeIndex = 0;
78+
7879
async function expectDeadlineFailure(mode: ResponseMode, expectedError: RegExp) {
7980
responseMode = mode;
8081
const previousRequestCount = requestCount;
8182
const previousClosedSocketCount = closedSocketCount;
8283

83-
const result = await probeTelegram("placeholder", 200, {
84+
const result = await probeTelegram(`deadline-${mode}-${++probeIndex}`, 200, {
8485
apiRoot,
8586
includeWebhookInfo: false,
8687
});

‎extensions/telegram/src/probe.response-body-timeout.test.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Telegram tests cover stalled diagnostic response body handling.
22
import { afterEach, describe, expect, it, vi, type Mock } from "vitest";
3-
import { probeTelegram, resetTelegramProbeFetcherCacheForTests } from "./probe.js";
3+
import { probeTelegram } from "./probe.js";
44

55
const resolveTelegramTransport = vi.hoisted(() => vi.fn());
66
const makeProxyFetch = vi.hoisted(() => vi.fn());
@@ -85,8 +85,10 @@ function makeTricklingJsonResponse(cancel: (reason?: unknown) => void): Response
8585
}
8686

8787
describe("probeTelegram response body timeouts", () => {
88+
let tokenIndex = 0;
89+
const nextToken = () => `response-body-${++tokenIndex}`;
90+
8891
afterEach(() => {
89-
resetTelegramProbeFetcherCacheForTests();
9092
resolveTelegramTransport.mockReset();
9193
makeProxyFetch.mockReset();
9294
vi.useRealTimers();
@@ -107,7 +109,7 @@ describe("probeTelegram response body timeouts", () => {
107109
);
108110

109111
vi.useFakeTimers();
110-
const probePromise = probeTelegram("placeholder", 50, {
112+
const probePromise = probeTelegram(nextToken(), 50, {
111113
includeWebhookInfo: false,
112114
});
113115
await vi.advanceTimersByTimeAsync(0);
@@ -127,7 +129,7 @@ describe("probeTelegram response body timeouts", () => {
127129
vi.useFakeTimers();
128130
fetchMock.mockResolvedValueOnce(makeTricklingJsonResponse(cancel));
129131

130-
const probePromise = probeTelegram("placeholder", 100, {
132+
const probePromise = probeTelegram(nextToken(), 100, {
131133
includeWebhookInfo: false,
132134
});
133135
await vi.advanceTimersByTimeAsync(0);
@@ -155,7 +157,7 @@ describe("probeTelegram response body timeouts", () => {
155157
);
156158

157159
vi.useFakeTimers();
158-
const probePromise = probeTelegram("placeholder", 50);
160+
const probePromise = probeTelegram(nextToken(), 50);
159161
await vi.advanceTimersByTimeAsync(0);
160162
await vi.advanceTimersByTimeAsync(60);
161163

‎extensions/telegram/src/probe.test.ts‎

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Telegram tests cover probe plugin behavior.
22
import { withFetchPreconnect } from "openclaw/plugin-sdk/test-env";
3-
import { afterEach, describe, expect, it, vi, type Mock } from "vitest";
4-
import { probeTelegram, resetTelegramProbeFetcherCacheForTests } from "./probe.js";
3+
import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from "vitest";
4+
import { probeTelegram } from "./probe.js";
55

66
const resolveTelegramTransport = vi.hoisted(() => vi.fn());
77
const makeProxyFetch = vi.hoisted(() => vi.fn());
@@ -31,8 +31,9 @@ vi.mock("openclaw/plugin-sdk/response-limit-runtime", async (importOriginal) =>
3131
});
3232

3333
describe("probeTelegram retry logic", () => {
34-
const token = "test-token";
3534
const timeoutMs = 5000;
35+
let tokenIndex = 0;
36+
let token = "";
3637
const originalFetch = global.fetch;
3738
let forceFallbackMock: Mock;
3839

@@ -92,11 +93,13 @@ describe("probeTelegram retry logic", () => {
9293
expect(result.bot?.username).toBe("test_bot");
9394
}
9495

96+
beforeEach(() => {
97+
token = `test-token-${++tokenIndex}`;
98+
});
99+
95100
afterEach(() => {
96-
resetTelegramProbeFetcherCacheForTests();
97101
resolveTelegramTransport.mockReset();
98102
makeProxyFetch.mockReset();
99-
vi.unstubAllEnvs();
100103
vi.clearAllMocks();
101104
if (originalFetch) {
102105
global.fetch = originalFetch;
@@ -237,7 +240,7 @@ describe("probeTelegram retry logic", () => {
237240
allows_users_to_create_topics: false,
238241
});
239242
expect(fetchMock).toHaveBeenCalledTimes(1);
240-
expect(fetchMock.mock.calls.at(0)?.[0]).toBe("https://api.telegram.org/bottest-token/getMe");
243+
expect(fetchMock.mock.calls.at(0)?.[0]).toBe(`https://api.telegram.org/bot${token}/getMe`);
241244
});
242245

243246
it("uses resolver-scoped Telegram fetch with probe network options", async () => {
@@ -264,9 +267,6 @@ describe("probeTelegram retry logic", () => {
264267

265268
it("reuses probe fetcher across repeated probes for the same account transport settings", async () => {
266269
const fetchMock = installFetchMock();
267-
vi.stubEnv("VITEST", "");
268-
vi.stubEnv("NODE_ENV", "production");
269-
270270
mockGetMeSuccess(fetchMock);
271271
mockGetWebhookInfoSuccess(fetchMock);
272272
await probeTelegram(`${token}-cache`, timeoutMs, {
@@ -290,9 +290,6 @@ describe("probeTelegram retry logic", () => {
290290

291291
it("does not reuse probe fetcher cache when network settings differ", async () => {
292292
const fetchMock = installFetchMock();
293-
vi.stubEnv("VITEST", "");
294-
vi.stubEnv("NODE_ENV", "production");
295-
296293
mockGetMeSuccess(fetchMock);
297294
mockGetWebhookInfoSuccess(fetchMock);
298295
await probeTelegram(`${token}-cache-variant`, timeoutMs, {
@@ -327,9 +324,6 @@ describe("probeTelegram retry logic", () => {
327324
close,
328325
};
329326
});
330-
vi.stubEnv("VITEST", "");
331-
vi.stubEnv("NODE_ENV", "production");
332-
333327
for (let i = 0; i < 65; i += 1) {
334328
mockGetMeSuccess(fetchMock);
335329
mockGetWebhookInfoSuccess(fetchMock);
@@ -349,9 +343,6 @@ describe("probeTelegram retry logic", () => {
349343

350344
it("reuses probe fetcher cache across token rotation when accountId is stable", async () => {
351345
const fetchMock = installFetchMock();
352-
vi.stubEnv("VITEST", "");
353-
vi.stubEnv("NODE_ENV", "production");
354-
355346
mockGetMeSuccess(fetchMock);
356347
mockGetWebhookInfoSuccess(fetchMock);
357348
await probeTelegram(`${token}-old`, timeoutMs, {

‎extensions/telegram/src/probe.ts‎

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ const MAX_PROBE_TRANSPORT_CACHE_SIZE = 64;
4949
// 4 MiB guards against a misbehaving or hostile API endpoint streaming an oversized payload.
5050
const TELEGRAM_BOT_API_MAX_RESPONSE_BYTES = 4 * 1024 * 1024;
5151

52-
export function resetTelegramProbeFetcherCacheForTests(): void {
53-
probeTransportCache.clear();
54-
}
55-
5652
function resolveProbeOptions(
5753
proxyOrOptions?: string | TelegramProbeOptions,
5854
): TelegramProbeOptions | undefined {
@@ -65,10 +61,6 @@ function resolveProbeOptions(
6561
return proxyOrOptions;
6662
}
6763

68-
function shouldUseProbeTransportCache(): boolean {
69-
return !process.env.VITEST && process.env.NODE_ENV !== "test";
70-
}
71-
7264
function buildProbeTransportCacheKey(token: string, options?: TelegramProbeOptions): string {
7365
const cacheIdentity = options?.accountId?.trim() || token;
7466
const cacheIdentityKind = options?.accountId?.trim() ? "account" : "token";
@@ -98,13 +90,10 @@ function setCachedProbeTransport(
9890
}
9991

10092
function resolveProbeTransport(token: string, options?: TelegramProbeOptions): TelegramTransport {
101-
const cacheEnabled = shouldUseProbeTransportCache();
102-
const cacheKey = cacheEnabled ? buildProbeTransportCacheKey(token, options) : null;
103-
if (cacheKey) {
104-
const cached = probeTransportCache.get(cacheKey);
105-
if (cached) {
106-
return cached;
107-
}
93+
const cacheKey = buildProbeTransportCacheKey(token, options);
94+
const cached = probeTransportCache.get(cacheKey);
95+
if (cached) {
96+
return cached;
10897
}
10998

11099
const proxyUrl = options?.proxyUrl?.trim();
@@ -113,10 +102,7 @@ function resolveProbeTransport(token: string, options?: TelegramProbeOptions): T
113102
network: options?.network,
114103
});
115104

116-
if (cacheKey) {
117-
return setCachedProbeTransport(cacheKey, transport);
118-
}
119-
return transport;
105+
return setCachedProbeTransport(cacheKey, transport);
120106
}
121107

122108
function normalizeBoolean(value: unknown): boolean | null {

0 commit comments

Comments
 (0)