|
| 1 | +"""Integration test for the ``compile`` telemetry event. |
| 2 | +
|
| 3 | +Drives a real ``AppHarness`` compile against an app that exercises every |
| 4 | +field surfaced in ``features_used``, captures ``telemetry.send`` to a JSONL |
| 5 | +file inside the test app, then asserts the emitted counts. |
| 6 | +
|
| 7 | +The compile pipeline runs end-to-end here, so a regression in |
| 8 | +``record_compile`` (such as the inherited-storage double-count fix) shows |
| 9 | +up at the integration boundary rather than only in collector unit tests. |
| 10 | +""" |
| 11 | + |
| 12 | +import functools |
| 13 | +import json |
| 14 | +from collections.abc import Generator |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from reflex.testing import AppHarness, chdir |
| 20 | + |
| 21 | + |
| 22 | +def TelemetryCompileApp(events_log_path: str = ""): |
| 23 | + """Reflex app exercising the features collected on the ``compile`` event. |
| 24 | +
|
| 25 | + Includes a parent + child state hierarchy with Cookie / LocalStorage / |
| 26 | + SessionStorage to guard against the inherited-field double-count |
| 27 | + regression, a background event handler, a SharedState user subclass, |
| 28 | + and a dynamic route. |
| 29 | +
|
| 30 | + Args: |
| 31 | + events_log_path: Filesystem path to a JSONL file. Every |
| 32 | + ``telemetry.send`` invocation during compile is appended as |
| 33 | + one JSON object per line so the test can read it back. |
| 34 | + """ |
| 35 | + import json |
| 36 | + import os |
| 37 | + from pathlib import Path |
| 38 | + |
| 39 | + import reflex as rx |
| 40 | + from reflex.istate.storage import Cookie, LocalStorage, SessionStorage |
| 41 | + from reflex.utils import telemetry |
| 42 | + |
| 43 | + # AppHarness force-disables telemetry. Re-enable it on the live config |
| 44 | + # singleton so the real _compile() path runs record_compile, and |
| 45 | + # redirect telemetry.send to disk so the test can read the payload. |
| 46 | + os.environ["REFLEX_TELEMETRY_ENABLED"] = "true" |
| 47 | + rx.config.get_config().telemetry_enabled = True |
| 48 | + |
| 49 | + sink = Path(events_log_path) |
| 50 | + sink.parent.mkdir(parents=True, exist_ok=True) |
| 51 | + |
| 52 | + def _capture(event, properties=None, **_kwargs): |
| 53 | + with sink.open("a") as fh: |
| 54 | + fh.write( |
| 55 | + json.dumps({"event": event, "properties": properties or {}}) + "\n" |
| 56 | + ) |
| 57 | + return True |
| 58 | + |
| 59 | + telemetry.send = _capture |
| 60 | + |
| 61 | + class StorageRoot(rx.State): |
| 62 | + token: str = Cookie() |
| 63 | + local_pref: str = LocalStorage() |
| 64 | + |
| 65 | + @rx.event(background=True) |
| 66 | + async def heavy(self): |
| 67 | + """Background handler used to assert detection.""" |
| 68 | + |
| 69 | + class StorageChild(StorageRoot): |
| 70 | + # ``token`` and ``local_pref`` are inherited here and must not be |
| 71 | + # re-counted on the child state. |
| 72 | + session: str = SessionStorage() |
| 73 | + |
| 74 | + class SharedThing(rx.SharedState): |
| 75 | + value: int = 0 |
| 76 | + |
| 77 | + def index(): |
| 78 | + return rx.box(rx.text("home")) |
| 79 | + |
| 80 | + def item_page(): |
| 81 | + return rx.box(rx.text("item")) |
| 82 | + |
| 83 | + app = rx.App() |
| 84 | + app.add_page(index) |
| 85 | + app.add_page(item_page, route="/items/[id]") |
| 86 | + |
| 87 | + |
| 88 | +@pytest.fixture |
| 89 | +def telemetry_events_log(tmp_path) -> Path: |
| 90 | + """Path to the JSONL telemetry sink the harness app writes into. |
| 91 | +
|
| 92 | + Args: |
| 93 | + tmp_path: pytest tmp_path fixture. |
| 94 | +
|
| 95 | + Returns: |
| 96 | + Path the harness app writes one JSON event per line to. |
| 97 | + """ |
| 98 | + return tmp_path / "telemetry_events.jsonl" |
| 99 | + |
| 100 | + |
| 101 | +@pytest.fixture |
| 102 | +def telemetry_compile_harness( |
| 103 | + tmp_path_factory, telemetry_events_log: Path |
| 104 | +) -> Generator[AppHarness, None, None]: |
| 105 | + """Run AppHarness for the telemetry-capture app. |
| 106 | +
|
| 107 | + Only ``_initialize_app()`` is invoked — that path imports the app |
| 108 | + module and calls ``App.__call__()`` which triggers ``_compile()``, so |
| 109 | + the captured JSONL contains the compile event by the time the test |
| 110 | + body runs. The dev backend / frontend lifecycle is skipped because |
| 111 | + this test only cares about compile-time telemetry. |
| 112 | +
|
| 113 | + Args: |
| 114 | + tmp_path_factory: pytest tmp_path_factory fixture. |
| 115 | + telemetry_events_log: Path the harness app writes events to. |
| 116 | +
|
| 117 | + Yields: |
| 118 | + The configured ``AppHarness`` instance after initialization. |
| 119 | + """ |
| 120 | + root = tmp_path_factory.mktemp("telemetry_compile_app") |
| 121 | + harness = AppHarness.create( |
| 122 | + root=root, |
| 123 | + app_source=functools.partial( |
| 124 | + TelemetryCompileApp, |
| 125 | + events_log_path=str(telemetry_events_log), |
| 126 | + ), |
| 127 | + app_name="telemetry_compile_app", |
| 128 | + ) |
| 129 | + harness._initialize_app() |
| 130 | + try: |
| 131 | + yield harness |
| 132 | + finally: |
| 133 | + if harness._registry_token is not None: |
| 134 | + from reflex_base.registry import RegistrationContext |
| 135 | + |
| 136 | + RegistrationContext.reset(harness._registry_token) |
| 137 | + |
| 138 | + |
| 139 | +def _read_compile_events(events_log: Path) -> list[dict]: |
| 140 | + """Return every ``compile`` event written to the sink. |
| 141 | +
|
| 142 | + Args: |
| 143 | + events_log: JSONL file the harness app appended to. |
| 144 | +
|
| 145 | + Returns: |
| 146 | + List of ``properties`` dicts for events named ``compile``. |
| 147 | + """ |
| 148 | + if not events_log.exists(): |
| 149 | + return [] |
| 150 | + out: list[dict] = [] |
| 151 | + for line in events_log.read_text().splitlines(): |
| 152 | + rec = json.loads(line) |
| 153 | + if rec.get("event") == "compile": |
| 154 | + out.append(rec["properties"]) |
| 155 | + return out |
| 156 | + |
| 157 | + |
| 158 | +def test_compile_event_features_used_initial_and_hot_reload( |
| 159 | + telemetry_compile_harness: AppHarness, |
| 160 | + telemetry_events_log: Path, |
| 161 | +): |
| 162 | + """Compile event payload is well-formed and stable across hot reload. |
| 163 | +
|
| 164 | + Two assertions in sequence, exercising the live ``_compile()`` path: |
| 165 | +
|
| 166 | + 1. The initial compile (driven by AppHarness during fixture setup) |
| 167 | + emits a ``compile`` event with exact ``features_used`` counts. |
| 168 | + Guards the inherited client-storage double-count regression: |
| 169 | + ``token`` and ``local_pref`` live on ``StorageRoot`` and are |
| 170 | + inherited by ``StorageChild`` — each must be counted exactly once. |
| 171 | +
|
| 172 | + 2. A second ``_compile`` call under ``trigger="hot_reload"`` re-derives |
| 173 | + the snapshot from scratch and must produce the same counts. A |
| 174 | + regression that cached or accumulated counters across compiles |
| 175 | + shows up here as drift between the two events. |
| 176 | + """ |
| 177 | + payloads = _read_compile_events(telemetry_events_log) |
| 178 | + assert payloads, "no compile event was captured by the AppHarness run" |
| 179 | + initial = payloads[-1] |
| 180 | + initial_features = initial["features_used"] |
| 181 | + assert initial_features["cookie_count"] == 1, ( |
| 182 | + "inherited cookie field was counted on parent and child" |
| 183 | + ) |
| 184 | + assert initial_features["local_storage_count"] == 1, ( |
| 185 | + "inherited LocalStorage field was counted on parent and child" |
| 186 | + ) |
| 187 | + assert initial_features["session_storage_count"] == 1 |
| 188 | + assert initial_features["background_event_handlers_count"] == 1 |
| 189 | + assert initial_features["shared_state_count"] == 1 |
| 190 | + assert initial_features["dynamic_routes_count"] == 1 |
| 191 | + assert initial["trigger"] in {"initial", "backend_startup", None} |
| 192 | + assert initial["exception"] is None |
| 193 | + |
| 194 | + app = telemetry_compile_harness.app_instance |
| 195 | + assert app is not None, "AppHarness did not populate app_instance" |
| 196 | + |
| 197 | + # The real reflex CLI invokes hot reloads from inside the app directory. |
| 198 | + # AppHarness chdir's into app_path during _initialize_app() but reverts |
| 199 | + # on exit, so we restore it here to match the live hot-reload environment. |
| 200 | + pre_event_count = len(payloads) |
| 201 | + with chdir(telemetry_compile_harness.app_path): |
| 202 | + app._compile(trigger="hot_reload") |
| 203 | + |
| 204 | + payloads = _read_compile_events(telemetry_events_log) |
| 205 | + assert len(payloads) == pre_event_count + 1, ( |
| 206 | + "hot reload did not emit exactly one additional compile event" |
| 207 | + ) |
| 208 | + reload = payloads[-1] |
| 209 | + assert reload["trigger"] == "hot_reload" |
| 210 | + assert reload["exception"] is None |
| 211 | + assert initial_features == reload["features_used"], ( |
| 212 | + "features_used drifted between initial compile and hot reload" |
| 213 | + ) |
| 214 | + assert initial["component_counts"] == reload["component_counts"] |
| 215 | + assert initial["pages_count"] == reload["pages_count"] |
| 216 | + assert [s["depth_from_root"] for s in initial["states"]] == [ |
| 217 | + s["depth_from_root"] for s in reload["states"] |
| 218 | + ] |
0 commit comments