from target_simulator.gui.sfp_debug_window import DebugPayloadRouter, SfpDebugWindow


def test_history_and_select_display():
    dr = DebugPayloadRouter()
    # create entries
    for i in range(3):
        dr.update_raw_packet(b'header' + bytes([i]), ('127.0.0.1', 1000 + i))

    w = SfpDebugWindow(master=None)
    # simulate that router has history and set it
    w.payload_router = dr
    # call refresh that populates the tree; should not raise
    w._refresh_history_tree()
    # select the first item and call selection handler
    children = w.history_tree.get_children()
    if children:
        iid = children[0]
        # set selection on tree
        w.history_tree._items[iid] = w.history_tree._items.get(iid)
        # call history select method
        w._on_history_select()
