




















@@ -1,4 +1,4 @@
1-import { readFile, rm } from "node:fs/promises";
1+import { readFile, rm, writeFile } from "node:fs/promises";
22import type { Message } from "@grammyjs/types";
33import { describe, expect, it, vi } from "vitest";
44import {
@@ -8,6 +8,28 @@ import {
88resolveTelegramMessageCachePath,
99} from "./message-cache.js";
101011+type PersistedCacheEntry = {
12+key: string;
13+node: {
14+sourceMessage: Message;
15+};
16+};
17+18+function persistedCacheEntry(messageId: number, text: string): PersistedCacheEntry {
19+return {
20+key: `default:7:${messageId}`,
21+node: {
22+sourceMessage: {
23+chat: { id: 7, type: "group", title: "Ops" },
24+message_id: messageId,
25+date: 1736380000 + messageId,
26+ text,
27+from: { id: messageId, is_bot: false, first_name: `User ${messageId}` },
28+} as Message,
29+},
30+};
31+}
32+1133describe("telegram message cache", () => {
1234it("hydrates reply chains from persisted cached messages", async () => {
1335const storePath = `/tmp/openclaw-telegram-message-cache-${process.pid}-${Date.now()}.json`;
@@ -253,6 +275,54 @@ describe("telegram message cache", () => {
253275}
254276});
255277278+it("loads mixed legacy array caches and rewrites them as line-delimited entries", async () => {
279+const storePath = `/tmp/openclaw-telegram-message-cache-legacy-${process.pid}-${Date.now()}.json`;
280+const persistedPath = resolveTelegramMessageCachePath(storePath);
281+await rm(persistedPath, { force: true });
282+try {
283+const legacyEntries = [
284+persistedCacheEntry(35033, "ocdbg-5818 one"),
285+persistedCacheEntry(35034, "ocdbg-5818 two"),
286+persistedCacheEntry(35035, "ocdbg-5818 three"),
287+];
288+const appendedEntries = [
289+persistedCacheEntry(35036, "ocdbg-5818 four"),
290+persistedCacheEntry(35037, "ocdbg-5818 five"),
291+];
292+await writeFile(
293+persistedPath,
294+`${JSON.stringify(legacyEntries)}${appendedEntries.map((entry) => JSON.stringify(entry)).join("\n")}\n`,
295+);
296+297+const cache = createTelegramMessageCache({ persistedPath });
298+299+expect(
300+cache
301+.around({
302+accountId: "default",
303+chatId: 7,
304+messageId: "35035",
305+before: 2,
306+after: 2,
307+})
308+.map((entry) => entry.messageId),
309+).toEqual(["35033", "35034", "35035", "35036", "35037"]);
310+311+const canonical = await readFile(persistedPath, "utf-8");
312+expect(canonical.startsWith("[")).toBe(false);
313+const lines = canonical.trim().split("\n");
314+expect(lines).toHaveLength(5);
315+expect(
316+lines.map((line) => {
317+const entry = JSON.parse(line) as PersistedCacheEntry;
318+return entry.node.sourceMessage.message_id;
319+}),
320+).toEqual([35033, 35034, 35035, 35036, 35037]);
321+} finally {
322+await rm(persistedPath, { force: true });
323+}
324+});
325+256326it("returns recent chat messages before the current message", () => {
257327const cache = createTelegramMessageCache();
258328for (const id of [41, 42, 43, 44]) {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。