diff --git a/shellhist/tests/test_time.py b/shellhist/tests/test_time.py index 7093ada..4101da4 100644 --- a/shellhist/tests/test_time.py +++ b/shellhist/tests/test_time.py @@ -1 +1,66 @@ -bmFtZTogQ0kKCm9uOgogIHB1c2g6CiAgICBicmFuY2hlczogW21haW5dCiAgcHVsbF9yZXF1ZXN0OgogICAgYnJhbmNoZXM6IFttYWluXQoKam9iczogCiAgdGVzdDoKICAgIHJ1bnMtb246IHVidW50dS1sYXRlc3QKICAgIHN0ZXBzOgogICAgICAtIHVzZXM6IGFjdGlvbnMvY2hlY2tvdXQdjNFgKICAgICAgdXNlczogYWN0aW9ucy9zZXR1cC1weXRob25fdjUKICAgICAgd2l0aDoKICAgICAgICBweXRob24tdmVyc2lvbjogJzMuMTEnCiAgICAtIHJ1bjogcGlwIGluc3RhbGwgLWUgIltcImRldlwiXSIKICAgIC0gcnVuOiBweXRlc3QgdGVzdHMvIC12CiAgICAtIHJ1bjogcnVmZiBjaGVjayAu \ No newline at end of file +"""Tests for time analysis functionality.""" + +from datetime import datetime, timedelta +from io import StringIO + + +from shellhist.core import HistoryEntry, HistoryStore + + +class TestTimeUtils: + """Test time utility functions.""" + + def test_parse_time_range_hours(self): + """Test parsing hour-based time range.""" + from shellhist.cli.time_analysis import _parse_time_range + + result = _parse_time_range("24h") + expected = datetime.now() - timedelta(hours=24) + + assert (result - expected).total_seconds() < 5 + + def test_parse_time_range_days(self): + """Test parsing day-based time range.""" + from shellhist.cli.time_analysis import _parse_time_range + + result = _parse_time_range("7d") + expected = datetime.now() - timedelta(days=7) + + assert (result - expected).total_seconds() < 5 + + def test_parse_time_range_weeks(self): + """Test parsing week-based time range.""" + from shellhist.cli.time_analysis import _parse_time_range + + result = _parse_time_range("2w") + expected = datetime.now() - timedelta(weeks=2) + + assert (result - expected).total_seconds() < 5 + + +class TestHourlyDistribution: + """Test hourly distribution analysis.""" + + def test_hourly_analysis(self): + """Test basic hourly distribution.""" + from shellhist.cli.time_analysis import _analyze_hourly_distribution + from rich.console import Console + + store = HistoryStore() + now = datetime.now() + + for i in range(24): + entry = HistoryEntry( + command="test command", + timestamp=now.replace(hour=i), + ) + store.add_entry(entry) + + console = Console() + output = StringIO() + console.file = output + + _analyze_hourly_distribution(console, list(store.entries)) + + result = output.getvalue() + assert "Hourly Command Distribution" in result