From 3cfb402bdad60f6e7caa94531555ecb498974164 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 13:08:05 +0000 Subject: [PATCH] refactor(test): reuse state-dir helper in agent runner suite --- .../reply/agent-runner.runreplyagent.test.ts | 43 +++---------------- 1 file changed, 5 insertions(+), 38 deletions(-) diff --git a/src/auto-reply/reply/agent-runner.runreplyagent.test.ts b/src/auto-reply/reply/agent-runner.runreplyagent.test.ts index f7fd979c1..cc43bdc07 100644 --- a/src/auto-reply/reply/agent-runner.runreplyagent.test.ts +++ b/src/auto-reply/reply/agent-runner.runreplyagent.test.ts @@ -5,6 +5,7 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vites import type { SessionEntry } from "../../config/sessions.js"; import * as sessions from "../../config/sessions.js"; import type { TypingMode } from "../../config/types.js"; +import { withStateDirEnv } from "../../test-helpers/state-dir-env.js"; import type { TemplateContext } from "../templating.js"; import type { GetReplyOptions } from "../types.js"; import type { FollowupRun, QueueSettings } from "./queue.js"; @@ -269,35 +270,11 @@ async function runReplyAgentWithBase(params: { } describe("runReplyAgent typing (heartbeat)", () => { - let fixtureRoot = ""; - let caseId = 0; - - type StateEnvSnapshot = { - OPENCLAW_STATE_DIR: string | undefined; - }; - - function snapshotStateEnv(): StateEnvSnapshot { - return { OPENCLAW_STATE_DIR: process.env.OPENCLAW_STATE_DIR }; - } - - function restoreStateEnv(snapshot: StateEnvSnapshot) { - if (snapshot.OPENCLAW_STATE_DIR === undefined) { - delete process.env.OPENCLAW_STATE_DIR; - } else { - process.env.OPENCLAW_STATE_DIR = snapshot.OPENCLAW_STATE_DIR; - } - } - async function withTempStateDir(fn: (stateDir: string) => Promise): Promise { - const stateDir = path.join(fixtureRoot, `case-${++caseId}`); - await fs.mkdir(stateDir, { recursive: true }); - const envSnapshot = snapshotStateEnv(); - process.env.OPENCLAW_STATE_DIR = stateDir; - try { - return await fn(stateDir); - } finally { - restoreStateEnv(envSnapshot); - } + return await withStateDirEnv( + "openclaw-typing-heartbeat-", + async ({ stateDir }) => await fn(stateDir), + ); } async function writeCorruptGeminiSessionFixture(params: { @@ -321,16 +298,6 @@ describe("runReplyAgent typing (heartbeat)", () => { return { storePath, sessionEntry, sessionStore, transcriptPath }; } - beforeAll(async () => { - fixtureRoot = await fs.mkdtemp(path.join(tmpdir(), "openclaw-typing-heartbeat-")); - }); - - afterAll(async () => { - if (fixtureRoot) { - await fs.rm(fixtureRoot, { recursive: true, force: true }); - } - }); - it("signals typing for normal runs", async () => { const onPartialReply = vi.fn(); state.runEmbeddedPiAgentMock.mockImplementationOnce(async (params: AgentRunParams) => {