Skip to content

Commit

Permalink
Test for CHA-PR3d spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
maratal committed Jan 4, 2025
1 parent 62d1e23 commit de29c26
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
11 changes: 11 additions & 0 deletions Sources/AblyChat/Room.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,14 @@ internal actor DefaultRoom<LifecycleManagerFactory: RoomLifecycleManagerFactory>
}
}
}

#if DEBUG
extension DefaultRoom {
var testsOnly_lifecycleManager: DefaultRoomLifecycleManager<DefaultRoomLifecycleContributor> {
guard let lifecycleManager = lifecycleManager as? DefaultRoomLifecycleManager<DefaultRoomLifecycleContributor> else {
preconditionFailure("DefaultRoomLifecycleManager is expected here.")
}
return lifecycleManager
}
}
#endif
68 changes: 68 additions & 0 deletions Tests/AblyChatTests/DefaultRoomPresenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,74 @@ struct DefaultRoomPresenceTests {
}
}

// @specPartial CHA-PR3d
@Test
func usersMayEnterPresenceWhileAttaching() async throws {
// Given
let realtimePresence = MockRealtimePresence(["client1"].map { .init(clientId: $0) })
let channelsList = [
MockRealtimeChannel(name: "basketball::$chat::$chatMessages", attachResult: .success, mockPresence: realtimePresence),
]
let channels = MockChannels(channels: channelsList)
let realtime = MockRealtime.create(channels: channels)
let room = try await DefaultRoom(realtime: realtime, chatAPI: ChatAPI(realtime: realtime), roomID: "basketball", options: .init(presence: .init()), logger: TestLogger(), lifecycleManagerFactory: DefaultRoomLifecycleManagerFactory())

let lifecycleManager = await room.testsOnly_lifecycleManager
let attachingStatusWaitSubscription = await lifecycleManager.testsOnly_subscribeToStatusChangeWaitEvents()

// When: The room is in the attaching state and presence enter is called
Task {
try await lifecycleManager.performAttachOperation()
}
// When: And presence enter is called
try await room.presence.enter()

// Then: The manager waits for its room status to change
_ = try #require(await attachingStatusWaitSubscription.first { _ in true })

// Then: Room eventually attached
#expect(await room.status == .attached)
}

// @specPartial CHA-PR3d
@Test
func usersMayEnterPresenceWhileAttachingWithFailure() async throws {
// Given: attachment failure
let attachError = ARTErrorInfo(domain: "SomeDomain", code: 123)

// Given
let realtimePresence = MockRealtimePresence(["client1"].map { .init(clientId: $0) })
let channelsList = [
MockRealtimeChannel(name: "basketball::$chat::$chatMessages", attachResult: .failure(attachError), mockPresence: realtimePresence),
]
let channels = MockChannels(channels: channelsList)
let realtime = MockRealtime.create(channels: channels)
let room = try await DefaultRoom(realtime: realtime, chatAPI: ChatAPI(realtime: realtime), roomID: "basketball", options: .init(presence: .init()), logger: TestLogger(), lifecycleManagerFactory: DefaultRoomLifecycleManagerFactory())

let lifecycleManager = await room.testsOnly_lifecycleManager
let attachingStatusWaitSubscription = await lifecycleManager.testsOnly_subscribeToStatusChangeWaitEvents()

// When: The room is in the attaching state
Task {
try await lifecycleManager.performAttachOperation()
}

// When: And fails to attach
await #expect(throws: ARTErrorInfo.self) {
do {
try await room.presence.enter()
} catch {
// Then: An exception with status code of 500 should be thrown
let error = try #require(error as? ARTErrorInfo)
#expect(error.statusCode == 500)
#expect(error.code == ErrorCode.roomInInvalidState.rawValue)
throw error
}
}
// Then: The manager were waiting for its room status to change from attaching
_ = try #require(await attachingStatusWaitSubscription.first { _ in true })
}

// @spec CHA-PR7a
// @spec CHA-PR7b
// @spec CHA-PR7c
Expand Down
6 changes: 3 additions & 3 deletions Tests/AblyChatTests/Mocks/MockRealtimeChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class MockRealtimeChannel: NSObject, RealtimeChannelProtocol {
}

var state: ARTRealtimeChannelState {
.attached
attachResult == .success ? .attached : .failed
}

var errorReason: ARTErrorInfo? {
Expand All @@ -87,7 +87,7 @@ final class MockRealtimeChannel: NSObject, RealtimeChannelProtocol {
fatalError("Not implemented")
}

enum AttachOrDetachResult {
enum AttachOrDetachResult: Equatable {
case success
case failure(ARTErrorInfo)

Expand Down Expand Up @@ -184,7 +184,7 @@ final class MockRealtimeChannel: NSObject, RealtimeChannelProtocol {
}

func on(_: @escaping (ARTChannelStateChange) -> Void) -> ARTEventListener {
fatalError("Not implemented")
ARTEventListener()
}

func once(_: ARTChannelEvent, callback _: @escaping (ARTChannelStateChange) -> Void) -> ARTEventListener {
Expand Down

0 comments on commit de29c26

Please sign in to comment.