@@ -241,6 +241,77 @@ struct DefaultMessagesTests {
241241 }
242242 }
243243
244+ // @spec CHA-M13a
245+ @Test
246+ func getMessage( ) async throws {
247+ // Given
248+ let realtime = MockRealtime {
249+ MockHTTPPaginatedResponse (
250+ items: [
251+ [
252+ " serial " : " 123456789-000@123456789:000 " ,
253+ " version " : [
254+ " serial " : " 123456789-000@123456789:000 " ,
255+ " timestamp " : 1_631_840_000_000 ,
256+ ] ,
257+ " metadata " : [ " key1 " : " val1 " ] ,
258+ " headers " : [ " key2 " : " val2 " ] ,
259+ " timestamp " : 1_631_840_000_000 ,
260+ " text " : " hey " ,
261+ " clientId " : " clientId " ,
262+ " action " : " message.create " ,
263+ ] ,
264+ ] ,
265+ statusCode: 200 ,
266+ headers: [ : ] ,
267+ )
268+ }
269+ let chatAPI = ChatAPI ( realtime: realtime)
270+ let channel = MockRealtimeChannel ( initialState: . attached)
271+ let defaultMessages = DefaultMessages ( channel: channel, chatAPI: chatAPI, roomName: " basketball " , logger: TestLogger ( ) )
272+
273+ // When
274+ let retrievedMessage = try await defaultMessages. get ( withSerial: " 123456789-000@123456789:000 " )
275+
276+ // Then
277+ #expect( retrievedMessage. serial == " 123456789-000@123456789:000 " )
278+ #expect( retrievedMessage. action == . messageCreate)
279+ #expect( retrievedMessage. text == " hey " )
280+ #expect( retrievedMessage. clientID == " clientId " )
281+ #expect( retrievedMessage. version. serial == " 123456789-000@123456789:000 " )
282+ #expect( retrievedMessage. version. timestamp == Date ( timeIntervalSince1970: 1_631_840_000_000 / 1000 ) )
283+ #expect( retrievedMessage. metadata == [ " key1 " : " val1 " ] )
284+ #expect( retrievedMessage. headers == [ " key2 " : " val2 " ] )
285+ #expect( retrievedMessage. timestamp == Date ( timeIntervalSince1970: 1_631_840_000_000 / 1000 ) )
286+ #expect( realtime. callRecorder. hasRecord (
287+ matching: " request(_:path:params:body:headers:) " ,
288+ arguments: [ " method " : " GET " , " path " : " /chat/v4/rooms/basketball/messages/123456789-000@123456789:000 " , " body " : [ : ] , " params " : [ : ] , " headers " : [ : ] ] ,
289+ ) )
290+ }
291+
292+ // @spec CHA-M13b
293+ @Test
294+ func errorShouldBeThrownIfErrorIsReturnedFromGetRESTChatAPI( ) async throws {
295+ // Given
296+ let realtime = MockRealtime { @Sendable ( ) throws ( ARTErrorInfo) in
297+ throw ARTErrorInfo ( domain: " SomeDomain " , code: 123 )
298+ }
299+ let chatAPI = ChatAPI ( realtime: realtime)
300+ let channel = MockRealtimeChannel ( )
301+ let defaultMessages = DefaultMessages ( channel: channel, chatAPI: chatAPI, roomName: " basketball " , logger: TestLogger ( ) )
302+
303+ // Then
304+ // TODO: avoids compiler crash (https://github.com/ably/ably-chat-swift/issues/233), revert once Xcode 16.3 released
305+ let doIt = {
306+ _ = try await defaultMessages. get ( withSerial: " 123456789-000@123456789:000 " )
307+ }
308+ await #expect {
309+ try await doIt ( )
310+ } throws: { error in
311+ error as? ARTErrorInfo == ARTErrorInfo ( domain: " SomeDomain " , code: 123 )
312+ }
313+ }
314+
244315 // @spec CHA-M5a
245316 // @specOneOf(4/6) CHA-RST6 - Escaping room name for API get messages
246317 @Test
0 commit comments