|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package ui |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + tea "github.com/charmbracelet/bubbletea" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/stacklok/toolhive/pkg/client" |
| 14 | + "github.com/stacklok/toolhive/pkg/groups" |
| 15 | +) |
| 16 | + |
| 17 | +func TestFilterClientsAlreadyRegistered(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + clients []client.ClientAppStatus |
| 23 | + selectedGroups []*groups.Group |
| 24 | + wantClients []client.ClientApp |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "no groups selected returns all clients", |
| 28 | + clients: []client.ClientAppStatus{ |
| 29 | + {ClientType: client.VSCode, Installed: true}, |
| 30 | + {ClientType: client.Cursor, Installed: true}, |
| 31 | + }, |
| 32 | + selectedGroups: nil, |
| 33 | + wantClients: []client.ClientApp{client.VSCode, client.Cursor}, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "client registered in all selected groups is hidden", |
| 37 | + clients: []client.ClientAppStatus{ |
| 38 | + {ClientType: client.VSCode, Installed: true}, |
| 39 | + {ClientType: client.Cursor, Installed: true}, |
| 40 | + }, |
| 41 | + selectedGroups: []*groups.Group{ |
| 42 | + {Name: "group1", RegisteredClients: []string{"vscode", "cursor"}}, |
| 43 | + {Name: "group2", RegisteredClients: []string{"vscode"}}, |
| 44 | + }, |
| 45 | + wantClients: []client.ClientApp{client.Cursor}, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "client registered in only some groups is kept", |
| 49 | + clients: []client.ClientAppStatus{ |
| 50 | + {ClientType: client.VSCode, Installed: true}, |
| 51 | + {ClientType: client.Cursor, Installed: true}, |
| 52 | + }, |
| 53 | + selectedGroups: []*groups.Group{ |
| 54 | + {Name: "group1", RegisteredClients: []string{"vscode"}}, |
| 55 | + {Name: "group2", RegisteredClients: []string{"cursor"}}, |
| 56 | + }, |
| 57 | + wantClients: []client.ClientApp{client.VSCode, client.Cursor}, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "all clients already registered returns empty", |
| 61 | + clients: []client.ClientAppStatus{ |
| 62 | + {ClientType: client.VSCode, Installed: true}, |
| 63 | + {ClientType: client.Cursor, Installed: true}, |
| 64 | + }, |
| 65 | + selectedGroups: []*groups.Group{ |
| 66 | + {Name: "group1", RegisteredClients: []string{"vscode", "cursor"}}, |
| 67 | + }, |
| 68 | + wantClients: nil, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "single group with no registered clients returns all", |
| 72 | + clients: []client.ClientAppStatus{ |
| 73 | + {ClientType: client.VSCode, Installed: true}, |
| 74 | + {ClientType: client.Cursor, Installed: true}, |
| 75 | + }, |
| 76 | + selectedGroups: []*groups.Group{ |
| 77 | + {Name: "group1", RegisteredClients: []string{}}, |
| 78 | + }, |
| 79 | + wantClients: []client.ClientApp{client.VSCode, client.Cursor}, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + result := FilterClientsAlreadyRegistered(tt.clients, tt.selectedGroups) |
| 87 | + |
| 88 | + var gotClients []client.ClientApp |
| 89 | + for _, c := range result { |
| 90 | + gotClients = append(gotClients, c.ClientType) |
| 91 | + } |
| 92 | + |
| 93 | + assert.Equal(t, tt.wantClients, gotClients) |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestSetupModelUpdate_GroupToClientTransition(t *testing.T) { |
| 99 | + t.Parallel() |
| 100 | + |
| 101 | + tests := []struct { |
| 102 | + name string |
| 103 | + allClients []client.ClientAppStatus |
| 104 | + grps []*groups.Group |
| 105 | + selectedGroups map[int]struct{} |
| 106 | + wantStep setupStep |
| 107 | + wantQuitting bool |
| 108 | + wantClientCount int |
| 109 | + }{ |
| 110 | + { |
| 111 | + name: "filters already-registered clients on transition", |
| 112 | + allClients: []client.ClientAppStatus{ |
| 113 | + {ClientType: client.VSCode, Installed: true}, |
| 114 | + {ClientType: client.Cursor, Installed: true}, |
| 115 | + {ClientType: client.ClaudeCode, Installed: true}, |
| 116 | + }, |
| 117 | + grps: []*groups.Group{ |
| 118 | + {Name: "group1", RegisteredClients: []string{"vscode"}}, |
| 119 | + }, |
| 120 | + selectedGroups: map[int]struct{}{0: {}}, |
| 121 | + wantStep: stepClientSelection, |
| 122 | + wantQuitting: false, |
| 123 | + wantClientCount: 2, // cursor and claude-code remain |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "quits when all clients are already registered", |
| 127 | + allClients: []client.ClientAppStatus{ |
| 128 | + {ClientType: client.VSCode, Installed: true}, |
| 129 | + {ClientType: client.Cursor, Installed: true}, |
| 130 | + }, |
| 131 | + grps: []*groups.Group{ |
| 132 | + {Name: "group1", RegisteredClients: []string{"vscode", "cursor"}}, |
| 133 | + }, |
| 134 | + selectedGroups: map[int]struct{}{0: {}}, |
| 135 | + wantStep: stepClientSelection, |
| 136 | + wantQuitting: true, |
| 137 | + wantClientCount: 0, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "does not transition without group selection", |
| 141 | + allClients: []client.ClientAppStatus{ |
| 142 | + {ClientType: client.VSCode, Installed: true}, |
| 143 | + }, |
| 144 | + grps: []*groups.Group{ |
| 145 | + {Name: "group1", RegisteredClients: []string{}}, |
| 146 | + }, |
| 147 | + selectedGroups: map[int]struct{}{}, // none selected |
| 148 | + wantStep: stepGroupSelection, // stays on group step |
| 149 | + wantQuitting: false, |
| 150 | + wantClientCount: 1, |
| 151 | + }, |
| 152 | + } |
| 153 | + |
| 154 | + for _, tt := range tests { |
| 155 | + t.Run(tt.name, func(t *testing.T) { |
| 156 | + t.Parallel() |
| 157 | + |
| 158 | + m := &setupModel{ |
| 159 | + AllClients: tt.allClients, |
| 160 | + Clients: tt.allClients, |
| 161 | + Groups: tt.grps, |
| 162 | + SelectedClients: make(map[int]struct{}), |
| 163 | + SelectedGroups: tt.selectedGroups, |
| 164 | + CurrentStep: stepGroupSelection, |
| 165 | + } |
| 166 | + |
| 167 | + // Press enter to transition |
| 168 | + updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) |
| 169 | + result := updated.(*setupModel) |
| 170 | + |
| 171 | + assert.Equal(t, tt.wantStep, result.CurrentStep) |
| 172 | + assert.Equal(t, tt.wantQuitting, result.Quitting) |
| 173 | + assert.Len(t, result.Clients, tt.wantClientCount) |
| 174 | + }) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +func TestSetupModelUpdate_ClientSelection(t *testing.T) { |
| 179 | + t.Parallel() |
| 180 | + |
| 181 | + clients := []client.ClientAppStatus{ |
| 182 | + {ClientType: client.VSCode, Installed: true}, |
| 183 | + {ClientType: client.Cursor, Installed: true}, |
| 184 | + } |
| 185 | + |
| 186 | + m := &setupModel{ |
| 187 | + AllClients: clients, |
| 188 | + Clients: clients, |
| 189 | + Groups: []*groups.Group{{Name: "g1"}}, |
| 190 | + SelectedClients: make(map[int]struct{}), |
| 191 | + SelectedGroups: map[int]struct{}{0: {}}, |
| 192 | + CurrentStep: stepClientSelection, |
| 193 | + } |
| 194 | + |
| 195 | + // Toggle first client with space |
| 196 | + updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{' '}}) |
| 197 | + result := updated.(*setupModel) |
| 198 | + _, selected := result.SelectedClients[0] |
| 199 | + assert.True(t, selected, "first client should be selected after space") |
| 200 | + |
| 201 | + // Toggle it off |
| 202 | + updated, _ = result.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{' '}}) |
| 203 | + result = updated.(*setupModel) |
| 204 | + _, selected = result.SelectedClients[0] |
| 205 | + assert.False(t, selected, "first client should be deselected after second space") |
| 206 | + |
| 207 | + // Confirm with enter |
| 208 | + updated, cmd := result.Update(tea.KeyMsg{Type: tea.KeyEnter}) |
| 209 | + result = updated.(*setupModel) |
| 210 | + assert.True(t, result.Confirmed) |
| 211 | + assert.True(t, result.Quitting) |
| 212 | + require.NotNil(t, cmd, "should return a quit command") |
| 213 | +} |
0 commit comments