perf(test): fold telegram update offset store into token suite

This commit is contained in:
Peter Steinberger
2026-02-16 00:40:54 +00:00
parent 65b5dbd6c1
commit 0e4eada580
2 changed files with 33 additions and 36 deletions

View File

@@ -1,14 +1,32 @@
import fs from "node:fs";
import fsPromises from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { resolveTelegramToken } from "./token.js";
import { readTelegramUpdateOffset, writeTelegramUpdateOffset } from "./update-offset-store.js";
function withTempDir(): string {
return fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-telegram-token-"));
}
async function withTempStateDir<T>(fn: (dir: string) => Promise<T>) {
const previous = process.env.OPENCLAW_STATE_DIR;
const dir = await fsPromises.mkdtemp(path.join(os.tmpdir(), "openclaw-telegram-"));
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 fsPromises.rm(dir, { recursive: true, force: true });
}
}
describe("resolveTelegramToken", () => {
afterEach(() => {
vi.unstubAllEnvs();
@@ -87,3 +105,18 @@ describe("resolveTelegramToken", () => {
expect(res.source).toBe("config");
});
});
describe("telegram update offset store", () => {
it("persists and reloads the last update id", async () => {
await withTempStateDir(async () => {
expect(await readTelegramUpdateOffset({ accountId: "primary" })).toBeNull();
await writeTelegramUpdateOffset({
accountId: "primary",
updateId: 421,
});
expect(await readTelegramUpdateOffset({ accountId: "primary" })).toBe(421);
});
});
});

View File

@@ -1,36 +0,0 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { 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-telegram-"));
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("telegram update offset store", () => {
it("persists and reloads the last update id", async () => {
await withTempStateDir(async () => {
expect(await readTelegramUpdateOffset({ accountId: "primary" })).toBeNull();
await writeTelegramUpdateOffset({
accountId: "primary",
updateId: 421,
});
expect(await readTelegramUpdateOffset({ accountId: "primary" })).toBe(421);
});
});
});