aboutsummaryrefslogtreecommitdiff
path: root/tests/managers/thl/test_user_streak.py
blob: 7728f9f7d1f632104a2e5b416f2c00f96eec6ea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import copy
from datetime import datetime, timezone, timedelta, date
from decimal import Decimal
from zoneinfo import ZoneInfo

import pytest

from generalresearch.managers.thl.user_streak import compute_streaks_from_days
from generalresearch.models.thl.definitions import StatusCode1, Status
from generalresearch.models.thl.user_streak import (
    UserStreak,
    StreakState,
    StreakPeriod,
    StreakFulfillment,
)


def test_compute_streaks_from_days():
    days = [
        date(2026, 1, 1),
        date(2026, 1, 4),
        date(2026, 1, 5),
        date(2026, 1, 6),
        date(2026, 1, 8),
        date(2026, 1, 9),
        date(2026, 2, 11),
        date(2026, 2, 12),
    ]

    # Active
    today = date(2026, 2, 12)
    res = compute_streaks_from_days(days, "us", period=StreakPeriod.DAY, today=today)
    assert res == (2, 3, StreakState.ACTIVE, date(2026, 2, 12))

    # At Risk
    today = date(2026, 2, 13)
    res = compute_streaks_from_days(days, "us", period=StreakPeriod.DAY, today=today)
    assert res == (2, 3, StreakState.AT_RISK, date(2026, 2, 12))

    # Broken
    today = date(2026, 2, 14)
    res = compute_streaks_from_days(days, "us", period=StreakPeriod.DAY, today=today)
    assert res == (0, 3, StreakState.BROKEN, date(2026, 2, 12))

    # Monthly, active
    today = date(2026, 2, 14)
    res = compute_streaks_from_days(days, "us", period=StreakPeriod.MONTH, today=today)
    assert res == (2, 2, StreakState.ACTIVE, date(2026, 2, 1))

    # monthly, at risk
    today = date(2026, 3, 1)
    res = compute_streaks_from_days(days, "us", period=StreakPeriod.MONTH, today=today)
    assert res == (2, 2, StreakState.AT_RISK, date(2026, 2, 1))

    # monthly, broken
    today = date(2026, 4, 1)
    res = compute_streaks_from_days(days, "us", period=StreakPeriod.MONTH, today=today)
    assert res == (0, 2, StreakState.BROKEN, date(2026, 2, 1))


@pytest.fixture
def broken_active_streak(user):
    return [
        UserStreak(
            period=StreakPeriod.DAY,
            fulfillment=StreakFulfillment.ACTIVE,
            country_iso="us",
            user_id=user.user_id,
            last_fulfilled_period_start=date(2025, 2, 11),
            current_streak=0,
            longest_streak=1,
            state=StreakState.BROKEN,
        ),
        UserStreak(
            period=StreakPeriod.WEEK,
            fulfillment=StreakFulfillment.ACTIVE,
            country_iso="us",
            user_id=user.user_id,
            last_fulfilled_period_start=date(2025, 2, 10),
            current_streak=0,
            longest_streak=1,
            state=StreakState.BROKEN,
        ),
        UserStreak(
            period=StreakPeriod.MONTH,
            fulfillment=StreakFulfillment.ACTIVE,
            country_iso="us",
            user_id=user.user_id,
            last_fulfilled_period_start=date(2025, 2, 1),
            current_streak=0,
            longest_streak=1,
            state=StreakState.BROKEN,
        ),
    ]


def create_session_fail(session_manager, start, user):
    session = session_manager.create_dummy(started=start, country_iso="us", user=user)
    session_manager.finish_with_status(
        session,
        finished=start + timedelta(minutes=1),
        status=Status.FAIL,
        status_code_1=StatusCode1.BUYER_FAIL,
    )


def create_session_complete(session_manager, start, user):
    session = session_manager.create_dummy(started=start, country_iso="us", user=user)
    session_manager.finish_with_status(
        session,
        finished=start + timedelta(minutes=1),
        status=Status.COMPLETE,
        status_code_1=StatusCode1.COMPLETE,
        payout=Decimal(1),
    )


def test_user_streak_empty(user_streak_manager, user):
    streaks = user_streak_manager.get_user_streaks(
        user_id=user.user_id, country_iso="us"
    )
    assert streaks == []


def test_user_streaks_active_broken(
    user_streak_manager, user, session_manager, broken_active_streak
):
    # Testing active streak, but broken (not today or yesterday)
    start1 = datetime(2025, 2, 12, tzinfo=timezone.utc)
    end1 = start1 + timedelta(minutes=1)

    # abandon counts as inactive
    session = session_manager.create_dummy(started=start1, country_iso="us", user=user)
    streak = user_streak_manager.get_user_streaks(user_id=user.user_id)
    assert streak == []

    # session start fail counts as inactive
    session_manager.finish_with_status(
        session,
        finished=end1,
        status=Status.FAIL,
        status_code_1=StatusCode1.SESSION_START_QUALITY_FAIL,
    )
    streak = user_streak_manager.get_user_streaks(
        user_id=user.user_id, country_iso="us"
    )
    assert streak == []

    # Create another as a real failure
    create_session_fail(session_manager, start1, user)

    # This is going to be the day before b/c midnight utc is 8pm US
    last_active_day = start1.astimezone(ZoneInfo("America/New_York")).date()
    assert last_active_day == date(2025, 2, 11)
    expected_streaks = broken_active_streak.copy()
    streaks = user_streak_manager.get_user_streaks(user_id=user.user_id)
    assert streaks == expected_streaks

    # Create another the next day
    start2 = start1 + timedelta(days=1)
    create_session_fail(session_manager, start2, user)

    last_active_day = start2.astimezone(ZoneInfo("America/New_York")).date()
    expected_streaks = copy.deepcopy(broken_active_streak)
    expected_streaks[0].longest_streak = 2
    expected_streaks[0].last_fulfilled_period_start = last_active_day

    streaks = user_streak_manager.get_user_streaks(
        user_id=user.user_id, country_iso="us"
    )
    assert streaks == expected_streaks


def test_user_streak_complete_active(user_streak_manager, user, session_manager):
    """Testing active streak that is today"""

    # They completed yesterday NY time. Today isn't over so streak is pending
    start1 = datetime.now(tz=ZoneInfo("America/New_York")) - timedelta(days=1)
    create_session_complete(session_manager, start1.astimezone(tz=timezone.utc), user)

    last_complete_day = start1.date()
    expected_streak = UserStreak(
        longest_streak=1,
        current_streak=1,
        state=StreakState.AT_RISK,
        last_fulfilled_period_start=last_complete_day,
        country_iso="us",
        user_id=user.user_id,
        fulfillment=StreakFulfillment.COMPLETE,
        period=StreakPeriod.DAY,
    )
    streaks = user_streak_manager.get_user_streaks(
        user_id=user.user_id, country_iso="us"
    )
    streak = [
        s
        for s in streaks
        if s.fulfillment == StreakFulfillment.COMPLETE and s.period == StreakPeriod.DAY
    ][0]
    assert streak == expected_streak

    # And now they complete today
    start2 = datetime.now(tz=ZoneInfo("America/New_York"))
    create_session_complete(session_manager, start2.astimezone(tz=timezone.utc), user)
    last_complete_day = start2.date()
    expected_streak = UserStreak(
        longest_streak=2,
        current_streak=2,
        state=StreakState.ACTIVE,
        last_fulfilled_period_start=last_complete_day,
        country_iso="us",
        user_id=user.user_id,
        fulfillment=StreakFulfillment.COMPLETE,
        period=StreakPeriod.DAY,
    )

    streaks = user_streak_manager.get_user_streaks(
        user_id=user.user_id, country_iso="us"
    )
    streak = [
        s
        for s in streaks
        if s.fulfillment == StreakFulfillment.COMPLETE and s.period == StreakPeriod.DAY
    ][0]
    assert streak == expected_streak