@@ -25,3 +25,118 @@ func TestValidateUUID(t *testing.T) {
2525 }
2626 }
2727}
28+
29+ func TestValidateOCIImage (t * testing.T ) {
30+ valid := []interface {}{
31+ // Valid UUIDs
32+ "4812ccbc-2a2e-4c6c-bae4-a3d04ed51c0e" ,
33+ "7f668938-7999-48a7-ad28-c24cbd46c51b" ,
34+ // Valid SHA256 with prefix
35+ "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" ,
36+ "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" ,
37+ // Valid bare SHA256
38+ "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" ,
39+ "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" ,
40+ }
41+ for _ , v := range valid {
42+ _ , errors := validateOCIImage (v , "oci_image" )
43+ if len (errors ) != 0 {
44+ t .Fatalf ("%q should be a valid OCI image identifier: %q" , v , errors )
45+ }
46+ }
47+
48+ invalid := []interface {}{
49+ // Invalid formats
50+ "foobarbaz" ,
51+ "my-app-name" ,
52+ "invalid-format-12345" ,
53+ // Invalid SHA256 (wrong length)
54+ "sha256:abcd1234" ,
55+ "abcd1234" ,
56+ // Invalid SHA256 (wrong prefix)
57+ "md5:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" ,
58+ // Non-string types
59+ 1 ,
60+ true ,
61+ nil ,
62+ }
63+ for _ , v := range invalid {
64+ _ , errors := validateOCIImage (v , "oci_image" )
65+ if len (errors ) == 0 {
66+ t .Fatalf ("%q should be an invalid OCI image identifier" , v )
67+ }
68+ }
69+ }
70+
71+ func TestValidateArtifactForGeneration (t * testing.T ) {
72+ // Test Cedar generation
73+ t .Run ("Cedar generation" , func (t * testing.T ) {
74+ // Valid: Cedar + slug_id
75+ err := validateArtifactForGeneration ("cedar" , true , false )
76+ if err != nil {
77+ t .Fatalf ("Cedar + slug_id should be valid: %v" , err )
78+ }
79+
80+ // Invalid: Cedar + oci_image
81+ err = validateArtifactForGeneration ("cedar" , false , true )
82+ if err == nil {
83+ t .Fatal ("Cedar + oci_image should be invalid" )
84+ }
85+ expectedMsg := "cedar generation apps must use slug_id, not oci_image"
86+ if err .Error () != expectedMsg {
87+ t .Fatalf ("Expected error message %q, got %q" , expectedMsg , err .Error ())
88+ }
89+
90+ // Invalid: Cedar + no slug_id
91+ err = validateArtifactForGeneration ("cedar" , false , false )
92+ if err == nil {
93+ t .Fatal ("Cedar without slug_id should be invalid" )
94+ }
95+ expectedMsg = "cedar generation apps require slug_id"
96+ if err .Error () != expectedMsg {
97+ t .Fatalf ("Expected error message %q, got %q" , expectedMsg , err .Error ())
98+ }
99+ })
100+
101+ // Test Fir generation
102+ t .Run ("Fir generation" , func (t * testing.T ) {
103+ // Valid: Fir + oci_image
104+ err := validateArtifactForGeneration ("fir" , false , true )
105+ if err != nil {
106+ t .Fatalf ("Fir + oci_image should be valid: %v" , err )
107+ }
108+
109+ // Invalid: Fir + slug_id
110+ err = validateArtifactForGeneration ("fir" , true , false )
111+ if err == nil {
112+ t .Fatal ("Fir + slug_id should be invalid" )
113+ }
114+ expectedMsg := "fir generation apps must use oci_image, not slug_id"
115+ if err .Error () != expectedMsg {
116+ t .Fatalf ("Expected error message %q, got %q" , expectedMsg , err .Error ())
117+ }
118+
119+ // Invalid: Fir + no oci_image
120+ err = validateArtifactForGeneration ("fir" , false , false )
121+ if err == nil {
122+ t .Fatal ("Fir without oci_image should be invalid" )
123+ }
124+ expectedMsg = "fir generation apps require oci_image"
125+ if err .Error () != expectedMsg {
126+ t .Fatalf ("Expected error message %q, got %q" , expectedMsg , err .Error ())
127+ }
128+ })
129+
130+ // Test unknown generation (should pass through)
131+ t .Run ("Unknown generation" , func (t * testing.T ) {
132+ err := validateArtifactForGeneration ("unknown" , true , false )
133+ if err != nil {
134+ t .Fatalf ("Unknown generation should pass through: %v" , err )
135+ }
136+
137+ err = validateArtifactForGeneration ("unknown" , false , true )
138+ if err != nil {
139+ t .Fatalf ("Unknown generation should pass through: %v" , err )
140+ }
141+ })
142+ }
0 commit comments