@@ -3,6 +3,7 @@ package v1alpha1
33import (
44 "fmt"
55 "testing"
6+ "time"
67
78 "k8s.io/apimachinery/pkg/api/meta"
89 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -28,7 +29,7 @@ func TestRecordHistoryEvent(t *testing.T) {
2829 {
2930 name : "prepends event to existing history" ,
3031 existingHistory : []FunctionStatusHistoryEntry {
31- {Time : metav1 .Now ( ), Message : "Older event" },
32+ {Time : metav1 .NewTime ( time . Date ( 2025 , 1 , 1 , 0 , 0 , 0 , 0 , time . UTC ) ), Message : "Older event" },
3233 },
3334 newMessage : "Newer event" ,
3435 expectedLen : 2 ,
@@ -38,10 +39,11 @@ func TestRecordHistoryEvent(t *testing.T) {
3839 {
3940 name : "trims oldest entries when exceeding max" ,
4041 existingHistory : func () []FunctionStatusHistoryEntry {
42+ base := time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )
4143 entries := make ([]FunctionStatusHistoryEntry , MaxHistoryEntries )
4244 for i := range entries {
4345 entries [i ] = FunctionStatusHistoryEntry {
44- Time : metav1 .Now ( ),
46+ Time : metav1 .NewTime ( base . Add ( time . Duration ( i ) * time . Minute ) ),
4547 Message : fmt .Sprintf ("Event %d" , i ),
4648 }
4749 }
@@ -50,7 +52,7 @@ func TestRecordHistoryEvent(t *testing.T) {
5052 newMessage : "Overflow event" ,
5153 expectedLen : MaxHistoryEntries ,
5254 expectedFirst : "Overflow event" ,
53- expectedLast : fmt .Sprintf ("Event %d" , MaxHistoryEntries - 2 ),
55+ expectedLast : fmt .Sprintf ("Event %d" , 1 ),
5456 },
5557 }
5658
@@ -76,9 +78,10 @@ func TestRecordHistoryEvent(t *testing.T) {
7678
7779func TestRecordHistoryEventFIFOOrder (t * testing.T ) {
7880 f := & Function {}
81+ base := time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )
7982
8083 for i := 0 ; i < MaxHistoryEntries + 5 ; i ++ {
81- f .RecordHistoryEvent (fmt .Sprintf ("Event %d" , i ))
84+ f .RecordHistoryEvent (fmt .Sprintf ("Event %d" , i ), WithHistoryEventTime ( base . Add ( time . Duration ( i ) * time . Minute )) )
8285 }
8386
8487 if len (f .Status .History ) != MaxHistoryEntries {
@@ -93,6 +96,28 @@ func TestRecordHistoryEventFIFOOrder(t *testing.T) {
9396 }
9497}
9598
99+ func TestRecordHistoryEventSortsByTime (t * testing.T ) {
100+ f := & Function {}
101+ now := time .Date (2025 , 6 , 15 , 12 , 0 , 0 , 0 , time .UTC )
102+
103+ f .RecordHistoryEvent ("Second event" , WithHistoryEventTime (now ))
104+ f .RecordHistoryEvent ("First event (older)" , WithHistoryEventTime (now .Add (- 1 * time .Hour )))
105+ f .RecordHistoryEvent ("Third event" , WithHistoryEventTime (now .Add (1 * time .Hour )))
106+
107+ if len (f .Status .History ) != 3 {
108+ t .Fatalf ("expected 3 entries, got %d" , len (f .Status .History ))
109+ }
110+ if f .Status .History [0 ].Message != "Third event" {
111+ t .Errorf ("expected first entry to be newest, got %q" , f .Status .History [0 ].Message )
112+ }
113+ if f .Status .History [1 ].Message != "Second event" {
114+ t .Errorf ("expected second entry to be middle, got %q" , f .Status .History [1 ].Message )
115+ }
116+ if f .Status .History [2 ].Message != "First event (older)" {
117+ t .Errorf ("expected last entry to be oldest, got %q" , f .Status .History [2 ].Message )
118+ }
119+ }
120+
96121func TestRecordHistoryEventSetsTime (t * testing.T ) {
97122 f := & Function {}
98123 f .RecordHistoryEvent ("test event" )
0 commit comments