@@ -6,6 +6,12 @@ import { getCredentialBackupFile, getLegacyCredentialBackupFile } from "./data-p
66
77const createdStateDirs : string [ ] = [ ] ;
88
9+ function createTempDir ( prefix : string ) : string {
10+ const dir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , prefix ) ) ;
11+ createdStateDirs . push ( dir ) ;
12+ return dir ;
13+ }
14+
915describe ( "qqbot credential backup paths" , ( ) => {
1016 afterEach ( ( ) => {
1117 vi . unstubAllEnvs ( ) ;
@@ -15,8 +21,7 @@ describe("qqbot credential backup paths", () => {
1521 } ) ;
1622
1723 it ( "scopes credential backups to the active OPENCLAW_STATE_DIR" , ( ) => {
18- const stateDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "qqbot-state-" ) ) ;
19- createdStateDirs . push ( stateDir ) ;
24+ const stateDir = createTempDir ( "qqbot-state-" ) ;
2025 vi . stubEnv ( "OPENCLAW_STATE_DIR" , stateDir ) ;
2126
2227 expect ( getCredentialBackupFile ( "default" ) ) . toBe (
@@ -28,9 +33,8 @@ describe("qqbot credential backup paths", () => {
2833 } ) ;
2934
3035 it ( "keeps same account IDs isolated across different state directories" , ( ) => {
31- const stateDirA = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "qqbot-state-a-" ) ) ;
32- const stateDirB = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "qqbot-state-b-" ) ) ;
33- createdStateDirs . push ( stateDirA , stateDirB ) ;
36+ const stateDirA = createTempDir ( "qqbot-state-a-" ) ;
37+ const stateDirB = createTempDir ( "qqbot-state-b-" ) ;
3438
3539 vi . stubEnv ( "OPENCLAW_STATE_DIR" , stateDirA ) ;
3640 const gatewayAPath = getCredentialBackupFile ( "default" ) ;
@@ -46,4 +50,25 @@ describe("qqbot credential backup paths", () => {
4650 ) ;
4751 expect ( gatewayBPath ) . not . toBe ( gatewayAPath ) ;
4852 } ) ;
53+
54+ it ( "uses OPENCLAW_HOME for default credential backup state" , ( ) => {
55+ const homeDir = createTempDir ( "qqbot-openclaw-home-" ) ;
56+ vi . stubEnv ( "OPENCLAW_STATE_DIR" , "" ) ;
57+ vi . stubEnv ( "OPENCLAW_HOME" , homeDir ) ;
58+
59+ expect ( getCredentialBackupFile ( "default" ) ) . toBe (
60+ path . join ( homeDir , ".openclaw" , "qqbot" , "data" , "credential-backup-default.json" ) ,
61+ ) ;
62+ } ) ;
63+
64+ it ( "expands tilde state-dir overrides through the canonical state resolver" , ( ) => {
65+ const homeDir = createTempDir ( "qqbot-home-" ) ;
66+ vi . stubEnv ( "HOME" , homeDir ) ;
67+ vi . stubEnv ( "OPENCLAW_HOME" , "" ) ;
68+ vi . stubEnv ( "OPENCLAW_STATE_DIR" , "~/gateway-a" ) ;
69+
70+ expect ( getCredentialBackupFile ( "default" ) ) . toBe (
71+ path . join ( homeDir , "gateway-a" , "qqbot" , "data" , "credential-backup-default.json" ) ,
72+ ) ;
73+ } ) ;
4974} ) ;
0 commit comments