from __future__ import annotations

import time

from app.models.db import Database, set_db
from app.strategy.engine import StrategyEngine


def test_enter_rest_after_close_sets_resting(tmp_path) -> None:
    db = Database(tmp_path / "rest.db")
    set_db(db)
    db.set_setting("rest_seconds", "120")
    now = int(time.time() * 1000)
    db.execute(
        """INSERT INTO groups(
            group_id, status, open_at_ms, close_at_ms, realized_pnl
        ) VALUES (?,?,?,?,?)""",
        ("G-20260729-01", "closed", now - 1000, now, 1.0),
    )
    eng = StrategyEngine()
    before = int(time.time() * 1000)
    eng.enter_rest_after_close()
    row = db.fetchone("SELECT * FROM strategy_state WHERE id=1")
    assert row is not None
    assert row["phase"] == "resting"
    assert int(row["rounds_done"] or 0) == 1
    until = int(row["rest_until_ms"] or 0)
    assert until >= before + 100_000
    db.close()
