Skip to content

Commit 813e29e

Browse files
committed
test.
Entire-Checkpoint: 68d85134573d
1 parent 69fe06f commit 813e29e

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

cmd/entire/cli/auth_context_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
// makeContextJWT builds a JWT-shaped token (non-"none" alg) carrying the
@@ -55,6 +56,74 @@ func TestRunAuthContexts(t *testing.T) {
5556
}
5657
}
5758

59+
func TestCompleteContextNames(t *testing.T) {
60+
cfgDir := t.TempDir()
61+
t.Setenv("ENTIRE_CONFIG_DIR", cfgDir)
62+
restore := tokenstore.UseFileBackendForTesting(filepath.Join(t.TempDir(), "tokens.json"))
63+
t.Cleanup(restore)
64+
65+
exp := time.Now().Add(time.Hour).Unix()
66+
67+
// Two contexts; the second one recorded with activate=true is current.
68+
if _, err := auth.RecordLoginContext(makeContextJWT(t, fmt.Sprintf(`{"iss":"https://core-a.example.com","handle":"alice","exp":%d}`, exp)), "", false); err != nil {
69+
t.Fatalf("record core-a: %v", err)
70+
}
71+
currentName, err := auth.RecordLoginContext(makeContextJWT(t, fmt.Sprintf(`{"iss":"https://core-b.example.com","handle":"bob","exp":%d}`, exp)), "", true)
72+
if err != nil {
73+
t.Fatalf("record core-b: %v", err)
74+
}
75+
76+
got, directive := completeContextNames(nil, nil, "")
77+
if directive != cobra.ShellCompDirectiveNoFileComp {
78+
t.Fatalf("directive = %v, want NoFileComp", directive)
79+
}
80+
if len(got) != 2 {
81+
t.Fatalf("completions = %v, want 2 entries", got)
82+
}
83+
84+
// Each entry is "name\tdescription" carrying handle and core URL; the
85+
// active context is annotated "(active)" and no other entry is.
86+
var activeCount int
87+
for _, entry := range got {
88+
name, desc, found := strings.Cut(entry, "\t")
89+
if !found {
90+
t.Fatalf("entry %q missing tab-separated description", entry)
91+
}
92+
if name == currentName {
93+
if !strings.Contains(desc, "(active)") {
94+
t.Fatalf("active entry %q missing (active) marker", entry)
95+
}
96+
if !strings.Contains(desc, "bob") || !strings.Contains(desc, "core-b.example.com") {
97+
t.Fatalf("active entry %q missing handle/core URL", entry)
98+
}
99+
activeCount++
100+
} else if strings.Contains(desc, "(active)") {
101+
t.Fatalf("non-active entry %q wrongly marked (active)", entry)
102+
}
103+
}
104+
if activeCount != 1 {
105+
t.Fatalf("want exactly one (active) entry, got %d", activeCount)
106+
}
107+
108+
// Past the single positional: nothing to complete.
109+
got, directive = completeContextNames(nil, []string{"already"}, "")
110+
if got != nil || directive != cobra.ShellCompDirectiveNoFileComp {
111+
t.Fatalf("with an arg present, want (nil, NoFileComp), got (%v, %v)", got, directive)
112+
}
113+
}
114+
115+
func TestCompleteContextNames_NoContexts(t *testing.T) {
116+
cfgDir := t.TempDir()
117+
t.Setenv("ENTIRE_CONFIG_DIR", cfgDir)
118+
restore := tokenstore.UseFileBackendForTesting(filepath.Join(t.TempDir(), "tokens.json"))
119+
t.Cleanup(restore)
120+
121+
got, directive := completeContextNames(nil, nil, "")
122+
if len(got) != 0 || directive != cobra.ShellCompDirectiveNoFileComp {
123+
t.Fatalf("no contexts: want (empty, NoFileComp), got (%v, %v)", got, directive)
124+
}
125+
}
126+
58127
func TestWarnIfCrossCoreContext(t *testing.T) {
59128
cfgDir := t.TempDir()
60129
t.Setenv("ENTIRE_CONFIG_DIR", cfgDir)

0 commit comments

Comments
 (0)