from __future__ import annotations

import json
from pathlib import Path

from rich.console import Console

from apps.paper_trader import cli


def test_resolve_rank_configs_selects_runnable_single_book_rows(tmp_path: Path, monkeypatch) -> None:
    monkeypatch.chdir(tmp_path)
    journal_dir = tmp_path / "journal"
    journal_dir.mkdir(parents=True)
    config_dir = tmp_path / "configs" / "experiments"
    config_dir.mkdir(parents=True)
    (config_dir / "return_max_long_demo.json").write_text("{}")

    registry = {
        "entries": [
            {
                "experiment_name": "return_max_long_missing",
                "strategy_family": "return_max_long",
                "sqs_score": 81.6,
                "trade_count": 10,
                "valid_trade_count": 3,
                "is_retired": False,
            },
            {
                "experiment_name": "return_max_long_demo",
                "strategy_family": "return_max_long",
                "sqs_score": 70.0,
                "trade_count": 10,
                "valid_trade_count": 3,
                "is_retired": False,
            },
        ],
        "updated_at": "2026-03-26T00:00:00+00:00",
    }
    (journal_dir / "experiment_registry.json").write_text(json.dumps(registry))

    console = Console(record=True, width=140)
    monkeypatch.setattr(cli, "_console", console)

    configs = cli._resolve_rank_configs(1, 1)

    assert configs == ["configs/experiments/return_max_long_demo.json"]
    assert console.export_text() == ""
