#!/usr/bin/env python3
"""
Tests for the independent narrow per-state ASC ground-truth floor query.

Extracted from ``test_mmv_floor_check.py`` (round 11) so each test
file stays under the 10-functions-per-file cap. Counting nested
``fake_request`` closures, the parent file was at 13 defs; pulling
``GroundTruthFloorTests`` out drops both files below the cap.

Covers ``mmv.get_ground_truth_floor`` -- the defence-in-depth narrow
query the floor check uses to validate the main fetch's combined
floor. Each test pins the per-state filter behaviour so a buggy main
fetch can't silently mask a higher ship-blocking row.
"""

from __future__ import annotations

import sys
import unittest
from pathlib import Path
from unittest import mock

sys.path.insert(0, str(Path(__file__).resolve().parent))

import asc_version_fetch  # noqa: E402
import manage_marketing_version as mmv  # noqa: E402

from tests_common import _FakeResponse  # noqa: E402


class GroundTruthFloorTests(unittest.TestCase):
    """Independent narrow per-state query is the self-correcting floor."""

    def test_ground_truth_floor_finds_ready_for_sale(self):
        def fake_request(method, path, token, *, params=None, **_kw):
            state_filter = (params or {}).get("filter[appStoreState]", "")
            if "READY_FOR_SALE" in state_filter:
                return _FakeResponse({
                    "data": [{
                        "id": "id-1.0.4",
                        "attributes": {
                            "versionString": "1.0.4",
                            "appStoreState": "READY_FOR_SALE",
                            "platform": "IOS",
                            "createdDate": "2025-04-01T00:00:00Z",
                        },
                    }],
                    "links": {},
                })
            return _FakeResponse({"data": [], "links": {}})

        with mock.patch.object(asc_version_fetch, "request", side_effect=fake_request):
            floor = mmv.get_ground_truth_floor("111", "tok")

        self.assertEqual(floor, "1.0.4")

    def test_ground_truth_floor_considers_all_ship_blocking_states(self):
        def fake_request(method, path, token, *, params=None, **_kw):
            state_filter = (params or {}).get("filter[appStoreState]", "")
            if "PENDING_APPLE_RELEASE" in state_filter:
                return _FakeResponse({
                    "data": [{
                        "id": "id-1.0.6",
                        "attributes": {
                            "versionString": "1.0.6",
                            "appStoreState": "PENDING_APPLE_RELEASE",
                            "platform": "IOS",
                            "createdDate": "2025-06-01T00:00:00Z",
                        },
                    }],
                    "links": {},
                })
            if "READY_FOR_SALE" in state_filter:
                return _FakeResponse({
                    "data": [{
                        "id": "id-1.0.4",
                        "attributes": {
                            "versionString": "1.0.4",
                            "appStoreState": "READY_FOR_SALE",
                            "platform": "IOS",
                            "createdDate": "2025-04-01T00:00:00Z",
                        },
                    }],
                    "links": {},
                })
            return _FakeResponse({"data": [], "links": {}})

        with mock.patch.object(asc_version_fetch, "request", side_effect=fake_request):
            floor = mmv.get_ground_truth_floor("111", "tok")

        self.assertEqual(floor, "1.0.6")

    def test_ground_truth_floor_returns_none_when_no_ship_blocking(self):
        def fake_request(method, path, token, *, params=None, **_kw):
            return _FakeResponse({"data": [], "links": {}})

        with mock.patch.object(asc_version_fetch, "request", side_effect=fake_request):
            floor = mmv.get_ground_truth_floor("111", "tok")

        self.assertIsNone(floor)


if __name__ == "__main__":
    unittest.main()
