From 793d38cb930af9c8a854cccc98a255920625b3fa Mon Sep 17 00:00:00 2001 From: belingud Date: Sat, 11 Jan 2025 23:39:45 +0800 Subject: [PATCH] refactor: simplify config input handling --- internal/ui/provider.go | 13 ++++++++++++- internal/ui/provider_test.go | 22 ++++------------------ 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/internal/ui/provider.go b/internal/ui/provider.go index 2264ab7..ec0bca6 100644 --- a/internal/ui/provider.go +++ b/internal/ui/provider.go @@ -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 diff --git a/internal/ui/provider_test.go b/internal/ui/provider_test.go index a45746c..5834b6d 100644 --- a/internal/ui/provider_test.go +++ b/internal/ui/provider_test.go @@ -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] @@ -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)") } @@ -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) {