Files
element-x-ios/UnitTests/Sources/MapTilerURLBuilderTests.swift
T
Mauro a520d94760 Swift 6 Migration (#5733)
* Enable Swift 6.2 with default main actor isolation for the app target

Turn on SWIFT_VERSION 6.2, SWIFT_APPROACHABLE_CONCURRENCY and SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor for the ElementX target, matching the NSE and Share Extension (which already use 6.2 without the main actor default).

# Conflicts:
#	ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

* Update the code generation templates for Swift 6

Emit nonisolated/@unchecked Sendable mocks and nonisolated string enums, and mark async mock methods @concurrent so they keep suspending like the real implementations (otherwise intermediate states are unobservable in tests). Includes the regenerated mocks and strings.

# Conflicts:
#	ElementX/Sources/Mocks/Generated/GeneratedMocks.swift

* Make the data layer and services nonisolated and Sendable

Annotate the value-type models, Rust SDK proxy wrappers, timeline item graph, settings, logging and NSE-shared types as nonisolated, and conform the service protocols to Sendable so their existentials can cross isolation boundaries. These types run off the main actor and no longer inherit the target's default main actor isolation.

* Adopt the new isolation across call sites, mocks and signatures

Update call sites, closure signatures and mock configuration helpers to match the nonisolated/Sendable data layer, including the main actor isolated mock inits and the @Sendable filter on recentlyVisitedRooms.

^ Conflicts:
^	ElementX/Sources/Services/SecureBackup/SecureBackupController.swift

* Remove redundant @MainActor annotations

With the app target defaulting to main actor isolation, the explicit @MainActor on view models, coordinators, views and their protocols is no longer required.

* Fix concurrency hotspots surfaced by the migration

Resolve the room summary main-thread deadlock by moving diff processing onto AsyncStreams, add the SDKListener.onMainActor FIFO bridge for SDK callbacks, hop KVO/timer/UIKit callbacks to the main actor via assumeIsolated, add isolated deinits, and apply the few justified nonisolated(unsafe) escapes for queue-confined Apple APIs.

* Fix the unit tests for Swift 6 concurrency

Hop the waitForConfirmation body onto the caller's actor (Swift Testing runs confirmation closures off-actor), and adjust the affected suites and async signatures.

* offload diffs on concurrent threads instead of main actor

* make room info protocols sendable

* use a task group for fetching data so that everything runs concurrently in the TextRoomTimelineView when getting the preview.

* use the generic onMainActor implementation, which already makes a stream and send the continuation on the main actor.

* small pr comments issues being addressed

* update enterprise

* Update StaticCode to 1.1.0 and specify the required Swift isolation.

* fix ElementCallServiceTests

* update enterprise

* re-generate mocks

* update enterprise

* enterprise new code conformed to swift 6

---------

Co-authored-by: Doug <douglase@element.io>
2026-06-18 17:55:31 +02:00

143 lines
6.6 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 CoreLocation
@testable import ElementX
import Testing
@MainActor
struct MapTilerURLBuilderTests {
private static let baseURL: URL = "http://www.foo.com"
private static let apiKey = "some_key"
private static let lightStyleID = "9bc819c8-e627-474a-a348-ec144fe3d810"
private static let darkStyleID = "dea61faf-292b-4774-9660-58fcef89a7f3"
var builder: MapTilerURLBuilderProtocol
init() {
builder = MapTilerSettings.configuration(.init(baseURL: Self.baseURL,
apiKey: Self.apiKey,
lightStyleID: Self.lightStyleID,
darkStyleID: Self.darkStyleID))
}
@Test
func staticMapBuilder() {
let url = builder.staticMapTileImageURL(for: .light,
coordinates: .init(latitude: 1, longitude: 2),
zoomLevel: 5,
size: .init(width: 300, height: 200),
attribution: .hidden)
let expectedURL: URL = "http://www.foo.com/9bc819c8-e627-474a-a348-ec144fe3d810/static/2.000000,1.000000,5.000000/300x200@2x.png?key=some_key&attribution=false"
#expect(url == expectedURL)
}
@Test
func staticMapBuilderWithAttribution() {
let url = builder.staticMapTileImageURL(for: .dark,
coordinates: .init(latitude: 1, longitude: 2),
zoomLevel: 5,
size: .init(width: 300, height: 200),
attribution: .topLeft)
let expectedURL: URL = "http://www.foo.com/dea61faf-292b-4774-9660-58fcef89a7f3/static/2.000000,1.000000,5.000000/300x200@2x.png?key=some_key&attribution=topleft"
#expect(url == expectedURL)
}
@Test
func dynamicMapBuilder() {
let url = builder.interactiveMapURL(for: .dark)
let expectedURL: URL = "http://www.foo.com/dea61faf-292b-4774-9660-58fcef89a7f3/style.json?key=some_key"
#expect(url == expectedURL)
}
@Test
mutating func nilAPIKey() {
let configuration = MapTilerSettings.configuration(.init(baseURL: Self.baseURL,
apiKey: nil,
lightStyleID: Self.lightStyleID,
darkStyleID: Self.darkStyleID))
#expect(!configuration.isEnabled)
builder = configuration
let staticMapURL = builder.staticMapTileImageURL(for: .dark,
coordinates: .init(latitude: 1, longitude: 2),
zoomLevel: 5,
size: .init(width: 300, height: 200),
attribution: .topLeft)
#expect(staticMapURL == nil)
let dynamicMapURL = builder.interactiveMapURL(for: .light)
#expect(dynamicMapURL == nil)
}
// MARK: - Override URL
@Test
mutating func overrideInteractiveMap() {
let overrideURL: URL = "https://maps.example.com/maps/streets-v2/style.json?key=server_key"
let configuration = MapTilerSettings.url(overrideURL)
#expect(configuration.isEnabled)
builder = configuration
// The override is returned as-is for the interactive map, ignoring light/dark.
#expect(builder.interactiveMapURL(for: .light) == overrideURL)
#expect(builder.interactiveMapURL(for: .dark) == overrideURL)
}
@Test
mutating func overrideStaticMapPreservesQueryItems() {
// The override includes an embedded API key as a query item — it must be preserved
// when deriving the static map URL.
let overrideURL: URL = "https://maps.example.com/maps/streets-v2/style.json?key=server_key"
builder = MapTilerSettings.url(overrideURL)
let url = builder.staticMapTileImageURL(for: .light,
coordinates: .init(latitude: 1, longitude: 2),
zoomLevel: 5,
size: .init(width: 300, height: 200),
attribution: .topLeft)
let expectedURL: URL = "https://maps.example.com/maps/streets-v2/static/2.000000,1.000000,5.000000/300x200@2x.png?key=server_key&attribution=topleft"
#expect(url == expectedURL)
}
@Test
mutating func overrideStaticMapWithoutQueryItems() {
// An override without a key (e.g. self-hosted server proxying the key) still works.
let overrideURL: URL = "https://maps.example.com/maps/streets-v2/style.json"
builder = MapTilerSettings.url(overrideURL)
let url = builder.staticMapTileImageURL(for: .dark,
coordinates: .init(latitude: 1, longitude: 2),
zoomLevel: 5,
size: .init(width: 300, height: 200),
attribution: .hidden)
let expectedURL: URL = "https://maps.example.com/maps/streets-v2/static/2.000000,1.000000,5.000000/300x200@2x.png?attribution=false"
#expect(url == expectedURL)
}
@Test
mutating func overrideMissingStyleJSONFails() {
// If the override URL doesn't end in /style.json we can't derive a static URL.
let overrideURL: URL = "https://maps.example.com/maps/streets-v2/"
builder = MapTilerSettings.url(overrideURL)
let url = builder.staticMapTileImageURL(for: .light,
coordinates: .init(latitude: 1, longitude: 2),
zoomLevel: 5,
size: .init(width: 300, height: 200),
attribution: .topLeft)
#expect(url == nil)
}
}