from dataclasses import dataclass

import pytest
from fasthtml.fastapp import fast_app

from myfasthtml.test.testclient import MyTestClient, TestableRange


@dataclass
class Data:
  value: str = "hello world"


@pytest.fixture
def test_app():
  test_app, rt = fast_app(default_hdrs=False)
  return test_app


@pytest.fixture
def rt(test_app):
  return test_app.route


@pytest.fixture
def test_client(test_app):
  return MyTestClient(test_app)


def test_i_can_read_range(test_client):
  html = '''<input type="range" name="range_name" min="0" max="100" step="10" value="50" />'''
  
  input_elt = TestableRange(test_client, html)
  
  assert input_elt.name == "range_name"
  assert input_elt.value == 50
  assert input_elt.min_value == 0
  assert input_elt.max_value == 100
  assert input_elt.step == 10


@pytest.mark.parametrize("value, expected", [
    (30, 30),
    (24, 20),  # step 10
    (-10, 0),  # min 0
    (110, 100),  # max 100
])
def test_i_can_set_value(test_client, value, expected):
  html = '''<input type="range" name="range_name" min="0" max="100" step="10" value="50" />'''
  
  input_elt = TestableRange(test_client, html)
  
  input_elt.set(value)
  assert input_elt.value == expected


def test_i_can_increase_value(test_client):
  html = '''<input type="range" name="range_name" min="0" max="100" step="10" value="50" />'''
  
  input_elt = TestableRange(test_client, html)
  
  input_elt.increase()
  assert input_elt.value == 60


def test_i_can_decrease_value(test_client):
  html = '''<input type="range" name="range_name" min="0" max="100" step="10" value="50" />'''
  
  input_elt = TestableRange(test_client, html)
  
  input_elt.decrease()
  assert input_elt.value == 40
