Files
element-x-ios/UnitTests/Sources/BugReportServiceTests.swift
T
Michael 22d14ff8e4 AppSettings are Mockable (#5589)
* create DependenciesProtocol and replace ServiceLocator registration with it

replace mocking calls for ServiceLocator to Dependencies

replace ServiceLocator with instance level calls

replace ServiceLocator configs with TestDependencies config in Tests

replace servicelocator usage in analytics service

replace remaining ServiceLocator calls in unit tests

that depended on the UnitTestAppCoordinator configuration with per suite dependencies instances

fix swiftlint warning

remove remaining references to ServiceLocator

make dependencies properties consistent in tests

delete Dependencies and DependenciesProtocol

fix whitespace modifications

updates from PR feedback

a pass over converting AppSettings and AnalyticsService test instance properties to per test instantiation

fix a missed PR feedback

Sync up after rebase

create DependenciesProtocol and replace ServiceLocator registration with it

replace mocking calls for ServiceLocator to Dependencies

replace ServiceLocator with instance level calls

replace ServiceLocator configs with TestDependencies config in Tests

replace servicelocator usage in analytics service

replace remaining ServiceLocator calls in unit tests

that depended on the UnitTestAppCoordinator configuration with per suite dependencies instances

fix swiftlint warning

remove remaining references to ServiceLocator

make dependencies properties consistent in tests

delete Dependencies and DependenciesProtocol

* AppSettings is instance level mockable now

* replace mocked and preview usages of reset + AppSettings global init with

AppSettings(store: UserDefaultsMock())

* AppSettings.UserDefaultsKeys is now a RawRep struct, making it extensible

* update tests with new AppSettings so it builds

* update remaining AppSettings test/mock inits

* AppSettings.UserDefaultsKey conformed to ExpressibleByStringLiteral

* addressed issues found in first pass ai review

* update AppSettings instantiations after rebase

* fix accessibility tests coordinator after rebase

* Rename to VolatileUserDefaults

* AppSettings init uses mandatory parameter

* Revert UserDefaultsKey to enum

* Add TrackedUserDefaults to be able to reset the defaults object

* Added volatile static func to AppSettings

* Use .volatile() static func instead of full init

* review fixups

* Resolve build issues after rebase

* updated enterprise module

* remove the static reset settings call from the preview tests

---------

Co-authored-by: Mauro Romito <mauro.romito@element.io>
2026-05-22 15:14:08 +02:00

181 lines
7.6 KiB
Swift

//
// Copyright 2025 Element Creations Ltd.
// Copyright 2022-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
@testable import ElementX
import Foundation
import Testing
final class BugReportServiceTests {
var appSettings: AppSettings!
var bugReportService: BugReportServiceProtocol!
init() throws {
appSettings = AppSettings.volatile()
appSettings.bugReportRageshakeURL.reset()
let bugReportServiceMock = BugReportServiceMock()
bugReportServiceMock.underlyingCrashedLastRun = false
bugReportServiceMock.submitBugReportProgressListenerReturnValue = .success(SubmitBugReportResponse(reportURL: "https://www.example.com/123"))
bugReportService = bugReportServiceMock
}
deinit {
appSettings.bugReportRageshakeURL.reset()
}
@Test
func initialStateWithMockService() {
#expect(!bugReportService.crashedLastRun)
}
@Test
func submitBugReportWithMockService() async throws {
let bugReport = BugReport(userID: "@mock:client.com",
deviceID: nil,
ed25519: nil,
curve25519: nil,
text: "i cannot send message",
logFiles: [URL(filePath: "/logs/1.log"), URL(filePath: "/logs/2.log")],
canContact: false,
githubLabels: [],
files: [])
let progressSubject = CurrentValueSubject<Double, Never>(0.0)
let response = try await bugReportService.submitBugReport(bugReport, progressListener: progressSubject).get()
let reportURL = try #require(response.reportURL)
#expect(!reportURL.isEmpty)
}
@Test
@MainActor
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())
#expect(service.isEnabled)
#expect(!service.crashedLastRun)
}
@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())
#expect(!service.isEnabled)
#expect(!service.crashedLastRun)
}
@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,
curve25519: nil,
text: "i cannot send message",
logFiles: Tracing.logFiles,
canContact: false,
githubLabels: [],
files: [])
let progressSubject = CurrentValueSubject<Double, Never>(0.0)
let response = try await service.submitBugReport(bugReport, progressListener: progressSubject).get()
#expect(response.reportURL == "https://example.com/123")
}
@Test
@MainActor
func configurations() async throws {
guard case let .url(initialURL) = appSettings.bugReportRageshakeURL.publisher.value else {
Issue.record("Unexpected initial configuration.")
return
}
let service = BugReportService(rageshakeURLPublisher: appSettings.bugReportRageshakeURL.publisher,
applicationID: "mock_app_id",
sdkGitSHA: "1234",
session: .mock,
appHooks: AppHooks())
#expect(service.isEnabled)
appSettings.bugReportRageshakeURL.applyRemoteValue(.disabled)
#expect(!service.isEnabled)
appSettings.bugReportRageshakeURL.applyRemoteValue(.url("https://bugs.server.net/submit"))
#expect(service.isEnabled)
let bugReport = BugReport(userID: "@mock:client.com",
deviceID: nil,
ed25519: nil,
curve25519: nil,
text: "i cannot send message",
logFiles: Tracing.logFiles,
canContact: false,
githubLabels: [],
files: [])
let progressSubject = CurrentValueSubject<Double, Never>(0.0)
let customConfigurationResponse = try await service.submitBugReport(bugReport, progressListener: progressSubject).get()
#expect(customConfigurationResponse.reportURL == "https://bugs.server.net/123")
appSettings.bugReportRageshakeURL.reset()
#expect(service.isEnabled)
let defaultConfigurationResponse = try await service.submitBugReport(bugReport, progressListener: progressSubject).get()
#expect(defaultConfigurationResponse.reportURL == initialURL.absoluteString.replacingOccurrences(of: "submit", with: "123"))
}
}
private class MockURLProtocol: URLProtocol {
override func startLoading() {
guard let url = request.url else { return }
let reportURL = url.deletingLastPathComponent().appending(path: "123")
let response = "{\"report_url\":\"\(reportURL.absoluteString)\"}"
if let data = response.data(using: .utf8),
let urlResponse = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil) {
client?.urlProtocol(self, didReceive: urlResponse, cacheStoragePolicy: .allowedInMemoryOnly)
client?.urlProtocol(self, didLoad: data)
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
}
}
private extension URLSession {
static var mock: URLSession {
let configuration = URLSessionConfiguration.default
configuration.protocolClasses = [MockURLProtocol.self] + (configuration.protocolClasses ?? [])
return URLSession(configuration: configuration)
}
}