Skip to content

The OSINT investigator's workbench. One case, one folder, every tool. Local-first: your media and your investigations never leave your machine.

JavaScript
67.8 KB
Raw Blame
"""Satellite capture API: bearing is honored and persisted with provenance."""

import io
from concurrent.futures import ThreadPoolExecutor
from threading import Event, Lock

import pytest

import graph_read
from PIL import Image

from azimut import layout
from azimut.api import satellite as satellite_api
from azimut.engine import geo, tilecache, tiles
from azimut.workspace import Case


def _fake_tile(client, url):  # offline: every tile is a solid green square
    return Image.new("RGB", (256, 256), (10, 120, 10))


def test_providers_flag_imagery_vs_street(client):
    # the UI uses `imagery` to disable the roads overlay over street maps
    providers = {p["id"]: p for p in client.get("/api/satellite/providers").json()}
    assert providers["esri-world-imagery"]["imagery"] is True
    assert providers["osm"]["imagery"] is False


def test_capture_persists_bearing(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]

    result = client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": 48.8584, "lon": 2.2945, "zoom": 16,
              "width": 640, "height": 480, "bearing": 90},
    ).json()
    assert result["bearing"] == 90.0

    listed = client.get(f"/api/cases/{cid}/satellite").json()
    assert len(listed) == 1
    assert listed[0]["bearing"] == 90.0


def test_capture_defaults_to_north(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]

    result = client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": 48.8584, "lon": 2.2945, "zoom": 16, "width": 512, "height": 512},
    ).json()
    assert result["bearing"] == 0.0


def _capture(client, cid, lat, lon, zoom=16):
    return client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": lat, "lon": lon, "zoom": zoom, "width": 512, "height": 512},
    ).json()


def _of_type(client, cid, etype):
    return [e for e in graph_read.entities(cid)
            if e["type"] == etype]


def _captures(client, cid):
    return _of_type(client, cid, "capture")


def _places(client, cid):
    return _of_type(client, cid, "place")


def test_capture_creates_its_own_entity(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    cap = _capture(client, cid, 48.8584, 2.2945)
    caps = _captures(client, cid)
    assert len(caps) == 1
    # the entity is tied to this exact capture by path, titled with its coords
    assert caps[0]["attrs"]["coords"] == "48.858400, 2.294500"
    assert caps[0]["attrs"]["path"] == cap["path"]
    assert caps[0]["label"] == "48.858400, 2.294500"
    assert caps[0]["attrs"]["zoom"] == 16
    assert caps[0]["attrs"]["bearing"] == 0.0
    assert caps[0]["attrs"]["provider"] == "esri-world-imagery"
    # capturing files it straight away — no confirm step
    assert caps[0]["provenance"]["status"] == "confirmed"


def test_capture_lands_in_media_library(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    cap = _capture(client, cid, 48.8584, 2.2945)
    # a capture now lives in media/ so the Media Library lists it and Inspect can open it
    assert cap["path"].startswith("media/")
    media = client.get(f"/api/cases/{cid}/media").json()
    entry = next(m for m in media if m["path"] == cap["path"])
    assert entry["kind"] == "image"
    assert entry["source"]["type"] == "satellite"
    # it carries a thumbnail like any other media item
    assert entry["thumbnail"]


def test_capture_records_both_dates(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    result = client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": 48.8584, "lon": 2.2945, "zoom": 16, "width": 512, "height": 512,
              "imagery_date": "2021-06-01"},
    ).json()
    # capture timestamp (when clicked) and imagery acquisition date (the scene)
    assert result["fetched_at"]  # ISO capture timestamp
    assert result["imagery_date"] == "2021-06-01"
    listed = client.get(f"/api/cases/{cid}/satellite").json()[0]
    assert listed["fetched_at"] == result["fetched_at"]
    assert listed["imagery_date"] == "2021-06-01"


def test_an_estimated_imagery_date_is_marked_approximate_on_the_axis(client, monkeypatch):
    """Esri's date is an estimate. The capture keeps the date it shows on its face,
    and the Time panel places it with a `~` rather than as a known day."""
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    estimated = client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": 48.8584, "lon": 2.2945, "zoom": 16, "width": 512, "height": 512,
              "imagery_date": "2021-06-01", "imagery_exact": False},
    ).json()
    assert estimated["imagery_date"] == "2021-06-01"
    item = next(m for m in client.get(f"/api/cases/{cid}/media").json() if m["path"] == estimated["path"])
    assert item["source"]["imagery_exact"] is False
    entity = Case.open(cid).find_entity(attr="path", value=estimated["path"])
    page = client.get(f"/api/cases/{cid}/timeline", params={"entity": entity["id"], "category": "media"}).json()
    imagery = next(row for row in page["items"] if row["kind"] == "imagery")
    assert imagery["raw"] == "2021-06-01~" and imagery["approximate"]


def test_capture_imagery_date_optional(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    # no imagery date known → recorded as null, capture still succeeds
    result = _capture(client, cid, 48.8584, 2.2945)
    assert result["imagery_date"] is None


def test_imagery_date_non_esri_is_unsupported(client):
    out = client.get(
        "/api/satellite/imagery-date",
        params={"lat": 48.8584, "lon": 2.2945, "zoom": 16, "provider": "osm"},
    ).json()
    assert out == {"supported": False, "date": None, "source": None}


def test_imagery_date_esri_best_effort(client, monkeypatch):
    class _Resp:
        def raise_for_status(self):
            pass

        def json(self):
            return {"results": [{"attributes": {"NICE_NAME": "Maxar", "SRC_DATE2": "20200103"}}]}

    monkeypatch.setattr(tiles.httpx, "get", lambda *a, **k: _Resp())
    out = client.get(
        "/api/satellite/imagery-date",
        params={"lat": 48.8584, "lon": 2.2945, "zoom": 16},
    ).json()
    assert out == {"supported": True, "date": "2020-01-03", "source": "Maxar"}


def test_imagery_date_service_down_is_graceful(client, monkeypatch):
    def boom(*a, **k):
        raise OSError("down")

    monkeypatch.setattr(tiles.httpx, "get", boom)
    out = client.get(
        "/api/satellite/imagery-date",
        params={"lat": 0, "lon": 0, "zoom": 10},
    ).json()
    assert out == {"supported": True, "date": None, "source": None}


@pytest.mark.parametrize(
    "params",
    [
        {"lat": -91, "lon": 0, "zoom": 10},
        {"lat": 91, "lon": 0, "zoom": 10},
        {"lat": 0, "lon": -181, "zoom": 10},
        {"lat": 0, "lon": 181, "zoom": 10},
        {"lat": 0, "lon": 0, "zoom": -1},
        {"lat": 0, "lon": 0, "zoom": 23},
    ],
)
def test_imagery_date_rejects_out_of_range_inputs_before_provider_call(client, monkeypatch, params):
    monkeypatch.setattr(tiles, "esri_capture_date", lambda *_args: pytest.fail("provider called"))
    assert client.get("/api/satellite/imagery-date", params=params).status_code == 422


def test_save_place_creates_place_without_image(client):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    place = client.post(
        f"/api/cases/{cid}/satellite/place",
        json={"lat": 48.8584, "lon": 2.2945, "zoom": 18, "bearing": 30},
    ).json()
    assert place["type"] == "place"
    assert place["label"] == "48.858400, 2.294500"
    # a bare place carries coordinates but no image path — nothing on disk
    assert "path" not in place["attrs"]
    assert place["attrs"]["zoom"] == 18 and place["attrs"]["bearing"] == 30
    assert len(_places(client, cid)) == 1
    assert _captures(client, cid) == []
    # no satellite image file was written
    assert client.get(f"/api/cases/{cid}/satellite").json() == []


def test_save_place_with_note(client):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    place = client.post(
        f"/api/cases/{cid}/satellite/place",
        json={"lat": 48.8584, "lon": 2.2945, "title": "HQ", "notes": "  seen on video  "},
    ).json()
    assert place["label"] == "HQ"
    assert place["attrs"]["notes"] == "seen on video"


def test_edit_place_title_and_note_via_entity_patch(client):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    place = client.post(
        f"/api/cases/{cid}/satellite/place",
        json={"lat": 48.8584, "lon": 2.2945},
    ).json()
    # the generic entity PATCH retitles and sets the note (used by the editor)
    updated = client.patch(
        f"/api/cases/{cid}/entities/{place['id']}",
        json={"label": "Command post", "attrs": {"notes": "north wall"}},
    ).json()
    assert updated["label"] == "Command post"
    assert updated["attrs"]["notes"] == "north wall"
    # coordinates are untouched by the merge
    assert updated["attrs"]["lat"] == 48.8584 and updated["attrs"]["lon"] == 2.2945


def test_save_place_honors_custom_title(client):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    place = client.post(
        f"/api/cases/{cid}/satellite/place",
        json={"lat": 48.8584, "lon": 2.2945, "title": "  Eiffel Tower  "},
    ).json()
    assert place["label"] == "Eiffel Tower"
    assert place["attrs"]["coords"] == "48.858400, 2.294500"


def test_each_capture_is_its_own_entity(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    # two captures at the same coordinates are two independent entities (1:1)
    _capture(client, cid, 48.8584, 2.2945, zoom=16)
    _capture(client, cid, 48.8584, 2.2945, zoom=17)
    assert len(_captures(client, cid)) == 2


def test_delete_capture_removes_only_its_entity(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    first = _capture(client, cid, 48.8584, 2.2945, zoom=16)
    second = _capture(client, cid, 48.8584, 2.2945, zoom=17)

    client.delete(f"/api/cases/{cid}/satellite", params={"path": first["path"]})
    # only the deleted capture's entity is gone; the sibling stays intact
    caps = _captures(client, cid)
    assert len(caps) == 1
    assert caps[0]["attrs"]["path"] == second["path"]
    remaining = client.get(f"/api/cases/{cid}/satellite").json()
    assert [c["path"] for c in remaining] == [second["path"]]


def test_delete_capture_entity_removes_only_its_image(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    first = _capture(client, cid, 48.8584, 2.2945, zoom=16)
    second = _capture(client, cid, 48.8584, 2.2945, zoom=17)
    cap = next(c for c in _captures(client, cid) if c["attrs"]["path"] == first["path"])

    # deleting the capture row from the sidebar wipes just its one image
    client.delete(f"/api/cases/{cid}/entities/{cap['id']}")
    assert len(_captures(client, cid)) == 1
    remaining = client.get(f"/api/cases/{cid}/satellite").json()
    assert [c["path"] for c in remaining] == [second["path"]]


def test_delete_place_entity_leaves_captures_alone(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    cap = _capture(client, cid, 48.8584, 2.2945, zoom=16)
    client.post(
        f"/api/cases/{cid}/satellite/place",
        json={"lat": 48.8600, "lon": 2.3000},
    )
    place = _places(client, cid)[0]

    # a bare place has no image; deleting it must not touch capture files
    client.delete(f"/api/cases/{cid}/entities/{place['id']}")
    assert _places(client, cid) == []
    assert [c["path"] for c in client.get(f"/api/cases/{cid}/satellite").json()] == [cap["path"]]


def test_default_marker_is_centered_crosshair(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    result = _capture(client, cid, 48.8584, 2.2945)
    # marker defaults to the crop center; recorded coords == center
    assert result["marker_style"] == "crosshair"
    assert result["marker_x"] == 0 and result["marker_y"] == 0
    assert result["lat"] == 48.8584 and result["lon"] == 2.2945
    assert result["center_lat"] == 48.8584 and result["center_lon"] == 2.2945


def test_pin_style_persists(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    result = client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": 48.8584, "lon": 2.2945, "zoom": 16,
              "width": 512, "height": 512, "marker_style": "pin"},
    ).json()
    assert result["marker_style"] == "pin"
    assert client.get(f"/api/cases/{cid}/satellite").json()[0]["marker_style"] == "pin"


def test_moved_marker_records_pin_coords_not_center(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    # crop framed on center, but the pin was dragged to a different point
    result = client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": 48.8584, "lon": 2.2945, "zoom": 16, "width": 512, "height": 512,
              "marker_style": "pin", "marker_x": 0, "marker_y": 200,
              "marker_lat": 48.8600, "marker_lon": 2.3000},
    ).json()
    # recorded coordinates & offset follow the pin, center stays the frame
    assert (result["lat"], result["lon"]) == (48.86, 2.3)
    assert (result["center_lat"], result["center_lon"]) == (48.8584, 2.2945)
    assert result["marker_y"] == 200
    # the mirrored capture entity is titled/located at the pin, not the center
    cap = _captures(client, cid)[0]
    assert cap["attrs"]["lat"] == 48.86 and cap["attrs"]["lon"] == 2.3
    assert cap["label"] == "48.860000, 2.300000"


def test_no_marker_leaves_image_unmarked(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    result = client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": 48.8584, "lon": 2.2945, "zoom": 16,
              "width": 256, "height": 256, "marker_style": "none"},
    ).json()
    assert result["marker_style"] == "none"
    assert result["crosshair"] is False


def test_edit_title_renames_capture_entity(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    cap = _capture(client, cid, 48.8584, 2.2945)

    updated = client.patch(
        f"/api/cases/{cid}/satellite",
        json={"path": cap["path"], "title": "Eiffel Tower"},
    ).json()
    assert updated["title"] == "Eiffel Tower"
    assert _captures(client, cid)[0]["label"] == "Eiffel Tower"

    # listing reflects the stored title
    listed = client.get(f"/api/cases/{cid}/satellite").json()
    assert listed[0]["title"] == "Eiffel Tower"

    # clearing the title falls back to the coordinates on both sides
    client.patch(
        f"/api/cases/{cid}/satellite",
        json={"path": updated["path"], "title": "  "},
    )
    assert _captures(client, cid)[0]["label"] == "48.858400, 2.294500"


def test_capture_title_follows_the_coordinate_format(client, monkeypatch):
    """A default title is minted in the user's format (Settings → General),
    while the machine-readable fields stay in decimal degrees."""
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    client.put("/api/settings/prefs", json={"coord_format": "mgrs"})
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]

    result = client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": 48.8583701, "lon": 2.2944813, "zoom": 16, "width": 512, "height": 512},
    ).json()
    assert result["title"] == "31U DQ 48250 11951"

    listed = client.get(f"/api/cases/{cid}/satellite").json()[0]
    assert listed["title"] == "31U DQ 48250 11951"
    # provenance is never re-expressed: a later reader still gets the numbers
    assert listed["lat"] == pytest.approx(48.8583701)
    assert listed["lon"] == pytest.approx(2.2944813)
    entity = graph_read.entities(cid)[0]
    assert entity["attrs"]["coords"] == "48.858370, 2.294481"


def test_changing_the_format_never_rewrites_existing_titles(client, monkeypatch):
    """Titles are minted once. Switching format later renames nothing — the
    case keeps reading the way it was written."""
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]

    client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": 48.8583701, "lon": 2.2944813, "zoom": 16, "width": 512, "height": 512},
    )
    assert client.get(f"/api/cases/{cid}/satellite").json()[0]["title"] == "48.858370, 2.294481"

    client.put("/api/settings/prefs", json={"coord_format": "dms"})
    # the old capture keeps its decimal title …
    assert client.get(f"/api/cases/{cid}/satellite").json()[0]["title"] == "48.858370, 2.294481"

    # … while a new one is titled the new way
    client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": 40.6892, "lon": -74.0445, "zoom": 16, "width": 512, "height": 512},
    )
    titles = {c["title"] for c in client.get(f"/api/cases/{cid}/satellite").json()}
    assert titles == {"48.858370, 2.294481", "40°41'21.12_N 74°02'40.20_W"}


def test_place_label_follows_the_coordinate_format(client):
    client.put("/api/settings/prefs", json={"coord_format": "dms"})
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]

    place = client.post(
        f"/api/cases/{cid}/satellite/place",
        json={"lat": 48.8583701, "lon": 2.2944813, "zoom": 16},
    ).json()
    assert place["label"] == "48°51'30.13\"N 2°17'40.13\"E"
    assert place["attrs"]["coords"] == "48.858370, 2.294481"  # attrs stay decimal

    # an explicit title always wins over the derived one
    titled = client.post(
        f"/api/cases/{cid}/satellite/place",
        json={"lat": 48.8583701, "lon": 2.2944813, "zoom": 16, "title": "Rooftop"},
    ).json()
    assert titled["label"] == "Rooftop"


def _png_bytes(w=320, h=200, color=(30, 90, 30)):
    import io

    buf = io.BytesIO()
    Image.new("RGB", (w, h), color).save(buf, format="PNG")
    return buf.getvalue()


def test_widget_usage_endpoint_counts_map_loads(client):
    client.put("/api/settings/keys", json={"google_js": "AIza.js"})
    assert client.post("/api/satellite/usage/google_js").json()["count"] == 1
    assert client.post("/api/satellite/usage/google_js").json()["count"] == 2
    # tile meters are counted by the proxy, never by the browser
    assert client.post("/api/satellite/usage/google").status_code == 404
    assert client.post("/api/satellite/usage/nope").status_code == 404


def test_screenshot_capture_files_with_burned_attribution(client):
    client.put("/api/settings/keys", json={"google_js": "AIza.js"})
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]

    r = client.post(
        f"/api/cases/{cid}/satellite/screenshot",
        files={"image": ("shot.png", _png_bytes(), "image/png")},
        data={"lat": "48.8584", "lon": "2.2945", "zoom": "18",
              "provider": "google-js", "bearing": "45"},
    )
    assert r.status_code == 200
    body = r.json()
    assert body["method"] == "screenshot"
    assert "Google" in body["attribution"]
    assert body["attribution_burned"] is True
    # the footer band grew the image — attribution is pixels, not metadata
    assert body["height"] == 200 + tiles.ATTRIBUTION_BAND

    listed = client.get(f"/api/cases/{cid}/satellite").json()
    assert len(listed) == 1
    assert listed[0]["method"] == "screenshot"
    assert listed[0]["bearing"] == 45.0
    # nothing said it was framed, so provenance must not imply the coordinates
    # are a crop centre
    assert body["framed"] is False


def test_a_tilted_view_is_filed_with_its_tilt_and_the_relief_it_was_drawn_on(client):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    r = client.post(
        f"/api/cases/{cid}/satellite/oblique",
        files={"image": ("tilted.png", _png_bytes(w=800), "image/png")},
        data={"lat": "46.5776", "lon": "8.0053", "zoom": "13.6274", "provider": "esri-world-imagery",
              "bearing": "150", "pitch": "62", "exaggeration": "1", "elevation": "711.9",
              "imagery_date": "2024-08-13", "imagery_exact": "false"},
    )
    assert r.status_code == 200, r.text
    body = r.json()
    assert (body["method"], body["pitch"], body["bearing"]) == ("render", 62.0, 150.0)
    # over relief the zoom's fraction is part of the camera, so it is kept
    assert body["zoom"] == 13.63
    assert body["relief"] == {"source": "mapterhorn", "exaggeration": 1.0}
    assert "Relief © Mapterhorn" in body["attribution"]
    assert "AWS" not in body["attribution"]
    assert "exaggerated" not in body["attribution"]
    # a tilted view has no one scale, so no bar or arrow is drawn on it
    assert body["marks"] is None
    assert body["height"] == 200 + tiles.ATTRIBUTION_BAND
    assert (body["imagery_date"], body["imagery_exact"]) == ("2024-08-13", False)
    listed = client.get(f"/api/cases/{cid}/satellite").json()
    assert listed[0]["pitch"] == 62.0
    # the saved list carries the camera, so the map can lean back to it
    row = client.get(f"/api/cases/{cid}/satellite/index").json()[0]
    assert (row["pitch"], row["exaggeration"], row["zoom"], row["bearing"]) == (62.0, 1.0, 13.63, 150.0)
    # …and the height its centre was held at, which with the zoom puts the eye back
    assert (body["elevation"], row["elevation"]) == (712, 712)


def test_a_tilted_view_drawn_partly_from_the_fallback_relief_credits_it(client):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    body = client.post(
        f"/api/cases/{cid}/satellite/oblique",
        files={"image": ("tilted.png", _png_bytes(w=1200), "image/png")},
        data={"lat": "46.5", "lon": "8.0", "zoom": "13", "provider": "esri-world-imagery",
              "pitch": "60", "relief_fallback": "true"},
    ).json()
    assert "Relief © Mapterhorn and Terrain Tiles, Mapzen and AWS Open Data" in body["attribution"]
    assert body["relief"] == {"source": "mapterhorn", "exaggeration": 1.0, "fallback": "aws-terrain"}


def test_a_flat_saved_row_carries_no_tilt(client):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    client.post(f"/api/cases/{cid}/satellite/place",
                json={"lat": 46.5, "lon": 8.0, "zoom": 14, "title": "Flat"})
    row = client.get(f"/api/cases/{cid}/satellite/index").json()[0]
    assert (row["pitch"], row["exaggeration"], row["elevation"]) == (None, None, None)


def test_a_tilted_view_with_free_far_ground_credits_both_providers(client):
    client.put("/api/settings/keys", json={"mapbox": "pk.test"})
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    body = client.post(
        f"/api/cases/{cid}/satellite/oblique",
        files={"image": ("tilted.png", _png_bytes(), "image/png")},
        data={"lat": "46.5", "lon": "8.0", "zoom": "14", "provider": "mapbox-satellite",
              "pitch": "60", "far_provider": "esri-world-imagery"},
    )
    assert body.status_code == 200, body.text
    answer = body.json()
    assert answer["far_provider"] == "esri-world-imagery"
    assert "Far ground Esri" in answer["attribution"]
    plain = client.post(
        f"/api/cases/{cid}/satellite/oblique",
        files={"image": ("tilted.png", _png_bytes(), "image/png")},
        data={"lat": "46.5", "lon": "8.0", "zoom": "14", "provider": "esri-world-imagery", "pitch": "60"},
    ).json()
    assert plain["far_provider"] is None


def test_an_exaggerated_relief_says_so_on_the_capture(client):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    body = client.post(
        f"/api/cases/{cid}/satellite/oblique",
        files={"image": ("tilted.png", _png_bytes(), "image/png")},
        data={"lat": "46.5", "lon": "8.0", "zoom": "12", "provider": "esri-world-imagery",
              "pitch": "45", "exaggeration": "2"},
    ).json()
    assert "heights exaggerated 2 times" in body["attribution"]
    assert body["relief"]["exaggeration"] == 2.0


@pytest.mark.parametrize("data, status", [
    ({"pitch": "0"}, 422),  # a flat view goes down the stitched road
    ({"pitch": "45", "provider": "google-js"}, 422),  # a widget is not drawn by the map
    ({"pitch": "45", "provider": "nowhere"}, 404),
    ({"pitch": "45", "exaggeration": "7"}, 422),
])
def test_a_tilted_capture_refuses_what_the_3d_map_cannot_have_drawn(client, data, status):
    client.put("/api/settings/keys", json={"google_js": "AIza.js"})
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    form = {"lat": "46.5", "lon": "8.0", "zoom": "12", "provider": "esri-world-imagery", **data}
    r = client.post(
        f"/api/cases/{cid}/satellite/oblique",
        files={"image": ("tilted.png", _png_bytes(), "image/png")},
        data=form,
    )
    assert r.status_code == status, r.text


def test_screenshot_provenance_distinguishes_a_framed_crop_from_a_pasted_image(client):
    """A screen crop taken through the capture frame is registered — its
    lat/lon *are* the crop centre — while a pasted screenshot's coordinates are
    only the map view at filing time. Provenance has to tell them apart, or a
    reader can't know what the coordinates mean."""
    client.put("/api/settings/keys", json={"google_js": "AIza.js"})
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]

    framed = client.post(
        f"/api/cases/{cid}/satellite/screenshot",
        files={"image": ("shot.png", _png_bytes(), "image/png")},
        data={"lat": "48.8584", "lon": "2.2945", "zoom": "18",
              "provider": "google-js", "framed": "true"},
    ).json()
    assert framed["framed"] is True

    pasted = client.post(
        f"/api/cases/{cid}/satellite/screenshot",
        files={"image": ("shot.png", _png_bytes(), "image/png")},
        data={"lat": "48.8584", "lon": "2.2945", "zoom": "18",
              "provider": "google-js", "framed": "false"},
    ).json()
    assert pasted["framed"] is False
    # both are still screenshots: framing never upgrades the method
    assert framed["method"] == pasted["method"] == "screenshot"

    listed = client.get(f"/api/cases/{cid}/satellite").json()
    assert sorted(c["framed"] for c in listed) == [False, True]


def test_screenshot_capture_refused_for_tile_providers(client):
    # tile providers have the real, pixel-registered capture path — routing a
    # screenshot around it would only launder away provenance
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    r = client.post(
        f"/api/cases/{cid}/satellite/screenshot",
        files={"image": ("shot.png", _png_bytes(), "image/png")},
        data={"lat": "0", "lon": "0", "zoom": "10", "provider": "esri-world-imagery"},
    )
    assert r.status_code == 422


def test_screenshot_capture_has_the_same_size_limit_as_extension_ingest(client, monkeypatch):
    from azimut.api import satellite

    monkeypatch.setattr(satellite, "MAX_IMAGE_BYTES", 100)
    client.put("/api/settings/keys", json={"google_js": "AIza.js"})
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    r = client.post(
        f"/api/cases/{cid}/satellite/screenshot",
        files={"image": ("shot.png", _png_bytes(), "image/png")},
        data={"lat": "0", "lon": "0", "zoom": "10", "provider": "google-js"},
    )
    assert r.status_code == 413
    assert graph_read.entities(cid) == []


def test_tile_proxy_rejects_out_of_range_coordinates(client):
    # a negative z used to reach `1 << z` and 500; bad input is a 422, not a crash
    assert client.get("/api/tiles/esri-world-imagery/-1/0/0").status_code == 422
    assert client.get("/api/tiles/esri-world-imagery/2/4/0").status_code == 422
    assert client.get("/api/tiles/esri-world-imagery/2/0/-1").status_code == 422
    # The zoom is rejected before exponentiation, so an absurd path value stays cheap.
    assert client.get("/api/tiles/esri-world-imagery/1000000/0/0").status_code == 422


def test_tile_proxy_reuses_one_pooled_client(client, monkeypatch):
    """Every tile goes through one shared, keep-alive client — not a fresh
    connection per tile. Several tiles must hit the same client instance."""
    import httpx

    from azimut.api import satellite

    seen: list[int] = []

    def handler(request: httpx.Request) -> httpx.Response:
        seen.append(id(request))
        return httpx.Response(200, content=_png_bytes(), headers={"content-type": "image/png"})

    pooled = httpx.Client(transport=httpx.MockTransport(handler))
    monkeypatch.setattr(satellite, "_tile_client", pooled)
    # spy: the route must fetch through the pooled client, never httpx.get
    monkeypatch.setattr(httpx, "get", lambda *a, **k: pytest.fail("used a one-shot connection"))

    calls = {"n": 0}
    real_get = pooled.get

    def counting_get(*a, **k):
        calls["n"] += 1
        return real_get(*a, **k)

    monkeypatch.setattr(pooled, "get", counting_get)

    for x in (16600, 16601, 16602):
        r = client.get(f"/api/tiles/esri-world-imagery/15/{x}/11278")
        assert r.status_code == 200
    assert calls["n"] == 3  # three tiles, all served by the one pooled client


def test_tile_proxy_climbs_past_a_parent_that_is_not_an_image(client, monkeypatch):
    """Overzoom fills a coverage gap by magnifying a parent tile. A provider can
    answer 200 with something that is not an image, and that parent is no use —
    the climb goes on to the next level rather than failing the map with a 500."""
    import httpx

    from azimut.api import satellite

    def handler(request: httpx.Request) -> httpx.Response:
        if "/15/" in request.url.path:  # the gap that starts the climb
            return httpx.Response(404)
        if "/14/" in request.url.path:  # 200, and not an image
            return httpx.Response(
                200, content=b"<html>upstream hiccup</html>", headers={"content-type": "image/png"}
            )
        return httpx.Response(200, content=_png_bytes(), headers={"content-type": "image/png"})

    monkeypatch.setattr(
        satellite, "_tile_client", httpx.Client(transport=httpx.MockTransport(handler))
    )
    r = client.get("/api/tiles/esri-world-imagery/15/16597/11278")
    assert r.status_code == 200
    assert r.headers["X-Azimut-Overzoom"] == "2"  # z14 skipped, z13 magnified


def test_tile_proxy_benches_google_on_a_persistent_403(client, monkeypatch):
    """A 403 that survives the session re-mint names the key (EEA policy,
    revoked key) — the basemap must stop being offered, with Google's own
    sentence stored as the reason."""
    import httpx

    from azimut.api import satellite
    from azimut.engine import google_tiles

    client.put("/api/settings/keys", json={"google": "AIza.x"})
    monkeypatch.setattr(
        google_tiles, "resolve_template", lambda url, **kw: url.replace("{session}", "tok")
    )
    monkeypatch.setattr(google_tiles, "invalidate", lambda key: None)
    eea = (
        b'{"error": {"code": 403, "status": "PERMISSION_DENIED", "message": '
        b'"Satellite tiles are not available for your account and region."}}'
    )

    def forbidden(request: httpx.Request) -> httpx.Response:
        return httpx.Response(
            403, content=eea, headers={"content-type": "application/json"},
        )

    # the tile proxy pools one shared client; swap in one with a mock transport
    monkeypatch.setattr(
        satellite, "_tile_client", httpx.Client(transport=httpx.MockTransport(forbidden))
    )
    r = client.get("/api/tiles/google-satellite/15/16597/11278")
    assert r.status_code == 403  # passthrough, as before

    status = client.get("/api/settings").json()["provider_status"]["google"]
    assert status["ok"] is False
    assert "not available for your account and region" in status["detail"]
    ids = {p["id"] for p in client.get("/api/satellite/providers").json()}
    assert "google-satellite" not in ids


# --- Grid Search: several saved grids per case (spec §5) -------------------


def _rect_grid(south=48.0, west=2.0, north=48.02, east=2.02, statuses=None, title=None):
    return {
        "title": title,
        "spec": {
            "azimut_grid": 1,
            "cell_m": 500,
            "anchor": {"lat": south, "lon": west},
            "lat_step": 0.0045,
            "lon_step": 0.0067,
            "aoi": {"type": "rect", "bounds": {
                "south": south, "west": west, "north": north, "east": east}},
            "statuses": statuses or {},
        },
    }


def test_search_grids_empty_list(client):
    cid = client.post("/api/cases", json={"name": "Grid"}).json()["id"]
    assert client.get(f"/api/cases/{cid}/search-grids").json() == []


def test_search_grid_save_and_reload(client):
    cid = client.post("/api/cases", json={"name": "Grid"}).json()["id"]
    body = _rect_grid(statuses={"0:0": "cleared", "1:2": "flagged"}, title="North sweep")
    saved = client.put(f"/api/cases/{cid}/search-grids/north-sweep", json=body).json()
    assert saved["name"] == "north-sweep"
    assert saved["title"] == "North sweep"
    assert saved["updated_at"]

    loaded = client.get(f"/api/cases/{cid}/search-grids/north-sweep").json()
    assert loaded["cell_m"] == 500
    assert loaded["aoi"]["type"] == "rect"
    assert loaded["statuses"] == {"0:0": "cleared", "1:2": "flagged"}
    assert loaded["azimut_grid"] == 1
    assert loaded["title"] == "North sweep"
    assert loaded["created_at"]


def test_search_grids_list_summaries(client):
    cid = client.post("/api/cases", json={"name": "Grid"}).json()["id"]
    client.put(f"/api/cases/{cid}/search-grids/a",
               json=_rect_grid(statuses={"0:0": "cleared"}, title="A"))
    client.put(f"/api/cases/{cid}/search-grids/b",
               json=_rect_grid(statuses={"0:0": "flagged", "0:1": "flagged"}, title="B"))
    listed = {g["name"]: g for g in client.get(f"/api/cases/{cid}/search-grids").json()}
    assert set(listed) == {"a", "b"}
    assert listed["a"]["cleared"] == 1
    assert listed["b"]["flagged"] == 2
    assert listed["a"]["aoi_type"] == "rect"


def test_search_grid_replace_keeps_created_at(client):
    cid = client.post("/api/cases", json={"name": "Grid"}).json()["id"]
    client.put(f"/api/cases/{cid}/search-grids/g", json=_rect_grid())
    first = client.get(f"/api/cases/{cid}/search-grids/g").json()["created_at"]

    body = _rect_grid(north=48.05, east=48.05, statuses={"3:3": "cleared"})
    body["spec"]["created_at"] = first  # the client round-trips it back
    client.put(f"/api/cases/{cid}/search-grids/g", json=body)
    again = client.get(f"/api/cases/{cid}/search-grids/g").json()
    assert again["created_at"] == first
    assert again["statuses"] == {"3:3": "cleared"}


def test_search_grid_sanitizes_bad_statuses(client):
    cid = client.post("/api/cases", json={"name": "Grid"}).json()["id"]
    body = _rect_grid(statuses={"0:0": "cleared", "0:1": "bogus", "0:2": "flagged"})
    client.put(f"/api/cases/{cid}/search-grids/g", json=body)
    loaded = client.get(f"/api/cases/{cid}/search-grids/g").json()
    assert loaded["statuses"] == {"0:0": "cleared", "0:2": "flagged"}


def test_search_grid_rejects_missing_aoi(client):
    cid = client.post("/api/cases", json={"name": "Grid"}).json()["id"]
    r = client.put(f"/api/cases/{cid}/search-grids/g", json={"spec": {"cell_m": 500}})
    assert r.status_code == 400


def test_search_grid_missing_is_404(client):
    cid = client.post("/api/cases", json={"name": "Grid"}).json()["id"]
    assert client.get(f"/api/cases/{cid}/search-grids/nope").status_code == 404


def test_search_grid_corruption_is_skipped_in_picker_and_reported_on_open(client):
    from azimut.workspace import Case

    cid = client.post("/api/cases", json={"name": "Grid"}).json()["id"]
    path = Case.open(cid).subdir(layout.SEARCH_DIR) / "broken.json"
    path.write_text('["not", "an", "object"]', encoding="utf-8")

    assert client.get(f"/api/cases/{cid}/search-grids").json() == []
    opened = client.get(f"/api/cases/{cid}/search-grids/broken")
    assert opened.status_code == 422
    assert "corrupt grid" in opened.json()["detail"]


def test_search_grid_write_is_atomic(client, monkeypatch):
    from azimut.api import satellite
    from azimut.workspace import Case

    cid = client.post("/api/cases", json={"name": "Grid"}).json()["id"]
    client.put(f"/api/cases/{cid}/search-grids/g", json=_rect_grid(title="Before"))
    path = Case.open(cid).subdir(layout.SEARCH_DIR) / "g.json"
    before = path.read_bytes()

    def fail_replace(_source, _target):
        raise OSError("replace failed")

    monkeypatch.setattr(satellite.os, "replace", fail_replace)
    with pytest.raises(OSError, match="replace failed"):
        satellite._write_grid_atomic(path, {**_rect_grid(title="After")["spec"], "title": "After"})

    assert path.read_bytes() == before
    assert list(path.parent.glob(".g.json.*.tmp")) == []


def test_concurrent_grid_patches_preserve_both_marks(client, monkeypatch):
    from azimut.api import satellite
    from azimut.workspace import Case

    cid = client.post("/api/cases", json={"name": "Concurrent grid"}).json()["id"]
    client.put(f"/api/cases/{cid}/search-grids/g", json=_rect_grid())
    case = Case.open(cid)
    original_write = satellite._write_grid_atomic
    first_in_write = Event()
    second_started = Event()
    second_finished = Event()
    calls_lock = Lock()
    calls = 0

    def controlled_write(path, spec):
        nonlocal calls
        with calls_lock:
            position = calls
            calls += 1
        if position == 0:
            first_in_write.set()
            assert second_started.wait(2)
            second_finished.wait(0.2)
        original_write(path, spec)

    def second_patch():
        second_started.set()
        try:
            return satellite.apply_grid_marks(case, "g", {"0:1": "flagged"})
        finally:
            second_finished.set()

    monkeypatch.setattr(satellite, "_write_grid_atomic", controlled_write)
    with ThreadPoolExecutor(max_workers=2) as pool:
        first = pool.submit(satellite.apply_grid_marks, case, "g", {"0:0": "cleared"})
        assert first_in_write.wait(2)
        second = pool.submit(second_patch)
        first.result(timeout=3)
        second.result(timeout=3)

    stored = client.get(f"/api/cases/{cid}/search-grids/g").json()
    assert stored["statuses"] == {"0:0": "cleared", "0:1": "flagged"}
    assert stored["revision"] == 3


def test_two_whole_grid_saves_cannot_accept_the_same_revision(client, monkeypatch):
    from azimut.api import satellite

    cid = client.post("/api/cases", json={"name": "Concurrent saves"}).json()["id"]
    client.put(f"/api/cases/{cid}/search-grids/g", json=_rect_grid())
    current = client.get(f"/api/cases/{cid}/search-grids/g").json()
    body = satellite.GridSaveIn(spec=current, base_revision=current["revision"])
    original_write = satellite._write_grid_atomic
    first_in_write = Event()
    second_started = Event()
    second_finished = Event()
    calls_lock = Lock()
    calls = 0

    def controlled_write(path, spec):
        nonlocal calls
        with calls_lock:
            position = calls
            calls += 1
        if position == 0:
            first_in_write.set()
            assert second_started.wait(2)
            second_finished.wait(0.2)
        original_write(path, spec)

    def second_save():
        second_started.set()
        try:
            return satellite.save_search_grid(cid, "g", body)
        finally:
            second_finished.set()

    monkeypatch.setattr(satellite, "_write_grid_atomic", controlled_write)
    with ThreadPoolExecutor(max_workers=2) as pool:
        first = pool.submit(satellite.save_search_grid, cid, "g", body)
        assert first_in_write.wait(2)
        second = pool.submit(second_save)
        first.result(timeout=3)
        with pytest.raises(satellite.HTTPException) as error:
            second.result(timeout=3)
    assert error.value.status_code == 409


def test_delete_waits_for_an_inflight_grid_patch(client, monkeypatch):
    from azimut.api import satellite
    from azimut.workspace import Case

    cid = client.post("/api/cases", json={"name": "Delete race"}).json()["id"]
    client.put(f"/api/cases/{cid}/search-grids/g", json=_rect_grid())
    case = Case.open(cid)
    original_write = satellite._write_grid_atomic
    writer_entered = Event()
    delete_started = Event()
    delete_finished = Event()

    def controlled_write(path, spec):
        writer_entered.set()
        assert delete_started.wait(2)
        delete_finished.wait(0.2)
        original_write(path, spec)

    def delete():
        delete_started.set()
        try:
            return satellite.delete_search_grid(cid, "g")
        finally:
            delete_finished.set()

    monkeypatch.setattr(satellite, "_write_grid_atomic", controlled_write)
    with ThreadPoolExecutor(max_workers=2) as pool:
        patching = pool.submit(satellite.apply_grid_marks, case, "g", {"0:0": "cleared"})
        assert writer_entered.wait(2)
        deleting = pool.submit(delete)
        patching.result(timeout=3)
        assert deleting.result(timeout=3)["deleted"] is True

    assert client.get(f"/api/cases/{cid}/search-grids/g").status_code == 404


def test_search_grid_delete_one(client):
    cid = client.post("/api/cases", json={"name": "Grid"}).json()["id"]
    client.put(f"/api/cases/{cid}/search-grids/a", json=_rect_grid(title="A"))
    client.put(f"/api/cases/{cid}/search-grids/b", json=_rect_grid(title="B"))
    assert client.delete(f"/api/cases/{cid}/search-grids/a").json()["deleted"] is True
    names = {g["name"] for g in client.get(f"/api/cases/{cid}/search-grids").json()}
    assert names == {"b"}
    # deleting again is a no-op, not an error
    assert client.delete(f"/api/cases/{cid}/search-grids/a").json()["deleted"] is False


def test_search_grids_are_not_entities(client):
    cid = client.post("/api/cases", json={"name": "Grid"}).json()["id"]
    client.put(f"/api/cases/{cid}/search-grids/g", json=_rect_grid())
    # grids are working aids, filed under search/, never in the case graph
    assert graph_read.entities(cid) == []


def test_capture_listing_carries_its_thumbnail_and_state(client, monkeypatch):
    # the proof panel pickers render this list in 150px cells: without a
    # thumbnail they would fall back to the full-size capture PNG.
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": 48.8584, "lon": 2.2945, "zoom": 16, "width": 640, "height": 480},
    )

    capture = client.get(f"/api/cases/{cid}/satellite").json()[0]
    assert capture["thumbnail"] and capture["thumbnail"] != capture["path"]
    assert capture["thumb_state"] == "ready"


# -- saved index & the Locate backfill ------------------------------------------------
# The Saved panel navigates hundreds of items off one compact list. What must
# hold: the list carries every kind of saved work with the fields the tree, the
# search modal and the map overlay read; reading it never touches the network;
# and the geography behind it is filled in without a save ever blocking on it.


def _address(payload):
    return lambda lat, lon, timeout=8, language=None: {
        "display_name": "x", "address": payload, "attribution": "x",
    }


_UKRAINE = _address({"country_code": "ua", "country": "Ukraine", "state": "Donetsk Oblast"})


def _index(client, cid):
    return client.get(f"/api/cases/{cid}/satellite/index").json()


def test_index_lists_places_and_captures_newest_first(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    client.post(
        f"/api/cases/{cid}/satellite/place",
        json={"lat": 48.0159, "lon": 37.8029, "title": "checkpoint north", "notes": "two vehicles"},
    )
    cap = _capture(client, cid, 50.4501, 30.5234)

    rows = _index(client, cid)
    assert [r["kind"] for r in rows] == ["capture", "place"]  # newest first

    capture, place = rows
    assert capture["path"] == cap["path"]
    assert capture["thumbnail"] and capture["thumbnail"] != capture["path"]
    assert capture["zoom"] == 16
    assert capture["provider"] == "Esri World Imagery"
    assert capture["lat"] == 50.4501

    assert place["title"] == "checkpoint north"
    assert place["notes"] == "two vehicles"
    assert place["path"] is None and place["thumbnail"] is None
    assert place["lat"] == 48.0159 and place["lon"] == 37.8029


def test_index_carries_the_geography_and_derives_the_continent(client, monkeypatch):
    monkeypatch.setattr(geo, "reverse_geocode", _UKRAINE)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    client.post(f"/api/cases/{cid}/satellite/place", json={"lat": 48.0159, "lon": 37.8029})

    row = _index(client, cid)[0]
    assert row["geo"] == {
        "state": "ok", "country_code": "ua", "country": "Ukraine", "region": "Donetsk Oblast",
    }
    # never stored, always derived — fixing the tables repairs old cases
    assert row["continent"] == "Europe"
    assert row["country_en"] == "Ukraine"


def test_index_files_a_transcontinental_country_by_the_point(client, monkeypatch):
    # one country, two continents: the coordinates decide, not the country
    monkeypatch.setattr(
        geo, "reverse_geocode",
        _address({"country_code": "ru", "country": "Russia", "state": "Primorsky Krai"}),
    )
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    client.post(f"/api/cases/{cid}/satellite/place", json={"lat": 43.1155, "lon": 131.8855})
    client.post(f"/api/cases/{cid}/satellite/place", json={"lat": 55.7558, "lon": 37.6173})

    moscow, vladivostok = _index(client, cid)
    assert moscow["continent"] == "Europe"
    assert vladivostok["continent"] == "Asia"


def test_index_names_the_country_in_english_beside_the_native_one(client, monkeypatch):
    # Nominatim answers in the local language; the English name is derived from
    # the code, so rows filed long before the tree spoke English gain it too
    monkeypatch.setattr(
        geo, "reverse_geocode",
        _address({"country_code": "ua", "country": "Україна", "state": "Донецька область"}),
    )
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    client.post(f"/api/cases/{cid}/satellite/place", json={"lat": 48.0159, "lon": 37.8029})

    row = _index(client, cid)[0]
    assert row["geo"]["country"] == "Україна"
    assert row["country_en"] == "Ukraine"


def test_index_carries_the_my_work_folder(client, monkeypatch):
    """The saved-work modal browses by folder, so the index has to say which one
    each item is in — the capture reads its sidecar, the place its own attrs."""
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    cap = _capture(client, cid, 50.4501, 30.5234)
    client.patch(
        f"/api/cases/{cid}/satellite", json={"path": cap["path"], "folder": "recon/bridges"}
    )
    client.post(
        f"/api/cases/{cid}/satellite/place",
        json={"lat": 48.0159, "lon": 37.8029, "title": "checkpoint", "folder": "recon"},
    )
    client.post(f"/api/cases/{cid}/satellite/place", json={"lat": 48.02, "lon": 37.81})

    folders = {row["title"]: row["folder"] for row in _index(client, cid)}
    assert folders["checkpoint"] == "recon"
    assert folders["50.450100, 30.523400"] == "recon/bridges"
    assert folders["48.020000, 37.810000"] == ""  # unfiled, never null


def _proof(client, cid, title, *srcs):
    return client.post(
        f"/api/cases/{cid}/proofs",
        json={
            "title": title,
            "spec": {"panels": [{"id": f"p{i}", "src": s} for i, s in enumerate(srcs)]},
        },
    ).json()


def test_index_counts_the_proofs_built_on_each_row(client, monkeypatch):
    """`All` shows every point once, so a worked capture is marked rather than
    doubled. The count is what the mark reads."""
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    worked = _capture(client, cid, 50.4501, 30.5234)
    _capture(client, cid, 48.8584, 2.2945)  # untouched
    client.post(f"/api/cases/{cid}/satellite/place", json={"lat": 48.0159, "lon": 37.8029})
    _proof(client, cid, "First", worked["path"])
    _proof(client, cid, "Second", worked["path"])

    counts = {row["title"]: row["proofs"] for row in _index(client, cid)}
    assert counts["50.450100, 30.523400"] == 2
    assert counts["48.858400, 2.294500"] == 0  # never null, so the mark is a plain test
    assert counts["48.015900, 37.802900"] == 0


def test_filing_a_capture_moves_the_entity_with_it(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    cap = _capture(client, cid, 48.8584, 2.2945)

    client.patch(f"/api/cases/{cid}/satellite", json={"path": cap["path"], "folder": "paris"})
    assert _captures(client, cid)[0]["attrs"]["folder"] == "paris"

    # a patch that says nothing about the folder leaves it where it was
    client.patch(f"/api/cases/{cid}/satellite", json={"path": cap["path"], "notes": "hangar"})
    assert _index(client, cid)[0]["folder"] == "paris"

    client.patch(f"/api/cases/{cid}/satellite", json={"path": cap["path"], "folder": ""})
    assert _index(client, cid)[0]["folder"] == ""


def test_index_makes_no_network_call(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    _capture(client, cid, 48.8584, 2.2945)
    client.post(f"/api/cases/{cid}/satellite/place", json={"lat": 48.8584, "lon": 2.2945})

    def boom(*args, **kwargs):
        raise AssertionError("reading saved work must never touch the network")

    monkeypatch.setattr(geo.httpx, "get", boom)
    assert len(_index(client, cid)) == 2


def test_index_reports_an_ingested_screenshot_as_its_own_kind(client):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    token = client.post("/api/settings/ingest-token").json()["ingest_token"]
    buf = io.BytesIO()
    Image.new("RGB", (320, 200), (30, 90, 30)).save(buf, format="PNG")
    client.post(
        "/api/ingest/screenshot",
        files={"image": ("shot.png", buf.getvalue(), "image/png")},
        data={"url": "https://yandex.com/maps/?ll=37.8029,48.0159&z=17&l=sat", "case_id": cid},
        headers={"X-Azimut-Token": token},
    )

    row = _index(client, cid)[0]
    assert row["kind"] == "screenshot"
    assert row["site"] == "yandex-maps"
    # the way back to a site we cannot embed is the recorded URL itself
    assert row["source_url"].startswith("https://yandex.com/maps/")


# -- geography on save ----------------------------------------------------------------


def test_saving_resolves_the_country_before_it_answers(client, monkeypatch):
    # the item must land in the Saved panel already grouped, not appear under
    # Unlocated and jump elsewhere a moment later
    monkeypatch.setattr(geo, "reverse_geocode", _UKRAINE)
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    client.post(f"/api/cases/{cid}/satellite/place", json={"lat": 48.0159, "lon": 37.8029})
    _capture(client, cid, 48.0159, 37.8029)

    assert [r["geo"]["country"] for r in _index(client, cid)] == ["Ukraine", "Ukraine"]


def test_the_save_lookup_is_bounded(client, monkeypatch):
    # a filed capture must never sit behind a slow geocoder: the save path asks
    # for a short timeout, the explicit backfill keeps the generous one
    seen = []
    monkeypatch.setattr(
        geo, "reverse_geocode",
        lambda lat, lon, timeout=8: seen.append(timeout) or _UKRAINE(lat, lon),
    )
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    client.post(f"/api/cases/{cid}/satellite/place", json={"lat": 48.0159, "lon": 37.8029})
    assert seen == [satellite_api.SAVE_LOOKUP_TIMEOUT]
    assert satellite_api.SAVE_LOOKUP_TIMEOUT < 8


def test_a_save_succeeds_when_the_lookup_fails(client, monkeypatch):
    # the conftest fixture already has Nominatim unreachable — saving is local
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    assert client.post(
        f"/api/cases/{cid}/satellite/place", json={"lat": 48.0159, "lon": 37.8029}
    ).status_code == 200
    _capture(client, cid, 48.0159, 37.8029)

    assert [r["geo"] for r in _index(client, cid)] == [{"state": "failed"}, {"state": "failed"}]

    # …and a later Locate pass resolves what the failure left behind
    monkeypatch.setattr(geo, "reverse_geocode", _UKRAINE)
    assert client.post(f"/api/cases/{cid}/satellite/locate").json() == {
        "located": 2, "failed": 0, "remaining": 0, "throttled": False,
    }
    assert {r["geo"]["country"] for r in _index(client, cid)} == {"Ukraine"}


def test_a_screenshot_without_coordinates_is_never_looked_up(client, monkeypatch):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    token = client.post("/api/settings/ingest-token").json()["ingest_token"]
    buf = io.BytesIO()
    Image.new("RGB", (320, 200), (30, 90, 30)).save(buf, format="PNG")
    client.post(
        "/api/ingest/screenshot",
        files={"image": ("shot.png", buf.getvalue(), "image/png")},
        data={"url": "https://www.openstreetmap.org/", "case_id": cid},
        headers={"X-Azimut-Token": token},
    )

    row = _index(client, cid)[0]
    assert row["lat"] is None
    assert row["geo"] == {"state": "nocoords"}

    def boom(lat, lon, timeout=8):
        raise AssertionError("a point with no coordinates has nothing to look up")

    monkeypatch.setattr(geo, "reverse_geocode", boom)
    assert client.post(f"/api/cases/{cid}/satellite/locate").json()["remaining"] == 0


# -- the Locate backfill ---------------------------------------------------------------


def _places_at(client, cid, count):
    for i in range(count):
        client.post(
            f"/api/cases/{cid}/satellite/place", json={"lat": 48.0 + i / 100, "lon": 37.8}
        )


def test_locate_works_through_the_backlog_and_reports_what_is_left(client, monkeypatch):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    _places_at(client, cid, 5)  # all saved offline → all failed

    monkeypatch.setattr(geo, "reverse_geocode", _UKRAINE)
    first = client.post(f"/api/cases/{cid}/satellite/locate", params={"limit": 2}).json()
    assert first == {"located": 2, "failed": 0, "remaining": 3, "throttled": False}

    second = client.post(f"/api/cases/{cid}/satellite/locate", params={"limit": 25}).json()
    assert second == {"located": 3, "failed": 0, "remaining": 0, "throttled": False}
    # settled items are never looked up again — re-running is a no-op
    assert client.post(f"/api/cases/{cid}/satellite/locate").json() == {
        "located": 0, "failed": 0, "remaining": 0, "throttled": False,
    }


def test_locate_leaves_a_failure_for_the_next_pass(client, monkeypatch):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    _places_at(client, cid, 2)

    assert client.post(f"/api/cases/{cid}/satellite/locate").json() == {
        "located": 0, "failed": 2, "remaining": 2, "throttled": False,
    }


def test_locate_reports_that_nominatim_is_throttling(client, monkeypatch):
    """A 429 is not an ordinary lookup failure: the analyst has to be told to
    wait rather than to run the pass again, so the verdict travels."""
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    _places_at(client, cid, 1)

    monkeypatch.setattr(geo, "throttled", lambda: True)
    assert client.post(f"/api/cases/{cid}/satellite/locate").json()["throttled"] is True


def test_locate_settles_open_sea_without_retrying_it(client, monkeypatch):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    client.post(f"/api/cases/{cid}/satellite/place", json={"lat": 0.0, "lon": -30.0})

    monkeypatch.setattr(geo, "reverse_geocode", _address({}))
    assert client.post(f"/api/cases/{cid}/satellite/locate").json() == {
        "located": 1, "failed": 0, "remaining": 0, "throttled": False,
    }
    assert _index(client, cid)[0]["geo"] == {"state": "nocountry"}
    assert _index(client, cid)[0]["continent"] is None


def test_locate_clamps_its_batch_size(client):
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    assert client.post(f"/api/cases/{cid}/satellite/locate", params={"limit": 26}).status_code == 422
    assert client.post(f"/api/cases/{cid}/satellite/locate", params={"limit": 0}).status_code == 422


def test_index_counts_relations_and_marks_a_proposed_point(client):
    """What the map popup needs to offer relations without the index carrying the
    edges: a count per row, and whether the point is a machine's proposal.
    Enrichment mints one suggested place per GPS-tagged file, so an index that
    could not tell them apart would let a camera's reading pass for analyst work.
    """
    from azimut.workspace import Case

    cid = client.post("/api/cases", json={"name": "Relations index"}).json()["id"]
    case = Case.open(cid)
    photo = case.add_entity("media", "Photo", {"path": "media/photo.jpg"}, by="user")
    proposed = case.add_entity(
        "place", "48.858370, 2.294481", {"lat": 48.85837, "lon": 2.294481},
        by="enrich", status="suggested",
    )
    mine = case.add_entity("place", "Checkpoint", {"lat": 50.4, "lon": 30.5}, by="user")
    case.add_link(photo["id"], proposed["id"], "located-at", by="enrich", status="suggested")

    rows = {row["title"]: row for row in _index(client, cid)}

    assert rows["48.858370, 2.294481"]["relations"] == 1
    assert rows["48.858370, 2.294481"]["status"] == "suggested"
    # a point the analyst pinned by hand carries neither mark
    assert rows["Checkpoint"]["relations"] == 0
    assert rows["Checkpoint"]["status"] == "confirmed"
    assert mine["id"] == rows["Checkpoint"]["id"]


# -- scale bar and north arrow -------------------------------------------------


def test_capture_burns_the_marks_only_when_the_menu_asked(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    frame = {"lat": 48.8584, "lon": 2.2945, "zoom": 16, "width": 640, "height": 480}

    plain = client.post(f"/api/cases/{cid}/satellite/capture", json=frame).json()
    assert plain["marks"] is None

    marked = client.post(
        f"/api/cases/{cid}/satellite/capture", json={**frame, "scale_north": True}
    ).json()
    assert marked["marks"]["scale"].endswith(" m")
    assert marked["marks"]["north"] == 0.0
    # and it rides on the filed capture, not only on the answer
    listed = client.get(f"/api/cases/{cid}/satellite").json()
    assert [row["marks"] for row in listed].count(None) == 1


def test_the_bar_is_read_in_the_analysts_units(client, monkeypatch):
    monkeypatch.setattr(tiles, "_default_fetch", _fake_tile)
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    frame = {"lat": 48.8584, "lon": 2.2945, "zoom": 16, "width": 640, "height": 480,
             "scale_north": True}

    client.put("/api/settings/prefs", json={"units": "imperial"})
    assert client.post(
        f"/api/cases/{cid}/satellite/capture", json=frame
    ).json()["marks"]["scale"].endswith(("ft", "mi"))

    client.put("/api/settings/prefs", json={"units": "metric"})
    assert client.post(
        f"/api/cases/{cid}/satellite/capture", json=frame
    ).json()["marks"]["scale"].endswith(("m", "km"))


def test_a_screen_crop_takes_the_marks_and_a_pasted_shot_never_does(client):
    """The bar states what a pixel is worth, and only a registered crop knows:
    a pasted image's coordinates describe the map view when it was filed."""
    client.put("/api/settings/keys", json={"google_js": "AIza.js"})
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    shot = {"lat": "48.8584", "lon": "2.2945", "zoom": "18", "provider": "google-js"}

    framed = client.post(
        f"/api/cases/{cid}/satellite/screenshot",
        files={"image": ("shot.png", _png_bytes(), "image/png")},
        data={**shot, "framed": "true", "scale_north": "true"},
    ).json()
    assert framed["marks"]["scale"]

    pasted = client.post(
        f"/api/cases/{cid}/satellite/screenshot",
        files={"image": ("shot.png", _png_bytes(), "image/png")},
        data={**shot, "framed": "false", "scale_north": "true"},
    ).json()
    assert pasted["marks"] is None


def test_a_denser_screen_states_the_ground_it_really_covers(client):
    """Two of the file's pixels per CSS pixel is half the ground per pixel, so
    the same view on a 2× screen must not be told it spans twice as far."""
    client.put("/api/settings/keys", json={"google_js": "AIza.js"})
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]
    shot = {"lat": "0", "lon": "0", "zoom": "14", "provider": "google-js",
            "framed": "true", "scale_north": "true"}

    def span(device_scale):
        body = client.post(
            f"/api/cases/{cid}/satellite/screenshot",
            files={"image": ("shot.png", _png_bytes(w=900, h=600), "image/png")},
            data={**shot, "device_scale": device_scale},
        ).json()
        return tiles.scale_span(
            tiles.meters_per_pixel(0.0, 14) / float(device_scale), 900 * tiles.SCALE_MAX_SHARE
        )[0], body["marks"]["scale"]

    expected_1x, drawn_1x = span("1")
    expected_2x, drawn_2x = span("2")
    assert drawn_1x == expected_1x
    assert drawn_2x == expected_2x
    assert drawn_1x != drawn_2x


# -- a refused key never comes back in an error (AUD-019) --------------------------

INSTANCE = "a1b2c3d4-0000-4bad-beef-instance0secret"
MAPBOX = "pk.eyJ1IjoidGVzdCJ9.secret-mapbox-token"


def _refusing(url, params=None, **_kwargs):
    import httpx

    return httpx.Response(401, request=httpx.Request("GET", url, params=params), text="denied")


@pytest.fixture
def keys_refused(monkeypatch):
    import httpx

    from azimut import config

    config.save_settings({**config.load_settings(), "api_keys": {"sentinelhub": INSTANCE, "mapbox": MAPBOX}})
    monkeypatch.setattr(httpx, "get", _refusing)
    monkeypatch.setattr(httpx, "post", lambda url, **kw: _refusing(url, **{k: v for k, v in kw.items() if k == "params"}))


@pytest.mark.parametrize(
    "method, path, body",
    [
        ("get", "/api/satellite/sentinel/dates?lat=48.8&lon=2.3&start=2026-01-01&end=2026-02-01", None),
        ("get", "/api/satellite/sentinel/layers?check=true", None),
        ("post", "/api/satellite/sentinel1/layer", {}),
        ("get", "/api/satellite/sentinel/coverage?lat=48.8&lon=2.3&layer=TRUE-COLOR&date=2026-01-02", None),
    ],
)
def test_a_refused_sentinel_key_is_never_echoed(client, keys_refused, method, path, body):
    answer = getattr(client, method)(path, **({"json": body} if body is not None else {}))

    assert INSTANCE not in answer.text
    assert "https://" not in answer.text


def test_a_refused_capture_key_is_never_echoed(client, keys_refused, monkeypatch):
    import httpx

    def refuse(_client, url):
        raise httpx.HTTPStatusError("401", request=httpx.Request("GET", url), response=httpx.Response(401))

    monkeypatch.setattr(tiles, "_default_fetch", lambda c, url: refuse(c, f"{url}?access_token={MAPBOX}"))
    cid = client.post("/api/cases", json={"name": "Sat"}).json()["id"]

    answer = client.post(
        f"/api/cases/{cid}/satellite/capture",
        json={"lat": 48.8584, "lon": 2.2945, "zoom": 16, "width": 256, "height": 256,
              "provider": "mapbox-satellite"},
    )

    assert answer.status_code == 502
    assert MAPBOX not in answer.text and "refused the key or the account (401)" in answer.text


def test_an_upstream_failure_names_the_code_and_not_the_address():
    import httpx

    request = httpx.Request("GET", f"https://services.sentinel-hub.com/ogc/wfs/{INSTANCE}?x=1")
    refused = httpx.HTTPStatusError("x", request=request, response=httpx.Response(403, request=request))
    assert tiles.upstream_failure(refused) == "the service refused the key or the account (403)"
    assert tiles.upstream_failure(httpx.ConnectTimeout("t", request=request)) == "the provider did not answer in time"
    assert INSTANCE not in tiles.upstream_failure(ValueError(f"bad reply from {request.url}"))


@pytest.mark.parametrize(
    ("code", "said"),
    [
        (401, "the service refused the key or the account (401)"),
        (429, "the service's allowance or rate limit is spent (429)"),
        (503, "the service is failing (503); try again later"),
        (400, "the service answered 400"),
    ],
)
def test_a_copernicus_refusal_says_what_it_means_and_the_service_own_words(code, said):
    # "answered 403" alone read like a bug in Azimut: a refused key or a spent
    # allowance is something the analyst can go and fix, so it is said that way
    import httpx

    request = httpx.Request("GET", f"https://sh.dataspace.copernicus.eu/ogc/wfs/{INSTANCE}?x=1")
    bare = httpx.HTTPStatusError("x", request=request, response=httpx.Response(code, request=request))
    assert tiles.upstream_failure(bare) == said
    # Sentinel Hub's own report follows, unwrapped, with nothing it was sent
    report = (
        '<ows:ExceptionReport><ows:Exception><ows:ExceptionText>Monthly processing units exhausted for '
        f'https://sh.dataspace.copernicus.eu/ogc/wms/{INSTANCE}</ows:ExceptionText></ows:Exception></ows:ExceptionReport>'
    )
    reported = httpx.HTTPStatusError(
        "x", request=request, response=httpx.Response(code, content=report.encode(), request=request)
    )
    told = tiles.upstream_failure(reported)
    assert told.startswith(f"{said}: Monthly processing units exhausted")
    assert INSTANCE not in told


# -- the tile cache expires what nobody reads again (AUD-024) ----------------------


def test_the_tile_cache_sweep_drops_what_is_past_its_days_and_keeps_the_rest(tmp_workspace):
    import os
    import time

    from azimut import config
    from azimut.engine import tilecache

    tilecache.put("s2-2025-01-01", 12, 1, 2, b"old", "image/png")
    tilecache.put("s2-2026-09-01", 12, 1, 2, b"new", "image/png")
    root = config.tile_cache_dir()
    old = root / "s2-2025-01-01" / "12" / "1_2.png"
    past = time.time() - (tilecache.TTL_DAYS + 1) * 86400
    os.utime(old, (past, past))

    assert tilecache.sweep() == 1

    assert not old.exists() and not (root / "s2-2025-01-01").exists()
    assert tilecache.get("s2-2026-09-01", 12, 1, 2) == (b"new", "image/png")


def test_opening_the_workspace_sweeps_the_tile_cache(tmp_workspace, monkeypatch):
    import threading

    from azimut import workspace
    from azimut.engine import tilecache

    swept = threading.Event()
    monkeypatch.setattr(tilecache, "sweep", lambda: swept.set())

    workspace.open_workspace()

    assert swept.wait(5)


def test_the_tile_cache_answers_whether_it_holds_a_tile_without_reading_it():
    # what Horizon asks before it lays imagery over a view (api/horizon.py)
    buffer = io.BytesIO()
    Image.new("RGB", (256, 256), (40, 90, 40)).save(buffer, format="PNG")
    assert not tilecache.has("esri-world-imagery", 3, 1, 1)
    tilecache.put("esri-world-imagery", 3, 1, 1, buffer.getvalue(), "image/png")
    assert tilecache.has("esri-world-imagery", 3, 1, 1)