Spaces:
Running

File size: 10,086 Bytes
25834e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
"""Tests for hot-URL promotion into the CF KV edge cache."""

from __future__ import annotations

import asyncio
import json
import random
from unittest.mock import AsyncMock, MagicMock

import pytest

from app.config import EdgeCacheSettings
from app.services.click.consumers.hotness import HotUrl
from app.services.edge_cache import (
    EdgeCacheEntry,
    PromoteToEdgeCacheAction,
    cache_key,
    promotion_skip_reason,
)
from tests.factories import make_url_cache

SYSTEM = "spoo.me"


def _hot(**overrides) -> HotUrl:
    kwargs = {
        "domain": SYSTEM,
        "short_code": "abc1234",
        "count": 50,
        "window_bucket": 12345,
    }
    kwargs.update(overrides)
    return HotUrl(**kwargs)


def _action(url_cache=None, kv=None, **overrides) -> PromoteToEdgeCacheAction:
    kwargs = {
        "system_domain": SYSTEM,
        "ttl_seconds": 300,
        "ttl_jitter_ratio": 0.2,
        "rng": random.Random(42),
    }
    kwargs.update(overrides)
    return PromoteToEdgeCacheAction(
        url_cache or MagicMock(), kv or MagicMock(), **kwargs
    )


class TestEligibility:
    def test_plain_active_url_is_eligible(self):
        url = make_url_cache()
        assert promotion_skip_reason(url, SYSTEM, SYSTEM) is None

    @pytest.mark.parametrize(

        ("overrides", "expected"),

        [

            ({"password_hash": "$argon2id$fake"}, "password_protected"),

            ({"max_clicks": 100}, "max_clicks"),

            ({"block_bots": True}, "block_bots"),

            ({"expiration_time": 1_900_000_000}, "has_expiration"),

            ({"start_time": 4_000_000_000}, "not_yet_live"),

            ({"url_status": "BLOCKED"}, "not_active"),

            ({"url_status": "EXPIRED"}, "not_active"),

            ({"url_status": "INACTIVE"}, "not_active"),

        ],

    )
    def test_restricted_urls_are_skipped(self, overrides, expected):
        url = make_url_cache(**overrides)
        assert promotion_skip_reason(url, SYSTEM, SYSTEM) == expected

    def test_tenant_domain_is_skipped(self):
        url = make_url_cache(domain="links.acme.com")
        reason = promotion_skip_reason(url, "links.acme.com", SYSTEM)
        assert reason == "non_system_domain"

    def test_v1_urls_are_eligible(self):
        """Legacy URLs cache with domain=system default → same rules apply."""
        url = make_url_cache(schema_version="v1", owner_id=None)
        assert promotion_skip_reason(url, SYSTEM, SYSTEM) is None

    def test_started_urls_are_eligible(self):
        """Once starts_at has passed the link is a plain link at the edge."""
        url = make_url_cache(start_time=1_000_000_000)
        assert promotion_skip_reason(url, SYSTEM, SYSTEM) is None

    def test_ab_variant_urls_are_skipped(self):
        """No Worker entry type makes a weighted pick — variant links stay

        origin-served so every click gets its variant_index."""
        from app.schemas.models.url import AbVariant

        url = make_url_cache(
            ab_variants=[AbVariant(url="https://example.com/b", weight=50)]
        )
        assert promotion_skip_reason(url, SYSTEM, SYSTEM) == "ab_variants"

    def test_geo_targeted_urls_are_eligible(self):
        """Geo links promote as geo_redirect entries — the Worker can make

        the per-country decision from request.cf.country."""
        url = make_url_cache(geo_rules={"IN": "https://example.in/"})
        assert promotion_skip_reason(url, SYSTEM, SYSTEM) is None


class TestPromoteAction:
    async def test_eligible_url_is_promoted(self):
        url_cache = MagicMock()
        url_cache.get = AsyncMock(
            return_value=make_url_cache(long_url="https://example.com/dest")
        )
        kv = MagicMock()
        kv.put = AsyncMock(return_value=True)

        await _action(url_cache, kv).promote(_hot())

        kv.put.assert_awaited_once()
        args = kv.put.await_args
        assert args.args[0] == cache_key(SYSTEM, "abc1234")
        entry = json.loads(args.args[1])
        assert entry == {
            "type": "redirect",
            "url": "https://example.com/dest",
            "status": 302,
        }
        # jitter keeps TTL within ±20% of 300s
        assert 240 <= args.kwargs["expiration_ttl"] <= 360

    async def test_geo_url_is_promoted_as_geo_redirect_entry(self):
        rules = {"IN": "https://example.in/", "US": "https://example.com/us"}
        url_cache = MagicMock()
        url_cache.get = AsyncMock(
            return_value=make_url_cache(
                long_url="https://example.com/default", geo_rules=rules
            )
        )
        kv = MagicMock()
        kv.put = AsyncMock(return_value=True)

        await _action(url_cache, kv).promote(_hot())

        kv.put.assert_awaited_once()
        entry = json.loads(kv.put.await_args.args[1])
        assert entry == {
            "type": "geo_redirect",
            "url": "https://example.com/default",
            "status": 302,
            "rules": rules,
        }

    async def test_og_link_promotion_embeds_og_html(self):
        """Hot og-links stay eligible; the entry carries the prerendered

        page so the worker can serve bots AND humans at the edge."""
        url_cache = MagicMock()
        url_cache.get = AsyncMock(
            return_value=make_url_cache(
                long_url="https://example.com/dest",
                meta_title="Hot Card",
                meta_color="#112233",
            )
        )
        kv = MagicMock()
        kv.put = AsyncMock(return_value=True)

        await _action(url_cache, kv).promote(_hot())

        kv.put.assert_awaited_once()
        entry = json.loads(kv.put.await_args.args[1])
        assert entry["type"] == "redirect"
        assert entry["url"] == "https://example.com/dest"
        assert 'content="Hot Card"' in entry["og_html"]

    async def test_cache_miss_skips_without_kv_write(self):
        url_cache = MagicMock()
        url_cache.get = AsyncMock(return_value=None)
        kv = MagicMock()
        kv.put = AsyncMock()

        await _action(url_cache, kv).promote(_hot())

        kv.put.assert_not_awaited()

    async def test_ineligible_url_skips_without_kv_write(self):
        url_cache = MagicMock()
        url_cache.get = AsyncMock(return_value=make_url_cache(block_bots=True))
        kv = MagicMock()
        kv.put = AsyncMock()

        await _action(url_cache, kv).promote(_hot())

        kv.put.assert_not_awaited()

    async def test_kv_failure_never_raises(self):
        url_cache = MagicMock()
        url_cache.get = AsyncMock(return_value=make_url_cache())
        kv = MagicMock()
        kv.put = AsyncMock(return_value=False)

        await _action(url_cache, kv).promote(_hot())  # must not raise

    async def test_jitter_never_goes_below_kv_minimum(self):
        """CF KV rejects expiration_ttl < 60 — the floor must hold even

        with tiny configured TTLs."""
        url_cache = MagicMock()
        url_cache.get = AsyncMock(return_value=make_url_cache())
        kv = MagicMock()
        kv.put = AsyncMock(return_value=True)

        await _action(url_cache, kv, ttl_seconds=60, ttl_jitter_ratio=0.5).promote(
            _hot()
        )

        assert kv.put.await_args.kwargs["expiration_ttl"] >= 60


class TestOnHotDetachment:
    async def test_on_hot_returns_before_slow_kv_completes(self):
        """A degraded CF API must not block the hotness consumer."""
        url_cache = MagicMock()
        url_cache.get = AsyncMock(return_value=make_url_cache())
        gate = asyncio.Event()

        async def slow_put(*args, **kwargs):
            await gate.wait()
            return True

        kv = MagicMock()
        kv.put = slow_put
        action = _action(url_cache, kv)

        await action.on_hot(_hot())  # must return immediately
        assert len(action._inflight) == 1
        kv_not_done = not gate.is_set()

        gate.set()
        await asyncio.gather(*action._inflight)
        assert kv_not_done
        assert len(action._inflight) == 0


class TestEdgeCacheEntryContract:
    def test_wire_shape_is_pinned(self):
        """The JSON the Worker parses — field names are the contract.

        Absent optionals are OMITTED (to_kv_json), never null — old

        workers must keep seeing exactly the pre-og_html shape."""
        entry = EdgeCacheEntry(url="https://example.com")
        assert json.loads(entry.to_kv_json()) == {
            "type": "redirect",
            "url": "https://example.com",
            "status": 302,
        }

    def test_og_only_wire_shape_is_pinned(self):
        entry = EdgeCacheEntry(type="og_only", og_html="<html></html>")
        assert json.loads(entry.to_kv_json()) == {
            "type": "og_only",
            "status": 302,
            "og_html": "<html></html>",
        }


class TestEdgeCacheSettings:
    def test_disabled_unless_all_three_set(self):
        assert EdgeCacheSettings(_env_file=None).enabled is False
        assert (
            EdgeCacheSettings(
                _env_file=None, cf_account_id="a", cf_api_token="t"
            ).enabled
            is False
        )

    def test_enabled_when_fully_configured(self):
        settings = EdgeCacheSettings(
            _env_file=None,
            cf_account_id="a",
            cf_api_token="t",
            kv_namespace_id="ns",
        )
        assert settings.enabled is True

    def test_api_base_substitutes_for_account_id(self):
        """Local dev: Explorer API base + dummy token, no CF account."""
        settings = EdgeCacheSettings(
            _env_file=None,
            cf_api_token="local-dev",
            kv_namespace_id="edge-cache-local",
            api_base="http://host.docker.internal:8787/cdn-cgi/explorer/api",
        )
        assert settings.enabled is True