gendesign/tradein-mvp/backend/tests/test_cian_state_parser.py
lekss361 9ba776f2ca
Some checks failed
Deploy Trade-In / build-backend (push) Blocked by required conditions
Deploy Trade-In / build-frontend (push) Blocked by required conditions
Deploy Trade-In / deploy (push) Blocked by required conditions
Deploy Trade-In / changes (push) Has been cancelled
feat(tradein): cian_state_parser shared utility + ScrapedLot Cian fields (#447)
2026-05-23 12:35:01 +00:00

46 lines
1.4 KiB
Python

"""Tests for cian_state_parser."""
import pytest
from app.services.scrapers.cian_state_parser import extract_state
def test_extract_state_simple_json():
html = '''
<html><body>
<script>
window._cianConfig['frontend-serp'] = window._cianConfig['frontend-serp'] || [];
window._cianConfig['frontend-serp'].push({
key: 'initialState',
value: {"results": {"offers": [{"id": 1}]}},
priority: 1
});
</script>
</body></html>
'''
state = extract_state(html, mfe='frontend-serp', key='initialState')
assert state is not None
assert state['results']['offers'][0]['id'] == 1
def test_extract_state_json_parse_form():
html = r'''
window._cianConfig['frontend-offer-card'].push({
key: 'defaultState',
value: JSON.parse("{\"offerData\":{\"offer\":{\"cianId\":123}}}"),
priority: 1
});
'''
state = extract_state(html, mfe='frontend-offer-card', key='defaultState')
assert state is not None
assert state['offerData']['offer']['cianId'] == 123
def test_extract_state_missing_returns_none():
html = "<html></html>"
assert extract_state(html, mfe='frontend-serp') is None
def test_extract_state_wrong_mfe_returns_none():
html = '''
window._cianConfig['other-mfe'].push({key: 'initialState', value: {}, priority: 1});
'''
assert extract_state(html, mfe='frontend-serp', key='initialState') is None