import json
import tempfile
from target_simulator.gui import sfp_debug_window


def test_display_json_and_bin_views():
    w = sfp_debug_window.SfpDebugWindow(master=None)

    # JSON payload should be pretty-printed into json_tab
    payload = b'{"a":1, "b": [1,2,3]}'
    w._display_json_data(payload, w.json_tab)
    out = w.json_tab.get("1.0", "end")
    assert '"a": 1' in out

    # BIN payload hex dump into bin_tab
    bin_payload = b"\x00\x01\x02\xFF"
    w._display_hex_data(bin_payload, w.bin_tab)
    out2 = w.bin_tab.get("1.0", "end")
    assert "00000000" in out2 and "FF" in out2


def test_image_parse_error_logs_and_sets_none(monkeypatch):
    w = sfp_debug_window.SfpDebugWindow(master=None)

    # craft a payload smaller than ImageLeaderData to trigger parse error
    small_payload = b"\x00\x01"
    # Call image display path; choose tab dict similar to _create_image_tab
    tab = {"image_label": w.mfd_tab["image_label"], "hex_view": w.mfd_tab["hex_view"]}
    # Should not raise and should set mfd_photo to None on failure
    w._display_image_data(small_payload, tab, "mfd_photo")
    assert getattr(w, "mfd_photo", None) is None
