|
| 1 | +import Ably |
| 2 | +import AblyChat |
| 3 | +import Testing |
| 4 | + |
| 5 | +/// Some very basic integration tests, just to check that things are kind of working. |
| 6 | +/// |
| 7 | +/// It would be nice to give this a time limit, but unfortunately the `timeLimit` trait is only available on iOS 16 etc and above. CodeRabbit suggested writing a timeout function myself and wrapping the contents of the test in it, but I didn’t have time to try understanding its suggested code, so it can wait. |
| 8 | +@Suite |
| 9 | +struct IntegrationTests { |
| 10 | + private static func createSandboxRealtime(apiKey: String) -> ARTRealtime { |
| 11 | + let realtimeOptions = ARTClientOptions(key: apiKey) |
| 12 | + realtimeOptions.environment = "sandbox" |
| 13 | + realtimeOptions.clientId = UUID().uuidString |
| 14 | + |
| 15 | + return ARTRealtime(options: realtimeOptions) |
| 16 | + } |
| 17 | + |
| 18 | + private static func createSandboxChatClient(apiKey: String) -> DefaultChatClient { |
| 19 | + let realtime = createSandboxRealtime(apiKey: apiKey) |
| 20 | + return DefaultChatClient(realtime: realtime, clientOptions: nil) |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + func basicIntegrationTest() async throws { |
| 25 | + let apiKey = try await Sandbox.createAPIKey() |
| 26 | + |
| 27 | + // (1) Create a couple of chat clients — one for sending and one for receiving |
| 28 | + let txClient = Self.createSandboxChatClient(apiKey: apiKey) |
| 29 | + let rxClient = Self.createSandboxChatClient(apiKey: apiKey) |
| 30 | + |
| 31 | + // (2) Fetch a room |
| 32 | + let roomID = "basketball" |
| 33 | + let txRoom = try await txClient.rooms.get(roomID: roomID, options: .init()) |
| 34 | + let rxRoom = try await rxClient.rooms.get(roomID: roomID, options: .init()) |
| 35 | + |
| 36 | + // (3) Subscribe to room status |
| 37 | + let rxRoomStatusSubscription = await rxRoom.onStatusChange(bufferingPolicy: .unbounded) |
| 38 | + |
| 39 | + // (4) Attach the room so we can receive messages on it |
| 40 | + try await rxRoom.attach() |
| 41 | + |
| 42 | + // (5) Check that we received an ATTACHED status change as a result of attaching the room |
| 43 | + _ = try #require(await rxRoomStatusSubscription.first { $0.current == .attached }) |
| 44 | + #expect(await rxRoom.status == .attached) |
| 45 | + |
| 46 | + // (6) Send a message before subscribing to messages, so that later on we can check history works. |
| 47 | + |
| 48 | + // Create a throwaway subscription and wait for it to receive a message. This is to make sure that rxRoom has seen the message that we send here, so that the first message we receive on the subscription created in (7) is that which we’ll send in (8), and not that which we send here. |
| 49 | + let throwawayRxMessageSubscription = try await rxRoom.messages.subscribe(bufferingPolicy: .unbounded) |
| 50 | + |
| 51 | + // Send the message |
| 52 | + let txMessageBeforeRxSubscribe = try await txRoom.messages.send(params: .init(text: "Hello from txRoom, before rxRoom subscribe")) |
| 53 | + |
| 54 | + // Wait for rxRoom to see the message we just sent |
| 55 | + let throwawayRxMessage = try #require(await throwawayRxMessageSubscription.first { _ in true }) |
| 56 | + #expect(throwawayRxMessage == txMessageBeforeRxSubscribe) |
| 57 | + |
| 58 | + // (7) Subscribe to messages |
| 59 | + let rxMessageSubscription = try await rxRoom.messages.subscribe(bufferingPolicy: .unbounded) |
| 60 | + |
| 61 | + // (8) Now that we’re subscribed to messages, send a message on the other client and check that we receive it on the subscription |
| 62 | + let txMessageAfterRxSubscribe = try await txRoom.messages.send(params: .init(text: "Hello from txRoom, after rxRoom subscribe")) |
| 63 | + let rxMessageFromSubscription = try #require(await rxMessageSubscription.first { _ in true }) |
| 64 | + #expect(rxMessageFromSubscription == txMessageAfterRxSubscribe) |
| 65 | + |
| 66 | + // (9) Fetch historical messages from before subscribing, and check we get txMessageBeforeRxSubscribe |
| 67 | + let rxMessagesBeforeSubscribing = try await rxMessageSubscription.getPreviousMessages(params: .init()) |
| 68 | + try #require(rxMessagesBeforeSubscribing.items.count == 1) |
| 69 | + #expect(rxMessagesBeforeSubscribing.items[0] == txMessageBeforeRxSubscribe) |
| 70 | + |
| 71 | + // (10) Detach the room |
| 72 | + try await rxRoom.detach() |
| 73 | + |
| 74 | + // (11) Check that we received a DETACHED status change as a result of detaching the room |
| 75 | + _ = try #require(await rxRoomStatusSubscription.first { $0.current == .detached }) |
| 76 | + #expect(await rxRoom.status == .detached) |
| 77 | + } |
| 78 | +} |
0 commit comments