|
| 1 | +// Copyright (c) 2026 Lerian Studio. All rights reserved. |
| 2 | +// Use of this source code is governed by the Elastic License 2.0 |
| 3 | +// that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package constant |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +// TestErrorConstants_LimitExtended tests that the new limit-related error constants exist. |
| 16 | +// These are required for WEEKLY, CUSTOM limit types and time window validation. |
| 17 | +func TestErrorConstants_LimitExtended(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + err error |
| 21 | + code string |
| 22 | + category string |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "ErrLimitTimeWindowMismatch exists with code TRC-0300", |
| 26 | + err: ErrLimitTimeWindowMismatch, |
| 27 | + code: "TRC-0300", |
| 28 | + category: "time window validation", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "ErrLimitTimeWindowZeroWidth exists with code TRC-0301", |
| 32 | + err: ErrLimitTimeWindowZeroWidth, |
| 33 | + code: "TRC-0301", |
| 34 | + category: "time window validation", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "ErrTimeOfDayInvalidFormat exists with code TRC-0302", |
| 38 | + err: ErrTimeOfDayInvalidFormat, |
| 39 | + code: "TRC-0302", |
| 40 | + category: "time of day parsing", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "ErrLimitCustomDatesRequired exists with code TRC-0303", |
| 44 | + err: ErrLimitCustomDatesRequired, |
| 45 | + code: "TRC-0303", |
| 46 | + category: "custom period validation", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "ErrLimitCustomDatesOrder exists with code TRC-0304", |
| 50 | + err: ErrLimitCustomDatesOrder, |
| 51 | + code: "TRC-0304", |
| 52 | + category: "custom period validation", |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "ErrLimitCustomDatesNotAllowed exists with code TRC-0305", |
| 56 | + err: ErrLimitCustomDatesNotAllowed, |
| 57 | + code: "TRC-0305", |
| 58 | + category: "custom period validation", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "ErrLimitUnknownType exists with code TRC-0306", |
| 62 | + err: ErrLimitUnknownType, |
| 63 | + code: "TRC-0306", |
| 64 | + category: "limit type validation", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "ErrLimitCustomPeriodTooLong exists with code TRC-0307", |
| 68 | + err: ErrLimitCustomPeriodTooLong, |
| 69 | + code: "TRC-0307", |
| 70 | + category: "custom period validation", |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "ErrLimitCustomPeriodExpired exists with code TRC-0308", |
| 74 | + err: ErrLimitCustomPeriodExpired, |
| 75 | + code: "TRC-0308", |
| 76 | + category: "custom period validation", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "ErrLimitInvalidCustomStartFormat exists with code TRC-0309", |
| 80 | + err: ErrLimitInvalidCustomStartFormat, |
| 81 | + code: "TRC-0309", |
| 82 | + category: "custom period validation", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "ErrLimitInvalidCustomEndFormat exists with code TRC-0310", |
| 86 | + err: ErrLimitInvalidCustomEndFormat, |
| 87 | + code: "TRC-0310", |
| 88 | + category: "custom period validation", |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + for _, tc := range tests { |
| 93 | + t.Run(tc.name, func(t *testing.T) { |
| 94 | + // Verify error is not nil |
| 95 | + assert.NotNil(t, tc.err, "error constant should exist") |
| 96 | + |
| 97 | + // Verify error message contains the expected code |
| 98 | + assert.Contains(t, tc.err.Error(), tc.code, |
| 99 | + "error message should contain code %s for %s", tc.code, tc.category) |
| 100 | + |
| 101 | + // Verify it's a proper error type |
| 102 | + wrapped := fmt.Errorf("wrapped: %w", tc.err) |
| 103 | + assert.True(t, errors.Is(wrapped, tc.err), |
| 104 | + "error should be unwrappable with errors.Is") |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// TestErrorConstants_UniquenessTRC0300Range tests that error codes in TRC-0300 range are unique. |
| 110 | +func TestErrorConstants_UniquenessTRC0300Range(t *testing.T) { |
| 111 | + // All TRC-0300 range errors that should be unique |
| 112 | + errorConstants := []error{ |
| 113 | + ErrLimitTimeWindowMismatch, // TRC-0300 |
| 114 | + ErrLimitTimeWindowZeroWidth, // TRC-0301 |
| 115 | + ErrTimeOfDayInvalidFormat, // TRC-0302 |
| 116 | + ErrLimitCustomDatesRequired, // TRC-0303 |
| 117 | + ErrLimitCustomDatesOrder, // TRC-0304 |
| 118 | + ErrLimitCustomDatesNotAllowed, // TRC-0305 |
| 119 | + ErrLimitUnknownType, // TRC-0306 |
| 120 | + ErrLimitCustomPeriodTooLong, // TRC-0307 |
| 121 | + ErrLimitCustomPeriodExpired, // TRC-0308 |
| 122 | + ErrLimitInvalidCustomStartFormat, // TRC-0309 |
| 123 | + ErrLimitInvalidCustomEndFormat, // TRC-0310 |
| 124 | + } |
| 125 | + |
| 126 | + // Check for duplicates using error messages |
| 127 | + seen := make(map[string]int) |
| 128 | + for i, err := range errorConstants { |
| 129 | + msg := err.Error() |
| 130 | + if prev, exists := seen[msg]; exists { |
| 131 | + t.Errorf("duplicate error code: index %d and %d both have message %q", prev, i, msg) |
| 132 | + } |
| 133 | + seen[msg] = i |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// TestErrorConstants_NonEmptyMessages tests that error constants have non-empty error text. |
| 138 | +func TestErrorConstants_NonEmptyMessages(t *testing.T) { |
| 139 | + tests := []struct { |
| 140 | + name string |
| 141 | + err error |
| 142 | + }{ |
| 143 | + { |
| 144 | + name: "time window mismatch has non-empty message", |
| 145 | + err: ErrLimitTimeWindowMismatch, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "zero width window has non-empty message", |
| 149 | + err: ErrLimitTimeWindowZeroWidth, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "custom dates required has non-empty message", |
| 153 | + err: ErrLimitCustomDatesRequired, |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "custom dates order has non-empty message", |
| 157 | + err: ErrLimitCustomDatesOrder, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "custom dates not allowed has non-empty message", |
| 161 | + err: ErrLimitCustomDatesNotAllowed, |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "custom period too long has non-empty message", |
| 165 | + err: ErrLimitCustomPeriodTooLong, |
| 166 | + }, |
| 167 | + { |
| 168 | + name: "invalid time of day format has non-empty message", |
| 169 | + err: ErrTimeOfDayInvalidFormat, |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "unknown limit type has non-empty message", |
| 173 | + err: ErrLimitUnknownType, |
| 174 | + }, |
| 175 | + { |
| 176 | + name: "custom period expired has non-empty message", |
| 177 | + err: ErrLimitCustomPeriodExpired, |
| 178 | + }, |
| 179 | + { |
| 180 | + name: "invalid custom start format has non-empty message", |
| 181 | + err: ErrLimitInvalidCustomStartFormat, |
| 182 | + }, |
| 183 | + { |
| 184 | + name: "invalid custom end format has non-empty message", |
| 185 | + err: ErrLimitInvalidCustomEndFormat, |
| 186 | + }, |
| 187 | + } |
| 188 | + |
| 189 | + for _, tc := range tests { |
| 190 | + t.Run(tc.name, func(t *testing.T) { |
| 191 | + assert.NotEmpty(t, tc.err.Error(), "error should have a non-empty message") |
| 192 | + }) |
| 193 | + } |
| 194 | +} |
0 commit comments