Files
element-x-ios/UnitTests/Sources/BugReportServiceTests.swift
Sedat-Qaja 3b7ed0a48b fix: remove rlement endpoints and links from the HealthChat
* fix(healthchat): switch off analytics, crash and bug reporting endpoints

Upstream ships *.localhost placeholders in Secrets.swift and marks the file assume-unchanged; with analytics on, the Element Call widget was handed Element's own PostHog and Sentry. nil hides the analytics prompt, the Analytics and Report a problem entries until HealthChat runs its own services.

* feat(healthchat): hide the Labs entry in settings

* fix(healthchat): point Copyright and the analytics terms at healthchat.ch

* docs(healthchat): add the 26.11.1 release notes

* feat(healthchat): drop the Element help links

* feat(healthchat): switch off location sharing

* test(healthchat): skip the analytics suite without PostHog
2026-10-19 14:40:20 +02:00

189 lines
8.1 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
@MainActor
final class BugReportServiceTests {
var userSettings: UserSettings!
var bugReportService: BugReportServiceProtocol!
init() throws {
userSettings = UserSettings.mock()
userSettings.app.bugReportRageshakeURL.reset()
let bugReportServiceMock = BugReportServiceMock()
bugReportServiceMock.lastCrashEventIDSubject = .init(nil)
bugReportServiceMock.submitBugReportProgressListenerReturnValue = .success(SubmitBugReportResponse(reportURL: "https://www.example.com/123"))
bugReportService = bugReportServiceMock
}
deinit {
userSettings.app.bugReportRageshakeURL.reset()
}
@Test
func initialStateWithMockService() {
#expect(bugReportService.lastCrashEventIDSubject.value == nil)
}
@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(bugReportService.lastCrashEventIDSubject.value == nil)
}
@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(bugReportService.lastCrashEventIDSubject.value == nil)
}
@Test
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 {
// HealthChat ships without a rageshake server (Secrets.rageshakeURL is nil), so the default
// configuration is `.disabled`; the transitions below are exercised from remote values instead.
let initialURL: URL? = if case let .url(url) = userSettings.app.bugReportRageshakeURL.publisher.value {
url
} else {
nil
}
let service = BugReportService(rageshakeURLPublisher: userSettings.app.bugReportRageshakeURL.publisher,
applicationID: "mock_app_id",
sdkGitSHA: "1234",
session: .mock,
appHooks: AppHooks())
#expect(service.isEnabled == (initialURL != nil))
userSettings.app.bugReportRageshakeURL.applyRemoteValue(.disabled)
#expect(!service.isEnabled)
userSettings.app.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")
userSettings.app.bugReportRageshakeURL.reset()
guard let initialURL else {
#expect(!service.isEnabled)
return
}
#expect(service.isEnabled)
let defaultConfigurationResponse = try await service.submitBugReport(bugReport, progressListener: progressSubject).get()
#expect(defaultConfigurationResponse.reportURL == initialURL.absoluteString.replacingOccurrences(of: "submit", with: "123"))
}
}
private nonisolated 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)
}
}