from __future__ import annotations

import configparser
import io
import pathlib
import os
import shutil
import sys
import tempfile
from unittest.mock import MagicMock
from urllib.error import URLError

import pytest

from DisplayCAL import config
from DisplayCAL.argyll import (
    get_argyll_latest_version,
    get_argyll_util,
    make_argyll_compatible_path,
)
from DisplayCAL.cgats import CGATS
from DisplayCAL.config import getcfg, initcfg, setcfg
from DisplayCAL.debughelpers import Error
from DisplayCAL.dev.mocks import check_call_str
from DisplayCAL.icc_profile import ICCProfile, LUT16Type
from DisplayCAL.meta import DOMAIN
from DisplayCAL.worker import (
    add_keywords_to_cgats,
    check_cal_isfile,
    check_create_dir,
    check_file_isfile,
    check_profile_isfile,
    check_ti3_criteria1,
    create_shaper_curves,
    extract_device_gray_primaries,
    get_argyll_version_string,
    get_options_from_profile,
    Sudo,
    Worker,
)
from tests.data.display_data import DisplayData


def test_get_options_from_profile_1(data_files):
    """Test ``DisplayCAL.worker.get_options_from_profile()`` function"""
    profile_path = data_files[
        "UP2516D #1 2022-03-23 16-06 D6500 2.2 F-S XYZLUT+MTX.icc"
    ].absolute()
    options = get_options_from_profile(profile=profile_path)
    assert options == (
        [
            "t6500",
            "g2.2",
            "f1.0",
            "A4.0",
            "d1",
            "c1",
            "yl",
            "P0.48923385077616427,0.8797619047619047,1.4894179894179895",
            "H",
        ],
        ["qh", "aX", 'A "Dell, Inc."'],
    )


def test_get_options_from_profile_2(data_files):
    """Test ``DisplayCAL.worker.get_options_from_profile()`` function, for #69"""
    profile_path = data_files["SW271 PM PenalNative_KB1_160_2022-03-17.icc"].absolute()
    options = get_options_from_profile(profile=profile_path)
    assert options == ([], [])  # no options on that profile


def test_make_argyll_compatible_path_1():
    """make_argyll_compatible_path is working properly with bytes input."""
    test_value = "C:\\Program Files\\some path\\executable.exe"
    result = make_argyll_compatible_path(test_value)
    if sys.platform == "win32":
        expected_result = "C:\\Program Files\\some path\\executable.exe"
    else:
        expected_result = "C_Program Files_some path_executable.exe"
    assert result == expected_result


def test_make_argyll_compatible_path_2():
    """make_argyll_compatible_path is working properly with bytes input."""
    test_value = b"C:\\Program Files\\some path\\executable.exe"
    result = make_argyll_compatible_path(test_value)
    if sys.platform == "win32":
        expected_result = b"C:\\Program Files\\some path\\executable.exe"
    else:
        expected_result = b"C_Program Files_some path_executable.exe"
    assert result == expected_result


def test_worker_get_instrument_name_1():
    """Worker.get_instrument_name() is working properly."""
    worker = Worker()
    result = worker.get_instrument_name()
    expected_result = ""
    assert result == expected_result


def test_worker_get_instrument_features():
    """Worker.get_instrument_features() is working properly."""
    worker = Worker()
    result = worker.get_instrument_features()
    assert result == {}


def test_worker_instrument_supports_css_1():
    """testing if Worker.instrument_supports_ccss is working properly"""
    worker = Worker()
    result = worker.instrument_supports_ccss()
    expected_result = None
    assert result == expected_result

# @pytest.mark.skip(reason="Test segfaults with python 3.12 - further investigation required.")
def test_generate_b2a_from_inverse_table(data_files, setup_argyll):
    """Test Worker.generate_B2A_from_inverse_table() method"""
    import wx
    from DisplayCAL.argyll import ARGYLL_UTILS
    from DisplayCAL.config import setcfg, writecfg

    # Other tests in this module call initcfg() which re-reads DisplayCAL.ini
    # from disk. Under xdist, a concurrent worker may have overwritten that
    # file with its own (now-deleted) temp Argyll path, poisoning our
    # in-process config. Re-assert the path from the session fixture and
    # flush the utility-lookup cache so get_argyll_util() searches fresh.
    setcfg("argyll.dir", str(setup_argyll))
    ARGYLL_UTILS.clear()
    writecfg()  # pool workers (SpawnPoolWorker-*) read config from disk

    # for some reason we sometimes need to have a wx.App() running
    _ = wx.GetApp() or wx.App()
    worker = Worker()
    icc_profile1 = ICCProfile(
        profile=data_files[
            "Monitor 1 #1 2022-03-09 16-13 D6500 2.2 F-S XYZLUT+MTX.icc"
        ].absolute()
    )
    logfile = io.StringIO()
    result = worker.generate_B2A_from_inverse_table(icc_profile1, logfile=logfile)
    assert result is True


def test_apply_black_offset_signature_uses_logfile() -> None:
    """ICCProfile/LUT16Type.apply_black_offset keep the ``logfile`` parameter.

    Guards the contract the caller relies on (see the functional test below).
    """
    import inspect

    for cls in (ICCProfile, LUT16Type):
        params = inspect.signature(cls.apply_black_offset).parameters
        assert "logfile" in params, (
            f"{cls.__name__}.apply_black_offset lost its 'logfile' parameter"
        )
        assert "logfiles" not in params, (
            f"{cls.__name__}.apply_black_offset must not use 'logfiles'"
        )


def test_blend_profile_blackpoint_calls_apply_black_offset_with_logfile(
    data_files, monkeypatch
) -> None:
    """blend_profile_blackpoint must call apply_black_offset with ``logfile=``.

    Regression: it used ``logfiles=``, which raised
    ``apply_black_offset() got an unexpected keyword argument 'logfiles'`` and
    aborted 3D LUT creation whenever a black offset was applied. profile1's
    apply_black_offset is replaced with a strict-signature stub (mirroring
    ICCProfile.apply_black_offset) so that a ``logfiles=`` call would raise
    TypeError here, exactly as it did in production.
    """
    icc_path = data_files[
        "UP2516D #1 2022-03-20 02-08 D6500 2.2 F-S XYZLUT+MTX.icc"
    ].absolute()
    profile1 = ICCProfile(profile=icc_path)
    profile2 = ICCProfile(profile=icc_path)

    received = {}

    def strict_apply_black_offset(
        XYZbp,
        power=40.0,
        include_A2B=True,
        set_blackpoint=True,
        logfile=None,
        thread_abort=None,
        abortmessage="Aborted",
        include_trc=True,
    ):
        received["logfile"] = logfile

    profile1.apply_black_offset = strict_apply_black_offset

    worker = Worker()
    monkeypatch.setattr(
        worker, "xicclu", lambda profile, idata, **kwargs: [[0.0, 0.0, 0.0]]
    )
    # apply_trc=False forces the branch that applies the black offset.
    worker.blend_profile_blackpoint(profile1, profile2, apply_trc=False)

    assert "logfile" in received, "apply_black_offset was not called"
    assert received["logfile"] is not None


def test_generate_b2a_whitepoint_check_tolerance(data_files, monkeypatch) -> None:
    """The whitepoint sanity check accepts near-D50 and rejects grossly wrong.

    Regression: the check required the normalised white to round to exactly
    D50 (0.964, 1.0, 0.825), so a real profile whose adapted white was off D50
    by ~0.001 (chromatic adaptation / measurement / 16-bit encoding) was
    wrongly rejected with "Invalid white XYZ". It now uses a 0.01 tolerance,
    which still catches a grossly wrong white from a failed inverse lookup.
    """
    import wx

    _ = wx.GetApp() or wx.App()
    worker = Worker()
    profile = ICCProfile(
        profile=data_files[
            "Monitor 1 #1 2022-03-09 16-13 D6500 2.2 F-S XYZLUT+MTX.icc"
        ].absolute()
    )
    # xicclu returns [black, white, R, G, B] in XYZ for idata
    # [[0,0,0],[1,1,1],[1,0,0],[0,1,0],[0,0,1]].
    primaries = [[0.43, 0.22, 0.02], [0.38, 0.71, 0.10], [0.18, 0.07, 0.72]]

    # White ~0.001 off D50 (normalises to ~0.965/1.0/0.824) - must be accepted.
    def xicclu_near_d50(prof, idata, *args, **kwargs):
        return [[0.001, 0.001, 0.001], [0.9647, 0.9995, 0.8240], *primaries]

    monkeypatch.setattr(worker, "xicclu", xicclu_near_d50)
    try:
        worker.generate_B2A_from_inverse_table(profile, logfile=io.StringIO())
    except Exception as exc:  # may still fail later for unrelated reasons
        assert "Invalid white" not in str(exc), (
            "a near-D50 white was wrongly rejected by the sanity check"
        )

    # Grossly wrong white (normalises far from D50) - must still be rejected.
    def xicclu_bad_white(prof, idata, *args, **kwargs):
        return [[0.0, 0.0, 0.0], [0.5, 0.5, 0.4], *primaries]

    monkeypatch.setattr(worker, "xicclu", xicclu_bad_white)
    with pytest.raises(Error, match="Invalid white"):
        worker.generate_B2A_from_inverse_table(profile, logfile=io.StringIO())


def test_create_rgb_xyz_clut_fwd_profile_rgb_in_within_device_range(
    data_files, monkeypatch
) -> None:
    """The upsampled cLUT lookup grid must stay within device 0..100%.

    Regression for the stale ``step`` bug in create_RGB_XYZ_cLUT_fwd_profile:
    RGB_in was built with the previous low-resolution iclutres step, so its
    coordinates ran far past device 100% (e.g. iclutres=5 -> 32 * 25 = 800).
    xicclu(scale=100) then clamped those out-of-range inputs to the white
    corner, filling most of the A2B cLUT with the white point.
    """
    setcfg("profile.quality", "h")
    worker = Worker()
    ti3 = CGATS(
        str(
            data_files[
                "UP2516D #1 2022-03-20 02-08 D6500 2.2 F-S XYZLUT+MTX.ti3"
            ].absolute()
        )
    )

    captured = {}

    def fake_xicclu(prof, idata, *args, **kwargs):
        # Capture the first lookup (the upsampling grid) and return in-gamut XYZ
        captured.setdefault("rgb_in", [list(row) for row in idata])
        return [[50.0, 50.0, 50.0] for _ in idata]

    monkeypatch.setattr(worker, "xicclu", fake_xicclu)
    worker.create_RGB_XYZ_cLUT_fwd_profile(ti3, "test", "", "", "")

    assert "rgb_in" in captured, "the cLUT upsampling lookup was never reached"
    max_value = max(max(row) for row in captured["rgb_in"])
    assert max_value <= 100.0 + 1e-6, (
        f"RGB_in grid overshoots device 100% (max={max_value:.2f}) - "
        "stale-step regression in create_RGB_XYZ_cLUT_fwd_profile"
    )


def test_sudo_class_initialization():
    """Test worker.Sudo class initialization"""
    sudo = Sudo()
    assert sudo is not None


def test_download_method_1():
    """Test Worker.download() method."""
    worker = Worker()
    uri = f"https://{DOMAIN}/i1d3"
    result = worker.download(uri)
    assert result is not None


def test_download_method_2():
    """Test Worker.download() method."""
    worker = Worker()
    uri = f"https://{DOMAIN}/i1d3"
    result = worker.download(uri, force=True)
    assert result is not None


def test_download_method_3():
    """Test Worker.download() method."""
    worker = Worker()
    uri = f"https://{DOMAIN}/spyd2"
    result = worker.download(uri)
    assert result is not None


def test_download_method_4():
    """Test Worker.download() method."""
    worker = Worker()
    uri = f"https://{DOMAIN}/spyd2"
    result = worker.download(uri, force=True)
    assert result is not None


def test_get_display_name_1():
    """Testing Worker.get_display_name() method."""
    initcfg()
    setcfg("display.number", 1)
    worker = Worker()
    result = worker.get_display_name(False, True, False)
    assert result == ""


def test_get_pwd():
    """Testing Worker.get_display_name() method."""
    initcfg()
    worker = Worker()
    test_value = "test_value"
    worker.pwd = test_value
    assert worker.pwd == test_value


def test_update_profile_1(random_icc_profile):
    """Testing Worker.update_profile() method."""
    from DisplayCAL import worker

    worker.dbus_session = None
    worker.dbus_system = None
    initcfg()
    worker = Worker()

    icc_profile, icc_profile_path = random_icc_profile
    with check_call_str(
        "DisplayCAL.worker.Worker.get_display_edid", DisplayData.DISPLAY_DATA_2
    ):
        worker.update_profile(icc_profile_path, tags=True)


def test_exec_cmd_1():
    """Test worker.exec_cmd() function for issue #73"""
    # Command line:
    cmd = "/home/eoyilmaz/.local/bin/Argyll_V2.3.0/bin/colprof"
    args = [
        "-v",
        "-qh",
        "-ax",
        "-bn",
        "-C",
        b"No copyright. Created with DisplayCAL 3.8.9.3 and Argyll CMS 2.3.0",
        "-A",
        "Dell, Inc.",
        "-D",
        "UP2516D_#1_2022-04-01_00-26_2.2_F-S_XYZLUT+MTX",
        "/tmp/DisplayCAL-i91d9z8_/UP2516D_#1_2022-04-01_00-26_2.2_F-S_XYZLUT+MTX",
    ]
    cwd = "/tmp/DisplayCAL-i91d9z8_"
    worker = Worker()
    worker.exec_cmd(cmd=cmd, args=args)


def test_is_allowed_1():
    """Test Sudo.is_allowed() function for issue #76"""
    sudo = Sudo()
    result = sudo.is_allowed()
    assert result != ""


def test_ti3_lookup_to_ti1_1(data_files, setup_argyll):
    """Test Worker.ti3_lookup_to_ti1() function for #129"""
    ti3_path = data_files["0_16_from_issue_129.ti3"].absolute()
    profile_path = data_files[
        "UP2516D #1 2022-03-23 16-06 D6500 2.2 F-S XYZLUT+MTX.icc"
    ].absolute()

    ti3_cgat = CGATS(ti3_path)
    icc_profile = ICCProfile(profile_path)
    config.initcfg()
    worker = Worker()
    ti1, ti3v = worker.ti3_lookup_to_ti1(ti3_cgat, icc_profile)
    assert isinstance(ti1, CGATS)
    assert isinstance(ti3v, CGATS)
    assert ti1 == {
        0: {
            "COLOR_REP": b"RGB",
            "DATA": {
                0: {
                    "RGB_B": 99.9959,
                    "RGB_G": 100.0,
                    "RGB_R": 97.1526,
                    "SAMPLE_ID": 1,
                    "XYZ_X": 95.0104,
                    "XYZ_Y": 100.0,
                    "XYZ_Z": 92.7202,
                },
                1: {
                    "RGB_B": 9.1428,
                    "RGB_G": 5.8338,
                    "RGB_R": 5.842,
                    "SAMPLE_ID": 2,
                    "XYZ_X": 0.277593,
                    "XYZ_Y": 0.255279,
                    "XYZ_Z": 0.423145,
                },
                2: {
                    "RGB_B": 11.6181,
                    "RGB_G": 9.1081,
                    "RGB_R": 8.0801,
                    "SAMPLE_ID": 3,
                    "XYZ_X": 0.51238,
                    "XYZ_Y": 0.536117,
                    "XYZ_Z": 0.705578,
                },
            },
            "DATA_FORMAT": {
                0: b"SAMPLE_ID",
                1: b"RGB_R",
                2: b"RGB_G",
                3: b"RGB_B",
                4: b"XYZ_X",
                5: b"XYZ_Y",
                6: b"XYZ_Z",
            },
            "DESCRIPTOR": b"Argyll Calibration Target chart information 1",
            "KEYWORDS": {0: b"COLOR_REP"},
            "NUMBER_OF_FIELDS": None,
            "NUMBER_OF_SETS": None,
        }
    }

    assert ti3v == {
        "COLOR_REP": b"RGB_XYZ",
        "CREATED": b"Sun Jun  5 13:08:54 2022",
        "DATA": {
            0: {
                "RGB_B": 99.9959,
                "RGB_G": 100.0,
                "RGB_R": 97.1526,
                "SAMPLE_ID": 1,
                "XYZ_X": 95.0104,
                "XYZ_Y": 100.0,
                "XYZ_Z": 92.7202,
            },
            1: {
                "RGB_B": 9.1428,
                "RGB_G": 5.8338,
                "RGB_R": 5.842,
                "SAMPLE_ID": 2,
                "XYZ_X": 0.277593,
                "XYZ_Y": 0.255279,
                "XYZ_Z": 0.423145,
            },
            2: {
                "RGB_B": 11.6181,
                "RGB_G": 9.1081,
                "RGB_R": 8.0801,
                "SAMPLE_ID": 3,
                "XYZ_X": 0.51238,
                "XYZ_Y": 0.536117,
                "XYZ_Z": 0.705578,
            },
        },
        "DATA_FORMAT": {
            0: b"SAMPLE_ID",
            1: b"RGB_R",
            2: b"RGB_G",
            3: b"RGB_B",
            4: b"XYZ_X",
            5: b"XYZ_Y",
            6: b"XYZ_Z",
        },
        "DESCRIPTOR": b"Argyll Calibration Target chart information 3",
        "DEVICE_CLASS": b"DISPLAY",
        "DISPLAY_TYPE_BASE_ID": 1,
        "DISPLAY_TYPE_REFRESH": b"NO",
        "INSTRUMENT_TYPE_SPECTRAL": b"NO",
        "LUMINANCE_XYZ_CDM2": b"42.204124 44.420532 41.186805",
        "NORMALIZED_TO_Y_100": b"YES",
        "NUMBER_OF_FIELDS": None,
        "NUMBER_OF_SETS": None,
        "ORIGINATOR": b"Argyll dispread",
        "TARGET_INSTRUMENT": b"Datacolor Spyder3",
        "VIDEO_LUT_CALIBRATION_POSSIBLE": b"YES",
    }


def test_add_keywords_to_cgats(data_files) -> None:
    """Test if keywords are added to cgats by add_keywords_to_cgats."""
    path = data_files["0_16.ti3"].absolute()
    cgats = CGATS(cgats=path)
    assert "keyword" not in cgats[0]
    options = {"keyword": "Value"}
    alternated_cgats = add_keywords_to_cgats(cgats, options)
    assert "keyword" in alternated_cgats[0]


def test_check_create_dir() -> None:
    """Test function 'check_create_dir'."""
    assert check_create_dir("test_dir") == True


@pytest.mark.parametrize("file", (True, False))
def test_check_cal_isfile(data_files, file: bool) -> None:
    """Test 'check_cal_isfile'."""
    path = data_files["Monitor.cal"].absolute() if file else "no_file"
    assert check_cal_isfile(path) == True if file else "error.calibration.file_missing"


@pytest.mark.parametrize("file", (True, False))
def test_check_profile_isfile(data_files, file: bool) -> None:
    """Test 'check_profile_isfile'."""
    path = data_files["Monitor.cal"].absolute() if file else "no_file"
    assert check_profile_isfile(path) == True if file else "error.profile.file_missing"


# todo: test is working locally but not on CI
@pytest.mark.skip(
    reason="First execution of test fails on remote CI server. "
    "All following tests are positive."
)
@pytest.mark.parametrize("silent", (True, False), ids=("silent", "not silent"))
@pytest.mark.parametrize(
    "path,result",
    (
        ("data/cgats0.txt", ("True", "True")),
        ("no_file", ("False", "file.missing")),
        (".", ("False", "file.notfile")),
    ),
)
def test_check_file_isfile(
    data_files, silent: bool, path: str, result: tuple[str, str]
) -> None:
    """Test if file gets detected."""
    assert (
        str(check_file_isfile(path, silent=silent)) == result[0]
        if silent
        else result[1]
    )


@pytest.mark.parametrize(
    "sample,result",
    (
        (
            {
                "SAMPLE_ID": 1,
                "RGB_R": 50,
                "RGB_G": 50,
                "RGB_B": 50,
                "XYZ_X": 0.5,
                "XYZ_Y": 0.5,
                "XYZ_Z": 0.5,
            },
            True,
        ),
        (
            {
                "SAMPLE_ID": 2,
                "RGB_R": 6,
                "RGB_G": 6,
                "RGB_B": 6,
                "XYZ_X": 0.5,
                "XYZ_Y": 0.5,
                "XYZ_Z": 0.5,
            },
            False,
        ),
    ),
)
def test_check_ti3_criteria1(sample: dict[str:float], result: bool) -> None:
    """Test for ti3 criteria1 check."""
    black = (0, 0, 0)
    white = (110, 110, 110)
    criteria = check_ti3_criteria1(
        (sample["RGB_R"], sample["RGB_G"], sample["RGB_B"]),
        (sample["XYZ_X"], sample["XYZ_Y"], sample["XYZ_Z"]),
        black,
        white,
        print_debuginfo=True,
    )
    assert criteria[3] == result


def test_prepare_colprof_for_271(monkeypatch, data_path):
    """Bug report 271."""
    assert isinstance(data_path, pathlib.Path)

    def patched_getcfg(key):
        """patched getcfg()"""
        cfg = {
            "argyll.version": "2.3.1",
            "profile.name.expanded": "test_profile",
            "profile.quality": "m",
            "profile.type": "l",
            "gamap_saturation": False,
            "gamap_perceptual": False,
            "profile.quality.b2a": "h",
            "profile.b2a.hires": True,
            "copyright": "",
            "extra_args.colprof": "",
            "profile.black_point_compensation": False,
            "profile.black_point_correction": False,
            "profile.b2a.hires.size": 17,
            "profile.b2a.hires.smooth": True,
            "measure.override_min_display_update_delay_ms": False,
            "measure.override_display_settle_time_mult": False,
            "patterngenerator.ffp_insertion": False,
            "testchart.patch_sequence": "",
            "3dlut.create": False,
        }
        return cfg[key]

    monkeypatch.setattr("DisplayCAL.worker.getcfg", patched_getcfg)

    def patched_os_path_exists(filepath):
        return True

    monkeypatch.setattr("DisplayCAL.worker.os.path.exists", patched_os_path_exists)

    def patched_os_path_isfile(filepath):
        return True

    monkeypatch.setattr("DisplayCAL.worker.os.path.isfile", patched_os_path_isfile)

    worker = Worker()
    in_out_file = pathlib.Path(worker.setup_inout("test_profile")).with_suffix(".ti3")

    # copy the test file to the target path
    test_file_path = data_path / "sample" / "issue271" / "test_profile.ti3"
    os.makedirs(in_out_file.parent, exist_ok=True)
    shutil.copy(test_file_path, in_out_file)

    # This should not raise the:
    # TypeError: startswith first arg must be bytes or a tuple of bytes, not str
    worker.prepare_colprof()


def test_prepare_dispcal_1():
    """Worker.prepare_dispcal() return value should be quoted properly."""
    worker = Worker()
    return_val = worker.prepare_dispcal()
    expected_result = [
        "-v2",
        "-d0",
        "-c1",
        return_val[1][3],  # '-yl',
        return_val[1][4],  # '-P0.5,0.5,1.0',
        "-ql",
        return_val[1][6],  # '-t',
        "-g2.2",
        "-f1.0",
        return_val[1][9],  # '-k0.0',
        "/var/folders/8l/xy1__ym94nn35x86xyg56xq80000gn/T/DisplayCAL-2fdjtyql/",
    ]
    assert return_val[0] == get_argyll_util("dispcal")
    assert isinstance(return_val[1], list)
    assert return_val[1][:-1] == expected_result[:-1]  # don't check the final part
    assert tempfile.gettempdir() in return_val[1][-1]  # this should be in a temp path


@pytest.mark.skipif(
    os.getenv("GITHUB_ACTIONS") == "true",
    reason="Not working properly on GitHub.",
)
def test_get_argyll_version_string_returns_a_proper_value():
    """get_argyll_version_string() returns a proper value."""
    import wx

    config.initcfg()
    app = wx.GetApp() or wx.App()

    assert "0.0.0" != get_argyll_version_string(name="ccxxmake", silent=False)


def test_get_argyll_latest_version_returns_str():
    """get_argyll_latest_version() returns a str."""
    result = get_argyll_latest_version()
    assert isinstance(result, str)


@pytest.mark.skipif(
    os.getenv("GITHUB_ACTIONS") == "true",
    reason="Test is randomly failing on CI machines."
)
def test_get_argyll_latest_version_returns_latest_argyll_cms_version():
    """get_argyll_latest_version() returns the latest argyll cms version."""
    result = get_argyll_latest_version()
    assert result == "3.5.0"


def test_get_argyll_latest_version_returns_the_default_version_if_no_internet_connect(
    monkeypatch,
):
    """get_argyll_latest_version() returns the default argyll cms version if no internet connection."""

    def patched_urlopen(*args, **kwargs):
        raise URLError(
            "<urlopen error [Errno 8] nodename nor servname provided, or not known>"
        )

    monkeypatch.setattr("DisplayCAL.argyll.urllib.request.urlopen", patched_urlopen)
    monkeypatch.setattr("DisplayCAL.argyll.time.sleep", lambda _: None)
    get_argyll_latest_version.cache_clear()
    result = get_argyll_latest_version()
    assert result == config.DEFAULTS.get("argyll.version")


@pytest.mark.skipif(
    os.getenv("GITHUB_ACTIONS") == "true" and sys.platform == "linux",
    reason="Not working properly on GitHub on Linux machines.",
)
def test_get_technology_strings_returns_dict(setup_argyll):
    """Test get_technology_strings() returns a dict."""
    worker = Worker()
    result = worker.get_technology_strings()
    assert isinstance(result, dict)


def test_get_technology_strings_without_argyll_returns_from_argyll_17():
    """Test get_technology_strings() returns a dictionary from argyll 1.7."""
    get_argyll_latest_version.cache_clear()
    worker = Worker()
    worker.argyll_version = [0, 0, 0]

    result = worker.get_technology_strings()
    assert result == {
        "c": "CRT",
        "m": "Plasma",
        "l": "LCD",
        "1": "LCD CCFL",
        "2": "LCD CCFL IPS",
        "3": "LCD CCFL VPA",
        "4": "LCD CCFL TFT",
        "L": "LCD CCFL Wide Gamut",
        "5": "LCD CCFL Wide Gamut IPS",
        "6": "LCD CCFL Wide Gamut VPA",
        "7": "LCD CCFL Wide Gamut TFT",
        "e": "LCD White LED",
        "8": "LCD White LED IPS",
        "9": "LCD White LED VPA",
        "d": "LCD White LED TFT",
        "b": "LCD RGB LED",
        "f": "LCD RGB LED IPS",
        "g": "LCD RGB LED VPA",
        "i": "LCD RGB LED TFT",
        "h": "LCD RG Phosphor",
        "j": "LCD RG Phosphor IPS",
        "k": "LCD RG Phosphor VPA",
        "n": "LCD RG Phosphor TFT",
        "o": "LED OLED",
        "a": "LED AMOLED",
        "p": "DLP Projector",
        "q": "DLP Projector RGB Filter Wheel",
        "r": "DPL Projector RGBW Filter Wheel",
        "s": "DLP Projector RGBCMY Filter Wheel",
        "u": "Unknown",
    }

@pytest.mark.skipif(
    os.getenv("GITHUB_ACTIONS") == "true" and sys.platform == "linux",
    reason="Not working properly on GitHub on Linux machines.",
)
def test_get_technology_strings_with_argyll_returns_expected_data(setup_argyll):
    """Test get_technology_strings() returns a dict with correct data."""
    get_argyll_latest_version.cache_clear()
    worker = Worker()
    assert worker.argyll_version != [0, 0, 0]
    result = worker.get_technology_strings()
    expected = {
        "c": "CRT",
        "m": "Plasma",
        "l": "LCD",
        "1": "LCD CCFL",
        "2": "LCD CCFL IPS",
        "3": "LCD CCFL PVA",
        "4": "LCD CCFL TFT",
        "L": "LCD CCFL Wide Gamut",
        "5": "LCD CCFL Wide Gamut IPS",
        "6": "LCD CCFL Wide Gamut PVA",
        "7": "LCD CCFL Wide Gamut TFT",
        "e": "LCD White LED",
        "8": "LCD White LED IPS",
        "9": "LCD White LED PVA",
        "d": "LCD White LED TFT",
        "b": "LCD RGB LED",
        "f": "LCD RGB LED IPS",
        "g": "LCD RGB LED PVA",
        "j": "LCD RGB LED TFT",
        "h": "LCD RG Phosphor",
        "k": "LCD RG Phosphor IPS",
        "n": "LCD RG Phosphor PVA",
        "q": "LCD RG Phosphor TFT",
        "r": "LCD PFS Phosphor",
        "s": "LCD PFS Phosphor IPS",
        "t": "LCD PFS Phosphor PVA",
        "v": "LCD PFS Phosphor TFT",
        "i": "LCD GB-R Phosphor",
        "x": "LCD GB-R Phosphor IPS",
        "y": "LCD GB-R Phosphor PVA",
        "z": "LCD GB-R Phosphor TFT",
        "o": "LED OLED",
        "a": "LED AMOLED",
        "w": "LED WOLED",
        "p": "DLP Projector",
        "A": "DLP Projector RGB Filter Wheel",
        "B": "DLP Projector RGBW Filter Wheel",
        "C": "DLP Projector RGBCMY Filter Wheel",
        "u": "Unknown",
    }
    assert result == expected


def test_get_technology_strings_parses_ccxxmake_output(monkeypatch):
    """Technology parser should extract -t entries from ccxxmake output."""
    worker = Worker()
    worker.argyll_version = [3, 5, 0]

    def patched_exec_cmd(*args, **kwargs):
        worker.output = [
            "-t c CRT",
            "-t q LCD PFS Phosphor TFT",
            "-t o LED OLED",
            "-Y ignored option section",
        ]
        return True

    monkeypatch.setattr(worker, "exec_cmd", patched_exec_cmd)
    result = worker.get_technology_strings()
    assert result == {
        "c": "CRT",
        "q": "LCD PFS Phosphor TFT",
        "o": "LED OLED",
    }


def _build_matrix_from_primaries(RGB_XYZ, remaining, white_scale=1.0):
    """Build a from-chromaticities matrix profile like ``_create_simple_matrix_profile``.

    ``white_scale`` lets the test simulate an inconsistency between the matrix
    white and the gray-ramp white, which is what makes the shaper curve endpoint
    drop below 1.0 (see issue #710).
    """
    from DisplayCAL import colormath

    XYZbp = RGB_XYZ[(0, 0, 0)]
    XYZwp = RGB_XYZ[(100, 100, 100)]
    xy = []
    for R, G, B in [(100, 0, 0), (0, 100, 0), (0, 0, 100), (100, 100, 100)]:
        src = RGB_XYZ if R == G == B else remaining
        X, Y, Z = src[(R, G, B)]
        if XYZbp != (0, 0, 0):
            X, Y, Z = colormath.blend_blackpoint(X, Y, Z, XYZbp, (0, 0, 0), XYZwp)
        xy.append(colormath.XYZ2xyY(*(v / 100 for v in (X, Y, Z)))[:2])
    profile = ICCProfile.from_chromaticities(
        xy[0][0], xy[0][1], xy[1][0], xy[1][1], xy[2][0], xy[2][1],
        xy[3][0], xy[3][1], 2.2, "t", "c", None, None, cat="Bradford",
    )
    if white_scale != 1.0:
        # Make the matrix map device (1, 1, 1) to a brighter-than-media white,
        # mimicking a matrix/gray-ramp white inconsistency.
        for channel in "rgb":
            tag = profile.tags[channel + "XYZ"]
            tag.X /= white_scale
            tag.Y /= white_scale
            tag.Z /= white_scale
    return profile


@pytest.mark.parametrize("white_scale", [1.0, 0.9757])
def test_create_shaper_curves_trc_endpoint_reaches_one(data_files, white_scale):
    """Matrix display shaper curves must reach 1.0 at device max (issue #710).

    A TRC endpoint below 1.0 makes device white map to a fraction of media
    white, which causes a CMM to clip near-white source values to device white
    when inverting the profile at limited precision.
    """
    from DisplayCAL import colormath

    ti3 = CGATS(
        str(data_files["UP2516D #1 2022-03-20 02-08 D6500 2.2 F-S XYZLUT+MTX.ti3"])
    )[0]
    _, RGB_XYZ, remaining = extract_device_gray_primaries(ti3, True, lambda *a: None)
    profile = _build_matrix_from_primaries(RGB_XYZ, remaining, white_scale)
    bwd_mtx = colormath.get_rgb_space(profile.get_rgb_space("pcs", 1))[-1].inverted()

    curves = create_shaper_curves(
        RGB_XYZ,
        bwd_mtx,
        single_curve=True,
        bpc=False,
        profile=profile,
        options_dispcal=[],
        optimize=True,
        cat="Bradford",
    )

    for curve in curves:
        # Endpoint reaches device max ...
        assert curve[-1] == pytest.approx(1.0, abs=1e-9)
        # ... and the curve is still monotonically increasing.
        assert all(curve[i] <= curve[i + 1] + 1e-9 for i in range(len(curve) - 1))


@pytest.mark.parametrize(
    "viewgam_output,expected_coverage",
    [
        # Unix-style paths (forward slash)
        (
            [
                "Intersecting volume = 1198577.5 cubic units",
                "'/usr/share/DisplayCAL/ref/ClayRGB1998.gam' volume = 1209985.9 cubic units, intersect = 99.06%",
                "'/home/user/profile.gam' volume = 1411797.2 cubic units, intersect = 84.90%",
            ],
            0.9906,
        ),
        # Windows-style paths (backslash) -- issue #693
        (
            [
                "Intersecting volume = 1198577.5 cubic units",
                r"'C:\Program Files\DisplayCAL\ref\ClayRGB1998.gam' volume = 1209985.9 cubic units, intersect = 99.06%",
                r"'C:\Users\user\profile.gam' volume = 1411797.2 cubic units, intersect = 84.90%",
            ],
            0.9906,
        ),
    ],
)
def test_create_gamut_view_worker_parses_coverage(viewgam_output, expected_coverage):
    """Coverage percentage is parsed from both Unix and Windows viewgam output paths.

    Regression test for issue #693: on Windows, viewgam outputs backslash paths
    which the old regex only matched forward slashes and missed Windows paths.
    """
    mock_worker = MagicMock()
    mock_worker.exec_cmd.return_value = True
    mock_worker.output = viewgam_output

    gamut_coverage = {}
    Worker.create_gamut_view_worker(
        mock_worker,
        viewgam="viewgam",
        args=[],
        key="adobe-rgb",
        src="ClayRGB1998",
        gamut_coverage=gamut_coverage,
    )

    assert "adobe-rgb" in gamut_coverage
    assert gamut_coverage["adobe-rgb"] == pytest.approx(expected_coverage, rel=1e-4)


@pytest.mark.parametrize(
    "old_value, expected",
    [
        ("2012_2", "2015_2"),
        ("2012_10", "2015_10"),
    ],
    ids=["2012_2->2015_2", "2012_10->2015_10"],
)
def test_enumerate_displays_migrates_cie2012_observer_names_for_argyll_34(
    monkeypatch, request, old_value, expected
):
    """ArgyllCMS 3.4.0 renamed the "2012_*" observers to "2015_*".

    Stored config values must be migrated automatically on first run with the new
    ArgyllCMS so they don't silently revert to the default "1931_2" observer.
    Without migration, getcfg("observer") sees "2012_2" as invalid (not in
    VALID_VALUES) and returns the default "1931_2", which means the -Q flag is never
    passed to dispcal/dispread, producing wrong calibration and measurement results
    (issue #623).
    """
    from DisplayCAL import config as _config

    observer_keys = [
        "observer",
        "colorimeter_correction.observer",
        "colorimeter_correction.observer.reference",
    ]

    # Save existing CFG values so the test doesn't pollute global config state.
    originals = {
        k: _config.CFG.get(configparser.DEFAULTSECT, k, fallback=None)
        for k in observer_keys
    }

    def restore_cfg():
        for key, val in originals.items():
            if val is None:
                _config.CFG.remove_option(configparser.DEFAULTSECT, key)
            else:
                _config.CFG.set(configparser.DEFAULTSECT, key, val)

    request.addfinalizer(restore_cfg)

    # Preserve VALID_VALUES entries so monkeypatch restores them after the test.
    for key in observer_keys:
        monkeypatch.setitem(
            _config.VALID_VALUES, key, list(_config.VALID_VALUES.get(key, []))
        )

    # Write old-style names directly to CFG, bypassing VALID_VALUES validation,
    # to simulate a config saved with ArgyllCMS < 3.4.0.
    for key in observer_keys:
        _config.CFG.set(configparser.DEFAULTSECT, key, old_value)

    worker = Worker()
    monkeypatch.setattr("DisplayCAL.worker.check_argyll_bin", lambda: True)
    monkeypatch.setattr("DisplayCAL.worker.writecfg", lambda *a, **kw: None)

    def fake_exec_cmd(*args, **kwargs):
        worker.output = ["dispcal  - Display calibration  Version 3.4.0"]
        return True

    monkeypatch.setattr(worker, "exec_cmd", fake_exec_cmd)

    worker.enumerate_displays_and_ports(
        silent=True, check_lut_access=False, enumerate_ports=False
    )

    for key in observer_keys:
        result = getcfg(key)
        assert result == expected, (
            f"{key!r} should have been migrated from {old_value!r} "
            f"to {expected!r}, got {result!r}"
        )


# ---------------------------------------------------------------------------
# check_instrument_calibration tests (issue #617 – ColorMunki infinite loop)
# ---------------------------------------------------------------------------

COLORMUNKI_CAL_MSG = "Set instrument sensor to calibration position"


def _make_worker_for_cal_test(monkeypatch):
    """Return a Worker with do_instrument_calibration and safe_send mocked."""
    worker = Worker()
    # These attributes are normally set in the worker-thread setup; init them
    # manually so the calibration method can be called without a running subprocess.
    worker.instrument_calibration_complete = False
    worker._last_calibration_msg = None
    worker.do_instrument_calibration_calls = []
    worker.safe_send_calls = []

    def fake_do_instrument_calibration(failed=False):
        worker.do_instrument_calibration_calls.append(failed)

    def fake_safe_send(data):
        worker.safe_send_calls.append(data)

    monkeypatch.setattr(worker, "do_instrument_calibration", fake_do_instrument_calibration)
    monkeypatch.setattr(worker, "safe_send", fake_safe_send)
    return worker


def test_check_instrument_calibration_first_prompt_shows_dialog(monkeypatch):
    """First occurrence of a calibration prompt must show the dialog."""
    worker = _make_worker_for_cal_test(monkeypatch)
    worker.check_instrument_calibration(COLORMUNKI_CAL_MSG)
    assert len(worker.do_instrument_calibration_calls) == 1
    assert worker.do_instrument_calibration_calls[0] is False
    assert worker.safe_send_calls == []
    assert worker._last_calibration_msg == COLORMUNKI_CAL_MSG


def test_check_instrument_calibration_duplicate_prompt_auto_retries(monkeypatch):
    """Second occurrence of the same prompt must auto-send a space, not show dialog."""
    worker = _make_worker_for_cal_test(monkeypatch)
    worker.check_instrument_calibration(COLORMUNKI_CAL_MSG)
    worker.check_instrument_calibration(COLORMUNKI_CAL_MSG)
    # Dialog shown only once; second call triggers a silent keypress
    assert len(worker.do_instrument_calibration_calls) == 1
    assert worker.safe_send_calls == [" "]


def test_check_instrument_calibration_complete_stops_further_dialogs(monkeypatch):
    """After calibration complete, subsequent prompts must be ignored."""
    worker = _make_worker_for_cal_test(monkeypatch)
    worker.check_instrument_calibration(COLORMUNKI_CAL_MSG)
    worker.check_instrument_calibration("Calibration complete")
    assert worker.instrument_calibration_complete is True
    assert worker._last_calibration_msg is None
    # A further prompt arrives – must be silently ignored
    worker.check_instrument_calibration(COLORMUNKI_CAL_MSG)
    assert len(worker.do_instrument_calibration_calls) == 1
    assert worker.safe_send_calls == []


def test_check_instrument_calibration_failure_allows_retry_dialog(monkeypatch):
    """After a calibration failure, the next calibration prompt must show a dialog.

    The duplicate-suppression state is reset on failure so the user is not stuck
    in a silent auto-retry loop after a failed calibration attempt.
    """
    worker = _make_worker_for_cal_test(monkeypatch)
    # First prompt: dialog shown, duplicate tracking starts
    worker.check_instrument_calibration(COLORMUNKI_CAL_MSG)
    assert len(worker.do_instrument_calibration_calls) == 1
    # Simulate duplicate that would normally be auto-retried silently
    worker.check_instrument_calibration(COLORMUNKI_CAL_MSG)
    assert worker.safe_send_calls == [" "]
    # Calibration fails: reset state so the NEXT prompt opens the dialog again
    worker._last_calibration_msg = None  # mirrors what do_instrument_calibration does on cancel
    worker.check_instrument_calibration(COLORMUNKI_CAL_MSG)
    assert len(worker.do_instrument_calibration_calls) == 2
    assert worker.do_instrument_calibration_calls[1] is False
    # No additional silent retries should have been sent
    assert len(worker.safe_send_calls) == 1
