Skip to content

Commit 59d4d8a

Browse files
Backfill more tests
1 parent cf9dcc0 commit 59d4d8a

File tree

4 files changed

+215
-3
lines changed

4 files changed

+215
-3
lines changed

MCMaps/Shared/Services/CartographyIntegrationService.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import CubiomesKit
9+
import Foundation
910
import MCMap
1011
import os
1112

@@ -90,7 +91,7 @@ actor CartographyIntegrationService {
9091
}
9192

9293
/// An enumeration of the errors that the service can throw when attempting to make requests.
93-
enum ServiceError: Error {
94+
enum ServiceError: Equatable, Error {
9495
/// The integration is disabled.
9596
case integrationDisabled
9697

@@ -102,6 +103,19 @@ actor CartographyIntegrationService {
102103
/// The fetch task failed.
103104
/// - Parameter error: The error that was thrown by the task.
104105
case fetchTaskFailed(Error)
106+
107+
static func == (lhs: ServiceError, rhs: ServiceError) -> Bool {
108+
switch (lhs, rhs) {
109+
case (.integrationDisabled, .integrationDisabled):
110+
return true
111+
case (.mismatchingService, .mismatchingService):
112+
return true
113+
case let (.fetchTaskFailed(lhsErr), .fetchTaskFailed(rhsErr)):
114+
return lhsErr.localizedDescription == rhsErr.localizedDescription
115+
default:
116+
return false
117+
}
118+
}
105119
}
106120

107121
/// The integration settings used to configure the service.
@@ -116,12 +130,16 @@ actor CartographyIntegrationService {
116130
/// Initialize a service of a given type with integration settings.
117131
/// - Parameter serviceType: The service type to use with the integration service.
118132
/// - Parameter integrationSettings: The settings for integrations used to configure this service.
119-
init(serviceType: ServiceType, integrationSettings: CartographyMapFile.Integrations) {
133+
init(
134+
serviceType: ServiceType,
135+
integrationSettings: CartographyMapFile.Integrations,
136+
session: any NetworkServiceable = URLSession(configuration: .default)
137+
) {
120138
self.serviceType = serviceType
121139
self.integrationSettings = integrationSettings
122140
switch serviceType {
123141
case .bluemap:
124-
self.service = CartographyBluemapService(withConfiguration: integrationSettings.bluemap)
142+
self.service = CartographyBluemapService(withConfiguration: integrationSettings.bluemap, session: session)
125143
}
126144
}
127145

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//
2+
// BluemapResultsTests.swift
3+
// MCMaps
4+
//
5+
// Created by Marquis Kurt on 05-08-2025.
6+
//
7+
8+
import CubiomesKit
9+
import MCMap
10+
import SwiftUI
11+
import Testing
12+
13+
@testable import Alidade
14+
15+
struct BluemapResultsTests {
16+
@Test func isEmpty() async throws {
17+
let bluemapResults = BluemapResults()
18+
#expect(bluemapResults.isEmpty)
19+
}
20+
21+
@Test func merged() async throws {
22+
let id = UUID()
23+
let original = BluemapResults()
24+
let newContent = BluemapResults(
25+
players: BluemapPlayerResponse(players: [
26+
BluemapPlayer(
27+
uuid: id,
28+
name: "lweiss",
29+
foreign: false,
30+
position: BluemapPosition(x: 10, y: 10, z: 10),
31+
rotation: BluemapPlayer.Rotation(pitch: 0, yaw: 0, roll: 0)
32+
)
33+
]))
34+
35+
let merged = original.merged(with: newContent)
36+
#expect(merged.players != nil)
37+
#expect(merged.markers == nil)
38+
}
39+
40+
@Test func annotations() async throws {
41+
let uuid = UUID()
42+
let results = BluemapResults(
43+
markers: [
44+
"foo": BluemapMarkerAnnotationGroup(
45+
label: "Foo",
46+
toggleable: true,
47+
defaultHidden: false,
48+
sorting: 0,
49+
markers: [
50+
"mymarker": BluemapMarkerAnnotation(
51+
classes: [""],
52+
label: "My Marker",
53+
position: BluemapPosition(x: 0, y: 0, z: 0),
54+
minDistance: 0,
55+
maxDistance: .greatestFiniteMagnitude,
56+
type: "poi",
57+
sorting: 0,
58+
listed: true
59+
)
60+
]
61+
)
62+
],
63+
players: BluemapPlayerResponse(
64+
players: [
65+
BluemapPlayer(
66+
uuid: uuid,
67+
name: "lweiss",
68+
foreign: false,
69+
position: BluemapPosition(x: 10, y: 10, z: 10),
70+
rotation: BluemapPlayer.Rotation(pitch: 0, yaw: 0, roll: 0)
71+
)
72+
]
73+
)
74+
)
75+
76+
let annotations = results.annotations(
77+
from: MCMapBluemapIntegration(
78+
baseURL: "https://example.com",
79+
enabled: true
80+
)
81+
)
82+
#expect(annotations.count == 2)
83+
84+
let first = try #require(annotations.last as? Marker)
85+
#expect(
86+
first
87+
== Marker(
88+
location: .zero,
89+
title: "My Marker",
90+
id: "mymarker",
91+
clusterIdentifier: "bmap-server-marker"
92+
)
93+
)
94+
95+
let last = try #require(annotations.first as? PlayerMarker)
96+
#expect(last == PlayerMarker(location: CGPoint(x: 10, y: 10), name: "lweiss", playerUUID: uuid))
97+
}
98+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// CartographyGalleryWindowContextTests.swift
3+
// MCMaps
4+
//
5+
// Created by Marquis Kurt on 05-08-2025.
6+
//
7+
8+
import Foundation
9+
import MCMap
10+
import Testing
11+
12+
@testable import Alidade
13+
14+
struct CartographyGalleryWindowContextTests {
15+
@Test func empty() async throws {
16+
let emptyContext = CartographyGalleryWindowContext.empty()
17+
#expect(emptyContext.imageCollection.isEmpty)
18+
#expect(emptyContext.documentBaseURL == nil)
19+
}
20+
21+
@Test func initFromFile() async throws {
22+
let file = CartographyMapFile(
23+
withManifest: MCMapManifest_v2(
24+
name: "My World",
25+
worldSettings: MCMapManifestWorldSettings(version: "1.21", seed: 123),
26+
pins: []
27+
),
28+
images: ["example": Data()]
29+
)
30+
let context = CartographyGalleryWindowContext(file: file, documentBaseURL: nil)
31+
#expect(!context.imageCollection.isEmpty)
32+
}
33+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// CartographyIntegrationServiceTests.swift
3+
// MCMaps
4+
//
5+
// Created by Marquis Kurt on 05-08-2025.
6+
//
7+
8+
import Foundation
9+
import MCMap
10+
import Testing
11+
12+
@testable import Alidade
13+
14+
struct CartographyIntegrationServiceTests {
15+
let service = MockNetworkServicable()
16+
let sampleFile = CartographyMapFile(withManifest: .sampleFile)
17+
18+
var integration: CartographyMapFile.Integrations {
19+
var original = sampleFile.integrations
20+
original.bluemap.baseURL = "bluemap.augenwaldburg.tld"
21+
original.bluemap.enabled = true
22+
original.bluemap.realtime = true
23+
original.bluemap.mapping.overworld = "world"
24+
return original
25+
}
26+
27+
@Test func syncNoopWhenDisabled() async throws {
28+
var disabled = integration
29+
disabled.bluemap.enabled = false
30+
31+
let service = CartographyIntegrationService(
32+
serviceType: .bluemap,
33+
integrationSettings: disabled,
34+
session: service
35+
)
36+
37+
await #expect(throws: CartographyIntegrationService.ServiceError.integrationDisabled) {
38+
let _: BluemapResults? = try await service.sync(dimension: .overworld)
39+
}
40+
}
41+
42+
@Test func bluemapRegularSync() async throws {
43+
let service = CartographyIntegrationService(
44+
serviceType: .bluemap,
45+
integrationSettings: integration,
46+
session: service
47+
)
48+
let response: BluemapResults? = try await service.sync(dimension: .overworld, syncType: .regular)
49+
#expect(response?.isEmpty == false)
50+
}
51+
52+
@Test func bluemapRealtimeSync() async throws {
53+
let service = CartographyIntegrationService(
54+
serviceType: .bluemap,
55+
integrationSettings: integration,
56+
session: service
57+
)
58+
let response: BluemapResults? = try await service.sync(dimension: .overworld, syncType: .realtime)
59+
#expect(response?.isEmpty == false)
60+
#expect(response?.markers == nil)
61+
#expect(response?.players != nil)
62+
}
63+
}

0 commit comments

Comments
 (0)