mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 22:14:34 +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 fs from "node:fs";
|
||||||
import fsPromises from "node:fs/promises";
|
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
|
import { withStateDirEnv } from "../test-helpers/state-dir-env.js";
|
||||||
import { resolveTelegramToken } from "./token.js";
|
import { resolveTelegramToken } from "./token.js";
|
||||||
import { readTelegramUpdateOffset, writeTelegramUpdateOffset } from "./update-offset-store.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-"));
|
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", () => {
|
describe("resolveTelegramToken", () => {
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
vi.unstubAllEnvs();
|
vi.unstubAllEnvs();
|
||||||
@@ -108,7 +92,7 @@ describe("resolveTelegramToken", () => {
|
|||||||
|
|
||||||
describe("telegram update offset store", () => {
|
describe("telegram update offset store", () => {
|
||||||
it("persists and reloads the last update id", async () => {
|
it("persists and reloads the last update id", async () => {
|
||||||
await withTempStateDir(async () => {
|
await withStateDirEnv("openclaw-telegram-", async () => {
|
||||||
expect(await readTelegramUpdateOffset({ accountId: "primary" })).toBeNull();
|
expect(await readTelegramUpdateOffset({ accountId: "primary" })).toBeNull();
|
||||||
|
|
||||||
await writeTelegramUpdateOffset({
|
await writeTelegramUpdateOffset({
|
||||||
|
|||||||
@@ -1,32 +1,14 @@
|
|||||||
import fs from "node:fs/promises";
|
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { withStateDirEnv } from "../test-helpers/state-dir-env.js";
|
||||||
import {
|
import {
|
||||||
deleteTelegramUpdateOffset,
|
deleteTelegramUpdateOffset,
|
||||||
readTelegramUpdateOffset,
|
readTelegramUpdateOffset,
|
||||||
writeTelegramUpdateOffset,
|
writeTelegramUpdateOffset,
|
||||||
} from "./update-offset-store.js";
|
} 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", () => {
|
describe("deleteTelegramUpdateOffset", () => {
|
||||||
it("removes the offset file so a new bot starts fresh", async () => {
|
it("removes the offset file so a new bot starts fresh", async () => {
|
||||||
await withTempStateDir(async () => {
|
await withStateDirEnv("openclaw-tg-offset-", async () => {
|
||||||
await writeTelegramUpdateOffset({ accountId: "default", updateId: 432_000_000 });
|
await writeTelegramUpdateOffset({ accountId: "default", updateId: 432_000_000 });
|
||||||
expect(await readTelegramUpdateOffset({ accountId: "default" })).toBe(432_000_000);
|
expect(await readTelegramUpdateOffset({ accountId: "default" })).toBe(432_000_000);
|
||||||
|
|
||||||
@@ -36,13 +18,13 @@ describe("deleteTelegramUpdateOffset", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("does not throw when the offset file does not exist", async () => {
|
it("does not throw when the offset file does not exist", async () => {
|
||||||
await withTempStateDir(async () => {
|
await withStateDirEnv("openclaw-tg-offset-", async () => {
|
||||||
await expect(deleteTelegramUpdateOffset({ accountId: "nonexistent" })).resolves.not.toThrow();
|
await expect(deleteTelegramUpdateOffset({ accountId: "nonexistent" })).resolves.not.toThrow();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("only removes the targeted account offset, leaving others intact", async () => {
|
it("only removes the targeted account offset, leaving others intact", async () => {
|
||||||
await withTempStateDir(async () => {
|
await withStateDirEnv("openclaw-tg-offset-", async () => {
|
||||||
await writeTelegramUpdateOffset({ accountId: "default", updateId: 100 });
|
await writeTelegramUpdateOffset({ accountId: "default", updateId: 100 });
|
||||||
await writeTelegramUpdateOffset({ accountId: "alerts", updateId: 200 });
|
await writeTelegramUpdateOffset({ accountId: "alerts", updateId: 200 });
|
||||||
|
|
||||||
|
|||||||
@@ -42,4 +42,24 @@ describe("state-dir-env helpers", () => {
|
|||||||
await expect(fs.stat(capturedStateDir)).rejects.toThrow();
|
await expect(fs.stat(capturedStateDir)).rejects.toThrow();
|
||||||
await expect(fs.stat(capturedTempRoot)).rejects.toThrow();
|
await expect(fs.stat(capturedTempRoot)).rejects.toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("withStateDirEnv restores env and cleans temp root when callback throws", async () => {
|
||||||
|
const prevOpenClaw = process.env.OPENCLAW_STATE_DIR;
|
||||||
|
const prevLegacy = process.env.CLAWDBOT_STATE_DIR;
|
||||||
|
|
||||||
|
let capturedTempRoot = "";
|
||||||
|
let capturedStateDir = "";
|
||||||
|
await expect(
|
||||||
|
withStateDirEnv("openclaw-state-dir-env-", async ({ tempRoot, stateDir }) => {
|
||||||
|
capturedTempRoot = tempRoot;
|
||||||
|
capturedStateDir = stateDir;
|
||||||
|
throw new Error("boom");
|
||||||
|
}),
|
||||||
|
).rejects.toThrow("boom");
|
||||||
|
|
||||||
|
expect(process.env.OPENCLAW_STATE_DIR).toBe(prevOpenClaw);
|
||||||
|
expect(process.env.CLAWDBOT_STATE_DIR).toBe(prevLegacy);
|
||||||
|
await expect(fs.stat(capturedStateDir)).rejects.toThrow();
|
||||||
|
await expect(fs.stat(capturedTempRoot)).rejects.toThrow();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user