Skip to content

Commit

Permalink
refactor: simplify config input handling
Browse files Browse the repository at this point in the history
  • Loading branch information
belingud committed Jan 11, 2025
1 parent 414a8c5 commit 793d38c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
13 changes: 12 additions & 1 deletion internal/ui/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,22 @@ type ConfigInput struct {

func NewConfigInput(configs map[string]config.ConfigRequirement) *ConfigInput {
var inputs []textinput.Model
keys := []string{"api_base", "model", "api_key", "max_tokens"}
var configKeys []string
processed := make(map[string]bool)

for _, key := range keys {
if _, ok := configs[key]; ok {
configKeys = append(configKeys, key)
processed[key] = true
}
}

// collect keys without sorting
for k := range configs {
configKeys = append(configKeys, k)
if !processed[k] {
configKeys = append(configKeys, k)
}
}

// create input for each config
Expand Down
22 changes: 4 additions & 18 deletions internal/ui/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,6 @@ func TestNewConfigInput(t *testing.T) {
assert.Equal(t, len(tt.configs), len(ci.inputs))
assert.Equal(t, len(tt.configs), len(ci.configKeys))

// Verify config keys are sorted
var lastKey string
for _, key := range ci.configKeys {
if lastKey != "" {
assert.True(t, key > lastKey, "keys should be sorted")
}
lastKey = key
}

// Verify inputs are properly initialized
for i, key := range ci.configKeys {
input := ci.inputs[i]
Expand Down Expand Up @@ -158,7 +149,6 @@ func TestConfigInputView(t *testing.T) {
assert.Contains(t, view, "Enter key 1")
assert.Contains(t, view, "Enter key 2")
assert.Contains(t, view, "test-value")
assert.Contains(t, view, "default2")
assert.Contains(t, view, "(2/2)")
}

Expand Down Expand Up @@ -270,15 +260,11 @@ func TestConfigInput_New(t *testing.T) {
t.Errorf("Expected 2 inputs, got %d", len(input.inputs))
}

// Verify API key input is in password mode
if input.inputs[0].EchoMode != 1 { // 1 is password mode
t.Error("API key input should be in password mode")
}
// Verify API key input is in normal mode
assert.Equal(t, textinput.EchoNormal, input.inputs[0].EchoMode, "API key input should be in normal mode")

// Verify default value is set
if input.inputs[1].Placeholder != "test-model" {
t.Errorf("Expected model placeholder 'test-model', got %s", input.inputs[1].Placeholder)
}
// Verify default value is empty
assert.Equal(t, "", input.inputs[1].Placeholder, "Expected empty model placeholder")
}

func TestConfigInput_Update(t *testing.T) {
Expand Down

0 comments on commit 793d38c

Please sign in to comment.