1+ /*
2+ * Copyright 2025 Han Li and contributors
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package commands
18+
19+ import (
20+ "testing"
21+
22+ "github.com/version-fox/vfox/internal/base"
23+ )
24+
25+ func TestUnuseScopeSelection (t * testing.T ) {
26+ // Test that scope selection logic works correctly
27+ tests := []struct {
28+ name string
29+ globalSet bool
30+ projectSet bool
31+ sessionSet bool
32+ expectedScope base.UseScope
33+ }{
34+ {
35+ name : "Default to session scope" ,
36+ globalSet : false ,
37+ projectSet : false ,
38+ sessionSet : false ,
39+ expectedScope : base .Session ,
40+ },
41+ {
42+ name : "Global scope when global flag set" ,
43+ globalSet : true ,
44+ projectSet : false ,
45+ sessionSet : false ,
46+ expectedScope : base .Global ,
47+ },
48+ {
49+ name : "Project scope when project flag set" ,
50+ globalSet : false ,
51+ projectSet : true ,
52+ sessionSet : false ,
53+ expectedScope : base .Project ,
54+ },
55+ {
56+ name : "Session scope when session flag set" ,
57+ globalSet : false ,
58+ projectSet : false ,
59+ sessionSet : true ,
60+ expectedScope : base .Session ,
61+ },
62+ {
63+ name : "Global takes precedence over project" ,
64+ globalSet : true ,
65+ projectSet : true ,
66+ sessionSet : false ,
67+ expectedScope : base .Global ,
68+ },
69+ {
70+ name : "Global takes precedence over session" ,
71+ globalSet : true ,
72+ projectSet : false ,
73+ sessionSet : true ,
74+ expectedScope : base .Global ,
75+ },
76+ {
77+ name : "Project takes precedence over session" ,
78+ globalSet : false ,
79+ projectSet : true ,
80+ sessionSet : true ,
81+ expectedScope : base .Project ,
82+ },
83+ {
84+ name : "Global takes precedence over all" ,
85+ globalSet : true ,
86+ projectSet : true ,
87+ sessionSet : true ,
88+ expectedScope : base .Global ,
89+ },
90+ }
91+
92+ for _ , tt := range tests {
93+ t .Run (tt .name , func (t * testing.T ) {
94+ // Simulate the scope selection logic from the unuse command
95+ scope := base .Session
96+ if tt .globalSet {
97+ scope = base .Global
98+ } else if tt .projectSet {
99+ scope = base .Project
100+ } else {
101+ scope = base .Session
102+ }
103+
104+ if scope != tt .expectedScope {
105+ t .Errorf ("Expected scope %v, but got %v" , tt .expectedScope , scope )
106+ }
107+ })
108+ }
109+ }
110+
111+ func TestUnuseCommandValidation (t * testing.T ) {
112+ // Test input validation logic
113+ tests := []struct {
114+ name string
115+ sdkName string
116+ expectError bool
117+ errorMsg string
118+ }{
119+ {
120+ name : "Valid SDK name" ,
121+ sdkName : "nodejs" ,
122+ expectError : false ,
123+ errorMsg : "" ,
124+ },
125+ {
126+ name : "Empty SDK name" ,
127+ sdkName : "" ,
128+ expectError : true ,
129+ errorMsg : "invalid parameter. format: <sdk-name>" ,
130+ },
131+ {
132+ name : "SDK name with special characters" ,
133+ sdkName : "node-js" ,
134+ expectError : false ,
135+ errorMsg : "" ,
136+ },
137+ {
138+ name : "SDK name with numbers" ,
139+ sdkName : "java8" ,
140+ expectError : false ,
141+ errorMsg : "" ,
142+ },
143+ }
144+
145+ for _ , tt := range tests {
146+ t .Run (tt .name , func (t * testing.T ) {
147+ // Simulate the validation logic from the unuse command
148+ hasError := len (tt .sdkName ) == 0
149+
150+ if hasError != tt .expectError {
151+ t .Errorf ("Expected error: %v, but got error: %v" , tt .expectError , hasError )
152+ }
153+ })
154+ }
155+ }
156+
157+ func TestScopeStringMapping (t * testing.T ) {
158+ // Test that scope values map correctly to their string representations
159+ tests := []struct {
160+ scope base.UseScope
161+ expected string
162+ }{
163+ {base .Global , "global" },
164+ {base .Project , "project" },
165+ {base .Session , "session" },
166+ }
167+
168+ for _ , tt := range tests {
169+ t .Run (tt .expected , func (t * testing.T ) {
170+ actual := tt .scope .String ()
171+ if actual != tt .expected {
172+ t .Errorf ("Expected scope string %q, but got %q" , tt .expected , actual )
173+ }
174+ })
175+ }
176+ }
0 commit comments