"""Screen-context parsing/annotation. The subprocess probes are platform
specific; the parsing they feed is not, so that's what's tested here."""

import sys
from pathlib import Path

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

from bolt_pet import screen_context


def test_parses_the_active_window_id():
    output = "_NET_ACTIVE_WINDOW(WINDOW): window id # 0x3c00007\n"
    assert screen_context.parse_xprop_window_id(output) == "0x3c00007"


def test_no_active_window_id_when_nothing_is_focused():
    assert screen_context.parse_xprop_window_id("_NET_ACTIVE_WINDOW(WINDOW): window id # 0x0") is None
    assert screen_context.parse_xprop_window_id("") is None


def test_parses_the_window_title():
    output = '_NET_WM_NAME(UTF8_STRING) = "bolt_pet/controller.py - Cursor"\n'
    assert screen_context.parse_xprop_window_name(output) == "bolt_pet/controller.py - Cursor"


def test_unset_title_property():
    assert screen_context.parse_xprop_window_name("_NET_WM_NAME: not found") is None


def test_fullscreen_state_detection():
    assert screen_context.parse_xprop_fullscreen(
        "_NET_WM_STATE(ATOM) = _NET_WM_STATE_FULLSCREEN, _NET_WM_STATE_FOCUSED") is True
    assert screen_context.parse_xprop_fullscreen("_NET_WM_STATE(ATOM) = _NET_WM_STATE_FOCUSED") is False


def test_desktop_titles_are_treated_as_no_context():
    assert screen_context.clean_title("Desktop") is None
    assert screen_context.clean_title("   ") is None


def test_long_titles_are_truncated():
    cleaned = screen_context.clean_title("x" * 500)
    assert len(cleaned) <= 160 and cleaned.endswith("…")


def test_annotate_appends_context_as_an_aside():
    annotated = screen_context.annotate("what's this error?", "app.py — Traceback")
    assert annotated.startswith("what's this error?")
    assert "[on screen right now: app.py — Traceback]" in annotated


def test_annotate_is_a_no_op_without_a_title_or_text():
    assert screen_context.annotate("hello", None) == "hello"
    assert screen_context.annotate("hello", "Desktop") == "hello"
    assert screen_context.annotate("", "Firefox") == ""
