@@ -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 { RuntimeEnv } from "../runtime.js" ;
7+ import { captureEnv , setTestEnvValue } from "../test-utils/env.js" ;
78import { createCrestodianTestRuntime } from "./crestodian.test-helpers.js" ;
89import { executeCrestodianOperation , parseCrestodianOperation } from "./operations.js" ;
910
@@ -192,12 +193,16 @@ vi.mock("../config/model-input.js", () => ({
192193} ) ) ;
193194
194195describe ( "parseCrestodianOperation" , ( ) => {
196+ let stateDirSnapshot : ReturnType < typeof captureEnv > | undefined ;
197+
195198 beforeEach ( ( ) => {
196199 mockConfig . reset ( ) ;
200+ stateDirSnapshot = captureEnv ( [ "OPENCLAW_STATE_DIR" ] ) ;
197201 vi . stubEnv ( "OPENCLAW_TEST_FAST" , "1" ) ;
198202 } ) ;
199203
200204 afterEach ( ( ) => {
205+ stateDirSnapshot ?. restore ( ) ;
201206 vi . unstubAllEnvs ( ) ;
202207 } ) ;
203208
@@ -321,7 +326,7 @@ describe("parseCrestodianOperation", () => {
321326
322327 it ( "applies config set through typed deps and writes an audit entry" , async ( ) => {
323328 const tempDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "crestodian-config-set-" ) ) ;
324- vi . stubEnv ( "OPENCLAW_STATE_DIR" , tempDir ) ;
329+ setTestEnvValue ( "OPENCLAW_STATE_DIR" , tempDir ) ;
325330 const { runtime, lines } = createCrestodianTestRuntime ( ) ;
326331 const runConfigSet = vi . fn ( async ( ) => { } ) ;
327332
@@ -357,7 +362,7 @@ describe("parseCrestodianOperation", () => {
357362
358363 it ( "applies SecretRef config set through typed deps and writes an audit entry" , async ( ) => {
359364 const tempDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "crestodian-config-ref-" ) ) ;
360- vi . stubEnv ( "OPENCLAW_STATE_DIR" , tempDir ) ;
365+ setTestEnvValue ( "OPENCLAW_STATE_DIR" , tempDir ) ;
361366 const { runtime, lines } = createCrestodianTestRuntime ( ) ;
362367 const runConfigSet = vi . fn ( async ( ) => { } ) ;
363368
@@ -435,7 +440,7 @@ describe("parseCrestodianOperation", () => {
435440
436441 it ( "installs plugins only after approval and audits the write" , async ( ) => {
437442 const tempDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "crestodian-plugin-install-" ) ) ;
438- vi . stubEnv ( "OPENCLAW_STATE_DIR" , tempDir ) ;
443+ setTestEnvValue ( "OPENCLAW_STATE_DIR" , tempDir ) ;
439444 const { runtime, lines } = createCrestodianTestRuntime ( ) ;
440445 const runPluginInstall = vi . fn ( async ( spec : string , pluginRuntime : RuntimeEnv ) => {
441446 pluginRuntime . log ( `installed ${ spec } ` ) ;
@@ -481,7 +486,7 @@ describe("parseCrestodianOperation", () => {
481486
482487 it ( "uninstalls plugins only after approval and audits the write" , async ( ) => {
483488 const tempDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "crestodian-plugin-uninstall-" ) ) ;
484- vi . stubEnv ( "OPENCLAW_STATE_DIR" , tempDir ) ;
489+ setTestEnvValue ( "OPENCLAW_STATE_DIR" , tempDir ) ;
485490 const { runtime, lines } = createCrestodianTestRuntime ( ) ;
486491 const runPluginUninstall = vi . fn ( async ( pluginId : string , pluginRuntime : RuntimeEnv ) => {
487492 pluginRuntime . log ( `uninstalled ${ pluginId } ` ) ;
@@ -527,7 +532,7 @@ describe("parseCrestodianOperation", () => {
527532
528533 it ( "runs setup bootstrap only after approval and audits it" , async ( ) => {
529534 const tempDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "crestodian-setup-" ) ) ;
530- vi . stubEnv ( "OPENCLAW_STATE_DIR" , tempDir ) ;
535+ setTestEnvValue ( "OPENCLAW_STATE_DIR" , tempDir ) ;
531536 vi . stubEnv ( "OPENAI_API_KEY" , "test-key" ) ;
532537 const { runtime, lines } = createCrestodianTestRuntime ( ) ;
533538
@@ -576,7 +581,7 @@ describe("parseCrestodianOperation", () => {
576581
577582 it ( "runs doctor repairs only after approval and audits them" , async ( ) => {
578583 const tempDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "crestodian-doctor-fix-" ) ) ;
579- vi . stubEnv ( "OPENCLAW_STATE_DIR" , tempDir ) ;
584+ setTestEnvValue ( "OPENCLAW_STATE_DIR" , tempDir ) ;
580585 const { runtime, lines } = createCrestodianTestRuntime ( ) ;
581586 const runDoctor = vi . fn ( async ( ) => { } ) ;
582587
0 commit comments