@@ -11,6 +11,7 @@ import (
1111
1212 "github.com/entireio/cli/cmd/entire/cli/auth"
1313 "github.com/entireio/cli/internal/entireclient/tokenstore"
14+ "github.com/spf13/cobra"
1415)
1516
1617// TestResolveStatusTarget_PrefersActiveContext pins the multi-core fix: status
@@ -89,6 +90,74 @@ func TestRunAuthContexts(t *testing.T) {
8990 }
9091}
9192
93+ func TestCompleteContextNames (t * testing.T ) {
94+ cfgDir := t .TempDir ()
95+ t .Setenv ("ENTIRE_CONFIG_DIR" , cfgDir )
96+ restore := tokenstore .UseFileBackendForTesting (filepath .Join (t .TempDir (), "tokens.json" ))
97+ t .Cleanup (restore )
98+
99+ exp := time .Now ().Add (time .Hour ).Unix ()
100+
101+ // Two contexts; the second one recorded with activate=true is current.
102+ if _ , err := auth .RecordLoginContext (makeContextJWT (t , fmt .Sprintf (`{"iss":"https://core-a.example.com","handle":"alice","exp":%d}` , exp )), "" , false ); err != nil {
103+ t .Fatalf ("record core-a: %v" , err )
104+ }
105+ currentName , err := auth .RecordLoginContext (makeContextJWT (t , fmt .Sprintf (`{"iss":"https://core-b.example.com","handle":"bob","exp":%d}` , exp )), "" , true )
106+ if err != nil {
107+ t .Fatalf ("record core-b: %v" , err )
108+ }
109+
110+ got , directive := completeContextNames (nil , nil , "" )
111+ if directive != cobra .ShellCompDirectiveNoFileComp {
112+ t .Fatalf ("directive = %v, want NoFileComp" , directive )
113+ }
114+ if len (got ) != 2 {
115+ t .Fatalf ("completions = %v, want 2 entries" , got )
116+ }
117+
118+ // Each entry is "name\tdescription" carrying handle and core URL; the
119+ // active context is annotated "(active)" and no other entry is.
120+ var activeCount int
121+ for _ , entry := range got {
122+ name , desc , found := strings .Cut (entry , "\t " )
123+ if ! found {
124+ t .Fatalf ("entry %q missing tab-separated description" , entry )
125+ }
126+ if name == currentName {
127+ if ! strings .Contains (desc , "(active)" ) {
128+ t .Fatalf ("active entry %q missing (active) marker" , entry )
129+ }
130+ if ! strings .Contains (desc , "bob" ) || ! strings .Contains (desc , "core-b.example.com" ) {
131+ t .Fatalf ("active entry %q missing handle/core URL" , entry )
132+ }
133+ activeCount ++
134+ } else if strings .Contains (desc , "(active)" ) {
135+ t .Fatalf ("non-active entry %q wrongly marked (active)" , entry )
136+ }
137+ }
138+ if activeCount != 1 {
139+ t .Fatalf ("want exactly one (active) entry, got %d" , activeCount )
140+ }
141+
142+ // Past the single positional: nothing to complete.
143+ got , directive = completeContextNames (nil , []string {"already" }, "" )
144+ if got != nil || directive != cobra .ShellCompDirectiveNoFileComp {
145+ t .Fatalf ("with an arg present, want (nil, NoFileComp), got (%v, %v)" , got , directive )
146+ }
147+ }
148+
149+ func TestCompleteContextNames_NoContexts (t * testing.T ) {
150+ cfgDir := t .TempDir ()
151+ t .Setenv ("ENTIRE_CONFIG_DIR" , cfgDir )
152+ restore := tokenstore .UseFileBackendForTesting (filepath .Join (t .TempDir (), "tokens.json" ))
153+ t .Cleanup (restore )
154+
155+ got , directive := completeContextNames (nil , nil , "" )
156+ if len (got ) != 0 || directive != cobra .ShellCompDirectiveNoFileComp {
157+ t .Fatalf ("no contexts: want (empty, NoFileComp), got (%v, %v)" , got , directive )
158+ }
159+ }
160+
92161func TestWarnIfCrossCoreContext (t * testing.T ) {
93162 cfgDir := t .TempDir ()
94163 t .Setenv ("ENTIRE_CONFIG_DIR" , cfgDir )
0 commit comments