





















@@ -4,6 +4,7 @@ import os from "node:os";
44import path from "node:path";
55import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
66import type { OpenClawConfig } from "../config/types.openclaw.js";
7+import { captureEnv, deleteTestEnvValue, setTestEnvValue } from "../test-utils/env.js";
78import {
89clearCurrentPluginMetadataSnapshot,
910resolvePluginMetadataControlPlaneFingerprint,
@@ -19,56 +20,16 @@ import type { PluginMetadataSnapshot } from "./plugin-metadata-snapshot.js";
1920import { createEmptyPluginRegistry } from "./registry-empty.js";
2021import { resetPluginRuntimeStateForTest, setActivePluginRegistry } from "./runtime.js";
212222-const ORIGINAL_ENV = {
23-OPENCLAW_STATE_DIR: process.env.OPENCLAW_STATE_DIR,
24-OPENCLAW_HOME: process.env.OPENCLAW_HOME,
25-OPENCLAW_DISABLE_BUNDLED_PLUGINS: process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS,
26-OPENCLAW_BUNDLED_PLUGINS_DIR: process.env.OPENCLAW_BUNDLED_PLUGINS_DIR,
27-} as const;
28-2923const tempDirs: string[] = [];
30-31-function restoreOpenClawStateDirEnv(): void {
32-const value = ORIGINAL_ENV.OPENCLAW_STATE_DIR;
33-if (value === undefined) {
34-delete process.env.OPENCLAW_STATE_DIR;
35-} else {
36-process.env.OPENCLAW_STATE_DIR = value;
37-}
38-}
39-40-function restoreOpenClawHomeEnv(): void {
41-const value = ORIGINAL_ENV.OPENCLAW_HOME;
42-if (value === undefined) {
43-delete process.env.OPENCLAW_HOME;
44-} else {
45-process.env.OPENCLAW_HOME = value;
46-}
47-}
48-49-function restoreOpenClawDisableBundledPluginsEnv(): void {
50-const value = ORIGINAL_ENV.OPENCLAW_DISABLE_BUNDLED_PLUGINS;
51-if (value === undefined) {
52-delete process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS;
53-} else {
54-process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS = value;
55-}
56-}
57-58-function restoreOpenClawBundledPluginsDirEnv(): void {
59-const value = ORIGINAL_ENV.OPENCLAW_BUNDLED_PLUGINS_DIR;
60-if (value === undefined) {
61-delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR;
62-} else {
63-process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = value;
64-}
65-}
24+const testEnvSnapshot = captureEnv([
25+"OPENCLAW_STATE_DIR",
26+"OPENCLAW_HOME",
27+"OPENCLAW_DISABLE_BUNDLED_PLUGINS",
28+"OPENCLAW_BUNDLED_PLUGINS_DIR",
29+]);
66306731function restoreEnv(): void {
68-restoreOpenClawStateDirEnv();
69-restoreOpenClawHomeEnv();
70-restoreOpenClawDisableBundledPluginsEnv();
71-restoreOpenClawBundledPluginsDirEnv();
32+testEnvSnapshot.restore();
7233}
73347435function makeTempDir(): string {
@@ -343,10 +304,10 @@ describe("manifest model id normalization", () => {
343304writeInstallIndex({ stateDir: stateDirA, pluginDir: pluginDirA });
344305writeNormalizerManifest({ pluginDir: pluginDirA, prefix: "alpha" });
345306346-process.env.OPENCLAW_STATE_DIR = stateDirA;
347-process.env.OPENCLAW_HOME = undefined;
348-process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS = "1";
349-process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = undefined;
307+setTestEnvValue("OPENCLAW_STATE_DIR", stateDirA);
308+deleteTestEnvValue("OPENCLAW_HOME");
309+setTestEnvValue("OPENCLAW_DISABLE_BUNDLED_PLUGINS", "1");
310+deleteTestEnvValue("OPENCLAW_BUNDLED_PLUGINS_DIR");
350311351312expect(normalizeDemoModel()).toBe("alpha/demo-model");
352313@@ -358,7 +319,7 @@ describe("manifest model id normalization", () => {
358319writeInstallIndex({ stateDir: stateDirB, pluginDir: pluginDirB });
359320writeNormalizerManifest({ pluginDir: pluginDirB, prefix: "charlie" });
360321361-process.env.OPENCLAW_STATE_DIR = stateDirB;
322+setTestEnvValue("OPENCLAW_STATE_DIR", stateDirB);
362323clearPluginMetadataLifecycleCaches();
363324expect(normalizeDemoModel()).toBe("charlie/demo-model");
364325});
@@ -370,10 +331,10 @@ describe("manifest model id normalization", () => {
370331writeInstallIndex({ stateDir, pluginDir });
371332writeNormalizerManifest({ pluginDir, prefix: "alpha" });
372333373-process.env.OPENCLAW_STATE_DIR = stateDir;
374-process.env.OPENCLAW_HOME = undefined;
375-process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS = "1";
376-process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = undefined;
334+setTestEnvValue("OPENCLAW_STATE_DIR", stateDir);
335+deleteTestEnvValue("OPENCLAW_HOME");
336+setTestEnvValue("OPENCLAW_DISABLE_BUNDLED_PLUGINS", "1");
337+deleteTestEnvValue("OPENCLAW_BUNDLED_PLUGINS_DIR");
377338378339const readFileSyncSpy = vi.spyOn(fs, "readFileSync");
379340此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。