Swift Testing for Unit Tests PART 1 (#5119)

* migrated a lot of unit tests to Swift Testing and added a new implementation for deferred fulfillment

more tests migration

Cleaned the code manually to establish some good patterns

more code improvements

some more code improvements

removed empty tests

update project

* more pr suggestions and cleanups

* removed the TestSetup pattern

* fixing claude not reusing tests

* pr suggestion + added indent rule to swiftformat so that we can prevent AIs to change that
This commit is contained in:
Mauro
2026-02-19 16:20:47 +01:00
committed by GitHub
parent c92e847ed7
commit 173b39a07f
118 changed files with 4630 additions and 4129 deletions

View File

@@ -9,13 +9,14 @@
import Combine
@testable import ElementX
import Foundation
import XCTest
import Testing
class BugReportServiceTests: XCTestCase {
@Suite
final class BugReportServiceTests {
var appSettings: AppSettings!
var bugReportService: BugReportServiceProtocol!
override func setUpWithError() throws {
init() throws {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings.bugReportRageshakeURL.reset()
@@ -26,15 +27,17 @@ class BugReportServiceTests: XCTestCase {
bugReportService = bugReportServiceMock
}
override func tearDown() {
deinit {
appSettings.bugReportRageshakeURL.reset()
}
func testInitialStateWithMockService() {
XCTAssertFalse(bugReportService.crashedLastRun)
@Test
func initialStateWithMockService() {
#expect(!bugReportService.crashedLastRun)
}
func testSubmitBugReportWithMockService() async throws {
@Test
func submitBugReportWithMockService() async throws {
let bugReport = BugReport(userID: "@mock:client.com",
deviceID: nil,
ed25519: nil,
@@ -46,40 +49,43 @@ class BugReportServiceTests: XCTestCase {
files: [])
let progressSubject = CurrentValueSubject<Double, Never>(0.0)
let response = try await bugReportService.submitBugReport(bugReport, progressListener: progressSubject).get()
let reportURL = try XCTUnwrap(response.reportURL)
XCTAssertFalse(reportURL.isEmpty)
let reportURL = try #require(response.reportURL)
#expect(!reportURL.isEmpty)
}
func testInitialStateWithRealService() {
@Test
func initialStateWithRealService() {
let urlPublisher: CurrentValueSubject<RageshakeConfiguration, Never> = .init(.url("https://example.com/submit"))
let service = BugReportService(rageshakeURLPublisher: urlPublisher.asCurrentValuePublisher(),
applicationID: "mock_app_id",
sdkGitSHA: "1234",
session: .mock,
appHooks: AppHooks())
XCTAssertTrue(service.isEnabled)
XCTAssertFalse(service.crashedLastRun)
#expect(service.isEnabled)
#expect(!service.crashedLastRun)
}
func testInitialStateWithRealServiceAndDisabled() {
@Test
func initialStateWithRealServiceAndDisabled() {
let urlPublisher: CurrentValueSubject<RageshakeConfiguration, Never> = .init(.disabled)
let service = BugReportService(rageshakeURLPublisher: urlPublisher.asCurrentValuePublisher(),
applicationID: "mock_app_id",
sdkGitSHA: "1234",
session: .mock,
appHooks: AppHooks())
XCTAssertFalse(service.isEnabled)
XCTAssertFalse(service.crashedLastRun)
#expect(!service.isEnabled)
#expect(!service.crashedLastRun)
}
@MainActor func testSubmitBugReportWithRealService() async throws {
@Test @MainActor
func submitBugReportWithRealService() async throws {
let urlPublisher: CurrentValueSubject<RageshakeConfiguration, Never> = .init(.url("https://example.com/submit"))
let service = BugReportService(rageshakeURLPublisher: urlPublisher.asCurrentValuePublisher(),
applicationID: "mock_app_id",
sdkGitSHA: "1234",
session: .mock,
appHooks: AppHooks())
let bugReport = BugReport(userID: "@mock:client.com",
deviceID: nil,
ed25519: nil,
@@ -92,12 +98,14 @@ class BugReportServiceTests: XCTestCase {
let progressSubject = CurrentValueSubject<Double, Never>(0.0)
let response = try await service.submitBugReport(bugReport, progressListener: progressSubject).get()
XCTAssertEqual(response.reportURL, "https://example.com/123")
#expect(response.reportURL == "https://example.com/123")
}
@MainActor func testConfigurations() async throws {
@Test
@MainActor
func configurations() async throws {
guard case let .url(initialURL) = appSettings.bugReportRageshakeURL.publisher.value else {
XCTFail("Unexpected initial configuration.")
Issue.record("Unexpected initial configuration.")
return
}
@@ -106,14 +114,14 @@ class BugReportServiceTests: XCTestCase {
sdkGitSHA: "1234",
session: .mock,
appHooks: AppHooks())
XCTAssertTrue(service.isEnabled)
#expect(service.isEnabled)
appSettings.bugReportRageshakeURL.applyRemoteValue(.disabled)
XCTAssertFalse(service.isEnabled)
#expect(!service.isEnabled)
appSettings.bugReportRageshakeURL.applyRemoteValue(.url("https://bugs.server.net/submit"))
XCTAssertTrue(service.isEnabled)
#expect(service.isEnabled)
let bugReport = BugReport(userID: "@mock:client.com",
deviceID: nil,
ed25519: nil,
@@ -126,14 +134,14 @@ class BugReportServiceTests: XCTestCase {
let progressSubject = CurrentValueSubject<Double, Never>(0.0)
let customConfigurationResponse = try await service.submitBugReport(bugReport, progressListener: progressSubject).get()
XCTAssertEqual(customConfigurationResponse.reportURL, "https://bugs.server.net/123")
#expect(customConfigurationResponse.reportURL == "https://bugs.server.net/123")
appSettings.bugReportRageshakeURL.reset()
XCTAssertTrue(service.isEnabled)
#expect(service.isEnabled)
let defaultConfigurationResponse = try await service.submitBugReport(bugReport, progressListener: progressSubject).get()
XCTAssertEqual(defaultConfigurationResponse.reportURL, initialURL.absoluteString.replacingOccurrences(of: "submit", with: "123"))
#expect(defaultConfigurationResponse.reportURL == initialURL.absoluteString.replacingOccurrences(of: "submit", with: "123"))
}
}
@@ -150,15 +158,15 @@ private class MockURLProtocol: URLProtocol {
client?.urlProtocolDidFinishLoading(self)
}
}
override func stopLoading() {
// no-op
}
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
request
}
override class func canInit(with request: URLRequest) -> Bool {
true
}