





























@@ -1,18 +1,13 @@
1-import fs from "node:fs/promises";
2-import os from "node:os";
3-import path from "node:path";
41import { afterEach, describe, expect, it, vi } from "vitest";
52import {
63deleteCachedTelegramBotInfo,
74readCachedTelegramBotInfo,
8-resolveTelegramBotInfoCachePath,
5+setTelegramBotInfoCacheStoreForTest,
96TELEGRAM_BOT_INFO_CACHE_MAX_AGE_MS,
107writeCachedTelegramBotInfo,
118} from "./bot-info-cache.js";
129import type { TelegramBotInfo } from "./bot-info.js";
131014-const tempRoots: string[] = [];
15-1611const botInfo: TelegramBotInfo = {
1712id: 123456,
1813is_bot: true,
@@ -28,103 +23,104 @@ const botInfo: TelegramBotInfo = {
2823allows_users_to_create_topics: false,
2924};
302531-async function useTempStateDir(): Promise<NodeJS.ProcessEnv> {
32-const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-tg-bot-info-"));
33-tempRoots.push(stateDir);
34-return { ...process.env, OPENCLAW_STATE_DIR: stateDir };
26+type BotInfoCacheValue = {
27+tokenFingerprint: string;
28+fetchedAt: string;
29+botInfo: TelegramBotInfo;
30+};
31+32+function useMemoryStore() {
33+const entries = new Map<string, BotInfoCacheValue>();
34+setTelegramBotInfoCacheStoreForTest({
35+async register(key, value) {
36+entries.set(key, value);
37+},
38+async lookup(key) {
39+return entries.get(key);
40+},
41+async delete(key) {
42+return entries.delete(key);
43+},
44+});
45+return entries;
3546}
364737-afterEach(async () => {
48+afterEach(() => {
3849vi.unstubAllEnvs();
39-await Promise.all(
40-tempRoots.splice(0).map((root) => fs.rm(root, { recursive: true, force: true })),
41-);
50+setTelegramBotInfoCacheStoreForTest(undefined);
4251});
43524453describe("Telegram bot info cache", () => {
4554it("reads botInfo for the same account and bot token", async () => {
46-const env = await useTempStateDir();
55+useMemoryStore();
47564857await writeCachedTelegramBotInfo({
4958accountId: "ops",
5059botToken: "123456:secret",
5160 botInfo,
52- env,
5361});
54625563await expect(
56-readCachedTelegramBotInfo({ accountId: "ops", botToken: "123456:secret", env }),
64+readCachedTelegramBotInfo({ accountId: "ops", botToken: "123456:secret" }),
5765).resolves.toMatchObject({ botInfo });
5866});
59676068it("ignores botInfo written for a different token fingerprint", async () => {
61-const env = await useTempStateDir();
69+useMemoryStore();
62706371await writeCachedTelegramBotInfo({
6472accountId: "ops",
6573botToken: "123456:old-secret",
6674 botInfo,
67- env,
6875});
69767077await expect(
71-readCachedTelegramBotInfo({ accountId: "ops", botToken: "123456:new-secret", env }),
78+readCachedTelegramBotInfo({ accountId: "ops", botToken: "123456:new-secret" }),
7279).resolves.toBeNull();
7380});
74817582it("treats stale botInfo as a cache miss", async () => {
76-const env = await useTempStateDir();
83+useMemoryStore();
77847885await writeCachedTelegramBotInfo({
7986accountId: "ops",
8087botToken: "123456:secret",
8188 botInfo,
82- env,
8389});
84908591await expect(
8692readCachedTelegramBotInfo({
8793accountId: "ops",
8894botToken: "123456:secret",
89- env,
9095now: new Date(Date.now() + TELEGRAM_BOT_INFO_CACHE_MAX_AGE_MS + 1),
9196}),
9297).resolves.toBeNull();
9398});
949995100it("deletes cached botInfo for an account", async () => {
96-const env = await useTempStateDir();
101+useMemoryStore();
9710298103await writeCachedTelegramBotInfo({
99104accountId: "ops",
100105botToken: "123456:secret",
101106 botInfo,
102- env,
103107});
104-await deleteCachedTelegramBotInfo({ accountId: "ops", env });
108+await deleteCachedTelegramBotInfo({ accountId: "ops" });
105109106110await expect(
107-readCachedTelegramBotInfo({ accountId: "ops", botToken: "123456:secret", env }),
111+readCachedTelegramBotInfo({ accountId: "ops", botToken: "123456:secret" }),
108112).resolves.toBeNull();
109113});
110114111-it("treats malformed persisted botInfo as a cache miss", async () => {
112-const env = await useTempStateDir();
113-const filePath = resolveTelegramBotInfoCachePath("ops", env);
114-await fs.mkdir(path.dirname(filePath), { recursive: true });
115-await fs.writeFile(
116-filePath,
117-JSON.stringify({
118-version: 1,
119-tokenFingerprint: "not-the-token",
120-fetchedAt: new Date().toISOString(),
121-botInfo: { id: 123456, is_bot: true },
122-}),
123-"utf8",
124-);
115+it("uses normalized account ids as store keys", async () => {
116+const entries = useMemoryStore();
125117126-await expect(
127-readCachedTelegramBotInfo({ accountId: "ops", botToken: "123456:secret", env }),
128-).resolves.toBeNull();
118+await writeCachedTelegramBotInfo({
119+accountId: "ops team",
120+botToken: "123456:secret",
121+ botInfo,
122+});
123+124+expect(entries.has("ops_team")).toBe(true);
129125});
130126});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。