Spaces:
Running

File size: 3,376 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
"""Unit tests for shared/alias_dispatch.py — the resolution-order table.



This order is a public contract shared by the redirect and the public

preview endpoint; the table below is the single place it is spelled out.

"""

from __future__ import annotations

import pytest

from app.schemas.models.url import SchemaVersion
from app.shared.alias_dispatch import (
    emoji_lookup_candidates,
    is_emoji_shaped,
    resolution_order,
    v2_lookup_code,
)

VS16 = "️"
STAR = "⭐"
ROCKET = "🚀"


@pytest.mark.parametrize(

    ("short_code", "expected"),

    [

        # emoji aliases → urlsV2 first (canonical), emojis collection fallback

        ("🚀", (SchemaVersion.V2, SchemaVersion.EMOJI)),

        ("🚀✨", (SchemaVersion.V2, SchemaVersion.EMOJI)),

        # percent-encoded emoji still detected (is_emoji_alias unquotes)

        ("%F0%9F%9A%80", (SchemaVersion.V2, SchemaVersion.EMOJI)),

        # redundant-VS16 variant (⭐️): invisible to is_emoji_alias on the

        # raw bytes, detected via its canonical form

        ("⭐️🚀", (SchemaVersion.V2, SchemaVersion.EMOJI)),

        # 6 chars → v1 first (v1 codes were generated at this length)

        ("abc123", (SchemaVersion.V1, SchemaVersion.V2)),

        ("Docs12", (SchemaVersion.V1, SchemaVersion.V2)),

        # 7 chars → v2 first (historically an explicit branch, same order

        # as the default)

        ("abcd123", (SchemaVersion.V2, SchemaVersion.V1)),

        # anything else → v2 first

        ("ab", (SchemaVersion.V2, SchemaVersion.V1)),

        ("short", (SchemaVersion.V2, SchemaVersion.V1)),

        ("a-much-longer-alias", (SchemaVersion.V2, SchemaVersion.V1)),

        ("", (SchemaVersion.V2, SchemaVersion.V1)),

    ],

)
def test_resolution_order_table(short_code, expected):
    assert resolution_order(short_code) == expected


class TestIsEmojiShaped:
    @pytest.mark.parametrize(

        "code",

        [

            ROCKET,

            STAR + ROCKET,

            STAR + VS16 + ROCKET,  # redundant VS16, canonical form rescues it

            "☺" + VS16,  # fully-qualified VS16 sequence, raw form matches

            "%F0%9F%9A%80",

        ],

    )
    def test_emoji_shaped(self, code):
        assert is_emoji_shaped(code) is True

    @pytest.mark.parametrize("code", ["abc123", "", "café", "abc" + ROCKET])
    def test_not_emoji_shaped(self, code):
        assert is_emoji_shaped(code) is False


class TestV2LookupCode:
    def test_emoji_code_canonicalized(self):
        assert v2_lookup_code(STAR + VS16 + ROCKET) == STAR + ROCKET

    def test_percent_encoded_emoji_decoded(self):
        assert v2_lookup_code("%F0%9F%9A%80") == ROCKET

    def test_non_emoji_untouched(self):
        # Alphanumeric codes pass through byte-identical — even ones that
        # would decode differently (%41 stays literal for non-emoji).
        assert v2_lookup_code("abc123") == "abc123"
        assert v2_lookup_code("café") == "café"


class TestEmojiLookupCandidates:
    def test_canonical_input_single_candidate(self):
        assert emoji_lookup_candidates(STAR + ROCKET) == (STAR + ROCKET,)

    def test_variant_input_raw_first_then_canonical(self):
        raw = STAR + VS16 + ROCKET
        assert emoji_lookup_candidates(raw) == (raw, STAR + ROCKET)