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>
This commit is contained in:
Copilot
2026-02-24 12:20:01 +00:00
committed by GitHub
parent 62f193eda1
commit e58c5801fc
38 changed files with 3460 additions and 3304 deletions
+119 -113
View File
@@ -8,10 +8,11 @@
import Combine
@testable import ElementX
import XCTest
import Testing
@Suite
@MainActor
class CreateRoomScreenViewModelTests: XCTestCase {
final class CreateRoomScreenViewModelTests {
var viewModel: CreateRoomScreenViewModelProtocol!
var clientProxy: ClientProxyMock!
var spaceService: SpaceServiceProxyMock!
@@ -23,7 +24,7 @@ class CreateRoomScreenViewModelTests: XCTestCase {
viewModel.context
}
override func tearDown() {
deinit {
AppSettings.resetAllSettings()
viewModel = nil
clientProxy = nil
@@ -31,28 +32,31 @@ class CreateRoomScreenViewModelTests: XCTestCase {
userSession = nil
}
func testDefaultState() {
@Test
func defaultState() {
setup()
XCTAssertEqual(context.viewState.bindings.selectedAccessType, .private)
XCTAssertNil(context.selectedSpace)
XCTAssertEqual(context.viewState.availableAccessTypes, [.public, .askToJoin, .private])
XCTAssertTrue(context.viewState.canSelectSpace)
#expect(context.viewState.bindings.selectedAccessType == .private)
#expect(context.selectedSpace == nil)
#expect(context.viewState.availableAccessTypes == [.public, .askToJoin, .private])
#expect(context.viewState.canSelectSpace)
}
func testCreateRoomRequirements() {
@Test
func createRoomRequirements() {
setup()
XCTAssertFalse(context.viewState.canCreateRoom)
#expect(!context.viewState.canCreateRoom)
context.send(viewAction: .updateRoomName("A"))
XCTAssertTrue(context.viewState.canCreateRoom)
#expect(context.viewState.canCreateRoom)
}
func testCreateRoom() async throws {
@Test
func createRoom() async throws {
setup()
// Given a form with a blank topic.
context.send(viewAction: .updateRoomName("A"))
context.roomTopic = ""
context.selectedAccessType = .private
XCTAssertTrue(context.viewState.canCreateRoom)
#expect(context.viewState.canCreateRoom)
// When creating the room.
clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReturnValue = .success("1")
@@ -64,14 +68,15 @@ class CreateRoomScreenViewModelTests: XCTestCase {
try await deferred.fulfill()
// Then the room should be created and a topic should not be set.
XCTAssertTrue(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartCalled)
XCTAssertEqual(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.name, "A")
XCTAssertNil(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.topic,
"The topic should be sent as nil when it is empty.")
XCTAssertEqual(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.accessType, .private)
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartCalled)
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.name == "A")
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.topic == nil,
"The topic should be sent as nil when it is empty.")
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.accessType == .private)
}
func testCreateSpace() async throws {
@Test
func createSpace() async throws {
setup(isSpace: true)
clientProxy = ClientProxyMock(.init(userIDServerName: "matrix.org",
userID: "@a:b.com",
@@ -92,7 +97,7 @@ class CreateRoomScreenViewModelTests: XCTestCase {
context.send(viewAction: .updateRoomName("A"))
context.roomTopic = ""
context.selectedAccessType = .private
XCTAssertTrue(context.viewState.canCreateRoom)
#expect(context.viewState.canCreateRoom)
// When creating the room.
clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReturnValue = .success("1")
@@ -106,14 +111,15 @@ class CreateRoomScreenViewModelTests: XCTestCase {
try await deferred.fulfill()
// Then the room should be created and a topic should not be set.
XCTAssertTrue(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartCalled)
XCTAssertEqual(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.name, "A")
XCTAssertNil(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.topic,
"The topic should be sent as nil when it is empty.")
XCTAssertEqual(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.accessType, .private)
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartCalled)
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.name == "A")
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.topic == nil,
"The topic should be sent as nil when it is empty.")
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.accessType == .private)
}
func testCreateKnockingRoom() async {
@Test
func createKnockingRoom() async {
setup()
context.send(viewAction: .updateRoomName("A"))
context.roomTopic = "B"
@@ -121,20 +127,21 @@ class CreateRoomScreenViewModelTests: XCTestCase {
// When setting the room as private we always reset the knocking state to the default value of false
// so we need to wait a main actor cycle to ensure the view state is updated
await Task.yield()
XCTAssertTrue(context.viewState.canCreateRoom)
#expect(context.viewState.canCreateRoom)
let expectation = expectation(description: "Wait for the room to be created")
clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartClosure = { _, _, accessType, _, _, _, localAliasPart in
XCTAssertEqual(accessType, .askToJoin)
XCTAssertEqual(localAliasPart, "a")
defer { expectation.fulfill() }
return .success("")
await waitForConfirmation("Wait for the room to be created") { confirmation in
clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartClosure = { _, _, accessType, _, _, _, localAliasPart in
#expect(accessType == .askToJoin)
#expect(localAliasPart == "a")
defer { confirmation() }
return .success("")
}
context.send(viewAction: .createRoom)
}
context.send(viewAction: .createRoom)
await fulfillment(of: [expectation])
}
func testCreatePublicRoomFailsForInvalidAlias() async throws {
@Test
func createPublicRoomFailsForInvalidAlias() async throws {
setup()
context.send(viewAction: .updateRoomName("A"))
context.roomTopic = "B"
@@ -154,10 +161,11 @@ class CreateRoomScreenViewModelTests: XCTestCase {
// blocked it
context.send(viewAction: .createRoom)
await Task.yield()
XCTAssertFalse(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartCalled)
#expect(!clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartCalled)
}
func testCreatePublicRoomFailsForExistingAlias() async throws {
@Test
func createPublicRoomFailsForExistingAlias() async throws {
setup()
clientProxy.isAliasAvailableReturnValue = .success(false)
context.send(viewAction: .updateRoomName("A"))
@@ -176,61 +184,61 @@ class CreateRoomScreenViewModelTests: XCTestCase {
// We also want to force the room creation in case the user may tap the button before the debounce
// blocked it
let expectation = expectation(description: "Wait for the alias to be checked again")
clientProxy.isAliasAvailableClosure = { _ in
defer {
expectation.fulfill()
await waitForConfirmation("Wait for the alias to be checked again") { confirmation in
clientProxy.isAliasAvailableClosure = { _ in
defer { confirmation() }
return .success(false)
}
return .success(false)
context.send(viewAction: .createRoom)
}
context.send(viewAction: .createRoom)
await fulfillment(of: [expectation])
XCTAssertEqual(clientProxy.isAliasAvailableCallsCount, 2)
XCTAssertFalse(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartCalled)
#expect(clientProxy.isAliasAvailableCallsCount == 2)
#expect(!clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartCalled)
}
func testNameAndAddressSync() async {
@Test
func nameAndAddressSync() async {
setup()
context.selectedAccessType = .private
await Task.yield()
context.send(viewAction: .updateRoomName("abc"))
XCTAssertEqual(context.viewState.aliasLocalPart, "abc")
XCTAssertEqual(context.viewState.roomName, "abc")
#expect(context.viewState.aliasLocalPart == "abc")
#expect(context.viewState.roomName == "abc")
context.send(viewAction: .updateRoomName("DEF"))
XCTAssertEqual(context.viewState.roomName, "DEF")
XCTAssertEqual(context.viewState.aliasLocalPart, "def")
#expect(context.viewState.roomName == "DEF")
#expect(context.viewState.aliasLocalPart == "def")
context.send(viewAction: .updateRoomName("a b c"))
XCTAssertEqual(context.viewState.aliasLocalPart, "a-b-c")
XCTAssertEqual(context.viewState.roomName, "a b c")
#expect(context.viewState.aliasLocalPart == "a-b-c")
#expect(context.viewState.roomName == "a b c")
context.send(viewAction: .updateAliasLocalPart("hello-world"))
// This removes the sync
XCTAssertEqual(context.viewState.aliasLocalPart, "hello-world")
XCTAssertEqual(context.viewState.roomName, "a b c")
#expect(context.viewState.aliasLocalPart == "hello-world")
#expect(context.viewState.roomName == "a b c")
context.send(viewAction: .updateRoomName("Hello Matrix!"))
XCTAssertEqual(context.viewState.aliasLocalPart, "hello-world")
XCTAssertEqual(context.viewState.roomName, "Hello Matrix!")
#expect(context.viewState.aliasLocalPart == "hello-world")
#expect(context.viewState.roomName == "Hello Matrix!")
// Deleting the whole name will restore the sync
context.send(viewAction: .updateRoomName(""))
XCTAssertEqual(context.viewState.aliasLocalPart, "")
XCTAssertEqual(context.viewState.roomName, "")
#expect(context.viewState.aliasLocalPart == "")
#expect(context.viewState.roomName == "")
context.send(viewAction: .updateRoomName("Hello# Matrix!"))
XCTAssertEqual(context.viewState.aliasLocalPart, "hello-matrix!")
XCTAssertEqual(context.viewState.roomName, "Hello# Matrix!")
#expect(context.viewState.aliasLocalPart == "hello-matrix!")
#expect(context.viewState.roomName == "Hello# Matrix!")
}
func testCreateRoomInASelectedSpaceFromTheList() async throws {
@Test
func createRoomInASelectedSpaceFromTheList() async throws {
let spaces = [SpaceServiceRoom].mockJoinedSpaces2
setup()
context.send(viewAction: .updateRoomName("A"))
context.selectedAccessType = .public
XCTAssertTrue(context.viewState.canCreateRoom)
XCTAssertNil(context.selectedSpace)
XCTAssertEqual(context.viewState.availableAccessTypes, [.public, .askToJoin, .private])
XCTAssertTrue(context.viewState.canSelectSpace)
#expect(context.viewState.canCreateRoom)
#expect(context.selectedSpace == nil)
#expect(context.viewState.availableAccessTypes == [.public, .askToJoin, .private])
#expect(context.viewState.canSelectSpace)
var deferred = deferFulfillment(context.$viewState) { viewState in
viewState.editableSpaces.map(\.id) == spaces.map(\.id)
@@ -248,64 +256,62 @@ class CreateRoomScreenViewModelTests: XCTestCase {
// When creating the room.
clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReturnValue = .success("1")
let expectation = expectation(description: "Wait for the addChild function to be called")
spaceService.addChildToClosure = { roomID, spaceID in
defer { expectation.fulfill() }
XCTAssertEqual(roomID, "1")
XCTAssertEqual(spaceID, spaces[0].id)
return .success(())
try await confirmation("Wait for the addChild function to be called") { confirm in
let deferredAction = deferFulfillment(viewModel.actions) { action in
guard case .createdRoom(let roomProxy, nil) = action, roomProxy.id == "1" else { return false }
return true
}
spaceService.addChildToClosure = { roomID, spaceID in
defer { confirm() }
#expect(roomID == "1")
#expect(spaceID == spaces[0].id)
return .success(())
}
context.send(viewAction: .createRoom)
try await deferredAction.fulfill()
}
let deferredAction = deferFulfillment(viewModel.actions) { action in
guard case .createdRoom(let roomProxy, nil) = action, roomProxy.id == "1" else { return false }
return true
}
context.send(viewAction: .createRoom)
await fulfillment(of: [expectation])
try await deferredAction.fulfill()
XCTAssertTrue(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartCalled)
XCTAssertEqual(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.name, "A")
XCTAssertEqual(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.accessType, .private)
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartCalled)
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.name == "A")
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.accessType == .private)
}
func testCreateRoomInAnAlreadySelectedSpace() async throws {
@Test
func createRoomInAnAlreadySelectedSpace() async throws {
let space = SpaceServiceRoom.mock(isSpace: true, joinRule: .invite)
setup(spacesSelectionMode: .editableSpacesList(preSelectedSpace: space))
context.send(viewAction: .updateRoomName("A"))
context.selectedAccessType = .spaceMembers
XCTAssertTrue(context.viewState.canCreateRoom)
XCTAssertEqual(context.selectedSpace?.id, space.id)
XCTAssertEqual(context.viewState.availableAccessTypes, [.spaceMembers, .askToJoinWithSpaceMembers, .private])
XCTAssertTrue(context.viewState.canSelectSpace)
#expect(context.viewState.canCreateRoom)
#expect(context.selectedSpace?.id == space.id)
#expect(context.viewState.availableAccessTypes == [.spaceMembers, .askToJoinWithSpaceMembers, .private])
#expect(context.viewState.canSelectSpace)
// When creating the room.
clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReturnValue = .success("1")
let expectation = expectation(description: "Wait for the addChild function to be called")
spaceService.addChildToClosure = { roomID, spaceID in
defer { expectation.fulfill() }
XCTAssertEqual(roomID, "1")
XCTAssertEqual(spaceID, space.id)
return .success(())
try await confirmation("Wait for the addChild function to be called") { confirm in
let deferredAction = deferFulfillment(viewModel.actions) { action in
guard case .createdRoom(let roomProxy, nil) = action, roomProxy.id == "1" else { return false }
return true
}
spaceService.addChildToClosure = { roomID, spaceID in
defer { confirm() }
#expect(roomID == "1")
#expect(spaceID == space.id)
return .success(())
}
context.send(viewAction: .createRoom)
try await deferredAction.fulfill()
}
let deferredAction = deferFulfillment(viewModel.actions) { action in
guard case .createdRoom(let roomProxy, nil) = action, roomProxy.id == "1" else { return false }
return true
}
context.send(viewAction: .createRoom)
await fulfillment(of: [expectation])
try await deferredAction.fulfill()
XCTAssertTrue(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartCalled)
XCTAssertEqual(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.name, "A")
XCTAssertEqual(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.accessType, .spaceMembers(spaceID: space.id))
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartCalled)
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.name == "A")
#expect(clientProxy.createRoomNameTopicAccessTypeIsSpaceUserIDsAvatarURLAliasLocalPartReceivedArguments?.accessType == .spaceMembers(spaceID: space.id))
}
func testCreateRoomInAnPublicSpaceAvailableTypes() {
@Test
func createRoomInAnPublicSpaceAvailableTypes() {
let space = SpaceServiceRoom.mock(isSpace: true, joinRule: .public)
setup(spacesSelectionMode: .editableSpacesList(preSelectedSpace: space))
@@ -313,10 +319,10 @@ class CreateRoomScreenViewModelTests: XCTestCase {
context.send(viewAction: .updateRoomName("A"))
context.roomTopic = ""
context.selectedAccessType = .spaceMembers
XCTAssertTrue(context.viewState.canCreateRoom)
XCTAssertEqual(context.selectedSpace?.id, space.id)
XCTAssertEqual(context.viewState.availableAccessTypes, [.public, .askToJoin, .private])
XCTAssertTrue(context.viewState.canSelectSpace)
#expect(context.viewState.canCreateRoom)
#expect(context.selectedSpace?.id == space.id)
#expect(context.viewState.availableAccessTypes == [.public, .askToJoin, .private])
#expect(context.viewState.canSelectSpace)
}
private func setup(isSpace: Bool = false, spacesSelectionMode: CreateRoomScreenSpaceSelectionMode = .editableSpacesList(preSelectedSpace: nil)) {