"""Regression test for unload, cleanup, and lazy chat model lifecycle."""

from __future__ import annotations

import subprocess
import sys

import pytest


@pytest.mark.operator_ui
def test_active_checkpoint_survives_cleanup_and_remains_chat_recoverable(tmp_path):
    """Exercise app state in isolation from Gradio's import-time worker sockets."""
    script = r'''
import pathlib
import sys

import app

theme = app.THEME.to_dict()["theme"]
assert theme["body_background_fill"] != theme["body_background_fill_dark"]
assert theme["body_text_color"] != theme["body_text_color_dark"]
assert theme["background_fill_secondary"] == "#ffffff"
assert theme["background_fill_secondary_dark"] == "#0d0d14"
assert ".chatbot .message.bot" in app.CSS
assert "color: var(--body-text-color) !important" in app.CSS

root = pathlib.Path(sys.argv[1])
active = root / "obliterated_1"
stale = root / "obliterated_2"
cache = root / "model-cache"
for directory in (active, stale, cache):
    directory.mkdir()
    (directory / "weights.bin").write_bytes(b"model")

app.dev.free_gpu_memory = lambda: None
app._state.update({
    "model": object(), "tokenizer": object(), "model_name": "org/model",
    "method": "advanced", "status": "ready", "output_dir": str(active),
})
app._session_models.clear()
app._session_models.update({
    "active": {"output_dir": str(active)},
    "stale": {"output_dir": str(stale)},
})

message = app._cleanup_disk(cache_roots=[cache], temp_root=root)
assert active.is_dir()
assert not stale.exists()
assert not cache.exists()
assert list(app._session_models) == ["active"]
assert app._state["model"] is None and app._state["tokenizer"] is None
assert app._state["status"] == "ready"
assert "will reload it automatically" in message
header = app.get_chat_header()
assert "unloaded from GPU" in header and "load automatically" in header

active.rename(root / "removed")
header = app.get_chat_header()
assert header.startswith("No model loaded")
assert app._state["status"] == "idle"
assert app._state["model_name"] is None
assert app._state["output_dir"] is None
'''

    result = subprocess.run(
        [sys.executable, "-c", script, str(tmp_path)],
        capture_output=True,
        text=True,
        timeout=60,
        check=False,
    )

    assert result.returncode == 0, result.stdout + result.stderr
