11// Covers OpenClaw's default fs-safe native helper configuration.
22import { afterEach , describe , expect , it , vi } from "vitest" ;
3+ import { withEnvAsync } from "../test-utils/env.js" ;
34
45const { configureFsSafeNative } = vi . hoisted ( ( ) => ( {
56 configureFsSafeNative : vi . fn ( ) ,
@@ -9,23 +10,28 @@ vi.mock("@openclaw/fs-safe/config", () => ({
910 configureFsSafeNative,
1011} ) ) ;
1112
12- async function importDefaults ( ) {
13+ async function importDefaults ( env : Record < string , string | undefined > = { } ) {
1314 vi . resetModules ( ) ;
14- await import ( "./fs-safe-defaults.js" ) ;
15+ await withEnvAsync (
16+ {
17+ FS_SAFE_NATIVE_MODE : undefined ,
18+ OPENCLAW_FS_SAFE_NATIVE_MODE : undefined ,
19+ openclaw_fs_safe_native_mode : undefined ,
20+ FS_SAFE_PYTHON_MODE : undefined ,
21+ OPENCLAW_FS_SAFE_PYTHON_MODE : undefined ,
22+ FS_SAFE_PYTHON : undefined ,
23+ OPENCLAW_FS_SAFE_PYTHON : undefined ,
24+ OPENCLAW_PINNED_PYTHON : undefined ,
25+ OPENCLAW_PINNED_WRITE_PYTHON : undefined ,
26+ } ,
27+ // Apply overrides after clearing aliases; Windows env names are case-insensitive.
28+ ( ) => withEnvAsync ( env , ( ) => import ( "./fs-safe-defaults.js" ) ) ,
29+ ) ;
1530}
1631
1732describe ( "fs-safe defaults" , ( ) => {
1833 afterEach ( ( ) => {
1934 configureFsSafeNative . mockReset ( ) ;
20- delete process . env . FS_SAFE_NATIVE_MODE ;
21- delete process . env . OPENCLAW_FS_SAFE_NATIVE_MODE ;
22- delete process . env . openclaw_fs_safe_native_mode ;
23- delete process . env . FS_SAFE_PYTHON_MODE ;
24- delete process . env . OPENCLAW_FS_SAFE_PYTHON_MODE ;
25- delete process . env . FS_SAFE_PYTHON ;
26- delete process . env . OPENCLAW_FS_SAFE_PYTHON ;
27- delete process . env . OPENCLAW_PINNED_PYTHON ;
28- delete process . env . OPENCLAW_PINNED_WRITE_PYTHON ;
2935 } ) ;
3036
3137 it ( "disables the native helper by default in OpenClaw" , async ( ) => {
@@ -35,42 +41,32 @@ describe("fs-safe defaults", () => {
3541 } ) ;
3642
3743 it ( "lets fs-safe env mode overrides opt back into the helper" , async ( ) => {
38- process . env . FS_SAFE_NATIVE_MODE = "require" ;
39-
40- await importDefaults ( ) ;
44+ await importDefaults ( { FS_SAFE_NATIVE_MODE : "require" } ) ;
4145
4246 expect ( configureFsSafeNative ) . not . toHaveBeenCalled ( ) ;
4347 } ) ;
4448
4549 it ( "honors the OpenClaw-specific env mode override" , async ( ) => {
46- process . env . OPENCLAW_FS_SAFE_NATIVE_MODE = "auto" ;
47-
48- await importDefaults ( ) ;
50+ await importDefaults ( { OPENCLAW_FS_SAFE_NATIVE_MODE : "auto" } ) ;
4951
5052 expect ( configureFsSafeNative ) . not . toHaveBeenCalled ( ) ;
5153 } ) ;
5254
5355 it ( "honors case-insensitive mode overrides on Windows" , async ( ) => {
5456 vi . spyOn ( process , "platform" , "get" ) . mockReturnValue ( "win32" ) ;
55- process . env . openclaw_fs_safe_native_mode = "require" ;
56-
57- await importDefaults ( ) ;
57+ await importDefaults ( { openclaw_fs_safe_native_mode : "require" } ) ;
5858
5959 expect ( configureFsSafeNative ) . not . toHaveBeenCalled ( ) ;
6060 } ) ;
6161
6262 it ( "lets fs-safe migrate legacy require mode without overriding it" , async ( ) => {
63- process . env . OPENCLAW_FS_SAFE_PYTHON_MODE = "require" ;
64-
65- await importDefaults ( ) ;
63+ await importDefaults ( { OPENCLAW_FS_SAFE_PYTHON_MODE : "require" } ) ;
6664
6765 expect ( configureFsSafeNative ) . not . toHaveBeenCalled ( ) ;
6866 } ) ;
6967
7068 it ( "does not treat a retired interpreter path as a native mode override" , async ( ) => {
71- process . env . OPENCLAW_FS_SAFE_PYTHON = "/usr/bin/python3" ;
72-
73- await importDefaults ( ) ;
69+ await importDefaults ( { OPENCLAW_FS_SAFE_PYTHON : "/usr/bin/python3" } ) ;
7470
7571 expect ( configureFsSafeNative ) . toHaveBeenCalledWith ( { mode : "off" } ) ;
7672 } ) ;
0 commit comments