|
| 1 | +// Copyright (c) 2026 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package entity |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func TestQueueStateInvariants(t *testing.T) { |
| 25 | + initial := QueueState{Queue: "repo/main", State: QueueEnablementDisabled, ChangedAtMs: 1000, Version: 1} |
| 26 | + for _, tc := range []struct { |
| 27 | + name string |
| 28 | + change func(*QueueState) |
| 29 | + valid bool |
| 30 | + }{ |
| 31 | + {"initial", func(*QueueState) {}, true}, |
| 32 | + {"enabled occurrence", func(s *QueueState) { s.Version = 2; s.State = QueueEnablementEnabled; s.EffectiveFromCommitURI = "A" }, true}, |
| 33 | + {"missing queue", func(s *QueueState) { s.Queue = "" }, false}, |
| 34 | + {"oversized queue", func(s *QueueState) { s.Queue = strings.Repeat("x", 256) }, false}, |
| 35 | + {"unknown state", func(s *QueueState) { s.State = QueueEnablementUnknown }, false}, |
| 36 | + {"invalid version", func(s *QueueState) { s.Version = 0 }, false}, |
| 37 | + {"missing timestamp", func(s *QueueState) { s.ChangedAtMs = 0 }, false}, |
| 38 | + {"enabled initial", func(s *QueueState) { s.State = QueueEnablementEnabled }, false}, |
| 39 | + {"initial boundary", func(s *QueueState) { s.EffectiveFromCommitURI = "A" }, false}, |
| 40 | + {"missing later boundary", func(s *QueueState) { s.Version = 2 }, false}, |
| 41 | + {"oversized boundary", func(s *QueueState) { s.Version = 2; s.EffectiveFromCommitURI = strings.Repeat("x", 256) }, false}, |
| 42 | + } { |
| 43 | + t.Run(tc.name, func(t *testing.T) { |
| 44 | + state := initial |
| 45 | + tc.change(&state) |
| 46 | + if tc.valid { |
| 47 | + require.NoError(t, state.Validate()) |
| 48 | + } else { |
| 49 | + require.Error(t, state.Validate()) |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | + log := QueueLog{ID: QueueLogID(1), Queue: initial.Queue, TimestampMs: initial.ChangedAtMs, State: initial.State, QueueStateVersion: 1} |
| 54 | + require.NoError(t, log.Validate()) |
| 55 | + require.Equal(t, initial, log.Snapshot()) |
| 56 | + log.ID = "other" |
| 57 | + require.Error(t, log.Validate()) |
| 58 | +} |
0 commit comments