| 1 | |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import unittest |
| 6 | |
| 7 | from src.compute import clock, study |
| 8 | |
| 9 | from .fakes import pomodoro |
| 10 | |
| 11 | DAY = 86_400_000 |
| 12 | HOUR = 3_600_000 |
| 13 | |
| 14 | NOW = 1_699_963_200_000 |
| 15 | |
| 16 | |
| 17 | def _at(days_ago: int, tr_hour: int) -> int: |
| 18 | |
| 19 | day = clock.tr_day(NOW) - days_ago |
| 20 | return day * DAY + tr_hour * HOUR - clock.TR_OFFSET_MS |
| 21 | |
| 22 | |
| 23 | class TestLocalTimeCorrection(unittest.TestCase): |
| 24 | def test_tr_midnight_session_lands_on_correct_local_day(self) -> None: |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | started = _at(1, 0) + 30 * 60_000 |
| 30 | self.assertEqual(clock.tr_hour(started), 0) |
| 31 | self.assertEqual(clock.tr_day(started), clock.tr_day(NOW) - 1) |
| 32 | grid = study.heatmap([pomodoro(started)]) |
| 33 | self.assertEqual(grid[clock.tr_weekday(started)][0], 1) |
| 34 | |
| 35 | def test_night_share_uses_tr_hours(self) -> None: |
| 36 | rows = [pomodoro(_at(1, 1)), pomodoro(_at(2, 14)), pomodoro(_at(3, 15)), |
| 37 | pomodoro(_at(4, 16)), pomodoro(_at(5, 17))] |
| 38 | result = study.student_profile(rows, NOW) |
| 39 | self.assertAlmostEqual(result["recent_28d"]["night_share"], 0.2) |
| 40 | |
| 41 | |
| 42 | class TestCountableRows(unittest.TestCase): |
| 43 | def test_uncounted_and_open_sessions_are_dropped(self) -> None: |
| 44 | |
| 45 | rows = [ |
| 46 | pomodoro(_at(1, 10)), |
| 47 | pomodoro(_at(2, 10), counted=False), |
| 48 | {"id": "open_x", "started_at": _at(3, 10), "finished_at": None, |
| 49 | "duration_ms": None, "counted": None}, |
| 50 | ] |
| 51 | self.assertEqual(len(study.countable(rows)), 1) |
| 52 | |
| 53 | def test_counters_from_profile_are_never_read(self) -> None: |
| 54 | |
| 55 | result = study.student_profile([], NOW) |
| 56 | self.assertEqual(result["recent_28d"]["n_stints"], 0) |
| 57 | |
| 58 | |
| 59 | class TestGates(unittest.TestCase): |
| 60 | def test_heatmap_gate_at_five_stints(self) -> None: |
| 61 | |
| 62 | rows = [pomodoro(_at(i + 1, 10)) for i in range(4)] |
| 63 | result = study.student_profile(rows, NOW) |
| 64 | self.assertIsNone(result["heatmap"]) |
| 65 | self.assertEqual(result["heatmap_progress"], "4/5") |
| 66 | |
| 67 | rows.append(pomodoro(_at(5, 10))) |
| 68 | result = study.student_profile(rows, NOW) |
| 69 | self.assertIsNotNone(result["heatmap"]) |
| 70 | self.assertIsNone(result["heatmap_progress"]) |
| 71 | |
| 72 | def test_regularity_gate_at_three_active_days(self) -> None: |
| 73 | rows = [pomodoro(_at(1, 10)), pomodoro(_at(1, 12))] |
| 74 | result = study.student_profile(rows, NOW) |
| 75 | self.assertTrue(result["recent_28d"]["regularity_suppressed"]) |
| 76 | self.assertIsNone(result["recent_28d"]["regularity"]) |
| 77 | |
| 78 | def test_regularity_value(self) -> None: |
| 79 | rows = [pomodoro(_at(i + 1, 10)) for i in range(7)] |
| 80 | result = study.student_profile(rows, NOW) |
| 81 | self.assertAlmostEqual(result["recent_28d"]["regularity"], 7 / 28) |
| 82 | |
| 83 | |
| 84 | class TestBurstiness(unittest.TestCase): |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | def test_burstiness_is_computed(self) -> None: |
| 94 | rows = [pomodoro(_at(1, 9 + i)) for i in range(6)] |
| 95 | rows += [pomodoro(_at(2, 10)), pomodoro(_at(3, 10))] |
| 96 | result = study.student_profile(rows, NOW) |
| 97 | |
| 98 | self.assertAlmostEqual(result["recent_28d"]["burstiness"], 6 / (8 / 3)) |
| 99 | |
| 100 | def test_extreme_burst_still_produces_no_flag(self) -> None: |
| 101 | rows = [pomodoro(_at(1, 8 + i)) for i in range(10)] |
| 102 | rows += [pomodoro(_at(2, 10)), pomodoro(_at(3, 10)), pomodoro(_at(4, 10))] |
| 103 | result = study.student_profile(rows, NOW) |
| 104 | |
| 105 | self.assertGreaterEqual(result["recent_28d"]["burstiness"], 3.0) |
| 106 | self.assertNotIn("bursty", result["recent_28d"]) |
| 107 | self.assertIn("ÖLÇMEZ", result["recent_28d"]["burstiness_limitation"]) |
| 108 | |
| 109 | def test_night_window_matches_scenario(self) -> None: |
| 110 | |
| 111 | self.assertEqual(study.NIGHT_HOURS, (22, 23, 0, 1)) |
| 112 | rows = [pomodoro(_at(1, 22)), pomodoro(_at(2, 1)), pomodoro(_at(3, 3))] |
| 113 | result = study.student_profile(rows, NOW) |
| 114 | self.assertAlmostEqual(result["recent_28d"]["night_share"], 2 / 3) |
| 115 | self.assertEqual(result["recent_28d"]["night_window_tr"], "[22:00, 02:00)") |
| 116 | |
| 117 | |
| 118 | class TestStreakAndComparison(unittest.TestCase): |
| 119 | def test_local_streak_counts_consecutive_tr_days(self) -> None: |
| 120 | rows = [pomodoro(_at(0, 10)), pomodoro(_at(1, 10)), pomodoro(_at(2, 10)), |
| 121 | pomodoro(_at(4, 10))] |
| 122 | self.assertEqual(study.local_streak(rows, NOW), 3) |
| 123 | |
| 124 | def test_streak_zero_when_gap(self) -> None: |
| 125 | rows = [pomodoro(_at(5, 10))] |
| 126 | self.assertEqual(study.local_streak(rows, NOW), 0) |
| 127 | |
| 128 | def test_self_comparison_uses_previous_window(self) -> None: |
| 129 | |
| 130 | rows = [pomodoro(_at(i + 1, 10)) for i in range(5)] |
| 131 | rows += [pomodoro(_at(30 + i, 10)) for i in range(10)] |
| 132 | result = study.student_profile(rows, NOW) |
| 133 | self.assertEqual(result["recent_28d"]["n_stints"], 5) |
| 134 | self.assertEqual(result["previous_28d"]["n_stints"], 10) |
| 135 | self.assertEqual(result["change"]["stints_delta"], -5) |
| 136 | |
| 137 | |
| 138 | class TestDeclaredLimits(unittest.TestCase): |
| 139 | def test_no_course_key_anywhere(self) -> None: |
| 140 | |
| 141 | result = study.student_profile([pomodoro(_at(1, 10))], NOW) |
| 142 | self.assertNotIn("course", result) |
| 143 | self.assertNotIn("course", result["recent_28d"]) |
| 144 | |
| 145 | def test_pre_exam_share_is_declared_unavailable(self) -> None: |
| 146 | result = study.student_profile([], NOW) |
| 147 | self.assertIsNone(result["pre_exam_share"]) |
| 148 | self.assertIn("Sınav takvimi", result["pre_exam_reason"]) |
| 149 | |
| 150 | def test_pre_deadline_share_is_computed_separately(self) -> None: |
| 151 | due = _at(1, 20) |
| 152 | rows = [pomodoro(_at(1, 10)), pomodoro(_at(10, 10))] |
| 153 | result = study.student_profile(rows, NOW, [due]) |
| 154 | self.assertAlmostEqual(result["pre_deadline_share"], 0.5) |
| 155 | |
| 156 | |
| 157 | if __name__ == "__main__": |
| 158 | unittest.main() |