"""Tests for deploy_interactives.inject_self_resize."""
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
SCRIPTS = os.path.join(HERE, "..", "scripts")
sys.path.insert(0, SCRIPTS)

import deploy_interactives as di  # noqa: E402


def test_injects_sentinel_before_body():
    src = "<html><body><p>Hello</p></body></html>"
    result = di.inject_self_resize(src)
    assert "asucb-iframe-autoresize" in result
    assert "window.frameElement.style.height" in result
    # sentinel appears before </body>
    assert result.index("asucb-iframe-autoresize") < result.lower().index("</body>")


def test_idempotent():
    src = "<html><body><p>Hello</p></body></html>"
    once = di.inject_self_resize(src)
    twice = di.inject_self_resize(once)
    assert once == twice


def test_leaves_existing_autoresize_unchanged():
    src = "<html><body><script>window.frameElement.style.height='500px'</script></body></html>"
    result = di.inject_self_resize(src)
    assert result == src


def test_appends_when_no_body_tag():
    src = "<html><p>No body tag here</p></html>"
    result = di.inject_self_resize(src)
    assert "asucb-iframe-autoresize" in result
    assert result.startswith(src)
