mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 11:31:23 +00:00
fix(telegram): clean up update offset on channels remove --delete (#18233)
This commit is contained in:
committed by
Peter Steinberger
parent
b91e43714b
commit
6757a9fedc
55
src/telegram/update-offset-store.test.ts
Normal file
55
src/telegram/update-offset-store.test.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
deleteTelegramUpdateOffset,
|
||||
readTelegramUpdateOffset,
|
||||
writeTelegramUpdateOffset,
|
||||
} from "./update-offset-store.js";
|
||||
|
||||
async function withTempStateDir<T>(fn: (dir: string) => Promise<T>) {
|
||||
const previous = process.env.OPENCLAW_STATE_DIR;
|
||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-tg-offset-"));
|
||||
process.env.OPENCLAW_STATE_DIR = dir;
|
||||
try {
|
||||
return await fn(dir);
|
||||
} finally {
|
||||
if (previous === undefined) {
|
||||
delete process.env.OPENCLAW_STATE_DIR;
|
||||
} else {
|
||||
process.env.OPENCLAW_STATE_DIR = previous;
|
||||
}
|
||||
await fs.rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
describe("deleteTelegramUpdateOffset", () => {
|
||||
it("removes the offset file so a new bot starts fresh", async () => {
|
||||
await withTempStateDir(async () => {
|
||||
await writeTelegramUpdateOffset({ accountId: "default", updateId: 432_000_000 });
|
||||
expect(await readTelegramUpdateOffset({ accountId: "default" })).toBe(432_000_000);
|
||||
|
||||
await deleteTelegramUpdateOffset({ accountId: "default" });
|
||||
expect(await readTelegramUpdateOffset({ accountId: "default" })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it("does not throw when the offset file does not exist", async () => {
|
||||
await withTempStateDir(async () => {
|
||||
await expect(deleteTelegramUpdateOffset({ accountId: "nonexistent" })).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
it("only removes the targeted account offset, leaving others intact", async () => {
|
||||
await withTempStateDir(async () => {
|
||||
await writeTelegramUpdateOffset({ accountId: "default", updateId: 100 });
|
||||
await writeTelegramUpdateOffset({ accountId: "alerts", updateId: 200 });
|
||||
|
||||
await deleteTelegramUpdateOffset({ accountId: "default" });
|
||||
|
||||
expect(await readTelegramUpdateOffset({ accountId: "default" })).toBeNull();
|
||||
expect(await readTelegramUpdateOffset({ accountId: "alerts" })).toBe(200);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -80,3 +80,19 @@ export async function writeTelegramUpdateOffset(params: {
|
||||
await fs.chmod(tmp, 0o600);
|
||||
await fs.rename(tmp, filePath);
|
||||
}
|
||||
|
||||
export async function deleteTelegramUpdateOffset(params: {
|
||||
accountId?: string;
|
||||
env?: NodeJS.ProcessEnv;
|
||||
}): Promise<void> {
|
||||
const filePath = resolveTelegramUpdateOffsetPath(params.accountId, params.env);
|
||||
try {
|
||||
await fs.unlink(filePath);
|
||||
} catch (err) {
|
||||
const code = (err as { code?: string }).code;
|
||||
if (code === "ENOENT") {
|
||||
return;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user