#!/usr/bin/env bash set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" fail() { echo "FAIL: $*" >&2; exit 1; } MAP_HOME="$ROOT/App/MapHomeView.swift" MAP_STATE="$ROOT/App/MapLocationState.swift" MAP_BRIDGE="$ROOT/App/MapViewRepresentable.swift" REALTIME="$ROOT/App/RealtimeLocationManager.swift" SETUP="$ROOT/App/SetupCoordinator.swift" PROXY="$ROOT/App/ProxyManager.swift" SETTINGS_NAVIGATOR="$ROOT/App/SystemSettingsNavigator.swift" DIAGNOSTICS="$ROOT/App/DiagnosticsView.swift" CONTENT="$ROOT/App/ContentView.swift" CONVERTER="$ROOT/Shared/CoordinateConverter.swift" NETWORK_MONITOR="$ROOT/Shared/NetworkMonitor.swift" for file in "$MAP_HOME" "$MAP_STATE" "$MAP_BRIDGE" "$REALTIME" "$SETUP" "$PROXY" "$SETTINGS_NAVIGATOR" "$DIAGNOSTICS" "$CONTENT" "$CONVERTER" "$NETWORK_MONITOR"; do test -f "$file" || fail "missing required refactor file: $file" done ! grep -q 'draftCoordinate' "$MAP_HOME" || fail "MapHomeView must not keep the old draftCoordinate authority" ! grep -q 'needsZoom' "$MAP_HOME" || fail "MapHomeView must use camera commands instead of needsZoom" ! grep -q '@Binding var coordinate' "$MAP_BRIDGE" || fail "map bridge must not write a coordinate Binding" grep -q 'showsUserLocation = true' "$MAP_BRIDGE" || fail "MapKit native user location must be visible" grep -q 'didUpdate userLocation' "$MAP_BRIDGE" || fail "MapKit native user location must feed realtime state" grep -q 'coordinate: userLocation.coordinate' "$MAP_BRIDGE" || fail "visible MapKit blue-point samples must use MKUserLocation.coordinate" ! grep -Eq 'userLocation\.location\??\.coordinate' "$MAP_BRIDGE" || fail "MapKit blue-point samples must not use the underlying Core Location coordinate" grep -q 'MapCameraCommand' "$MAP_BRIDGE" || fail "map bridge must consume MapCameraCommand" grep -q 'let initialViewportMeters:' "$MAP_BRIDGE" || fail "map bridge must receive initial viewport from MapLocationState" ! grep -q 'ViewportStore.loadOrDefault()' "$MAP_BRIDGE" || fail "map bridge must not bypass MapLocationState for initial viewport" grep -q 'activeCameraCommandID' "$MAP_BRIDGE" || fail "programmatic map callbacks must be associated with the active camera command" grep -q 'UIPanGestureRecognizer' "$MAP_BRIDGE" || fail "map panning must be recognized explicitly" grep -q 'UIPinchGestureRecognizer' "$MAP_BRIDGE" || fail "pinch zoom must not be treated as a selected-center pan" ! grep -q 'RealtimeLocationAnnotation' "$MAP_BRIDGE" || fail "custom realtime point must be removed in favor of MKUserLocation" grep -q 'selectionRevision' "$MAP_STATE" || fail "map state must reject stale async results by revision" grep -q 'isApproximatelyEqual(to: coordinate)' "$MAP_STATE" || fail "pure viewport changes must not replace an unchanged selection" grep -q 'displayName(viewportMeters:' "$MAP_STATE" || fail "place labels must depend on viewport size" grep -q 'var road:' "$MAP_STATE" || fail "place labels must keep road granularity separate from doorplate details" grep -q 'district: placemark.subLocality' "$MAP_HOME" || fail "Chinese-style sub-locality must feed the district zoom level" grep -Eq '@Published private\(set\) var location' "$REALTIME" || fail "realtime location must be read-only outside its manager" grep -q 'CLLocationCoordinate2DIsValid' "$REALTIME" || fail "realtime manager must reject invalid coordinates" grep -q 'horizontalAccuracy >= 0' "$REALTIME" || fail "realtime manager must reject invalid accuracy samples" grep -q 'kCLErrorDomain' "$REALTIME" || fail "denied Core Location errors must be terminal" grep -q 'case awaitingAuthorization' "$REALTIME" || fail "location requests must wait for authorization before requesting a sample" grep -q 'var location: CLLocation?' "$REALTIME" || fail "Core Location driver must expose its cached native sample" grep -q 'oneShotTimeoutNanoseconds' "$REALTIME" || fail "one-shot and fallback timeouts must be independent" ! grep -q 'pendingContinuation' "$REALTIME" || fail "unversioned pendingContinuation must be removed" grep -q 'applyVerified' "$MAP_HOME" || fail "verified location commits must be synchronous after revision validation" grep -q 'func applyVerificationResult' "$SETUP" || fail "verification results must have one setup-state reducer" grep -q 'case .success:' "$SETUP" || fail "successful verification must converge setup state" grep -q 'case .certNotTrusted:' "$SETUP" || fail "certificate failure must converge setup state" grep -q 'setupStep = .proxy' "$SETUP" || fail "proxy failure must converge setup state" grep -q 'realtimeRequestTask' "$MAP_HOME" || fail "realtime button requests must be synchronously serialized" grep -q 'RealtimeLocationRequestContext' "$MAP_HOME" || fail "a realtime button tap must retarget an in-flight startup request instead of being ignored" grep -q 'CLError.network' "$MAP_HOME" || fail "reverse geocoding network failures must use bounded retry" grep -q 'SystemSettingsNavigator' "$MAP_HOME" || fail "settings actions must use the shared navigator" grep -q '复制全部日志' "$DIAGNOSTICS" || fail "diagnostics must show a standalone copy button" grep -q '清空日志' "$DIAGNOSTICS" || fail "diagnostics must show a standalone clear button" grep -q '日志自动清理,仅保留近 3 天' "$DIAGNOSTICS" || fail "diagnostics must disclose the three-day retention policy" grep -q 'retentionInterval: TimeInterval = 3 \* 24 \* 60 \* 60' "$ROOT/Shared/RuntimeLog.swift" || fail "runtime logs must retain only three days" if grep -q 'logEvent("CONNECT " + host + " -> passthrough")' "$ROOT/Core/proxy.go"; then fail "proxy diagnostics must not log unrelated passthrough CONNECT hosts" fi grep -q 'enum SystemSettingsNavigator' "$SETTINGS_NAVIGATOR" || fail "shared settings navigator is missing" grep -q 'await CoordinateConverter.resolveInitialMapCoordinateSystem()' "$CONTENT" || fail "map type must resolve before MapHomeView construction" grep -q 'refreshRuntimeMapCoordinateSystem(reason:' "$CONVERTER" || fail "fixed-anchor map type must support runtime refresh" grep -q 'scheduleBluePointMapCoordinateSystemRefresh()' "$MAP_HOME" || fail "native blue-point samples must trigger runtime map-type refresh while spoofing" ! grep -A3 'private func scheduleBluePointMapCoordinateSystemRefresh' "$MAP_HOME" | grep -q 'spoofState == .active' || fail "blue-point map-type refresh must also detect the return to physical location" grep -q 'awaitCoordinatedMapCoordinateSystemRefresh(reason: "点击实时定位")' "$MAP_HOME" || fail "realtime button must await the coordinated map-type refresh" grep -q 'awaitCoordinatedMapCoordinateSystemRefresh(reason: "保存收藏")' "$MAP_HOME" || fail "favorite save must await the coordinated map-type refresh" ! grep -q 'source == .coreLocation.*correctMapCoordinateSystemUsingRealtime' "$MAP_HOME" || fail "runtime Core Location samples must not infer MapKit type" grep -q 'clearRealtimeLocationForMapCoordinateSystemChange' "$MAP_HOME" || fail "map-type changes must discard superseded blue-point samples" grep -q '地图坐标类型已变化' "$MAP_HOME" || fail "map-type changes must emit an explicit searchable business log" grep -q '图钉已按新类型重设' "$MAP_HOME" || fail "map-type change log must report pin reprojection" grep -q '国内标准 GCJ-02' "$MAP_HOME" || fail "current selection panel must show GCJ-02" grep -q '国际标准 WGS-84' "$MAP_HOME" || fail "current selection panel must show WGS-84" grep -q 'phase = .map' "$CONTENT" || fail "ContentView must explicitly gate MapHomeView construction" ! grep -q 'startTileProbe' "$MAP_HOME" || fail "MapHomeView must not start a second fixed-anchor coordinate-system probe" ! grep -q 'initializeMap()' "$MAP_HOME" || fail "MapHomeView must not replay a second map initialization from onAppear" grep -q '地图创建前请求实时定位' "$CONTENT" || fail "fresh realtime position must resolve before map construction" ! grep -q 'lastTileCheck' "$CONVERTER" || fail "map coordinate-system detection must not use a time cache" ! grep -q '跳过(缓存' "$CONVERTER" || fail "map coordinate-system detection must not skip using a cached result" ! grep -q '瓦片检测' "$CONVERTER" || fail "coordinate-system probe logs must not claim to inspect map tiles" grep -q 'minimumCountForSuppression = 3' Shared/AppGroup.swift || fail "automatic tip suppression must require three successful operations" grep -q 'activeTip = .deactivation' "$MAP_HOME" || fail "manual deactivation help must use the non-suppressible generic tip sheet" grep -q 'stabilizationNanoseconds: UInt64 = 3_000_000_000' "$MAP_HOME" || fail "Wi-Fi changes must wait three seconds before environment verification" grep -q 'result.wifiChangeReminderTipKind' "$MAP_HOME" || fail "Wi-Fi proxy failures must use the background reminder mapping" grep -q 'if result == .certNotTrusted' "$MAP_HOME" || fail "Wi-Fi certificate failures must enter certificate setup" grep -q 'setup.applyVerificationResult(result)' "$MAP_HOME" || fail "Wi-Fi certificate failures must use the setup routing reducer" ! grep -q 'case certificate' "$ROOT/App/TipViews.swift" || fail "generic certificate tip must not coexist with certificate setup" ! grep -q 'CertificateTipContent' "$ROOT/App/TipViews.swift" || fail "certificate failures must use the complete setup flow" ! grep -q 'onChange(of: net.isAirplaneMode)' "$MAP_HOME" || fail "airplane recovery must not race the Wi-Fi change verifier" grep -q 'hasReceivedInitialPath' "$NETWORK_MONITOR" || fail "initial network path must not be reported as a Wi-Fi switch" grep -q 'lastKnownSSID' "$NETWORK_MONITOR" || fail "SSID polling must preserve a baseline across temporary nil readings" echo "PASS: map location state refactor contract"