|
| 1 | +/* |
| 2 | +Copyright © 2025 Stackaroo Contributors |
| 3 | +SPDX-License-Identifier: BSD-3-Clause |
| 4 | +*/ |
| 5 | +package prompt |
| 6 | + |
| 7 | +import ( |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/mock" |
| 13 | +) |
| 14 | + |
| 15 | +// MockPrompter is a mock implementation of the Prompter interface for testing |
| 16 | +type MockPrompter struct { |
| 17 | + mock.Mock |
| 18 | +} |
| 19 | + |
| 20 | +// ConfirmDeployment mock implementation |
| 21 | +func (m *MockPrompter) ConfirmDeployment(stackName string) (bool, error) { |
| 22 | + args := m.Called(stackName) |
| 23 | + return args.Bool(0), args.Error(1) |
| 24 | +} |
| 25 | + |
| 26 | +// TestMockPrompter_Interface verifies MockPrompter implements Prompter interface |
| 27 | +func TestMockPrompter_Interface(t *testing.T) { |
| 28 | + var _ Prompter = (*MockPrompter)(nil) |
| 29 | +} |
| 30 | + |
| 31 | +// TestMockPrompter_ConfirmDeployment tests the mock prompter functionality |
| 32 | +func TestMockPrompter_ConfirmDeployment(t *testing.T) { |
| 33 | + mockPrompter := &MockPrompter{} |
| 34 | + |
| 35 | + // Test confirmation |
| 36 | + mockPrompter.On("ConfirmDeployment", "test-stack").Return(true, nil).Once() |
| 37 | + |
| 38 | + result, err := mockPrompter.ConfirmDeployment("test-stack") |
| 39 | + |
| 40 | + assert.NoError(t, err) |
| 41 | + assert.True(t, result) |
| 42 | + mockPrompter.AssertExpectations(t) |
| 43 | +} |
| 44 | + |
| 45 | +// TestMockPrompter_ConfirmDeployment_Rejection tests mock prompter rejection |
| 46 | +func TestMockPrompter_ConfirmDeployment_Rejection(t *testing.T) { |
| 47 | + mockPrompter := &MockPrompter{} |
| 48 | + |
| 49 | + // Test rejection |
| 50 | + mockPrompter.On("ConfirmDeployment", "test-stack").Return(false, nil).Once() |
| 51 | + |
| 52 | + result, err := mockPrompter.ConfirmDeployment("test-stack") |
| 53 | + |
| 54 | + assert.NoError(t, err) |
| 55 | + assert.False(t, result) |
| 56 | + mockPrompter.AssertExpectations(t) |
| 57 | +} |
| 58 | + |
| 59 | +// TestSetPrompter_ChangesDefaultPrompter tests the SetPrompter functionality |
| 60 | +func TestSetPrompter_ChangesDefaultPrompter(t *testing.T) { |
| 61 | + // Store original prompter to restore later |
| 62 | + originalPrompter := defaultPrompter |
| 63 | + defer SetPrompter(originalPrompter) |
| 64 | + |
| 65 | + // Create and set mock prompter |
| 66 | + mockPrompter := &MockPrompter{} |
| 67 | + mockPrompter.On("ConfirmDeployment", "test-stack").Return(true, nil).Once() |
| 68 | + |
| 69 | + SetPrompter(mockPrompter) |
| 70 | + |
| 71 | + // Call the package-level function which should use our mock |
| 72 | + result, err := ConfirmDeployment("test-stack") |
| 73 | + |
| 74 | + assert.NoError(t, err) |
| 75 | + assert.True(t, result) |
| 76 | + mockPrompter.AssertExpectations(t) |
| 77 | +} |
| 78 | + |
| 79 | +// TestDefaultPrompter_IsStdinPrompter verifies default prompter type |
| 80 | +func TestDefaultPrompter_IsStdinPrompter(t *testing.T) { |
| 81 | + // Verify that the default prompter is a StdinPrompter |
| 82 | + _, ok := defaultPrompter.(*StdinPrompter) |
| 83 | + assert.True(t, ok, "Default prompter should be a StdinPrompter") |
| 84 | +} |
| 85 | + |
| 86 | +// TestConfirmDeployment_ResponseParsing tests the logic for parsing user responses |
| 87 | +func TestConfirmDeployment_ResponseParsing(t *testing.T) { |
| 88 | + tests := []struct { |
| 89 | + name string |
| 90 | + input string |
| 91 | + expected bool |
| 92 | + }{ |
| 93 | + {"yes lowercase", "yes", true}, |
| 94 | + {"yes uppercase", "YES", true}, |
| 95 | + {"yes mixed case", "Yes", true}, |
| 96 | + {"y lowercase", "y", true}, |
| 97 | + {"y uppercase", "Y", true}, |
| 98 | + {"no", "no", false}, |
| 99 | + {"n", "n", false}, |
| 100 | + {"empty", "", false}, |
| 101 | + {"whitespace only", " ", false}, |
| 102 | + {"other text", "maybe", false}, |
| 103 | + {"partial match", "yeah", false}, |
| 104 | + {"with whitespace", " y ", true}, |
| 105 | + {"with whitespace no", " no ", false}, |
| 106 | + } |
| 107 | + |
| 108 | + for _, tt := range tests { |
| 109 | + t.Run(tt.name, func(t *testing.T) { |
| 110 | + // Test the core logic that ConfirmDeployment uses |
| 111 | + response := strings.ToLower(strings.TrimSpace(tt.input)) |
| 112 | + result := response == "y" || response == "yes" |
| 113 | + |
| 114 | + assert.Equal(t, tt.expected, result, |
| 115 | + "Input '%s' should return %t", tt.input, tt.expected) |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// TestConfirmDeployment_StackNameFormatting tests that stack name is properly included in prompt |
| 121 | +func TestConfirmDeployment_StackNameFormatting(t *testing.T) { |
| 122 | + // This test documents the expected prompt format |
| 123 | + // Full interactive testing would require stdin mocking |
| 124 | + |
| 125 | + stackName := "test-vpc-stack" |
| 126 | + expectedPromptContent := "Do you want to apply these changes to stack test-vpc-stack? [y/N]:" |
| 127 | + |
| 128 | + // Verify the prompt message format is as expected |
| 129 | + assert.Contains(t, expectedPromptContent, stackName, |
| 130 | + "Prompt should contain the stack name") |
| 131 | + assert.Contains(t, expectedPromptContent, "[y/N]", |
| 132 | + "Prompt should indicate default is No") |
| 133 | +} |
| 134 | + |
| 135 | +// TestConfirmDeployment_Documentation documents the expected behaviour |
| 136 | +func TestConfirmDeployment_Documentation(t *testing.T) { |
| 137 | + // This test serves as documentation for the expected behaviour |
| 138 | + |
| 139 | + t.Run("accepts_only_explicit_yes", func(t *testing.T) { |
| 140 | + // Only "y" and "yes" (case insensitive) should return true |
| 141 | + yesResponses := []string{"y", "Y", "yes", "YES", "Yes"} |
| 142 | + for _, response := range yesResponses { |
| 143 | + normalized := strings.ToLower(strings.TrimSpace(response)) |
| 144 | + result := normalized == "y" || normalized == "yes" |
| 145 | + assert.True(t, result, "Response '%s' should be accepted as confirmation", response) |
| 146 | + } |
| 147 | + }) |
| 148 | + |
| 149 | + t.Run("rejects_all_other_input", func(t *testing.T) { |
| 150 | + // Everything else should return false |
| 151 | + noResponses := []string{"n", "no", "NO", "", " ", "maybe", "ok", "sure", "yep", "nope"} |
| 152 | + for _, response := range noResponses { |
| 153 | + normalized := strings.ToLower(strings.TrimSpace(response)) |
| 154 | + result := normalized == "y" || normalized == "yes" |
| 155 | + assert.False(t, result, "Response '%s' should be rejected", response) |
| 156 | + } |
| 157 | + }) |
| 158 | + |
| 159 | + t.Run("default_behaviour", func(t *testing.T) { |
| 160 | + // Empty input or whitespace should default to "no" (false) |
| 161 | + emptyInputs := []string{"", " ", "\t", "\n"} |
| 162 | + for _, input := range emptyInputs { |
| 163 | + normalized := strings.ToLower(strings.TrimSpace(input)) |
| 164 | + result := normalized == "y" || normalized == "yes" |
| 165 | + assert.False(t, result, "Empty/whitespace input should default to no") |
| 166 | + } |
| 167 | + }) |
| 168 | +} |
| 169 | + |
| 170 | +// TestConfirmDeployment_UsesDefaultPrompter verifies package function uses default prompter |
| 171 | +func TestConfirmDeployment_UsesDefaultPrompter(t *testing.T) { |
| 172 | + // Store original prompter to restore later |
| 173 | + originalPrompter := defaultPrompter |
| 174 | + defer SetPrompter(originalPrompter) |
| 175 | + |
| 176 | + // Create mock that expects to be called |
| 177 | + mockPrompter := &MockPrompter{} |
| 178 | + mockPrompter.On("ConfirmDeployment", "my-stack").Return(false, nil).Once() |
| 179 | + |
| 180 | + SetPrompter(mockPrompter) |
| 181 | + |
| 182 | + // Call package function |
| 183 | + result, err := ConfirmDeployment("my-stack") |
| 184 | + |
| 185 | + assert.NoError(t, err) |
| 186 | + assert.False(t, result) |
| 187 | + mockPrompter.AssertExpectations(t) |
| 188 | +} |
| 189 | + |
| 190 | +// Note: The MockPrompter allows full testing of deployment flows without requiring |
| 191 | +// actual user input. Tests can configure expected responses and verify behavior. |
| 192 | +// For interactive testing of the StdinPrompter, manual testing is recommended. |
0 commit comments