@@ -87,6 +87,89 @@ func TestLoginMFAPriorityQueue_PushPopByKey(t *testing.T) {
8787 }
8888}
8989
90+ // TestLoginMFAPriorityQueue_PeekByKey tests the PeekByKey method of
91+ // LoginMFAPriorityQueue. It verifies that PeekByKey returns the correct item
92+ // without removing it from the queue, returns errors on unhappy paths, handles
93+ // non-existing keys appropriately, works correctly on empty queues, and
94+ // properly handles empty key strings.
95+ func TestLoginMFAPriorityQueue_PeekByKey (t * testing.T ) {
96+ pq := NewLoginMFAPriorityQueue ()
97+ tc := testCases ()
98+ expectedLength := len (tc )
99+
100+ // Peek from empty queue
101+ peekedItem , err := pq .PeekByKey ("item-2" )
102+ if peekedItem != nil {
103+ t .Fatal ("expected nil when peeking from empty queue, got item" )
104+ }
105+ if err == nil {
106+ t .Fatal ("expected an error when peeking from empty queue, got nil" )
107+ }
108+ if pq .Len () != 0 {
109+ t .Fatalf ("expected empty queue to remain size 0, got %d" , pq .Len ())
110+ }
111+
112+ // Push test items
113+ for _ , item := range tc {
114+ if err := pq .Push (item ); err != nil {
115+ t .Fatal (err )
116+ }
117+ }
118+
119+ // Peek with empty key
120+ peekedItem , err = pq .PeekByKey ("" )
121+ if peekedItem != nil {
122+ t .Fatal ("expected nil for empty key, got item" )
123+ }
124+ if err == nil {
125+ t .Fatal ("expected error when peeking with empty key , got nil" )
126+ }
127+ // Verify queue size unchanged
128+ if pq .Len () != expectedLength {
129+ t .Fatalf ("expected queue size to remain %d, got %d" , expectedLength , pq .Len ())
130+ }
131+
132+ // Peek at non-existing item
133+ peekedItem , err = pq .PeekByKey ("non-existing-key" )
134+ if peekedItem != nil {
135+ t .Fatal ("expected nil for non-existing key, got item" )
136+ }
137+ if err == nil {
138+ t .Fatal ("expected error when peeking with non-existing key, got nil" )
139+ }
140+ // Verify queue size unchanged
141+ if pq .Len () != expectedLength {
142+ t .Fatalf ("expected queue size to remain %d, got %d" , expectedLength , pq .Len ())
143+ }
144+
145+ // Peek at a specific item
146+ peekedItem , err = pq .PeekByKey (tc [2 ].RequestID )
147+ if peekedItem == nil {
148+ t .Fatal ("expected to peek item-2, got nil" )
149+ }
150+ if err != nil {
151+ t .Fatal ("expected no error when peeking existing key, got" , err )
152+ }
153+ if peekedItem .RequestID != tc [2 ].RequestID {
154+ t .Fatal ("expected the same item on subsequent peeks, got different items" )
155+ }
156+ // Verify queue size unchanged
157+ if pq .Len () != expectedLength {
158+ t .Fatalf ("expected queue size to remain %d, got %d" , expectedLength , pq .Len ())
159+ }
160+ // Verify item still exists in queue
161+ stillExists , err := pq .PeekByKey (tc [2 ].RequestID )
162+ if stillExists == nil {
163+ t .Fatal ("item should still exist after peek" )
164+ }
165+ if err != nil {
166+ t .Fatal ("expected no error when peeking existing key for the second time, got" , err )
167+ }
168+ if stillExists .RequestID != tc [2 ].RequestID {
169+ t .Fatal ("expected the same item on subsequent peeks, got different items" )
170+ }
171+ }
172+
90173func TestLoginMFARemoveStaleEntries (t * testing.T ) {
91174 pq := NewLoginMFAPriorityQueue ()
92175
0 commit comments