diff --git a/src/infra/heartbeat-runner.model-override.test.ts b/src/infra/heartbeat-runner.model-override.test.ts index fd5aa40fd..3897a2473 100644 --- a/src/infra/heartbeat-runner.model-override.test.ts +++ b/src/infra/heartbeat-runner.model-override.test.ts @@ -1,6 +1,3 @@ -import fs from "node:fs/promises"; -import os from "node:os"; -import path from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { telegramPlugin } from "../../extensions/telegram/src/channel.js"; import { setTelegramRuntime } from "../../extensions/telegram/src/runtime.js"; @@ -13,6 +10,7 @@ import { setActivePluginRegistry } from "../plugins/runtime.js"; import { createPluginRuntime } from "../plugins/runtime/index.js"; import { createTestRegistry } from "../test-utils/channel-plugins.js"; import { runHeartbeatOnce } from "./heartbeat-runner.js"; +import { seedSessionStore, withTempHeartbeatSandbox } from "./heartbeat-runner.test-utils.js"; // Avoid pulling optional runtime deps during isolated runs. vi.mock("jiti", () => ({ createJiti: () => () => ({}) })); @@ -30,34 +28,20 @@ async function withHeartbeatFixture( seedSession: (sessionKey: string, input: SeedSessionInput) => Promise; }) => Promise, ): Promise { - const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hb-model-")); - const storePath = path.join(tmpDir, "sessions.json"); - - const seedSession = async (sessionKey: string, input: SeedSessionInput) => { - await fs.writeFile( - storePath, - JSON.stringify( - { - [sessionKey]: { - sessionId: "sid", - updatedAt: input.updatedAt ?? Date.now(), - lastChannel: input.lastChannel, - lastTo: input.lastTo, - }, - }, - null, - 2, - ), - ); - }; - - await fs.writeFile(path.join(tmpDir, "HEARTBEAT.md"), "- Check status\n", "utf-8"); - - try { - return await run({ tmpDir, storePath, seedSession }); - } finally { - await fs.rm(tmpDir, { recursive: true, force: true }); - } + return withTempHeartbeatSandbox( + async ({ tmpDir, storePath }) => { + const seedSession = async (sessionKey: string, input: SeedSessionInput) => { + await seedSessionStore(storePath, sessionKey, { + updatedAt: input.updatedAt, + lastChannel: input.lastChannel, + lastProvider: input.lastChannel, + lastTo: input.lastTo, + }); + }; + return run({ tmpDir, storePath, seedSession }); + }, + { prefix: "openclaw-hb-model-" }, + ); } beforeEach(() => {