From b60c81c0135455e6543b7827ebc0b26ab3722747 Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Wed, 6 Nov 2024 21:11:29 -0300 Subject: [PATCH] Add some basic integration tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s unlikely that we’re going to have a working unified test suite before the beta release, so here are some very basic smoke tests just to give us a _little bit_ of confidence that things are kind of working and that we don’t introduce major regressions. Would be good to have a way of separating these from the unit tests so that they don’t slow them down, but can figure that out later; I don’t have loads of time to spend on this at the moment. --- .github/workflows/check.yaml | 12 ++++ .gitmodules | 3 + .prettierignore | 3 + CONTRIBUTING.md | 6 +- Package.swift | 3 + Tests/AblyChatTests/Helpers/Sandbox.swift | 50 ++++++++++++++++ Tests/AblyChatTests/IntegrationTests.swift | 69 ++++++++++++++++++++++ Tests/AblyChatTests/ably-common | 1 + 8 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 .gitmodules create mode 100644 Tests/AblyChatTests/Helpers/Sandbox.swift create mode 100644 Tests/AblyChatTests/IntegrationTests.swift create mode 160000 Tests/AblyChatTests/ably-common diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 60580552..4669f5ef 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -17,6 +17,8 @@ jobs: steps: - uses: actions/checkout@v4 + with: + submodules: true # This step can be removed once the runners’ default version of Xcode is 16 or above - uses: maxim-lobanov/setup-xcode@v1 @@ -42,6 +44,8 @@ jobs: runs-on: macos-15 steps: - uses: actions/checkout@v4 + with: + submodules: true # This step can be removed once the runners’ default version of Xcode is 16 or above - uses: maxim-lobanov/setup-xcode@v1 @@ -59,6 +63,8 @@ jobs: matrix: ${{ steps.generation-step.outputs.matrix }} steps: - uses: actions/checkout@v4 + with: + submodules: true # This step can be removed once the runners’ default version of Xcode is 16 or above - uses: maxim-lobanov/setup-xcode@v1 @@ -78,6 +84,8 @@ jobs: steps: - uses: actions/checkout@v4 + with: + submodules: true - uses: maxim-lobanov/setup-xcode@v1 with: xcode-version: ${{ matrix.tooling.xcodeVersion }} @@ -97,6 +105,8 @@ jobs: steps: - uses: actions/checkout@v4 + with: + submodules: true - uses: maxim-lobanov/setup-xcode@v1 with: xcode-version: ${{ matrix.tooling.xcodeVersion }} @@ -115,6 +125,8 @@ jobs: steps: - uses: actions/checkout@v4 + with: + submodules: true - uses: maxim-lobanov/setup-xcode@v1 with: xcode-version: ${{ matrix.tooling.xcodeVersion }} diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..f7fb23dc --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Tests/AblyChatTests/ably-common"] + path = Tests/AblyChatTests/ably-common + url = https://github.com/ably/ably-common diff --git a/.prettierignore b/.prettierignore index 1443d6cc..b8d88c97 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,5 @@ # Don’t try and format the asset catalogue JSON files, which are managed by Xcode *.xcassets/ + +# Submodules +Tests/AblyChatTests/ably-common diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 794ada92..6224a071 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,8 +8,9 @@ ## Setup -1. `mint bootstrap` — this will take quite a long time (~5 minutes on my machine) the first time you run it -2. `npm install` +1. `git submodule update --init` +2. `mint bootstrap` — this will take quite a long time (~5 minutes on my machine) the first time you run it +3. `npm install` ## Running the tests @@ -25,6 +26,7 @@ To check formatting and code quality, run `swift run BuildTool lint`. Run with ` ## Development guidelines - The aim of the [example app](README.md#example-app) is that it demonstrate all of the core functionality of the SDK. So if you add a new feature, try to add something to the example app to demonstrate this feature. +- If you add a new feature, try to extend the `IntegrationTests` tests to perform a smoke test of its core functionality. - We should aim to make it easy for consumers of the SDK to be able to mock out the SDK in the tests for their own code. A couple of things that will aid with this: - Describe the SDK’s functionality via protocols (when doing so would still be sufficiently idiomatic to Swift). - When defining a `struct` that is emitted by the public API of the library, make sure to define a public memberwise initializer so that users can create one to be emitted by their mocks. (There is no way to make Swift’s autogenerated memberwise initializer public, so you will need to write one yourself. In Xcode, you can do this by clicking at the start of the type declaration and doing Editor → Refactor → Generate Memberwise Initializer.) diff --git a/Package.swift b/Package.swift index f6ff655e..06d664d4 100644 --- a/Package.swift +++ b/Package.swift @@ -58,6 +58,9 @@ let package = Package( name: "AsyncAlgorithms", package: "swift-async-algorithms" ), + ], + resources: [ + .copy("ably-common"), ] ), .executableTarget( diff --git a/Tests/AblyChatTests/Helpers/Sandbox.swift b/Tests/AblyChatTests/Helpers/Sandbox.swift new file mode 100644 index 00000000..fc0e2531 --- /dev/null +++ b/Tests/AblyChatTests/Helpers/Sandbox.swift @@ -0,0 +1,50 @@ +import Foundation + +/// Provides the ``createAPIKey()`` function to create an API key for the Ably sandbox environment. +enum Sandbox { + private struct TestApp: Codable { + var keys: [Key] + + struct Key: Codable { + var keyStr: String + } + } + + enum Error: Swift.Error { + case badResponseStatus(Int) + } + + private static func loadAppCreationRequestBody() async throws -> Data { + let testAppSetupFileURL = Bundle.module.url( + forResource: "test-app-setup", + withExtension: "json", + subdirectory: "ably-common/test-resources" + )! + + let (data, _) = try await URLSession.shared.data(for: .init(url: testAppSetupFileURL)) + // swiftlint:disable:next force_cast + let dictionary = try JSONSerialization.jsonObject(with: data) as! [String: Any] + return try JSONSerialization.data(withJSONObject: dictionary["post_apps"]!) + } + + static func createAPIKey() async throws -> String { + var request = URLRequest(url: .init(string: "https://sandbox-rest.ably.io/apps")!) + request.httpMethod = "POST" + request.addValue("application/json", forHTTPHeaderField: "Content-Type") + request.httpBody = try await loadAppCreationRequestBody() + + let (data, response) = try await URLSession.shared.data(for: request) + + // swiftlint:disable:next force_cast + let statusCode = (response as! HTTPURLResponse).statusCode + + guard (200 ..< 300).contains(statusCode) else { + throw Error.badResponseStatus(statusCode) + } + + let testApp = try JSONDecoder().decode(TestApp.self, from: data) + + // From JS chat repo at 7985ab7 — "The key we need to use is the one at index 5, which gives enough permissions to interact with Chat and Channels" + return testApp.keys[5].keyStr + } +} diff --git a/Tests/AblyChatTests/IntegrationTests.swift b/Tests/AblyChatTests/IntegrationTests.swift new file mode 100644 index 00000000..00a3aa96 --- /dev/null +++ b/Tests/AblyChatTests/IntegrationTests.swift @@ -0,0 +1,69 @@ +import Ably +import AblyChat +import Testing + +/// Some very basic integration tests, just to check that things are kind of working. +/// +/// 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. +@Suite +struct IntegrationTests { + private static func createSandboxRealtime(apiKey: String) -> ARTRealtime { + let realtimeOptions = ARTClientOptions(key: apiKey) + realtimeOptions.environment = "sandbox" + realtimeOptions.clientId = UUID().uuidString + + return ARTRealtime(options: realtimeOptions) + } + + private static func createSandboxChatClient(apiKey: String) -> DefaultChatClient { + let realtime = createSandboxRealtime(apiKey: apiKey) + return DefaultChatClient(realtime: realtime, clientOptions: nil) + } + + @Test + func basicIntegrationTest() async throws { + let apiKey = try await Sandbox.createAPIKey() + + // 1. Create a couple of chat clients — one for sending and one for receiving + let txClient = Self.createSandboxChatClient(apiKey: apiKey) + let rxClient = Self.createSandboxChatClient(apiKey: apiKey) + + // 2. Fetch a room + let roomID = "basketball" + let txRoom = try await txClient.rooms.get(roomID: roomID, options: .init()) + let rxRoom = try await rxClient.rooms.get(roomID: roomID, options: .init()) + + // 2. Subscribe to room status + let rxRoomStatusSubscription = await rxRoom.onStatusChange(bufferingPolicy: .unbounded) + + // 3. Attach the room so we can receive messages on it + try await rxRoom.attach() + + // 4. Check that we received an ATTACHED status change as a result of attaching the room + _ = try #require(await rxRoomStatusSubscription.first { $0.current == .attached }) + #expect(await rxRoom.status == .attached) + + // 5. Send a message before subscribing to messages, so that later on we can check history works + let txMessageBeforeRxSubscribe = try await txRoom.messages.send(params: .init(text: "Hello from txRoom, before rxRoom subscribe")) + + // 6. Subscribe to messages + let rxMessageSubscription = try await rxRoom.messages.subscribe(bufferingPolicy: .unbounded) + + // 7. Now that we’re subscribed to messages, send a message on the other client and check that we receive it on the subscription + let txMessageAfterRxSubscribe = try await txRoom.messages.send(params: .init(text: "Hello from txRoom, after rxRoom subscribe")) + let rxMessageFromSubscription = try #require(await rxMessageSubscription.first { _ in true }) + #expect(rxMessageFromSubscription == txMessageAfterRxSubscribe) + + // 8. Fetch historical messages from before subscribing, and check we get txMessageBeforeRxSubscribe + let rxMessagesBeforeSubscribing = try await rxMessageSubscription.getPreviousMessages(params: .init()) + try #require(rxMessagesBeforeSubscribing.items.count == 1) + #expect(rxMessagesBeforeSubscribing.items[0] == txMessageBeforeRxSubscribe) + + // 9. Detach the room + try await rxRoom.detach() + + // 10. Check that we received a DETACHED status change as a result of detaching the room + _ = try #require(await rxRoomStatusSubscription.first { $0.current == .detached }) + #expect(await rxRoom.status == .detached) + } +} diff --git a/Tests/AblyChatTests/ably-common b/Tests/AblyChatTests/ably-common new file mode 160000 index 00000000..60fd9cf1 --- /dev/null +++ b/Tests/AblyChatTests/ably-common @@ -0,0 +1 @@ +Subproject commit 60fd9cf106abb1d6292fdd87d63ea33c552c8f33