1616
1717//
1818
19+ import Foundation
1920import Testing
2021
2122@testable import ContainerizationOCI
@@ -36,6 +37,30 @@ struct OCITests {
3637
3738 #expect( descriptor. platform? . architecture == " arm64 " )
3839 #expect( descriptor. platform? . os == " linux " )
40+ #expect( descriptor. artifactType == nil )
41+ }
42+
43+ @Test func descriptorWithArtifactType( ) throws {
44+ let testArtifactType = " application/vnd.example.test.v1+json "
45+ let descriptor = ContainerizationOCI . Descriptor (
46+ mediaType: MediaTypes . imageManifest,
47+ digest: " sha256:abc123 " ,
48+ size: 1234 ,
49+ artifactType: testArtifactType
50+ )
51+ #expect( descriptor. artifactType == testArtifactType)
52+
53+ let data = try JSONEncoder ( ) . encode ( descriptor)
54+ let decoded = try JSONDecoder ( ) . decode ( ContainerizationOCI . Descriptor. self, from: data)
55+ #expect( decoded. artifactType == testArtifactType)
56+ }
57+
58+ @Test func descriptorWithoutArtifactTypeDecodesAsNil( ) throws {
59+ let json = """
60+ { " mediaType " : " application/vnd.oci.descriptor.v1+json " , " digest " : " sha256:abc " , " size " :0}
61+ """
62+ let decoded = try JSONDecoder ( ) . decode ( ContainerizationOCI . Descriptor. self, from: json. data ( using: . utf8) !)
63+ #expect( decoded. artifactType == nil )
3964 }
4065
4166 @Test func index( ) {
@@ -47,6 +72,37 @@ struct OCITests {
4772
4873 let index = ContainerizationOCI . Index ( schemaVersion: 1 , manifests: descriptors)
4974 #expect( index. manifests. count == 5 )
75+ #expect( index. subject == nil )
76+ #expect( index. artifactType == nil )
77+ }
78+
79+ @Test func indexWithSubjectAndArtifactType( ) throws {
80+ let testArtifactType = " application/vnd.example.test.v1+json "
81+ let subject = ContainerizationOCI . Descriptor ( mediaType: MediaTypes . imageManifest, digest: " sha256:subject " , size: 512 )
82+ let index = ContainerizationOCI . Index (
83+ schemaVersion: 2 ,
84+ manifests: [ ] ,
85+ subject: subject,
86+ artifactType: testArtifactType
87+ )
88+ #expect( index. subject? . digest == " sha256:subject " )
89+ #expect( index. artifactType == testArtifactType)
90+
91+ let data = try JSONEncoder ( ) . encode ( index)
92+ let decoded = try JSONDecoder ( ) . decode ( ContainerizationOCI . Index. self, from: data)
93+ #expect( decoded. subject? . digest == " sha256:subject " )
94+ #expect( decoded. artifactType == testArtifactType)
95+ }
96+
97+ @Test func indexDecodesWithoutNewFields( ) throws {
98+ let json = """
99+ { " schemaVersion " :2, " manifests " :[{ " mediaType " : " application/vnd.oci.descriptor.v1+json " , " digest " : " sha256:abc " , " size " :10}]}
100+ """
101+ let decoded = try JSONDecoder ( ) . decode ( ContainerizationOCI . Index. self, from: json. data ( using: . utf8) !)
102+ #expect( decoded. schemaVersion == 2 )
103+ #expect( decoded. manifests. count == 1 )
104+ #expect( decoded. subject == nil )
105+ #expect( decoded. artifactType == nil )
50106 }
51107
52108 @Test func manifests( ) {
@@ -61,5 +117,48 @@ struct OCITests {
61117 let manifest = ContainerizationOCI . Manifest ( schemaVersion: 1 , config: config, layers: descriptors)
62118 #expect( manifest. config. digest == " 123 " )
63119 #expect( manifest. layers. count == 5 )
120+ #expect( manifest. subject == nil )
121+ #expect( manifest. artifactType == nil )
122+ }
123+
124+ @Test func manifestWithSubjectAndArtifactType( ) throws {
125+ let testArtifactType = " application/vnd.example.test.v1+json "
126+ let config = ContainerizationOCI . Descriptor ( mediaType: MediaTypes . emptyJSON, digest: " sha256:empty " , size: 2 )
127+ let subject = ContainerizationOCI . Descriptor ( mediaType: MediaTypes . imageManifest, digest: " sha256:target " , size: 1234 )
128+ let layer = ContainerizationOCI . Descriptor (
129+ mediaType: testArtifactType,
130+ digest: " sha256:meta " ,
131+ size: 89 ,
132+ annotations: [ " org.opencontainers.image.title " : " metadata.json " ]
133+ )
134+
135+ let manifest = ContainerizationOCI . Manifest (
136+ config: config,
137+ layers: [ layer] ,
138+ subject: subject,
139+ artifactType: testArtifactType
140+ )
141+ #expect( manifest. subject? . digest == " sha256:target " )
142+ #expect( manifest. artifactType == testArtifactType)
143+ #expect( manifest. layers [ 0 ] . annotations ? [ " org.opencontainers.image.title " ] == " metadata.json " )
144+
145+ let data = try JSONEncoder ( ) . encode ( manifest)
146+ let decoded = try JSONDecoder ( ) . decode ( ContainerizationOCI . Manifest. self, from: data)
147+ #expect( decoded. subject? . digest == " sha256:target " )
148+ #expect( decoded. artifactType == testArtifactType)
149+ }
150+
151+ @Test func manifestDecodesWithoutNewFields( ) throws {
152+ let json = """
153+ {
154+ " schemaVersion " : 2,
155+ " config " : { " mediaType " : " application/vnd.oci.empty.v1+json " , " digest " : " sha256:abc " , " size " :2},
156+ " layers " : []
157+ }
158+ """
159+ let decoded = try JSONDecoder ( ) . decode ( ContainerizationOCI . Manifest. self, from: json. data ( using: . utf8) !)
160+ #expect( decoded. schemaVersion == 2 )
161+ #expect( decoded. subject == nil )
162+ #expect( decoded. artifactType == nil )
64163 }
65164}
0 commit comments