11package cmd
22
33import (
4- "os"
54 "testing"
65
7- "github.com/spf13/cobra"
86 "github.com/stackvista/stackstate-cli/internal/di"
97 "github.com/stretchr/testify/assert"
108)
119
12- const dashboardCommand = "dashboard"
13-
14- // Helper function to manage STS_EXPERIMENTAL_DASHBOARD environment variable
15- func withDashboardEnv (value string , fn func ()) {
16- originalValue := os .Getenv ("STS_EXPERIMENTAL_DASHBOARD" )
17- defer func () {
18- if originalValue == "" {
19- _ = os .Unsetenv ("STS_EXPERIMENTAL_DASHBOARD" )
20- } else {
21- _ = os .Setenv ("STS_EXPERIMENTAL_DASHBOARD" , originalValue )
22- }
23- }()
24-
25- if value == "" {
26- _ = os .Unsetenv ("STS_EXPERIMENTAL_DASHBOARD" )
27- } else {
28- _ = os .Setenv ("STS_EXPERIMENTAL_DASHBOARD" , value )
29- }
30-
31- fn ()
32- }
33-
3410func TestSTSCommand (t * testing.T ) {
3511 cli := di .NewMockDeps (t )
3612 cmd := STSCommand (& cli .Deps )
@@ -42,135 +18,46 @@ func TestSTSCommand(t *testing.T) {
4218}
4319
4420func TestSTSCommandContainsExpectedSubcommands (t * testing.T ) {
45- withDashboardEnv ("" , func () {
46- cli := di .NewMockDeps (t )
47- cmd := STSCommand (& cli .Deps )
48-
49- expectedCommands := []string {
50- "context" ,
51- "version" ,
52- "script" ,
53- "settings" ,
54- "stackpack" ,
55- "monitor" ,
56- "service-token" ,
57- "health" ,
58- "license" ,
59- "graph" ,
60- "rbac" ,
61- "topic" ,
62- "topology-sync" ,
63- "agent" ,
64- "user-session" ,
65- }
21+ cli := di .NewMockDeps (t )
22+ cmd := STSCommand (& cli .Deps )
6623
67- // Verify expected commands are present
68- for _ , expectedCmd := range expectedCommands {
69- found := false
70- for _ , subCmd := range cmd .Commands () {
71- if subCmd .Use == expectedCmd {
72- found = true
73- break
74- }
75- }
76- assert .True (t , found , "Expected command '%s' not found" , expectedCmd )
77- }
24+ expectedCommands := []string {
25+ "context" ,
26+ "version" ,
27+ "script" ,
28+ "settings" ,
29+ "stackpack" ,
30+ "monitor" ,
31+ "service-token" ,
32+ "health" ,
33+ "license" ,
34+ "graph" ,
35+ "rbac" ,
36+ "topic" ,
37+ "topology-sync" ,
38+ "agent" ,
39+ "user-session" ,
40+ "dashboard" ,
41+ }
7842
79- // Verify dashboard command is NOT present when env var is not set
80- dashboardFound := false
43+ // Verify expected commands are present
44+ for _ , expectedCmd := range expectedCommands {
45+ found := false
8146 for _ , subCmd := range cmd .Commands () {
82- if subCmd .Use == dashboardCommand {
83- dashboardFound = true
47+ if subCmd .Use == expectedCmd {
48+ found = true
8449 break
8550 }
8651 }
87- assert .False (t , dashboardFound , "Dashboard command should not be present when STS_EXPERIMENTAL_DASHBOARD is not set" )
88- })
89- }
90-
91- func TestSTSCommandDashboardExperimentalFeature (t * testing.T ) {
92- tests := []struct {
93- name string
94- envVarValue string
95- shouldIncludeDashboard bool
96- }{
97- {
98- name : "Dashboard command not included when env var is empty" ,
99- envVarValue : "" ,
100- shouldIncludeDashboard : false ,
101- },
102- {
103- name : "Dashboard command included when env var is set to 'true'" ,
104- envVarValue : "true" ,
105- shouldIncludeDashboard : true ,
106- },
107- {
108- name : "Dashboard command included when env var is set to any non-empty value" ,
109- envVarValue : "any-value" ,
110- shouldIncludeDashboard : true ,
111- },
112- }
113-
114- for _ , tt := range tests {
115- t .Run (tt .name , func (t * testing.T ) {
116- withDashboardEnv (tt .envVarValue , func () {
117- cli := di .NewMockDeps (t )
118- cmd := STSCommand (& cli .Deps )
119-
120- // Check if dashboard command is present
121- dashboardFound := false
122- for _ , subCmd := range cmd .Commands () {
123- if subCmd .Use == dashboardCommand {
124- dashboardFound = true
125- break
126- }
127- }
128-
129- if tt .shouldIncludeDashboard {
130- assert .True (t , dashboardFound , "Dashboard command should be present when STS_EXPERIMENTAL_DASHBOARD='%s'" , tt .envVarValue )
131- } else {
132- assert .False (t , dashboardFound , "Dashboard command should not be present when STS_EXPERIMENTAL_DASHBOARD='%s'" , tt .envVarValue )
133- }
134- })
135- })
52+ assert .True (t , found , "Expected command '%s' not found" , expectedCmd )
13653 }
13754}
13855
13956func TestSTSCommandStructure (t * testing.T ) {
140- withDashboardEnv ("1" , func () {
141- cli := di .NewMockDeps (t )
142- cmd := STSCommand (& cli .Deps )
143-
144- // Verify the command has the expected number of subcommands (15 regular + 1 dashboard)
145- assert .Len (t , cmd .Commands (), 16 , "Expected 16 subcommands when dashboard is enabled" )
146-
147- // Verify that dashboard command is included and properly configured
148- var dashboardCmd * cobra.Command
149- for _ , subCmd := range cmd .Commands () {
150- if subCmd .Use == dashboardCommand {
151- dashboardCmd = subCmd
152- break
153- }
154- }
155- assert .NotNil (t , dashboardCmd , "Dashboard command should be present" )
156- assert .Equal (t , dashboardCommand , dashboardCmd .Use )
157- assert .NotEmpty (t , dashboardCmd .Short )
158- })
159- }
160-
161- func TestSTSCommandWithoutDashboard (t * testing.T ) {
162- withDashboardEnv ("" , func () {
163- cli := di .NewMockDeps (t )
164- cmd := STSCommand (& cli .Deps )
165-
166- // Verify the command has the expected number of subcommands (15 regular, no dashboard)
167- assert .Len (t , cmd .Commands (), 15 , "Expected 15 subcommands when dashboard is disabled" )
57+ cli := di .NewMockDeps (t )
58+ cmd := STSCommand (& cli .Deps )
16859
169- // Double-check that no command has "dashboard" as its Use field
170- for _ , subCmd := range cmd .Commands () {
171- assert .NotEqual (t , dashboardCommand , subCmd .Use , "Dashboard command should not be present" )
172- }
173- })
60+ assert .Len (t , cmd .Commands (), 16 , "Expected 16 subcommands" )
17461}
17562
17663func TestSTSCommandUsageTemplate (t * testing.T ) {
0 commit comments