@@ -77,3 +77,70 @@ func TestConfigNetworkSettingsDefaultsAndValidation(t *testing.T) {
7777 t .Fatal ("Validate() error = nil, want non-nil for negative retry count" )
7878 }
7979}
80+
81+ func TestDefaultPathUsesCloudCanalCLIDirectory (t * testing.T ) {
82+ home := t .TempDir ()
83+ t .Setenv ("HOME" , home )
84+
85+ got := config .DefaultPath ()
86+ want := filepath .Join (home , ".cloudcanal-cli" , "config.json" )
87+ if got != want {
88+ t .Fatalf ("DefaultPath() = %q, want %q" , got , want )
89+ }
90+ }
91+
92+ func TestServiceLoadMigratesLegacyDefaultConfig (t * testing.T ) {
93+ home := t .TempDir ()
94+ t .Setenv ("HOME" , home )
95+
96+ legacyPath := config .LegacyDefaultPath ()
97+ if err := os .MkdirAll (filepath .Dir (legacyPath ), 0o755 ); err != nil {
98+ t .Fatalf ("MkdirAll() error = %v" , err )
99+ }
100+ content := []byte (`{"apiBaseUrl":"https://cc.example.com","accessKey":"legacy-ak","secretKey":"legacy-sk","language":"zh"}` )
101+ if err := os .WriteFile (legacyPath , content , 0o600 ); err != nil {
102+ t .Fatalf ("WriteFile() error = %v" , err )
103+ }
104+
105+ service := config .NewService ("" )
106+ if ! service .Exists () {
107+ t .Fatal ("Exists() = false, want true when only legacy config exists" )
108+ }
109+
110+ loaded , err := service .Load ()
111+ if err != nil {
112+ t .Fatalf ("Load() error = %v" , err )
113+ }
114+ if loaded .APIBaseURL != "https://cc.example.com" || loaded .AccessKey != "legacy-ak" || loaded .SecretKey != "legacy-sk" || loaded .Language != "zh" {
115+ t .Fatalf ("loaded config = %+v, want legacy values" , loaded )
116+ }
117+
118+ newPath := config .DefaultPath ()
119+ if _ , err := os .Stat (newPath ); err != nil {
120+ t .Fatalf ("new config path stat error = %v" , err )
121+ }
122+ if _ , err := os .Stat (legacyPath ); ! os .IsNotExist (err ) {
123+ t .Fatalf ("legacy config still exists, stat err = %v" , err )
124+ }
125+ }
126+
127+ func TestServiceLoadWithCustomPathDoesNotReadLegacyConfig (t * testing.T ) {
128+ home := t .TempDir ()
129+ t .Setenv ("HOME" , home )
130+
131+ legacyPath := config .LegacyDefaultPath ()
132+ if err := os .MkdirAll (filepath .Dir (legacyPath ), 0o755 ); err != nil {
133+ t .Fatalf ("MkdirAll() error = %v" , err )
134+ }
135+ if err := os .WriteFile (legacyPath , []byte (`{"apiBaseUrl":"https://cc.example.com","accessKey":"legacy-ak","secretKey":"legacy-sk"}` ), 0o600 ); err != nil {
136+ t .Fatalf ("WriteFile() error = %v" , err )
137+ }
138+
139+ service := config .NewService (filepath .Join (t .TempDir (), "config.json" ))
140+ if service .Exists () {
141+ t .Fatal ("Exists() = true, want false for custom path without file" )
142+ }
143+ if _ , err := service .Load (); ! os .IsNotExist (err ) {
144+ t .Fatalf ("Load() error = %v, want os.ErrNotExist" , err )
145+ }
146+ }
0 commit comments