import pandas as pd
import pytest

from tools.run_mlp_state_policy_experiment import window_metrics
from tools.validate_chart_data import validate_file


def test_validator_reads_unix_seconds_and_detects_truncation(tmp_path):
    path = tmp_path / 'COINBASE_BTCUSD, 360.csv'
    pd.DataFrame({'time': [1704067200, 1704088800], 'open': [100, 100],
                  'high': [101, 101], 'low': [99, 99], 'close': [100, 100]}).to_csv(path, index=False)
    errors, _, stats = validate_file(str(path))
    assert stats['first'] == pd.Timestamp('2024-01-01')
    assert any('TRUNCATED' in error for error in errors)


def test_window_includes_last_day_and_carry_in_returns_without_new_entries():
    frame = pd.DataFrame({'time': pd.to_datetime(['2025-12-31 18:00', '2026-01-01 00:00', '2026-01-01 18:00']),
                          'position': [1, 1, 1], 'execute_entry': [False]*3,
                          'execute_exit': [False]*3, 'close': [100, 90, 80]})
    result = window_metrics(frame, '2026-01-01', '2026-01-01')
    assert result['entries'] == 0
    assert result['pnl_pct'] == pytest.approx(-20)
    assert result['dd_pct'] == pytest.approx(-20)
