From caae4c91092dabc89d64001ad7aaf90ad019097a Mon Sep 17 00:00:00 2001 From: Shakker Date: Sat, 6 Jun 2026 22:10:40 +0100 Subject: [PATCH] test: manage update startup env --- src/infra/update-startup.test.ts | 58 +++++++++++++------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/src/infra/update-startup.test.ts b/src/infra/update-startup.test.ts index 7a36f2de0dd3..d02f6b54ee01 100644 --- a/src/infra/update-startup.test.ts +++ b/src/infra/update-startup.test.ts @@ -1,10 +1,12 @@ // Covers startup update check and auto-update behavior. import fs from "node:fs/promises"; import path from "node:path"; -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { formatCliCommand } from "../cli/command-format.js"; -import { createSuiteTempRootTracker } from "../test-helpers/temp-dir.js"; -import { captureEnv } from "../test-utils/env.js"; +import { + createOpenClawTestState, + type OpenClawTestState, +} from "../test-utils/openclaw-test-state.js"; import type { UpdateCheckResult } from "./update-check.js"; const { @@ -78,9 +80,8 @@ vi.mock("./update-managed-service-handoff.js", () => ({ })); describe("update-startup", () => { - const suiteRootTracker = createSuiteTempRootTracker({ prefix: "openclaw-update-check-suite-" }); let tempDir: string; - let envSnapshot: ReturnType; + let testState: OpenClawTestState; let resolveOpenClawPackageRoot: (typeof import("./openclaw-root.js"))["resolveOpenClawPackageRoot"]; let checkUpdateStatus: (typeof import("./update-check.js"))["checkUpdateStatus"]; @@ -100,33 +101,26 @@ describe("update-startup", () => { return call; } - beforeAll(async () => { - await suiteRootTracker.setup(); - }); - beforeEach(async () => { vi.useFakeTimers(); vi.setSystemTime(new Date("2026-01-17T10:00:00Z")); - tempDir = await suiteRootTracker.make("case"); - envSnapshot = captureEnv([ - "OPENCLAW_NO_AUTO_UPDATE", - "OPENCLAW_STATE_DIR", - "OPENCLAW_SERVICE_KIND", - "OPENCLAW_SERVICE_MARKER", - "OPENCLAW_GATEWAY_SERVICE_PID", - "OPENCLAW_LAUNCHD_LABEL", - "OPENCLAW_SYSTEMD_UNIT", - "OPENCLAW_WINDOWS_TASK_NAME", - "INVOCATION_ID", - "NODE_ENV", - "VITEST", - ]); - process.env.OPENCLAW_STATE_DIR = tempDir; - - process.env.NODE_ENV = "test"; - - // Ensure update checks don't short-circuit in test mode. - delete process.env.VITEST; + testState = await createOpenClawTestState({ + layout: "state-only", + prefix: "openclaw-update-check-suite-", + env: { + OPENCLAW_NO_AUTO_UPDATE: undefined, + OPENCLAW_SERVICE_KIND: undefined, + OPENCLAW_SERVICE_MARKER: undefined, + OPENCLAW_GATEWAY_SERVICE_PID: undefined, + OPENCLAW_LAUNCHD_LABEL: undefined, + OPENCLAW_SYSTEMD_UNIT: undefined, + OPENCLAW_WINDOWS_TASK_NAME: undefined, + INVOCATION_ID: undefined, + NODE_ENV: "test", + VITEST: undefined, + }, + }); + tempDir = testState.stateDir; // Perf: load mocked modules once (after timers/env are set up). if (!loaded) { @@ -160,14 +154,10 @@ describe("update-startup", () => { afterEach(async () => { vi.useRealTimers(); - envSnapshot.restore(); + await testState.cleanup(); resetUpdateAvailableStateForTest(); }); - afterAll(async () => { - await suiteRootTracker.cleanup(); - }); - function mockPackageUpdateStatus(tag = "latest", version = "2.0.0") { mockPackageInstallStatus(); mockNpmChannelTag(tag, version);