Repository Public License
1// ORIX Convert — Packages/EngineGeo/Tests/EngineGeoTests/PathTests.swift2// Description: Every declared path converts a hand-written fixture, passes validation, and gives back the exact3// source coordinates and attribute values. Also covers the engine surface (paths, options, inspect).4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import Testing7import ConvertCore8@testable import EngineGeo910struct Pair: Sendable, CustomStringConvertible {11 let source: FormatID, destination: FormatID12 var description: String { "\(source)→\(destination)" }13 static let all: [Pair] = GeoEngine.formats.flatMap { s in GeoEngine.formats.filter { $0 != s }.map { Pair(source: s, destination: $0) } }14}1516@Suite struct PathTests {17 @Test func declaresEveryOrderedPair() {18 let engine = GeoEngine()19 #expect(engine.paths.count == 36) // 6 × 5 ordered pairs + KMZ (source only) to the six formats20 #expect(Set(engine.paths.map(\.id)).count == 36)21 #expect(makeEngines().first?.id == "geo")22 for path in engine.paths {23 #expect(!engine.options(for: path).isEmpty)24 #expect(Set(engine.options(for: path).map(\.id)).count == engine.options(for: path).count)25 }26 }2728 @Test(arguments: Pair.all) func convertsCities(_ pair: Pair) async throws {29 let temp = TempDir()30 let source = Cities.source(pair.source, in: temp)31 let result = try await convert(source, pair.source, to: pair.destination, in: temp)32 let features = result.features33 #expect(features.count == 3)34 // Exact coordinates, in order.35 for (i, f) in features.enumerated() {36 guard case .point(let c)? = f.geometry else { Issue.record("\(pair): feature \(i) is not a point"); continue }37 #expect(c.x == Cities.xyz[i].0 && c.y == Cities.xyz[i].1, "\(pair): xy of feature \(i) = \(c.x), \(c.y)")38 #expect(c.z == Cities.xyz[i].2, "\(pair): z of feature \(i) = \(String(describing: c.z))")39 #expect(f["name"] == .string(Cities.names[i]), "\(pair): name of feature \(i) = \(String(describing: f["name"]))")40 }41 #expect(result.output.facts["geo.features"] == "3")42 #expect(result.output.facts["geo.source-crs"] == "EPSG:4326 (WGS 84)")43 #expect(result.output.facts["geo.destination-crs"] == "EPSG:4326 (WGS 84)")44 #expect(result.output.facts["geo.transformation"] == "none")45 if pair.source != .gpx, pair.destination != .gpx {46 for (i, f) in features.enumerated() {47 #expect(GeoSummary.canonical(f["pop"] ?? .null) == String(Cities.pop[i]), "\(pair): pop")48 #expect(GeoSummary.canonical(f["score"] ?? .null) == GeoSummary.canonical(.real(Cities.score[i])), "\(pair): score")49 #expect(GeoSummary.canonical(f["flag"] ?? .null) == (Cities.flag[i] ? "true" : "false"), "\(pair): flag")50 #expect(GeoSummary.canonical(f["founded"] ?? .null) == Cities.founded[i], "\(pair): founded")51 }52 #expect(result.warnings.all.filter { $0.severity == .dataLoss }.isEmpty, "\(pair): unexpected data-loss warnings \(result.warnings.codes)")53 } else if pair.destination == .gpx, pair.source != .gpx {54 // pop, score, flag, founded × 3 features cannot be stored in GPX.55 let w = try #require(result.warnings.first("gpx-attributes-dropped"))56 #expect(w.severity == .dataLoss)57 #expect(w.message.hasPrefix("12 attribute value(s)"), "\(w.message)")58 }59 // Declared types survive in typed destinations.60 if pair.source != .gpx, [.geojson, .geopackage, .shapefile].contains(pair.destination) {61 #expect(features[0]["pop"] == .integer(1_762_949))62 #expect(features[0]["score"] == .real(8.25))63 #expect(features[1]["flag"] == .bool(false))64 }65 if pair.destination == .shapefile {66 let folder = try #require(result.output.files.first)67 let names = try FileManager.default.contentsOfDirectory(atPath: folder.path).sorted()68 #expect(names == ["cities.cpg", "cities.dbf", "cities.prj", "cities.shp", "cities.shx"])69 }70 }7172 @Test(arguments: GeoEngine.formats) func inspectsAndPreviews(_ format: FormatID) async throws {73 let temp = TempDir()74 let source = Cities.source(format, in: temp)75 let inspection = try await GeoEngine().inspect(source, format: format)76 #expect(inspection.rowCount == 3)77 #expect(inspection.fields.contains { $0.key == "Reference system" && $0.value.contains("EPSG:4326") })78 #expect(inspection.fields.contains { $0.key == "Geometry types" && $0.value == "Point × 3" })79 let preview = try GeoPreview.preview(url: source, format: format, limit: 2)80 #expect(preview.layers.count == 1)81 let layer = try #require(preview.layers.first)82 #expect(layer.featureCount == 3 && layer.features.count == 2)83 #expect(layer.hasZ)84 #expect(layer.boundingBox.minX == -75.6972 && layer.boundingBox.maxY == 46.8139)85 #expect(layer.features[0].geometryType == "Point")86 #expect(layer.features[0].geometryText == "POINT Z (-73.5673 45.5017 36.5)")87 #expect(layer.features[0].attributes.first { $0.name == "name" }?.value == .string("Montréal"))88 if format != .gpx { #expect(layer.fields.map(\.name).contains("pop")) }89 }9091 @Test func cancellationStopsConversion() async throws {92 let temp = TempDir()93 var text = "{\"type\":\"FeatureCollection\",\"features\":[\n"94 for i in 0..<60_000 { text += "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[\(Double(i) / 1000),45]},\"properties\":{\"i\":\(i)}}\(i == 59_999 ? "" : ",")\n" }95 text += "]}"96 let source = temp.write("big.geojson", text)97 let work = temp.sub("work")98 let task = Task {99 try await GeoEngine().convert(ConversionRequest(source: source, sourceFormat: .geojson, destinationFormat: .geopackage), context: ConversionContext(workDirectory: work))100 }101 task.cancel()102 await #expect(throws: CancellationError.self) { _ = try await task.value }103 }104105 @Test func largeRoundTripThroughEveryFormat() async throws {106 // 20 000 polygons with holes: GeoJSON → Shapefile → GeoPackage → KML → GeoJSON, coordinates compared exactly.107 let temp = TempDir()108 var text = "{\"type\":\"FeatureCollection\",\"features\":[\n"109 let n = 20_000110 for i in 0..<n {111 let x = -80 + Double(i % 200) * 0.05, y = 40 + Double(i / 200) * 0.05112 text += "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[\(x),\(y)],[\(x + 0.04),\(y)],[\(x + 0.04),\(y + 0.04)],[\(x),\(y + 0.04)],[\(x),\(y)]],[[\(x + 0.01),\(y + 0.01)],[\(x + 0.01),\(y + 0.03)],[\(x + 0.03),\(y + 0.03)],[\(x + 0.03),\(y + 0.01)],[\(x + 0.01),\(y + 0.01)]]]},\"properties\":{\"id\":\(i),\"label\":\"cell \(i)\"}}\(i == n - 1 ? "" : ",")\n"113 }114 text += "]}"115 let source = temp.write("grid.geojson", text)116 let original = try GeoReader.read(source, format: .geojson, options: GeoReadOptions(), check: {}).layers[0].features117 let a = try await convert(source, .geojson, to: .shapefile, in: temp)118 let shp = a.output.files[0].appendingPathComponent("grid.shp")119 let b = try await convert(shp, .shapefile, to: .geopackage, in: temp)120 let c = try await convert(b.output.files[0], .geopackage, to: .kml, in: temp)121 let d = try await convert(c.output.files[0], .kml, to: .geojson, in: temp)122 #expect(d.features.count == n)123 var same = 0124 for (o, f) in zip(original, d.features) {125 guard case .polygon(let r1)? = o.geometry, case .polygon(let r2)? = f.geometry, r1.count == 2, r2.count == 2 else { continue }126 // Ring direction may be normalised; the vertex sets and the exterior / hole roles must be identical.127 if Set(r1[0]) == Set(r2[0]), Set(r1[1]) == Set(r2[1]), o["id"] == f["id"], o["label"] == f["label"] { same += 1 }128 }129 #expect(same == n)130 }131}132