Files
letro-ios/UnitTests/Sources/TestUtilities/DeferredFulfillment.swift
Copilot 4834f453ef Finish migration of UnitTests target from XCTestCase to Swift Testing (#5129)
* Initial plan

* Migrate 3 test files from XCTest to Swift Testing

- MediaUploadPreviewScreenViewModelTests: @MainActor @Suite struct with init(),
  BundleFinder class for Bundle(for:), mutating test/setup functions,
  [self] capture replacing [weak self] in closures
- NotificationManagerTests: @MainActor @Suite final class with init()/deinit,
  expectation/fulfillment(of:) replaced with confirmation(...), test_ prefix stripped
- NotificationSettingsScreenViewModelTests: @MainActor @Suite struct with
  init() throws, non-optional stored properties, test prefix stripped

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Migrate 3 XCTest files to Swift Testing

- NotificationSettingsEditScreenViewModelTests: @MainActor @Suite struct with init() throws, mutating test methods
- TimelineViewModelTests: @MainActor @Suite final class with init() async throws + deinit
- AttributedStringBuilderTests: @Suite struct with init() async throws

All XCT assertions replaced with #expect/#require/Issue.record

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Migrate 4 test files from XCTest to Swift Testing

- TimelineMediaPreviewViewModelTests: @Suite struct, mutating @Test funcs,
  testLoadingItem renamed to loadingItem (called internally by other tests)
- ServerConfirmationScreenViewModelTests: @Suite final class with init()/deinit
- CompletionSuggestionServiceTests: @Suite struct with init()
- RoomFlowCoordinatorTests: @Suite final class with deinit

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Migrate 4 test files from XCTest to Swift Testing

- VoiceMessageRecorderTests: @Suite struct with init() async throws,
  added BundleFinder class for Bundle lookup, migrated all assertions
- SpaceScreenViewModelTests: @Suite struct, private mutating setupViewModel,
  all test funcs mutating, XCTestExpectation → confirmation
- RoomNotificationSettingsScreenViewModelTests: @Suite struct with
  init() throws, cancellable tests marked mutating
- JoinRoomScreenViewModelTests: @Suite final class with init()/deinit,
  XCTestExpectation → confirmation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Migrate 6 test files from XCTestCase to Swift Testing

Co-authored-by: pixlwave <6060466+pixlwave@users.noreply.github.com>

* Fix trailing blank line in RoomPollsHistoryScreenViewModelTests

Co-authored-by: pixlwave <6060466+pixlwave@users.noreply.github.com>

* Migrate 3 test files from XCTest to Swift Testing

- MediaUploadingPreprocessorTests: @Suite final class with init()/deinit,
  removed executionTimeAllowance, XCTAssertEqual(accuracy:) → abs(Double)
- SecurityAndPrivacyScreenViewModelTests: @MainActor @Suite final class,
  5 expectation+fulfillment → await confirmation(...)
- CreateRoomViewModelTests: @MainActor @Suite final class,
  4 expectation+fulfillment → await confirmation(...)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Migrate RoomScreenViewModelTests and RoomDetailsScreenViewModelTests to Swift Testing

- Replace XCTest with Testing framework
- RoomScreenViewModelTests: final class with init() async throws + deinit
- RoomDetailsScreenViewModelTests: struct with init() and mutating funcs
- Convert XCT assertions to #expect / Issue.record
- Convert XCTestExpectation patterns to confirmation { confirm in }
- Strip 'test' prefix from all test function names

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Migrate ComposerToolbarViewModelTests from XCTest to Swift Testing

- Replace import XCTest with import Testing
- Convert XCTestCase class to @MainActor @Suite final class
- Replace setUp()/tearDown() with init()/deinit
- Strip 'test' prefix from all 41 test method names and add @Test
- Replace XCTAssert* with #expect()/#require()
- Replace try XCTUnwrap() with try #require()
- Convert expectation+wait patterns to deferFulfillment with PassthroughSubject
- Convert isInverted expectation to boolean flag checked after await
- Use deferFulfillment on $viewState for state-transition tests

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Address comments with Copilot.

* Fix the failing tests.

* Fixed flaky tests (#5137)

resolved flaky tests

* Tweaks and fixes.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: pixlwave <6060466+pixlwave@users.noreply.github.com>
Co-authored-by: Doug <douglase@element.io>
Co-authored-by: Mauro <34335419+Velin92@users.noreply.github.com>
2026-02-24 12:20:01 +00:00

277 lines
13 KiB
Swift

//
// Copyright 2025 Element Creations Ltd.
// Copyright 2023-2025 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Combine
import Testing
struct DeferredFulfillment<T> {
private let closure: () async throws -> T
fileprivate init(_ closure: @escaping () async throws -> T) {
self.closure = closure
}
@discardableResult
func fulfill() async throws -> T {
try await closure()
}
}
private struct DeferredFulfillmentError: Error {
static func noOutput(message: String?, sourceLocation: SourceLocation) -> Self {
defer { Issue.record(Comment(rawValue: message ?? "No Output"), sourceLocation: sourceLocation) }
return .init()
}
static func unexpectedFulfillment(message: String?, sourceLocation: SourceLocation) -> Self {
defer { Issue.record(Comment(rawValue: message ?? "Unexpected Fulfillment"), sourceLocation: sourceLocation) }
return .init()
}
static var empty: Self {
.init()
}
}
/// Test utility that assists in subscribing to a publisher and deferring the fulfilment and results until some other actions have been performed.
/// - Parameters:
/// - publisher: The publisher to wait on.
/// - timeout: A timeout after which we give up.
/// - message: An optional message to include in the error if the condition is never met.
/// - sourceLocation: The source location to attach to any recorded issues.
/// - until: callback that evaluates outputs until some condition is reached
/// - Returns: The deferred fulfilment to be executed after some actions and that returns the result of the publisher.
func deferFulfillment<P: Publisher<P.Output, Never>>(_ publisher: P,
timeout: Duration = .seconds(10),
message: String? = nil,
sourceLocation: SourceLocation = #_sourceLocation,
until condition: @escaping (P.Output) -> Bool) -> DeferredFulfillment<P.Output> {
let (stream, continuation) = AsyncStream<P.Output>.makeStream()
let cancellable = publisher
.sink { _ in
continuation.finish()
} receiveValue: { value in
guard condition(value) else { return }
continuation.yield(value)
continuation.finish()
}
return DeferredFulfillment {
defer { cancellable.cancel() }
return try await withThrowingTaskGroup(of: P.Output.self) { group in
group.addTask {
for await result in stream {
return result
}
guard !Task.isCancelled else {
// Required to avoid a double recording of the issue in the case where the task is cancelled due to timeout.
throw DeferredFulfillmentError.empty
}
throw DeferredFulfillmentError.noOutput(message: message, sourceLocation: sourceLocation)
}
group.addTask {
try await Task.sleep(for: timeout)
throw DeferredFulfillmentError.noOutput(message: message, sourceLocation: sourceLocation)
}
defer { group.cancelAll() }
return try #require(try await group.next())
}
}
}
/// Test utility that assists in observing an async sequence, deferring the fulfilment and results until some condition has been met.
/// - Parameters:
/// - asyncSequence: The sequence to wait on.
/// - timeout: A timeout after which we give up.
/// - message: An optional message to include in the error if the condition is never met.
/// - sourceLocation: The source location to attach to any recorded issues.
/// - until: callback that evaluates outputs until some condition is reached
/// - Returns: The deferred fulfilment to be executed after some actions and that returns the result of the sequence.
func deferFulfillment<Value>(_ asyncSequence: any AsyncSequence<Value, Never>,
timeout: Duration = .seconds(10),
message: String? = nil,
sourceLocation: SourceLocation = #_sourceLocation,
until condition: @escaping (Value) -> Bool) -> DeferredFulfillment<Value> {
let (stream, continuation) = AsyncStream<Value>.makeStream()
let task = Task {
for await value in asyncSequence where condition(value) {
continuation.yield(value)
continuation.finish()
return
}
continuation.finish()
}
return DeferredFulfillment {
defer { task.cancel() }
return try await withThrowingTaskGroup(of: Value.self) { group in
group.addTask {
for await value in stream {
return value
}
guard !Task.isCancelled else {
// Required to avoid a double recording of the issue in the case where the task is cancelled due to timeout.
throw DeferredFulfillmentError.empty
}
throw DeferredFulfillmentError.noOutput(message: message, sourceLocation: sourceLocation)
}
group.addTask {
try await Task.sleep(for: timeout)
throw DeferredFulfillmentError.noOutput(message: message, sourceLocation: sourceLocation)
}
defer { group.cancelAll() }
return try #require(await group.next())
}
}
}
/// Test utility that assists in subscribing to a publisher and deferring the fulfilment and results until some other actions have been performed.
/// - Parameters:
/// - publisher: The publisher to wait on.
/// - keyPath: the key path for the expected values
/// - transitionValues: the values through which the keypath needs to transition through
/// - timeout: A timeout after which we give up.
/// - sourceLocation: The source location to attach to any recorded issues.
/// - Returns: The deferred fulfilment to be executed after some actions and that returns the result of the publisher.
func deferFulfillment<P: Publisher<P.Output, Never>, K: KeyPath<P.Output, V>, V: Equatable>(_ publisher: P,
keyPath: K,
transitionValues: [V],
timeout: Duration = .seconds(10),
message: String? = nil,
sourceLocation: SourceLocation = #_sourceLocation) -> DeferredFulfillment<P.Output> {
var expectedOrder = transitionValues
return deferFulfillment(publisher, timeout: timeout, message: message, sourceLocation: sourceLocation) { value in
let receivedValue = value[keyPath: keyPath]
if let index = expectedOrder.firstIndex(where: { $0 == receivedValue }), index == 0 {
expectedOrder.remove(at: index)
}
return expectedOrder.isEmpty
}
}
/// Test utility that assists in subscribing to an async sequence and deferring the fulfilment and results until some other actions have been performed.
/// - Parameters:
/// - asyncSequence: The sequence to wait on.
/// - transitionValues: the values through which the sequence needs to transition through
/// - timeout: A timeout after which we give up.
/// - sourceLocation: The source location to attach to any recorded issues.
/// - Returns: The deferred fulfilment to be executed after some actions and that returns the result of the sequence.
func deferFulfillment<Value: Equatable>(_ asyncSequence: any AsyncSequence<Value, Never>,
transitionValues: [Value],
timeout: Duration = .seconds(10),
message: String? = nil,
sourceLocation: SourceLocation = #_sourceLocation) -> DeferredFulfillment<Value> {
var expectedOrder = transitionValues
return deferFulfillment(asyncSequence, timeout: timeout, message: message, sourceLocation: sourceLocation) { value in
if let index = expectedOrder.firstIndex(where: { $0 == value }), index == 0 {
expectedOrder.remove(at: index)
}
return expectedOrder.isEmpty
}
}
/// Test utility that assists in subscribing to a publisher and deferring the failure for a particular value until some other actions have been performed.
/// - Parameters:
/// - publisher: The publisher to wait on.
/// - timeout: A timeout after which we give up.
/// - message: An optional message to include in the error if the condition is unexpectedly met.
/// - sourceLocation: The source location to attach to any recorded issues.
/// - until: callback that evaluates outputs until some condition is reached
/// - Returns: The deferred fulfilment to be executed after some actions. The publisher's result is not returned from this fulfilment.
func deferFailure<P: Publisher<P.Output, Never>>(_ publisher: P,
timeout: Duration,
message: String? = nil,
sourceLocation: SourceLocation = #_sourceLocation,
until condition: @escaping (P.Output) -> Bool) -> DeferredFulfillment<Void> where P.Failure == Never {
let (stream, continuation) = AsyncStream<Void>.makeStream()
let cancellable = publisher
.sink { value in
guard condition(value) else { return }
continuation.yield(())
continuation.finish()
}
return DeferredFulfillment {
defer { cancellable.cancel() }
try await withThrowingTaskGroup(of: Void.self) { group in
// If the condition fires before timeout, that's the unexpected failure.
group.addTask {
for await _ in stream {
throw DeferredFulfillmentError.unexpectedFulfillment(message: message, sourceLocation: sourceLocation)
}
// Stream finished without condition firing this shouldn't happen
// but is safe to treat as success.
}
// Timeout elapsing without the condition firing = success.
group.addTask {
try await Task.sleep(for: timeout)
}
defer { group.cancelAll() }
return try #require(try await group.next())
}
}
}
/// Test utility that assists in subscribing to an async sequence and deferring the failure for a particular value until some other actions have been performed.
/// - Parameters:
/// - asyncSequence: The sequence to wait on.
/// - timeout: A timeout after which we give up.
/// - message: An optional message to include in the error if the condition is unexpectedly met.
/// - sourceLocation: The source location to attach to any recorded issues.
/// - until: callback that evaluates outputs until some condition is reached
/// - Returns: The deferred fulfilment to be executed after some actions. The sequence's result is not returned from this fulfilment.
func deferFailure<Value>(_ asyncSequence: any AsyncSequence<Value, Never>,
timeout: Duration,
message: String? = nil,
sourceLocation: SourceLocation = #_sourceLocation,
until condition: @escaping (Value) -> Bool) -> DeferredFulfillment<Void> {
let (stream, continuation) = AsyncStream<Void>.makeStream()
let task = Task {
for await value in asyncSequence where condition(value) {
continuation.yield(())
continuation.finish()
return
}
continuation.finish()
}
return DeferredFulfillment {
defer { task.cancel() }
try await withThrowingTaskGroup(of: Void.self) { group in
// If the condition fires before timeout, that's the unexpected failure.
group.addTask {
for await _ in stream {
throw DeferredFulfillmentError.unexpectedFulfillment(message: message, sourceLocation: sourceLocation)
}
}
// Timeout elapsing without the condition firing = success.
group.addTask {
try await Task.sleep(for: timeout)
}
defer { group.cancelAll() }
return try #require(try await group.next())
}
}
}