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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
"""Unit tests for UrlCache, UrlCacheData, and DualCache."""

import json
from unittest.mock import AsyncMock, patch

from bson import ObjectId

from app.infrastructure.cache.dual_cache import DualCache
from app.infrastructure.cache.url_cache import UrlCache, UrlCacheData

from .conftest import _fake_redis, _url_data

DOMAIN = "spoo.me"


class TestUrlCache:
    async def test_get_returns_none_when_redis_none(self):
        cache = UrlCache(redis_client=None)
        assert await cache.get("abc", DOMAIN) is None

    async def test_get_returns_data_on_hit(self):
        data = _url_data()
        r = _fake_redis(get_returns=json.dumps(data.__dict__))
        cache = UrlCache(r)
        result = await cache.get("abc1234", DOMAIN)
        assert result is not None
        assert result.long_url == "https://example.com"
        assert result.url_status == "ACTIVE"

    async def test_get_returns_none_on_miss(self):
        r = _fake_redis(get_returns=None)
        cache = UrlCache(r)
        assert await cache.get("missing", DOMAIN) is None

    async def test_get_decodes_legacy_payload_without_domain(self):
        # Pre-PR1 cached entries have no `domain` field. The model's empty
        # default keeps them decodable so a deploy doesn't 5xx until the
        # entire cache TTLs out.
        legacy_payload = {
            "_id": "507f1f77bcf86cd799439011",
            "alias": "abc1234",
            "long_url": "https://example.com",
            "block_bots": False,
            "password_hash": None,
            "expiration_time": None,
            "max_clicks": None,
            "url_status": "ACTIVE",
            "schema_version": "v2",
            "owner_id": "507f1f77bcf86cd799439012",
        }
        r = _fake_redis(get_returns=json.dumps(legacy_payload))
        cache = UrlCache(r)
        result = await cache.get("abc1234", DOMAIN)
        assert result is not None
        assert result.domain == ""

    async def test_set_calls_setex_with_ttl(self):
        r = _fake_redis()
        cache = UrlCache(r, ttl_seconds=300)
        await cache.set("abc1234", _url_data(domain=DOMAIN))
        r.setex.assert_called_once()
        call_args = r.setex.call_args[0]
        assert call_args[0] == f"url_cache:{DOMAIN}:abc1234"
        assert call_args[1] == 300

    async def test_set_noop_when_redis_none(self):
        cache = UrlCache(redis_client=None)
        await cache.set("abc", _url_data(domain=DOMAIN))  # must not raise

    async def test_invalidate_deletes_key(self):
        r = _fake_redis()
        cache = UrlCache(r)
        await cache.invalidate("abc1234", DOMAIN)
        r.delete.assert_called_once_with(f"url_cache:{DOMAIN}:abc1234")

    async def test_invalidate_noop_when_redis_none(self):
        cache = UrlCache(redis_client=None)
        await cache.invalidate("abc", DOMAIN)  # must not raise

    async def test_invalidate_many_deletes_all_keys(self):
        r = _fake_redis()
        cache = UrlCache(r)
        await cache.invalidate_many(["a", "b", "c"], DOMAIN)
        r.delete.assert_called_once_with(
            f"url_cache:{DOMAIN}:a",
            f"url_cache:{DOMAIN}:b",
            f"url_cache:{DOMAIN}:c",
        )

    async def test_invalidate_many_noop_on_empty_list(self):
        r = _fake_redis()
        cache = UrlCache(r)
        await cache.invalidate_many([], DOMAIN)
        r.delete.assert_not_called()

    async def test_invalidate_many_noop_when_redis_none(self):
        cache = UrlCache(redis_client=None)
        await cache.invalidate_many(["a", "b"], DOMAIN)  # must not raise

    async def test_set_stores_json_serialisable_data(self):
        r = _fake_redis()
        cache = UrlCache(r)
        await cache.set("x", _url_data(domain=DOMAIN, password_hash="$argon2id$..."))
        _, _, payload = r.setex.call_args[0]
        parsed = json.loads(payload)
        assert parsed["password_hash"] == "$argon2id$..."

    async def test_keys_scoped_per_domain(self):
        # Same alias, different domains → different cache slots.
        r = _fake_redis(get_returns=None)
        cache = UrlCache(r)
        await cache.get("sale", "spoo.me")
        await cache.get("sale", "links.acme.com")
        keys_used = [c.args[0] for c in r.get.call_args_list]
        assert keys_used == [
            "url_cache:spoo.me:sale",
            "url_cache:links.acme.com:sale",
        ]


class TestUrlCacheDataMetaFields:
    def test_decodes_payload_without_meta_fields(self):
        # Exact shape of a pre-meta-tags Redis entry: new fields must
        # default to None so a deploy doesn't 5xx until the cache TTLs out.
        from app.infrastructure.cache.url_cache import UrlCacheData

        legacy = (
            '{"_id":"507f1f77bcf86cd799439011","alias":"a","long_url":"https://x",'
            '"block_bots":false,"password_hash":null,"expiration_time":null,'
            '"max_clicks":null,"url_status":"ACTIVE","schema_version":"v2",'
            '"owner_id":null,"total_clicks":0,"domain":"spoo.me"}'
        )
        data = UrlCacheData.model_validate_json(legacy)
        assert data.meta_title is None
        assert data.meta_description is None
        assert data.meta_image is None
        assert data.meta_color is None
        assert data.meta_image_width is None
        assert data.meta_image_height is None
        assert data.expired_redirect_url is None

    def test_meta_fields_roundtrip_json(self):
        from app.infrastructure.cache.url_cache import UrlCacheData

        data = _url_data(
            meta_title="T", meta_image="https://x/i.png", meta_color="#112233"
        )
        restored = UrlCacheData.model_validate_json(data.model_dump_json(by_alias=True))
        assert restored.meta_title == "T"
        assert restored.meta_image == "https://x/i.png"
        assert restored.meta_color == "#112233"


class TestUrlCacheDataVerifyPassword:
    """Unit tests for UrlCacheData.verify_password()."""

    def test_no_password_returns_true_for_none(self):
        data = _url_data(password_hash=None)
        assert data.verify_password(None) is True

    def test_no_password_returns_true_for_any_input(self):
        data = _url_data(password_hash=None)
        assert data.verify_password("anything") is True

    def test_v2_correct_password(self):
        data = _url_data(password_hash="$argon2id$hash", schema_version="v2")
        with patch(
            "app.infrastructure.cache.url_cache.verify_password_hash", return_value=True
        ):
            assert data.verify_password("correct") is True

    def test_v2_wrong_password(self):
        data = _url_data(password_hash="$argon2id$hash", schema_version="v2")
        with patch(
            "app.infrastructure.cache.url_cache.verify_password_hash",
            return_value=False,
        ):
            assert data.verify_password("wrong") is False

    def test_v2_none_password_short_circuits_without_hashing(self):
        data = _url_data(password_hash="$argon2id$hash", schema_version="v2")
        with patch("app.infrastructure.cache.url_cache.verify_password_hash") as mock:
            result = data.verify_password(None)
            assert result is False
            mock.assert_not_called()

    def test_v1_plaintext_correct(self):
        data = _url_data(password_hash="secret123", schema_version="v1")
        assert data.verify_password("secret123") is True

    def test_v1_plaintext_wrong(self):
        data = _url_data(password_hash="secret123", schema_version="v1")
        assert data.verify_password("wrong") is False

    def test_emoji_plaintext_correct(self):
        data = _url_data(password_hash="mypass", schema_version="emoji")
        assert data.verify_password("mypass") is True

    def test_emoji_plaintext_wrong(self):
        data = _url_data(password_hash="mypass", schema_version="emoji")
        assert data.verify_password("nope") is False

    def test_v1_none_password_does_not_match(self):
        data = _url_data(password_hash="secret", schema_version="v1")
        assert data.verify_password(None) is False


class TestDualCache:
    async def test_returns_live_data_on_primary_hit(self):
        r = AsyncMock()
        r.get = AsyncMock(side_effect=[json.dumps({"v": 1}), None])
        r.set = AsyncMock(return_value=True)
        cache = DualCache(r)
        result = await cache.get_or_set("key", AsyncMock(return_value={"v": 99}))
        assert result == {"v": 1}

    async def test_returns_stale_and_schedules_refresh(self):
        r = AsyncMock()
        # primary miss, stale hit
        r.get = AsyncMock(side_effect=[None, json.dumps({"v": "stale"})])
        r.set = AsyncMock(return_value=True)
        cache = DualCache(r)
        result = await cache.get_or_set("key", AsyncMock(return_value={"v": "fresh"}))
        assert result == {"v": "stale"}

    async def test_calls_query_fn_on_full_miss(self):
        r = AsyncMock()
        r.get = AsyncMock(return_value=None)
        r.set = AsyncMock(return_value=True)  # lock acquired
        r.setex = AsyncMock()
        r.delete = AsyncMock()
        query = AsyncMock(return_value={"v": "fresh"})
        cache = DualCache(r)
        result = await cache.get_or_set("key", query)
        assert result == {"v": "fresh"}
        query.assert_awaited_once()

    async def test_returns_none_when_redis_none(self):
        called = False

        async def query():
            nonlocal called
            called = True
            return {"v": 1}

        cache = DualCache(redis_client=None)
        result = await cache.get_or_set("key", query)
        # When redis is None, query is called directly
        assert called
        assert result == {"v": 1}

    async def test_returns_none_on_lock_contention(self):
        r = AsyncMock()
        r.get = AsyncMock(return_value=None)  # both miss
        r.set = AsyncMock(return_value=None)  # lock NOT acquired
        cache = DualCache(r)
        result = await cache.get_or_set("key", AsyncMock(return_value={"v": 1}))
        assert result is None


class TestUrlCacheDataGeoRules:
    async def test_pre_geo_payload_decodes_with_none(self):
        """Entries cached before geo_rules existed must stay decodable —

        the field defaults to None, so no cache version bump is needed."""
        payload = {
            "_id": "507f1f77bcf86cd799439011",
            "alias": "abc1234",
            "long_url": "https://example.com",
            "block_bots": False,
            "password_hash": None,
            "expiration_time": None,
            "max_clicks": None,
            "url_status": "ACTIVE",
            "schema_version": "v2",
            "owner_id": "507f1f77bcf86cd799439012",
            "domain": DOMAIN,
        }
        r = _fake_redis(get_returns=json.dumps(payload))
        cache = UrlCache(r)
        result = await cache.get("abc1234", DOMAIN)
        assert result is not None
        assert result.geo_rules is None

    async def test_geo_rules_round_trip(self):
        rules = {"IN": "https://example.in/", "US": "https://example.com/us"}
        data = _url_data(domain=DOMAIN)
        data = data.model_copy(update={"geo_rules": rules})
        r = _fake_redis()
        cache = UrlCache(r)
        await cache.set("abc1234", data)
        stored_json = r.setex.call_args[0][2]

        r2 = _fake_redis(get_returns=stored_json)
        cache2 = UrlCache(r2)
        result = await cache2.get("abc1234", DOMAIN)
        assert result is not None
        assert result.geo_rules == rules


class TestUrlCacheDataAbVariants:
    async def test_pre_variants_payload_decodes_with_none(self):
        payload = {
            "_id": "507f1f77bcf86cd799439011",
            "alias": "abc1234",
            "long_url": "https://example.com",
            "block_bots": False,
            "password_hash": None,
            "expiration_time": None,
            "max_clicks": None,
            "url_status": "ACTIVE",
            "schema_version": "v2",
            "owner_id": "507f1f77bcf86cd799439012",
            "domain": DOMAIN,
        }
        r = _fake_redis(get_returns=json.dumps(payload))
        cache = UrlCache(r)
        result = await cache.get("abc1234", DOMAIN)
        assert result is not None
        assert result.ab_variants is None

    async def test_ab_variants_round_trip(self):
        from app.schemas.models.url import AbVariant

        variants = [AbVariant(url="https://example.com/b", weight=40)]
        data = _url_data(domain=DOMAIN).model_copy(update={"ab_variants": variants})
        r = _fake_redis()
        cache = UrlCache(r)
        await cache.set("abc1234", data)
        stored_json = r.setex.call_args[0][2]

        r2 = _fake_redis(get_returns=stored_json)
        result = await UrlCache(r2).get("abc1234", DOMAIN)
        assert result is not None
        assert result.ab_variants == variants


class TestUrlCacheDataIsTimeExpired:
    def _data(self, expiration_time):
        return UrlCacheData(
            id="x",
            alias="abc1234",
            long_url="https://example.com",
            block_bots=False,
            password_hash=None,
            expiration_time=expiration_time,
            max_clicks=None,
            url_status="ACTIVE",
            schema_version="v2",
            owner_id=None,
        )

    def test_none_never_expires(self):
        assert self._data(None).is_time_expired(1_800_000_000) is False

    def test_past_is_expired(self):
        assert self._data(1_000).is_time_expired(2_000) is True

    def test_boundary_equal_is_expired(self):
        # <= convention, shared with UrlV2Doc.effective_status
        assert self._data(2_000).is_time_expired(2_000) is True

    def test_future_is_not_expired(self):
        assert self._data(3_000).is_time_expired(2_000) is False


class TestFromV2DocExpirationNormalization:
    def test_naive_expire_after_treated_as_utc(self):
        """Mongo returns naive UTC — the projection must not interpret it

        in host-local time."""
        from datetime import datetime, timezone

        from app.schemas.models.url import UrlV2Doc

        naive = datetime(2025, 6, 1, 12, 0, 0)  # naive UTC from Mongo
        doc = UrlV2Doc.from_mongo(
            {
                "_id": ObjectId(),
                "alias": "abc1234",
                "domain": "spoo.me",
                "created_at": naive,
                "long_url": "https://example.com",
                "expire_after": naive,
            }
        )
        data = UrlCacheData.from_v2_doc(doc)
        expected = int(naive.replace(tzinfo=timezone.utc).timestamp())
        assert data.expiration_time == expected


class TestUrlCacheDataIsNotYetLive:
    def _data(self, start_time, pre_start_url=None):
        return UrlCacheData(
            id="x",
            alias="abc1234",
            long_url="https://example.com",
            block_bots=False,
            password_hash=None,
            expiration_time=None,
            max_clicks=None,
            url_status="ACTIVE",
            schema_version="v2",
            owner_id=None,
            start_time=start_time,
            pre_start_url=pre_start_url,
        )

    def test_none_is_live(self):
        assert self._data(None).is_not_yet_live(1_800_000_000) is False

    def test_future_is_not_yet_live(self):
        assert self._data(3_000).is_not_yet_live(2_000) is True

    def test_boundary_equal_is_live(self):
        # Strict > : the link goes live at the exact second.
        assert self._data(2_000).is_not_yet_live(2_000) is False

    def test_past_is_live(self):
        assert self._data(1_000).is_not_yet_live(2_000) is False

    def test_entries_written_before_the_field_deserialize_live(self):
        """Redis entries from before scheduling shipped carry no start_time;

        they must read as live, not fail validation."""
        raw = (
            '{"_id":"x","alias":"abc1234","long_url":"https://example.com",'
            '"block_bots":false,"password_hash":null,"expiration_time":null,'
            '"max_clicks":null,"url_status":"ACTIVE","schema_version":"v2",'
            '"owner_id":null}'
        )
        data = UrlCacheData.model_validate_json(raw)
        assert data.start_time is None
        assert data.pre_start_url is None
        assert data.is_not_yet_live(1_800_000_000) is False

    def test_from_v2_doc_carries_start_and_fallback(self):
        from datetime import datetime, timezone

        from app.schemas.models.url import UrlV2Doc

        starts = datetime(2030, 6, 1, 12, 0, 0)  # naive UTC from Mongo
        doc = UrlV2Doc.from_mongo(
            {
                "_id": ObjectId(),
                "alias": "abc1234",
                "domain": "spoo.me",
                "created_at": starts,
                "long_url": "https://example.com",
                "starts_at": starts,
                "pre_start_url": "https://example.org/soon",
            }
        )
        data = UrlCacheData.from_v2_doc(doc)
        assert data.start_time == int(starts.replace(tzinfo=timezone.utc).timestamp())
        assert data.pre_start_url == "https://example.org/soon"


class TestFromV2DocFractionalStart:
    def test_fractional_start_rounds_up_so_the_link_is_never_live_early(self):
        from datetime import datetime, timezone

        from app.schemas.models.url import UrlV2Doc

        starts = datetime(2030, 6, 1, 12, 0, 0, 900_000, tzinfo=timezone.utc)
        doc = UrlV2Doc.from_mongo(
            {
                "_id": ObjectId(),
                "alias": "abc1234",
                "domain": "spoo.me",
                "created_at": starts,
                "long_url": "https://example.com",
                "starts_at": starts,
            }
        )
        data = UrlCacheData.from_v2_doc(doc)
        whole = int(starts.replace(microsecond=0).timestamp())
        assert data.start_time == whole + 1
        # At the floor second the link is still not live; one second on it is.
        assert data.is_not_yet_live(whole) is True
        assert data.is_not_yet_live(whole + 1) is False