@@ -135,6 +135,138 @@ struct IOSMediaArtifactLoaderTests {
135135 #expect ( media . mimeType == " video/mp4 " )
136136 }
137137
138+ @ Test @ MainActor func `requests playback rendition for buffered audio` ( ) async throws {
139+ let config = try Self . config ( url : #require ( URL ( string : " wss://gateway.example " ) ) )
140+ let loader = IOSMediaArtifactLoader (
141+ connectionProvider : {
142+ IOSMediaArtifactLoader . Connection (
143+ config : config ,
144+ gatewayID : config . effectiveStableID ,
145+ customHeaders : [ : ] )
146+ } ,
147+ requestFactory : { _ , _ in
148+ { request in
149+ #expect ( request . url ? . absoluteString == Self . ticketedPlaybackAbsoluteURL )
150+ #expect ( request . value ( forHTTPHeaderField : " Range " ) == nil )
151+ return try Self . response (
152+ for : request ,
153+ mimeType : " audio/mp4 " ,
154+ data : Data ( [ 10 , 11 , 12 ] ) )
155+ }
156+ } )
157+
158+ let loaded = try await loader . load (
159+ response : Self . downloadResult ( mimeType : " audio/x-caf " ) ,
160+ kind : . audio ,
161+ playback : . transcode ,
162+ expectedGatewayID : config . effectiveStableID )
163+
164+ guard case let . data ( media ) = loaded else {
165+ Issue . record ( " audio rendition should be buffered " )
166+ return
167+ }
168+ #expect ( media . data == Data ( [ 10 , 11 , 12 ] ) )
169+ #expect ( media . mimeType == " audio/mp4 " )
170+ }
171+
172+ @ Test @ MainActor func `transcode playback bypasses inline bytes for ticketed rendition` ( ) async throws {
173+ let config = try Self . config ( url : #require ( URL ( string : " wss://gateway.example " ) ) )
174+ let loader = IOSMediaArtifactLoader (
175+ connectionProvider : {
176+ IOSMediaArtifactLoader . Connection (
177+ config : config ,
178+ gatewayID : config . effectiveStableID ,
179+ customHeaders : [ : ] )
180+ } ,
181+ requestFactory : { _ , _ in
182+ { request in
183+ #expect ( request . url ? . absoluteString == Self . ticketedPlaybackAbsoluteURL )
184+ return try Self . response (
185+ for : request ,
186+ mimeType : " audio/mp4 " ,
187+ data : Data ( [ 20 , 21 , 22 ] ) )
188+ }
189+ } )
190+
191+ let loaded = try await loader . load (
192+ response : Self . downloadResult (
193+ mimeType : " audio/x-caf " ,
194+ inlineData : Data ( [ 99 , 98 , 97 ] ) ) ,
195+ kind : . audio ,
196+ playback : . transcode ,
197+ expectedGatewayID : config . effectiveStableID )
198+
199+ guard case let . data ( media ) = loaded else {
200+ Issue . record ( " transcode playback should fetch the ticketed rendition " )
201+ return
202+ }
203+ #expect ( media . data == Data ( [ 20 , 21 , 22 ] ) )
204+ #expect ( media . mimeType == " audio/mp4 " )
205+ }
206+
207+ @ Test @ MainActor func `native playback retains inline byte fast path` ( ) async throws {
208+ let config = Self . config ( )
209+ let loader = IOSMediaArtifactLoader (
210+ connectionProvider : {
211+ IOSMediaArtifactLoader . Connection (
212+ config : config ,
213+ gatewayID : config . effectiveStableID ,
214+ customHeaders : [ : ] )
215+ } ,
216+ requestFactory : { _ , _ in
217+ Issue . record ( " native inline bytes must not reach the URL path " )
218+ return { _ in throw CancellationError ( ) }
219+ } )
220+
221+ let loaded = try await loader . load (
222+ response : Self . downloadResult (
223+ mimeType : " audio/mpeg " ,
224+ inlineData : Data ( [ 30 , 31 , 32 ] ) ) ,
225+ kind : . audio ,
226+ playback : . native ,
227+ expectedGatewayID : config . effectiveStableID )
228+
229+ guard case let . data ( media ) = loaded else {
230+ Issue . record ( " native playback should retain inline bytes " )
231+ return
232+ }
233+ #expect ( media . data == Data ( [ 30 , 31 , 32 ] ) )
234+ #expect ( media . mimeType == " audio/mpeg " )
235+ }
236+
237+ @ Test @ MainActor func `returns preparing for a pending streamed video rendition` ( ) async throws {
238+ let config = try Self . config ( url : #require ( URL ( string : " wss://gateway.example " ) ) )
239+ let loader = IOSMediaArtifactLoader (
240+ connectionProvider : {
241+ IOSMediaArtifactLoader . Connection (
242+ config : config ,
243+ gatewayID : config . effectiveStableID ,
244+ customHeaders : [ : ] )
245+ } ,
246+ requestFactory : { _ , _ in
247+ { request in
248+ #expect ( request . url ? . absoluteString == Self . ticketedPlaybackAbsoluteURL )
249+ #expect ( request . value ( forHTTPHeaderField : " Range " ) == " bytes=0-0 " )
250+ return try Self . response (
251+ for : request ,
252+ statusCode : 202 ,
253+ mimeType : " application/json " ,
254+ data : Data ( #"{"status":"preparing"}"# . utf8 ) )
255+ }
256+ } )
257+
258+ let loaded = try await loader . load (
259+ response : Self . downloadResult ( mimeType : " video/x-matroska " ) ,
260+ kind : . video ,
261+ playback : . transcode ,
262+ expectedGatewayID : config . effectiveStableID )
263+
264+ guard case . preparing = loaded else {
265+ Issue . record ( " pending rendition should remain in preparing state " )
266+ return
267+ }
268+ }
269+
138270 @ Test @ MainActor func `rejects absolute and unticketed paths before fetching` ( ) async {
139271 let config = Self . config ( )
140272 let loader = IOSMediaArtifactLoader (
@@ -170,10 +302,12 @@ struct IOSMediaArtifactLoaderTests {
170302 private static let ticketedPath =
171303 " /api/chat/media/outgoing/main/11111111-1111-4111-8111-111111111111/full?mediaTicket=ticket "
172304 private static let ticketedAbsoluteURL = " https://gateway.example \( ticketedPath ) "
305+ private static let ticketedPlaybackAbsoluteURL = " \( ticketedAbsoluteURL ) &playback=1 "
173306
174307 private static func downloadResult (
175308 mimeType : String ? ,
176309 sizeBytes : Int ? = nil ,
310+ inlineData : Data ? = nil ,
177311 url : String = ticketedPath ) -> ArtifactsDownloadResult
178312 {
179313 ArtifactsDownloadResult (
@@ -184,18 +318,21 @@ struct IOSMediaArtifactLoaderTests {
184318 mimetype : mimeType ,
185319 sizebytes : sizeBytes ,
186320 download : [ " mode " : AnyCodable ( " url " ) ] ) ,
321+ encoding : inlineData == nil ? nil : " base64 " ,
322+ data : inlineData ? . base64EncodedString ( ) ,
187323 url : url )
188324 }
189325
190326 private static func response (
191327 for request : URLRequest ,
328+ statusCode : Int = 200 ,
192329 mimeType : String ,
193330 data : Data ) throws -> ( Data , URLResponse )
194331 {
195332 let responseURL = try #require ( request . url )
196333 let response = try #require ( HTTPURLResponse (
197334 url : responseURL ,
198- statusCode : 200 ,
335+ statusCode : statusCode ,
199336 httpVersion : nil ,
200337 headerFields : [ " Content-Type " : mimeType ] ) )
201338 return ( data , response )
0 commit comments