File size: 13,146 Bytes
d939dd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
from __future__ import annotations

import hashlib
import json
import re
from collections import Counter
from collections.abc import Mapping
from decimal import Decimal, InvalidOperation
from pathlib import Path
from typing import Any
from urllib.parse import urlsplit


SEARCH_COMMERCE_FIXTURE = "search-commerce-current-2026-07-22.json"
SEARCH_COMMERCE_SCHEMA = "amazon-clone.search-commerce-cards.v1"
SEARCH_COMMERCE_CAPTURED_AT = "2026-07-22T16:10:00+08:00"
SEARCH_COMMERCE_QUERY = "best sellers"
SEARCH_COMMERCE_EVIDENCE_CLASS = "direct-search-card"
EXPECTED_DEPARTMENT_COUNTS = {
    "books": 6,
    "beauty-personal-care": 6,
    "home-kitchen": 2,
    "toys-games": 1,
    "computers": 5,
}
EXPECTED_CAPTURE_POLICY = {
    "offerRule": "A frozen card is transaction-ready only when the same visible result card exposed a non-empty USD price and an Add to cart button.",
    "selectionBoundary": "Each card establishes only its displayed default offer. No unobserved size, color, format, seller, inventory count, delivery date, list price, or variant cross-product is inferred.",
    "pdpBoundary": "These are current public search-card observations, not complete PDP captures. The clone may expose a card-evidence detail shell but must not claim a full source PDP.",
    "reviewBoundary": "ratingDisplay and reviewsDisplay preserve visible aggregate copy. Values abbreviated with K are not converted into exact review counts.",
    "sponsoredBoundary": "Sponsored status is retained as observed; it does not change the price or Add to cart evidence rule.",
}
CARD_FIELDS = frozenset(
    {
        "asin",
        "sourceIndex",
        "sourceDepartment",
        "department",
        "sponsored",
        "title",
        "slug",
        "canonicalPath",
        "format",
        "price_minor",
        "ratingDisplay",
        "reviewsDisplay",
        "reviewsExact",
        "sourceImageUrl",
        "image_path",
        "asset",
    }
)
ASSET_FIELDS = frozenset({"bytes", "width", "height", "mime", "sha256"})
ASIN_RE = re.compile(r"[A-Z0-9]{10}\Z")
SHA256_RE = re.compile(r"[0-9a-f]{64}\Z")
REVIEW_DISPLAY_RE = re.compile(r"(?:[1-9][0-9]*|[1-9][0-9]*\.[0-9]+)K?\Z")


class SearchCommerceError(ValueError):
    pass


def _jpeg_dimensions(payload: bytes) -> tuple[int, int] | None:
    """Read JPEG SOF dimensions without adding a runtime imaging dependency."""

    if len(payload) < 4 or payload[:2] != b"\xff\xd8":
        return None
    offset = 2
    sof_markers = {
        0xC0,
        0xC1,
        0xC2,
        0xC3,
        0xC5,
        0xC6,
        0xC7,
        0xC9,
        0xCA,
        0xCB,
        0xCD,
        0xCE,
        0xCF,
    }
    while offset < len(payload):
        while offset < len(payload) and payload[offset] != 0xFF:
            offset += 1
        while offset < len(payload) and payload[offset] == 0xFF:
            offset += 1
        if offset >= len(payload):
            break
        marker = payload[offset]
        offset += 1
        if marker in {0x01, *range(0xD0, 0xDA)}:
            continue
        if offset + 2 > len(payload):
            return None
        segment_length = int.from_bytes(payload[offset : offset + 2], "big")
        if segment_length < 2 or offset + segment_length > len(payload):
            return None
        if marker in sof_markers:
            if segment_length < 7:
                return None
            height = int.from_bytes(payload[offset + 3 : offset + 5], "big")
            width = int.from_bytes(payload[offset + 5 : offset + 7], "big")
            return width, height
        offset += segment_length
    return None


def _validate_sources(raw: Any) -> None:
    if not isinstance(raw, list) or len(raw) != 2:
        raise SearchCommerceError("search commerce sources must contain two captures")
    expected_departments = ("All Departments", "Computers")
    for source, department in zip(raw, expected_departments, strict=True):
        if (
            not isinstance(source, Mapping)
            or set(source) != {"query", "department", "url"}
            or source.get("query") != SEARCH_COMMERCE_QUERY
            or source.get("department") != department
        ):
            raise SearchCommerceError("search commerce source identity changed")
        parsed = urlsplit(str(source.get("url") or ""))
        if (
            parsed.scheme != "https"
            or parsed.hostname not in {"amazon.com", "www.amazon.com"}
            or parsed.path != "/s"
        ):
            raise SearchCommerceError("search commerce source URL is invalid")


def normalize_search_commerce_cards(
    payload: Mapping[str, Any], fixture_root: Path
) -> tuple[dict[str, Any], ...]:
    """Validate the immutable card capture and project only evidenced fields."""

    fixture_root = fixture_root.resolve()
    if (
        payload.get("schema") != SEARCH_COMMERCE_SCHEMA
        or payload.get("capturedAt") != SEARCH_COMMERCE_CAPTURED_AT
        or payload.get("locale") != "en-US"
        or payload.get("deliveryRegion") != "Singapore"
        or payload.get("currency") != "USD"
        or payload.get("capturePolicy") != EXPECTED_CAPTURE_POLICY
    ):
        raise SearchCommerceError("search commerce capture boundary is invalid")
    _validate_sources(payload.get("sources"))
    raw_products = payload.get("products")
    if not isinstance(raw_products, list) or len(raw_products) != 20:
        raise SearchCommerceError("search commerce capture must contain 20 cards")

    normalized: list[dict[str, Any]] = []
    seen_asins: set[str] = set()
    seen_source_positions: set[tuple[str, int]] = set()
    static_root = (fixture_root.parent / "static").resolve()
    for raw in raw_products:
        if not isinstance(raw, Mapping) or set(raw) not in {
            CARD_FIELDS,
            CARD_FIELDS - {"format"},
        }:
            raise SearchCommerceError("search commerce card fields changed")
        asin = raw.get("asin")
        source_index = raw.get("sourceIndex")
        source_department = raw.get("sourceDepartment")
        department = raw.get("department")
        title = raw.get("title")
        slug = raw.get("slug")
        canonical_path = raw.get("canonicalPath")
        price_minor = raw.get("price_minor")
        rating_display = raw.get("ratingDisplay")
        reviews_display = raw.get("reviewsDisplay")
        reviews_exact = raw.get("reviewsExact")
        image_path = raw.get("image_path")
        asset = raw.get("asset")
        if (
            not isinstance(asin, str)
            or ASIN_RE.fullmatch(asin) is None
            or asin in seen_asins
            or isinstance(source_index, bool)
            or not isinstance(source_index, int)
            or source_index < 1
            or source_department not in {"All Departments", "Computers"}
            or (source_department, source_index) in seen_source_positions
            or department not in EXPECTED_DEPARTMENT_COUNTS
            or (department == "computers") != (source_department == "Computers")
            or not isinstance(raw.get("sponsored"), bool)
            or not isinstance(title, str)
            or not title.strip()
            or not isinstance(slug, str)
            or not slug
            or "/" in slug
            or canonical_path != f"/{slug}/dp/{asin}"
            or isinstance(price_minor, bool)
            or not isinstance(price_minor, int)
            or price_minor < 0
        ):
            raise SearchCommerceError(f"invalid search commerce identity: {asin!r}")

        product_format = raw.get("format")
        if (
            (department == "books" and (not isinstance(product_format, str) or not product_format))
            or (department != "books" and product_format is not None)
        ):
            raise SearchCommerceError(f"invalid captured format boundary: {asin}")
        try:
            rating_value = Decimal(str(rating_display))
        except InvalidOperation as exc:
            raise SearchCommerceError(f"invalid captured rating: {asin}") from exc
        if (
            not isinstance(rating_display, str)
            or not rating_value.is_finite()
            or rating_value < 0
            or rating_value > 5
            or not isinstance(reviews_display, str)
            or REVIEW_DISPLAY_RE.fullmatch(reviews_display) is None
            or (
                reviews_display.endswith("K")
                and reviews_exact is not None
            )
            or (
                not reviews_display.endswith("K")
                and (
                    isinstance(reviews_exact, bool)
                    or not isinstance(reviews_exact, int)
                    or reviews_exact <= 0
                    or str(reviews_exact) != reviews_display
                )
            )
        ):
            raise SearchCommerceError(f"invalid captured review boundary: {asin}")

        source_image = urlsplit(str(raw.get("sourceImageUrl") or ""))
        if (
            source_image.scheme != "https"
            or source_image.hostname != "m.media-amazon.com"
            or not isinstance(image_path, str)
            or image_path
            != f"/static/assets/source-current/2026-07-22/search-commerce/{asin}.jpg"
            or not isinstance(asset, Mapping)
            or set(asset) != ASSET_FIELDS
            or asset.get("mime") != "image/jpeg"
            or isinstance(asset.get("bytes"), bool)
            or not isinstance(asset.get("bytes"), int)
            or int(asset["bytes"]) <= 0
            or isinstance(asset.get("width"), bool)
            or not isinstance(asset.get("width"), int)
            or int(asset["width"]) <= 0
            or isinstance(asset.get("height"), bool)
            or not isinstance(asset.get("height"), int)
            or int(asset["height"]) <= 0
            or not isinstance(asset.get("sha256"), str)
            or SHA256_RE.fullmatch(str(asset["sha256"])) is None
        ):
            raise SearchCommerceError(f"invalid captured image metadata: {asin}")
        local_asset = (fixture_root.parent / image_path.removeprefix("/")).resolve()
        if static_root not in local_asset.parents or not local_asset.is_file():
            raise SearchCommerceError(f"search commerce image is unavailable: {asin}")
        image_bytes = local_asset.read_bytes()
        if (
            len(image_bytes) != asset["bytes"]
            or hashlib.sha256(image_bytes).hexdigest() != asset["sha256"]
            or _jpeg_dimensions(image_bytes) != (asset["width"], asset["height"])
        ):
            raise SearchCommerceError(f"search commerce image integrity failed: {asin}")

        seen_asins.add(asin)
        seen_source_positions.add((str(source_department), int(source_index)))
        normalized.append(
            {
                "asin": asin,
                "slug": slug,
                "canonical_path": canonical_path,
                "title": title,
                "brand": "",
                "capacity": "",
                "color": "",
                "price_minor": price_minor,
                "list_price_minor": None,
                "currency": "USD",
                "rating": rating_display,
                # The legacy offer table needs an integer. Zero deliberately
                # means "not exact"; presentation uses reviews_display below.
                "reviews": reviews_exact if isinstance(reviews_exact, int) else 0,
                "reviews_exact": reviews_exact,
                "reviews_display": reviews_display,
                "image_path": image_path,
                "badge": "",
                "evidence_class": SEARCH_COMMERCE_EVIDENCE_CLASS,
                "department_slugs": (department,),
                "captured_queries": (SEARCH_COMMERCE_QUERY,),
                "source_index": source_index,
                "source_department": source_department,
                "sponsored": bool(raw["sponsored"]),
                "format": product_format,
                "card_evidence_key": (
                    f"{SEARCH_COMMERCE_FIXTURE}:{source_department}:"
                    f"{source_index}:{asin}"
                ),
            }
        )

    if dict(
        Counter(product["department_slugs"][0] for product in normalized)
    ) != EXPECTED_DEPARTMENT_COUNTS:
        raise SearchCommerceError("search commerce department counts changed")
    return tuple(normalized)


def load_search_commerce_cards(fixture_root: Path) -> tuple[dict[str, Any], ...]:
    fixture_root = fixture_root.resolve()
    candidate = (fixture_root / SEARCH_COMMERCE_FIXTURE).resolve()
    if fixture_root not in candidate.parents or not candidate.is_file():
        raise SearchCommerceError("search commerce fixture is unavailable")
    try:
        payload = json.loads(candidate.read_text(encoding="utf-8"))
    except (OSError, json.JSONDecodeError) as exc:
        raise SearchCommerceError("search commerce fixture is invalid") from exc
    if not isinstance(payload, Mapping):
        raise SearchCommerceError("search commerce fixture must be an object")
    return normalize_search_commerce_cards(payload, fixture_root)