package api import ( "testing" "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) func TestEnsureFunctionTimestampsSetsBothOnCreate(t *testing.T) { fn := &unstructured.Unstructured{} now := mustParseRFC3339("2026-04-28T10:11:12Z") ensureFunctionTimestamps(fn, now) ann := fn.GetAnnotations() if ann[functionCreatedAtAnnotation] != now.Format(time.RFC3339) { t.Fatalf("created_at = %q, want %q", ann[functionCreatedAtAnnotation], now.Format(time.RFC3339)) } if ann[functionUpdatedAtAnnotation] != now.Format(time.RFC3339) { t.Fatalf("updated_at = %q, want %q", ann[functionUpdatedAtAnnotation], now.Format(time.RFC3339)) } } func TestEnsureFunctionTimestampsPreservesExistingCreatedAt(t *testing.T) { fn := &unstructured.Unstructured{} created := "2026-04-27T09:10:11Z" annotations := map[string]string{functionCreatedAtAnnotation: created} fn.SetAnnotations(annotations) now := mustParseRFC3339("2026-04-28T10:11:12Z") ensureFunctionTimestamps(fn, now) ann := fn.GetAnnotations() if ann[functionCreatedAtAnnotation] != created { t.Fatalf("created_at = %q, want %q", ann[functionCreatedAtAnnotation], created) } if ann[functionUpdatedAtAnnotation] != now.Format(time.RFC3339) { t.Fatalf("updated_at = %q, want %q", ann[functionUpdatedAtAnnotation], now.Format(time.RFC3339)) } } func TestEnsureFunctionTimestampsBackfillsCreatedAtFromCreationTimestamp(t *testing.T) { fn := &unstructured.Unstructured{} fn.SetCreationTimestamp(metav1.NewTime(mustParseRFC3339("2026-04-26T08:09:10Z"))) now := mustParseRFC3339("2026-04-28T10:11:12Z") ensureFunctionTimestamps(fn, now) ann := fn.GetAnnotations() if ann[functionCreatedAtAnnotation] != "2026-04-26T08:09:10Z" { t.Fatalf("created_at = %q", ann[functionCreatedAtAnnotation]) } if ann[functionUpdatedAtAnnotation] != now.Format(time.RFC3339) { t.Fatalf("updated_at = %q, want %q", ann[functionUpdatedAtAnnotation], now.Format(time.RFC3339)) } } func TestFunctionTimestampResponse(t *testing.T) { fn := &unstructured.Unstructured{} annotations := map[string]string{ functionCreatedAtAnnotation: "2026-04-26T08:09:10Z", functionUpdatedAtAnnotation: "2026-04-28T10:11:12Z", } fn.SetAnnotations(annotations) resp := functionTimestampResponse(fn) if resp["created_at"] != "2026-04-26T08:09:10Z" { t.Fatalf("created_at = %q", resp["created_at"]) } if resp["updated_at"] != "2026-04-28T10:11:12Z" { t.Fatalf("updated_at = %q", resp["updated_at"]) } } func TestFunctionTimestampResponseFallsBackToCreationTimestamp(t *testing.T) { fn := &unstructured.Unstructured{} fn.SetCreationTimestamp(metav1.NewTime(mustParseRFC3339("2026-04-26T08:09:10Z"))) resp := functionTimestampResponse(fn) if resp["created_at"] != "2026-04-26T08:09:10Z" { t.Fatalf("created_at = %q", resp["created_at"]) } if resp["updated_at"] != "2026-04-26T08:09:10Z" { t.Fatalf("updated_at = %q", resp["updated_at"]) } }