← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


Tests/Unit/AddressFillTests.swift (4318B)

1import Foundation
2import Testing
3
4@testable import Reportage
5
6@Suite("AddressFill")
7struct AddressFillTests {
8    /// Gives each coordinate an address naming its latitude, throttling the
9    /// first lookup and finding nothing south of the equator.
10    private actor FakeLookup: AddressLooking {
11        private(set) var lookups: [Coordinate] = []
12        private var hasThrottled = false
13
14        func placemarks(at coordinate: Coordinate) async throws -> [Placemark] {
15            if !hasThrottled {
16                hasThrottled = true
17                throw AddressLookupError.throttled
18            }
19            lookups.append(coordinate)
20            guard coordinate.latitude > 0 else { return [] }
21            return [
22                Placemark(name: "\(coordinate.latitude)", language: "en", cityWithContext: "City"),
23                Placemark(name: "\(coordinate.latitude)", language: "ja", cityWithContext: "市"),
24            ]
25        }
26    }
27
28    private static func report(latitude: Double, placemark: Placemark?) -> Report {
29        var context = Context()
30        context.location = LocationContext(
31            coordinate: Coordinate(latitude: latitude, longitude: 139.7), placemark: placemark)
32        return Report(
33            id: UUID(), date: Date(timeIntervalSince1970: 0), timeZone: "Asia/Tokyo", kind: .manual,
34            trigger: .app, state: .complete, context: context, modifiedAt: Date(timeIntervalSince1970: 0))
35    }
36
37    @Test("Reports within the radius share a spot, the spots with most reports first")
38    func spots() {
39        func target(_ metresNorth: Double) -> AddressTarget {
40            AddressTarget(id: UUID(), coordinate: Coordinate(latitude: 35 + metresNorth / 111_320, longitude: 139.7))
41        }
42        let targets = [target(0), target(500), target(10), target(20), target(510), target(30)]
43        let spots = AddressSpots.group(targets, within: 25)
44        #expect(spots.map(\.reportIDs) == [
45            [targets[0].id, targets[2].id, targets[3].id],
46            [targets[1].id, targets[4].id],
47            [targets[5].id],
48        ])
49        #expect(abs(AddressSpots.distance(targets[0].coordinate, targets[1].coordinate) - 500) < 0.01)
50    }
51
52    @Test("Older addresses are replaced with MapKit's, and others are left alone")
53    func fill() async throws {
54        let store = ReportStore(persistence: makeTestPersistence())
55        let imported = Self.report(latitude: 35, placemark: Placemark(name: "Old", locality: "杉並区"))
56        let nearby = Self.report(latitude: 35.0001, placemark: Placemark(name: "Old"))
57        let missing = Self.report(latitude: 35.0002, placemark: nil)
58        let current = Self.report(latitude: 36, placemark: Placemark(name: "New", language: "en"))
59        let nowhere = Self.report(latitude: -10, placemark: Placemark(name: "Sea"))
60        for report in [imported, nearby, missing, current, nowhere] {
61            try await store.insert(report)
62        }
63
64        let lookup = FakeLookup()
65        let fill = AddressFill(store: store, lookup: lookup, throttlePause: .zero)
66        let progress = try await fill.fill()
67        #expect(progress == AddressFillProgress(total: 3, updated: 2, unfound: 1))
68        // Two reports 11 m apart share a lookup, retried once after throttling.
69        #expect(await lookup.lookups.count == 2)
70
71        let updated = try #require(try await store.report(id: imported.id))
72        #expect(updated.context.location?.placemark?.cityWithContext == "City")
73        #expect(updated.context.location?.localisedPlacemarks?.map(\.language) == ["ja"])
74        #expect(updated.modifiedAt > imported.modifiedAt)
75        let shared = try #require(try await store.report(id: nearby.id))
76        #expect(shared.context.location?.placemark == updated.context.location?.placemark)
77        // Without an address, there's nothing to upgrade.
78        #expect(try await store.report(id: missing.id)?.context == missing.context)
79        #expect(try await store.report(id: current.id)?.context == current.context)
80        #expect(try await store.report(id: nowhere.id)?.context == nowhere.context)
81
82        // Run again, only the report Apple Maps had no address for is looked up.
83        let again = try await fill.fill()
84        #expect(again == AddressFillProgress(total: 1, updated: 0, unfound: 1))
85    }
86}