mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 16:14:58 +00:00
refactor(test): reuse state-dir helper in telegram tests
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
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 { withStateDirEnv } from "../test-helpers/state-dir-env.js";
|
||||
import { resolveTelegramToken } from "./token.js";
|
||||
import { readTelegramUpdateOffset, writeTelegramUpdateOffset } from "./update-offset-store.js";
|
||||
|
||||
@@ -11,22 +11,6 @@ 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();
|
||||
@@ -108,7 +92,7 @@ describe("resolveTelegramToken", () => {
|
||||
|
||||
describe("telegram update offset store", () => {
|
||||
it("persists and reloads the last update id", async () => {
|
||||
await withTempStateDir(async () => {
|
||||
await withStateDirEnv("openclaw-telegram-", async () => {
|
||||
expect(await readTelegramUpdateOffset({ accountId: "primary" })).toBeNull();
|
||||
|
||||
await writeTelegramUpdateOffset({
|
||||
|
||||
Reference in New Issue
Block a user