test_app.py 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. from __future__ import annotations
  2. import io
  3. import json
  4. import os.path
  5. import re
  6. import unittest.mock
  7. import uuid
  8. from pathlib import Path
  9. from typing import Generator, List, Tuple, Type
  10. from unittest.mock import AsyncMock
  11. import pytest
  12. import sqlmodel
  13. from fastapi import FastAPI, UploadFile
  14. from starlette_admin.auth import AuthProvider
  15. from starlette_admin.contrib.sqla.admin import Admin
  16. from starlette_admin.contrib.sqla.view import ModelView
  17. import reflex as rx
  18. from reflex import AdminDash, constants
  19. from reflex.app import (
  20. App,
  21. ComponentCallable,
  22. OverlayFragment,
  23. default_overlay_component,
  24. process,
  25. upload,
  26. )
  27. from reflex.components import Component, Cond, Fragment
  28. from reflex.components.radix.themes.typography.text import Text
  29. from reflex.event import Event
  30. from reflex.middleware import HydrateMiddleware
  31. from reflex.model import Model
  32. from reflex.state import (
  33. BaseState,
  34. OnLoadInternalState,
  35. RouterData,
  36. State,
  37. StateManagerMemory,
  38. StateManagerRedis,
  39. StateUpdate,
  40. _substate_key,
  41. )
  42. from reflex.style import Style
  43. from reflex.utils import exceptions, format
  44. from reflex.vars import ComputedVar
  45. from .conftest import chdir
  46. from .states import (
  47. ChildFileUploadState,
  48. FileStateBase1,
  49. FileUploadState,
  50. GenState,
  51. GrandChildFileUploadState,
  52. )
  53. class EmptyState(BaseState):
  54. """An empty state."""
  55. pass
  56. @pytest.fixture
  57. def index_page():
  58. """An index page.
  59. Returns:
  60. The index page.
  61. """
  62. def index():
  63. return rx.box("Index")
  64. return index
  65. @pytest.fixture
  66. def about_page():
  67. """An about page.
  68. Returns:
  69. The about page.
  70. """
  71. def about():
  72. return rx.box("About")
  73. return about
  74. class ATestState(BaseState):
  75. """A simple state for testing."""
  76. var: int
  77. @pytest.fixture()
  78. def test_state() -> Type[BaseState]:
  79. """A default state.
  80. Returns:
  81. A default state.
  82. """
  83. return ATestState
  84. @pytest.fixture()
  85. def redundant_test_state() -> Type[BaseState]:
  86. """A default state.
  87. Returns:
  88. A default state.
  89. """
  90. class RedundantTestState(BaseState):
  91. var: int
  92. return RedundantTestState
  93. @pytest.fixture(scope="session")
  94. def test_model() -> Type[Model]:
  95. """A default model.
  96. Returns:
  97. A default model.
  98. """
  99. class TestModel(Model, table=True): # type: ignore
  100. pass
  101. return TestModel
  102. @pytest.fixture(scope="session")
  103. def test_model_auth() -> Type[Model]:
  104. """A default model.
  105. Returns:
  106. A default model.
  107. """
  108. class TestModelAuth(Model, table=True): # type: ignore
  109. """A test model with auth."""
  110. pass
  111. return TestModelAuth
  112. @pytest.fixture()
  113. def test_get_engine():
  114. """A default database engine.
  115. Returns:
  116. A default database engine.
  117. """
  118. enable_admin = True
  119. url = "sqlite:///test.db"
  120. return sqlmodel.create_engine(
  121. url,
  122. echo=False,
  123. connect_args={"check_same_thread": False} if enable_admin else {},
  124. )
  125. @pytest.fixture()
  126. def test_custom_auth_admin() -> Type[AuthProvider]:
  127. """A default auth provider.
  128. Returns:
  129. A default default auth provider.
  130. """
  131. class TestAuthProvider(AuthProvider):
  132. """A test auth provider."""
  133. login_path: str = "/login"
  134. logout_path: str = "/logout"
  135. def login(self):
  136. """Login."""
  137. pass
  138. def is_authenticated(self):
  139. """Is authenticated."""
  140. pass
  141. def get_admin_user(self):
  142. """Get admin user."""
  143. pass
  144. def logout(self):
  145. """Logout."""
  146. pass
  147. return TestAuthProvider
  148. def test_default_app(app: App):
  149. """Test creating an app with no args.
  150. Args:
  151. app: The app to test.
  152. """
  153. assert app.middleware == [HydrateMiddleware()]
  154. assert app.style == Style()
  155. assert app.admin_dash is None
  156. def test_multiple_states_error(monkeypatch, test_state, redundant_test_state):
  157. """Test that an error is thrown when multiple classes subclass rx.BaseState.
  158. Args:
  159. monkeypatch: Pytest monkeypatch object.
  160. test_state: A test state subclassing rx.BaseState.
  161. redundant_test_state: Another test state subclassing rx.BaseState.
  162. """
  163. monkeypatch.delenv(constants.PYTEST_CURRENT_TEST)
  164. with pytest.raises(ValueError):
  165. App()
  166. def test_add_page_default_route(app: App, index_page, about_page):
  167. """Test adding a page to an app.
  168. Args:
  169. app: The app to test.
  170. index_page: The index page.
  171. about_page: The about page.
  172. """
  173. assert app.pages == {}
  174. app.add_page(index_page)
  175. assert set(app.pages.keys()) == {"index"}
  176. app.add_page(about_page)
  177. assert set(app.pages.keys()) == {"index", "about"}
  178. def test_add_page_set_route(app: App, index_page, windows_platform: bool):
  179. """Test adding a page to an app.
  180. Args:
  181. app: The app to test.
  182. index_page: The index page.
  183. windows_platform: Whether the system is windows.
  184. """
  185. route = "test" if windows_platform else "/test"
  186. assert app.pages == {}
  187. app.add_page(index_page, route=route)
  188. assert set(app.pages.keys()) == {"test"}
  189. def test_add_page_set_route_dynamic(index_page, windows_platform: bool):
  190. """Test adding a page with dynamic route variable to an app.
  191. Args:
  192. index_page: The index page.
  193. windows_platform: Whether the system is windows.
  194. """
  195. app = App(state=EmptyState)
  196. route = "/test/[dynamic]"
  197. if windows_platform:
  198. route.lstrip("/").replace("/", "\\")
  199. assert app.pages == {}
  200. app.add_page(index_page, route=route)
  201. assert set(app.pages.keys()) == {"test/[dynamic]"}
  202. assert "dynamic" in app.state.computed_vars
  203. assert app.state.computed_vars["dynamic"]._deps(objclass=EmptyState) == {
  204. constants.ROUTER
  205. }
  206. assert constants.ROUTER in app.state()._computed_var_dependencies
  207. def test_add_page_set_route_nested(app: App, index_page, windows_platform: bool):
  208. """Test adding a page to an app.
  209. Args:
  210. app: The app to test.
  211. index_page: The index page.
  212. windows_platform: Whether the system is windows.
  213. """
  214. route = "test\\nested" if windows_platform else "/test/nested"
  215. assert app.pages == {}
  216. app.add_page(index_page, route=route)
  217. assert set(app.pages.keys()) == {route.strip(os.path.sep)}
  218. def test_add_page_invalid_api_route(app: App, index_page):
  219. """Test adding a page with an invalid route to an app.
  220. Args:
  221. app: The app to test.
  222. index_page: The index page.
  223. """
  224. with pytest.raises(ValueError):
  225. app.add_page(index_page, route="api")
  226. with pytest.raises(ValueError):
  227. app.add_page(index_page, route="/api")
  228. with pytest.raises(ValueError):
  229. app.add_page(index_page, route="/api/")
  230. with pytest.raises(ValueError):
  231. app.add_page(index_page, route="api/foo")
  232. with pytest.raises(ValueError):
  233. app.add_page(index_page, route="/api/foo")
  234. # These should be fine
  235. app.add_page(index_page, route="api2")
  236. app.add_page(index_page, route="/foo/api")
  237. def test_initialize_with_admin_dashboard(test_model):
  238. """Test setting the admin dashboard of an app.
  239. Args:
  240. test_model: The default model.
  241. """
  242. app = App(admin_dash=AdminDash(models=[test_model]))
  243. assert app.admin_dash is not None
  244. assert len(app.admin_dash.models) > 0
  245. assert app.admin_dash.models[0] == test_model
  246. def test_initialize_with_custom_admin_dashboard(
  247. test_get_engine,
  248. test_custom_auth_admin,
  249. test_model_auth,
  250. ):
  251. """Test setting the custom admin dashboard of an app.
  252. Args:
  253. test_get_engine: The default database engine.
  254. test_model_auth: The default model for an auth admin dashboard.
  255. test_custom_auth_admin: The custom auth provider.
  256. """
  257. custom_auth_provider = test_custom_auth_admin()
  258. custom_admin = Admin(engine=test_get_engine, auth_provider=custom_auth_provider)
  259. app = App(admin_dash=AdminDash(models=[test_model_auth], admin=custom_admin))
  260. assert app.admin_dash is not None
  261. assert app.admin_dash.admin is not None
  262. assert len(app.admin_dash.models) > 0
  263. assert app.admin_dash.models[0] == test_model_auth
  264. assert app.admin_dash.admin.auth_provider == custom_auth_provider
  265. def test_initialize_admin_dashboard_with_view_overrides(test_model):
  266. """Test setting the admin dashboard of an app with view class overridden.
  267. Args:
  268. test_model: The default model.
  269. """
  270. class TestModelView(ModelView):
  271. pass
  272. app = App(
  273. admin_dash=AdminDash(
  274. models=[test_model], view_overrides={test_model: TestModelView}
  275. )
  276. )
  277. assert app.admin_dash is not None
  278. assert app.admin_dash.models == [test_model]
  279. assert app.admin_dash.view_overrides[test_model] == TestModelView
  280. @pytest.mark.asyncio
  281. async def test_initialize_with_state(test_state: Type[ATestState], token: str):
  282. """Test setting the state of an app.
  283. Args:
  284. test_state: The default state.
  285. token: a Token.
  286. """
  287. app = App(state=test_state)
  288. assert app.state == test_state
  289. # Get a state for a given token.
  290. state = await app.state_manager.get_state(_substate_key(token, test_state))
  291. assert isinstance(state, test_state)
  292. assert state.var == 0 # type: ignore
  293. if isinstance(app.state_manager, StateManagerRedis):
  294. await app.state_manager.close()
  295. @pytest.mark.asyncio
  296. async def test_set_and_get_state(test_state):
  297. """Test setting and getting the state of an app with different tokens.
  298. Args:
  299. test_state: The default state.
  300. """
  301. app = App(state=test_state)
  302. # Create two tokens.
  303. token1 = str(uuid.uuid4()) + f"_{test_state.get_full_name()}"
  304. token2 = str(uuid.uuid4()) + f"_{test_state.get_full_name()}"
  305. # Get the default state for each token.
  306. state1 = await app.state_manager.get_state(token1)
  307. state2 = await app.state_manager.get_state(token2)
  308. assert state1.var == 0 # type: ignore
  309. assert state2.var == 0 # type: ignore
  310. # Set the vars to different values.
  311. state1.var = 1
  312. state2.var = 2
  313. await app.state_manager.set_state(token1, state1)
  314. await app.state_manager.set_state(token2, state2)
  315. # Get the states again and check the values.
  316. state1 = await app.state_manager.get_state(token1)
  317. state2 = await app.state_manager.get_state(token2)
  318. assert state1.var == 1 # type: ignore
  319. assert state2.var == 2 # type: ignore
  320. if isinstance(app.state_manager, StateManagerRedis):
  321. await app.state_manager.close()
  322. @pytest.mark.asyncio
  323. async def test_dynamic_var_event(test_state: Type[ATestState], token: str):
  324. """Test that the default handler of a dynamic generated var
  325. works as expected.
  326. Args:
  327. test_state: State Fixture.
  328. token: a Token.
  329. """
  330. state = test_state() # type: ignore
  331. state.add_var("int_val", int, 0)
  332. result = await state._process(
  333. Event(
  334. token=token,
  335. name=f"{test_state.get_name()}.set_int_val",
  336. router_data={"pathname": "/", "query": {}},
  337. payload={"value": 50},
  338. )
  339. ).__anext__()
  340. assert result.delta == {test_state.get_name(): {"int_val": 50}}
  341. @pytest.mark.asyncio
  342. @pytest.mark.parametrize(
  343. "event_tuples",
  344. [
  345. pytest.param(
  346. [
  347. (
  348. "list_mutation_test_state.make_friend",
  349. {
  350. "list_mutation_test_state": {
  351. "plain_friends": ["Tommy", "another-fd"]
  352. }
  353. },
  354. ),
  355. (
  356. "list_mutation_test_state.change_first_friend",
  357. {
  358. "list_mutation_test_state": {
  359. "plain_friends": ["Jenny", "another-fd"]
  360. }
  361. },
  362. ),
  363. ],
  364. id="append then __setitem__",
  365. ),
  366. pytest.param(
  367. [
  368. (
  369. "list_mutation_test_state.unfriend_first_friend",
  370. {"list_mutation_test_state": {"plain_friends": []}},
  371. ),
  372. (
  373. "list_mutation_test_state.make_friend",
  374. {"list_mutation_test_state": {"plain_friends": ["another-fd"]}},
  375. ),
  376. ],
  377. id="delitem then append",
  378. ),
  379. pytest.param(
  380. [
  381. (
  382. "list_mutation_test_state.make_friends_with_colleagues",
  383. {
  384. "list_mutation_test_state": {
  385. "plain_friends": ["Tommy", "Peter", "Jimmy"]
  386. }
  387. },
  388. ),
  389. (
  390. "list_mutation_test_state.remove_tommy",
  391. {"list_mutation_test_state": {"plain_friends": ["Peter", "Jimmy"]}},
  392. ),
  393. (
  394. "list_mutation_test_state.remove_last_friend",
  395. {"list_mutation_test_state": {"plain_friends": ["Peter"]}},
  396. ),
  397. (
  398. "list_mutation_test_state.unfriend_all_friends",
  399. {"list_mutation_test_state": {"plain_friends": []}},
  400. ),
  401. ],
  402. id="extend, remove, pop, clear",
  403. ),
  404. pytest.param(
  405. [
  406. (
  407. "list_mutation_test_state.add_jimmy_to_second_group",
  408. {
  409. "list_mutation_test_state": {
  410. "friends_in_nested_list": [["Tommy"], ["Jenny", "Jimmy"]]
  411. }
  412. },
  413. ),
  414. (
  415. "list_mutation_test_state.remove_first_person_from_first_group",
  416. {
  417. "list_mutation_test_state": {
  418. "friends_in_nested_list": [[], ["Jenny", "Jimmy"]]
  419. }
  420. },
  421. ),
  422. (
  423. "list_mutation_test_state.remove_first_group",
  424. {
  425. "list_mutation_test_state": {
  426. "friends_in_nested_list": [["Jenny", "Jimmy"]]
  427. }
  428. },
  429. ),
  430. ],
  431. id="nested list",
  432. ),
  433. pytest.param(
  434. [
  435. (
  436. "list_mutation_test_state.add_jimmy_to_tommy_friends",
  437. {
  438. "list_mutation_test_state": {
  439. "friends_in_dict": {"Tommy": ["Jenny", "Jimmy"]}
  440. }
  441. },
  442. ),
  443. (
  444. "list_mutation_test_state.remove_jenny_from_tommy",
  445. {
  446. "list_mutation_test_state": {
  447. "friends_in_dict": {"Tommy": ["Jimmy"]}
  448. }
  449. },
  450. ),
  451. (
  452. "list_mutation_test_state.tommy_has_no_fds",
  453. {"list_mutation_test_state": {"friends_in_dict": {"Tommy": []}}},
  454. ),
  455. ],
  456. id="list in dict",
  457. ),
  458. ],
  459. )
  460. async def test_list_mutation_detection__plain_list(
  461. event_tuples: List[Tuple[str, List[str]]],
  462. list_mutation_state: State,
  463. token: str,
  464. ):
  465. """Test list mutation detection
  466. when reassignment is not explicitly included in the logic.
  467. Args:
  468. event_tuples: From parametrization.
  469. list_mutation_state: A state with list mutation features.
  470. token: a Token.
  471. """
  472. for event_name, expected_delta in event_tuples:
  473. result = await list_mutation_state._process(
  474. Event(
  475. token=token,
  476. name=event_name,
  477. router_data={"pathname": "/", "query": {}},
  478. payload={},
  479. )
  480. ).__anext__()
  481. assert result.delta == expected_delta
  482. @pytest.mark.asyncio
  483. @pytest.mark.parametrize(
  484. "event_tuples",
  485. [
  486. pytest.param(
  487. [
  488. (
  489. "dict_mutation_test_state.add_age",
  490. {
  491. "dict_mutation_test_state": {
  492. "details": {"name": "Tommy", "age": 20}
  493. }
  494. },
  495. ),
  496. (
  497. "dict_mutation_test_state.change_name",
  498. {
  499. "dict_mutation_test_state": {
  500. "details": {"name": "Jenny", "age": 20}
  501. }
  502. },
  503. ),
  504. (
  505. "dict_mutation_test_state.remove_last_detail",
  506. {"dict_mutation_test_state": {"details": {"name": "Jenny"}}},
  507. ),
  508. ],
  509. id="update then __setitem__",
  510. ),
  511. pytest.param(
  512. [
  513. (
  514. "dict_mutation_test_state.clear_details",
  515. {"dict_mutation_test_state": {"details": {}}},
  516. ),
  517. (
  518. "dict_mutation_test_state.add_age",
  519. {"dict_mutation_test_state": {"details": {"age": 20}}},
  520. ),
  521. ],
  522. id="delitem then update",
  523. ),
  524. pytest.param(
  525. [
  526. (
  527. "dict_mutation_test_state.add_age",
  528. {
  529. "dict_mutation_test_state": {
  530. "details": {"name": "Tommy", "age": 20}
  531. }
  532. },
  533. ),
  534. (
  535. "dict_mutation_test_state.remove_name",
  536. {"dict_mutation_test_state": {"details": {"age": 20}}},
  537. ),
  538. (
  539. "dict_mutation_test_state.pop_out_age",
  540. {"dict_mutation_test_state": {"details": {}}},
  541. ),
  542. ],
  543. id="add, remove, pop",
  544. ),
  545. pytest.param(
  546. [
  547. (
  548. "dict_mutation_test_state.remove_home_address",
  549. {
  550. "dict_mutation_test_state": {
  551. "address": [{}, {"work": "work address"}]
  552. }
  553. },
  554. ),
  555. (
  556. "dict_mutation_test_state.add_street_to_home_address",
  557. {
  558. "dict_mutation_test_state": {
  559. "address": [
  560. {"street": "street address"},
  561. {"work": "work address"},
  562. ]
  563. }
  564. },
  565. ),
  566. ],
  567. id="dict in list",
  568. ),
  569. pytest.param(
  570. [
  571. (
  572. "dict_mutation_test_state.change_friend_name",
  573. {
  574. "dict_mutation_test_state": {
  575. "friend_in_nested_dict": {
  576. "name": "Nikhil",
  577. "friend": {"name": "Tommy"},
  578. }
  579. }
  580. },
  581. ),
  582. (
  583. "dict_mutation_test_state.add_friend_age",
  584. {
  585. "dict_mutation_test_state": {
  586. "friend_in_nested_dict": {
  587. "name": "Nikhil",
  588. "friend": {"name": "Tommy", "age": 30},
  589. }
  590. }
  591. },
  592. ),
  593. (
  594. "dict_mutation_test_state.remove_friend",
  595. {
  596. "dict_mutation_test_state": {
  597. "friend_in_nested_dict": {"name": "Nikhil"}
  598. }
  599. },
  600. ),
  601. ],
  602. id="nested dict",
  603. ),
  604. ],
  605. )
  606. async def test_dict_mutation_detection__plain_list(
  607. event_tuples: List[Tuple[str, List[str]]],
  608. dict_mutation_state: State,
  609. token: str,
  610. ):
  611. """Test dict mutation detection
  612. when reassignment is not explicitly included in the logic.
  613. Args:
  614. event_tuples: From parametrization.
  615. dict_mutation_state: A state with dict mutation features.
  616. token: a Token.
  617. """
  618. for event_name, expected_delta in event_tuples:
  619. result = await dict_mutation_state._process(
  620. Event(
  621. token=token,
  622. name=event_name,
  623. router_data={"pathname": "/", "query": {}},
  624. payload={},
  625. )
  626. ).__anext__()
  627. assert result.delta == expected_delta
  628. @pytest.mark.asyncio
  629. @pytest.mark.parametrize(
  630. ("state", "delta"),
  631. [
  632. (
  633. FileUploadState,
  634. {"state.file_upload_state": {"img_list": ["image1.jpg", "image2.jpg"]}},
  635. ),
  636. (
  637. ChildFileUploadState,
  638. {
  639. "state.file_state_base1.child_file_upload_state": {
  640. "img_list": ["image1.jpg", "image2.jpg"]
  641. }
  642. },
  643. ),
  644. (
  645. GrandChildFileUploadState,
  646. {
  647. "state.file_state_base1.file_state_base2.grand_child_file_upload_state": {
  648. "img_list": ["image1.jpg", "image2.jpg"]
  649. }
  650. },
  651. ),
  652. ],
  653. )
  654. async def test_upload_file(tmp_path, state, delta, token: str, mocker):
  655. """Test that file upload works correctly.
  656. Args:
  657. tmp_path: Temporary path.
  658. state: The state class.
  659. delta: Expected delta
  660. token: a Token.
  661. mocker: pytest mocker object.
  662. """
  663. mocker.patch(
  664. "reflex.state.State.class_subclasses",
  665. {state if state is FileUploadState else FileStateBase1},
  666. )
  667. state._tmp_path = tmp_path
  668. # The App state must be the "root" of the state tree
  669. app = App(state=State)
  670. app.event_namespace.emit = AsyncMock() # type: ignore
  671. current_state = await app.state_manager.get_state(_substate_key(token, state))
  672. data = b"This is binary data"
  673. # Create a binary IO object and write data to it
  674. bio = io.BytesIO()
  675. bio.write(data)
  676. request_mock = unittest.mock.Mock()
  677. request_mock.headers = {
  678. "reflex-client-token": token,
  679. "reflex-event-handler": f"{state.get_full_name()}.multi_handle_upload",
  680. }
  681. file1 = UploadFile(
  682. filename=f"image1.jpg",
  683. file=bio,
  684. )
  685. file2 = UploadFile(
  686. filename=f"image2.jpg",
  687. file=bio,
  688. )
  689. upload_fn = upload(app)
  690. streaming_response = await upload_fn(request_mock, [file1, file2])
  691. async for state_update in streaming_response.body_iterator:
  692. assert (
  693. state_update
  694. == StateUpdate(delta=delta, events=[], final=True).json() + "\n"
  695. )
  696. current_state = await app.state_manager.get_state(_substate_key(token, state))
  697. state_dict = current_state.dict()[state.get_full_name()]
  698. assert state_dict["img_list"] == [
  699. "image1.jpg",
  700. "image2.jpg",
  701. ]
  702. if isinstance(app.state_manager, StateManagerRedis):
  703. await app.state_manager.close()
  704. @pytest.mark.asyncio
  705. @pytest.mark.parametrize(
  706. "state",
  707. [FileUploadState, ChildFileUploadState, GrandChildFileUploadState],
  708. )
  709. async def test_upload_file_without_annotation(state, tmp_path, token):
  710. """Test that an error is thrown when there's no param annotated with rx.UploadFile or List[UploadFile].
  711. Args:
  712. state: The state class.
  713. tmp_path: Temporary path.
  714. token: a Token.
  715. """
  716. state._tmp_path = tmp_path
  717. app = App(state=State)
  718. request_mock = unittest.mock.Mock()
  719. request_mock.headers = {
  720. "reflex-client-token": token,
  721. "reflex-event-handler": f"{state.get_full_name()}.handle_upload2",
  722. }
  723. file_mock = unittest.mock.Mock(filename="image1.jpg")
  724. fn = upload(app)
  725. with pytest.raises(ValueError) as err:
  726. await fn(request_mock, [file_mock])
  727. assert (
  728. err.value.args[0]
  729. == f"`{state.get_full_name()}.handle_upload2` handler should have a parameter annotated as List[rx.UploadFile]"
  730. )
  731. if isinstance(app.state_manager, StateManagerRedis):
  732. await app.state_manager.close()
  733. @pytest.mark.asyncio
  734. @pytest.mark.parametrize(
  735. "state",
  736. [FileUploadState, ChildFileUploadState, GrandChildFileUploadState],
  737. )
  738. async def test_upload_file_background(state, tmp_path, token):
  739. """Test that an error is thrown handler is a background task.
  740. Args:
  741. state: The state class.
  742. tmp_path: Temporary path.
  743. token: a Token.
  744. """
  745. state._tmp_path = tmp_path
  746. app = App(state=State)
  747. request_mock = unittest.mock.Mock()
  748. request_mock.headers = {
  749. "reflex-client-token": token,
  750. "reflex-event-handler": f"{state.get_full_name()}.bg_upload",
  751. }
  752. file_mock = unittest.mock.Mock(filename="image1.jpg")
  753. fn = upload(app)
  754. with pytest.raises(TypeError) as err:
  755. await fn(request_mock, [file_mock])
  756. assert (
  757. err.value.args[0]
  758. == f"@rx.background is not supported for upload handler `{state.get_full_name()}.bg_upload`."
  759. )
  760. if isinstance(app.state_manager, StateManagerRedis):
  761. await app.state_manager.close()
  762. class DynamicState(BaseState):
  763. """State class for testing dynamic route var.
  764. This is defined at module level because event handlers cannot be addressed
  765. correctly when the class is defined as a local.
  766. There are several counters:
  767. * loaded: counts how many times `on_load` was triggered by the hydrate middleware
  768. * counter: counts how many times `on_counter` was triggered by a non-navigational event
  769. -> these events should NOT trigger reload or recalculation of router_data dependent vars
  770. * side_effect_counter: counts how many times a computed var was
  771. recalculated when the dynamic route var was dirty
  772. """
  773. is_hydrated: bool = False
  774. loaded: int = 0
  775. counter: int = 0
  776. # side_effect_counter: int = 0
  777. def on_load(self):
  778. """Event handler for page on_load, should trigger for all navigation events."""
  779. self.loaded = self.loaded + 1
  780. def on_counter(self):
  781. """Increment the counter var."""
  782. self.counter = self.counter + 1
  783. @ComputedVar
  784. def comp_dynamic(self) -> str:
  785. """A computed var that depends on the dynamic var.
  786. Returns:
  787. same as self.dynamic
  788. """
  789. # self.side_effect_counter = self.side_effect_counter + 1
  790. return self.dynamic
  791. on_load_internal = OnLoadInternalState.on_load_internal.fn
  792. @pytest.mark.asyncio
  793. async def test_dynamic_route_var_route_change_completed_on_load(
  794. index_page,
  795. windows_platform: bool,
  796. token: str,
  797. app_module_mock: unittest.mock.Mock,
  798. mocker,
  799. ):
  800. """Create app with dynamic route var, and simulate navigation.
  801. on_load should fire, allowing any additional vars to be updated before the
  802. initial page hydrate.
  803. Args:
  804. index_page: The index page.
  805. windows_platform: Whether the system is windows.
  806. token: a Token.
  807. app_module_mock: Mocked app module.
  808. mocker: pytest mocker object.
  809. """
  810. arg_name = "dynamic"
  811. route = f"/test/[{arg_name}]"
  812. if windows_platform:
  813. route.lstrip("/").replace("/", "\\")
  814. app = app_module_mock.app = App(state=DynamicState)
  815. assert arg_name not in app.state.vars
  816. app.add_page(index_page, route=route, on_load=DynamicState.on_load) # type: ignore
  817. assert arg_name in app.state.vars
  818. assert arg_name in app.state.computed_vars
  819. assert app.state.computed_vars[arg_name]._deps(objclass=DynamicState) == {
  820. constants.ROUTER
  821. }
  822. assert constants.ROUTER in app.state()._computed_var_dependencies
  823. substate_token = _substate_key(token, DynamicState)
  824. sid = "mock_sid"
  825. client_ip = "127.0.0.1"
  826. state = await app.state_manager.get_state(substate_token)
  827. assert state.dynamic == ""
  828. exp_vals = ["foo", "foobar", "baz"]
  829. def _event(name, val, **kwargs):
  830. return Event(
  831. token=kwargs.pop("token", token),
  832. name=name,
  833. router_data=kwargs.pop(
  834. "router_data", {"pathname": route, "query": {arg_name: val}}
  835. ),
  836. payload=kwargs.pop("payload", {}),
  837. **kwargs,
  838. )
  839. def _dynamic_state_event(name, val, **kwargs):
  840. return _event(
  841. name=format.format_event_handler(getattr(DynamicState, name)), # type: ignore
  842. val=val,
  843. **kwargs,
  844. )
  845. prev_exp_val = ""
  846. for exp_index, exp_val in enumerate(exp_vals):
  847. on_load_internal = _event(
  848. name=f"{state.get_full_name()}.{constants.CompileVars.ON_LOAD_INTERNAL.rpartition('.')[2]}",
  849. val=exp_val,
  850. )
  851. exp_router_data = {
  852. "headers": {},
  853. "ip": client_ip,
  854. "sid": sid,
  855. "token": token,
  856. **on_load_internal.router_data,
  857. }
  858. exp_router = RouterData(exp_router_data)
  859. process_coro = process(
  860. app,
  861. event=on_load_internal,
  862. sid=sid,
  863. headers={},
  864. client_ip=client_ip,
  865. )
  866. update = await process_coro.__anext__()
  867. # route change (on_load_internal) triggers: [call on_load events, call set_is_hydrated(True)]
  868. assert update == StateUpdate(
  869. delta={
  870. state.get_name(): {
  871. arg_name: exp_val,
  872. f"comp_{arg_name}": exp_val,
  873. constants.CompileVars.IS_HYDRATED: False,
  874. # "side_effect_counter": exp_index,
  875. "router": exp_router,
  876. }
  877. },
  878. events=[
  879. _dynamic_state_event(
  880. name="on_load",
  881. val=exp_val,
  882. ),
  883. _event(
  884. name="state.set_is_hydrated",
  885. payload={"value": True},
  886. val=exp_val,
  887. router_data={},
  888. ),
  889. ],
  890. )
  891. if isinstance(app.state_manager, StateManagerRedis):
  892. # When redis is used, the state is not updated until the processing is complete
  893. state = await app.state_manager.get_state(substate_token)
  894. assert state.dynamic == prev_exp_val
  895. # complete the processing
  896. with pytest.raises(StopAsyncIteration):
  897. await process_coro.__anext__()
  898. # check that router data was written to the state_manager store
  899. state = await app.state_manager.get_state(substate_token)
  900. assert state.dynamic == exp_val
  901. process_coro = process(
  902. app,
  903. event=_dynamic_state_event(name="on_load", val=exp_val),
  904. sid=sid,
  905. headers={},
  906. client_ip=client_ip,
  907. )
  908. on_load_update = await process_coro.__anext__()
  909. assert on_load_update == StateUpdate(
  910. delta={
  911. state.get_name(): {
  912. # These computed vars _shouldn't_ be here, because they didn't change
  913. arg_name: exp_val,
  914. f"comp_{arg_name}": exp_val,
  915. "loaded": exp_index + 1,
  916. },
  917. },
  918. events=[],
  919. )
  920. # complete the processing
  921. with pytest.raises(StopAsyncIteration):
  922. await process_coro.__anext__()
  923. process_coro = process(
  924. app,
  925. event=_dynamic_state_event(
  926. name="set_is_hydrated", payload={"value": True}, val=exp_val
  927. ),
  928. sid=sid,
  929. headers={},
  930. client_ip=client_ip,
  931. )
  932. on_set_is_hydrated_update = await process_coro.__anext__()
  933. assert on_set_is_hydrated_update == StateUpdate(
  934. delta={
  935. state.get_name(): {
  936. # These computed vars _shouldn't_ be here, because they didn't change
  937. arg_name: exp_val,
  938. f"comp_{arg_name}": exp_val,
  939. "is_hydrated": True,
  940. },
  941. },
  942. events=[],
  943. )
  944. # complete the processing
  945. with pytest.raises(StopAsyncIteration):
  946. await process_coro.__anext__()
  947. # a simple state update event should NOT trigger on_load or route var side effects
  948. process_coro = process(
  949. app,
  950. event=_dynamic_state_event(name="on_counter", val=exp_val),
  951. sid=sid,
  952. headers={},
  953. client_ip=client_ip,
  954. )
  955. update = await process_coro.__anext__()
  956. assert update == StateUpdate(
  957. delta={
  958. state.get_name(): {
  959. # These computed vars _shouldn't_ be here, because they didn't change
  960. f"comp_{arg_name}": exp_val,
  961. arg_name: exp_val,
  962. "counter": exp_index + 1,
  963. }
  964. },
  965. events=[],
  966. )
  967. # complete the processing
  968. with pytest.raises(StopAsyncIteration):
  969. await process_coro.__anext__()
  970. prev_exp_val = exp_val
  971. state = await app.state_manager.get_state(substate_token)
  972. assert state.loaded == len(exp_vals)
  973. assert state.counter == len(exp_vals)
  974. # print(f"Expected {exp_vals} rendering side effects, got {state.side_effect_counter}")
  975. # assert state.side_effect_counter == len(exp_vals)
  976. if isinstance(app.state_manager, StateManagerRedis):
  977. await app.state_manager.close()
  978. @pytest.mark.asyncio
  979. async def test_process_events(mocker, token: str):
  980. """Test that an event is processed properly and that it is postprocessed
  981. n+1 times. Also check that the processing flag of the last stateupdate is set to
  982. False.
  983. Args:
  984. mocker: mocker object.
  985. token: a Token.
  986. """
  987. router_data = {
  988. "pathname": "/",
  989. "query": {},
  990. "token": token,
  991. "sid": "mock_sid",
  992. "headers": {},
  993. "ip": "127.0.0.1",
  994. }
  995. app = App(state=GenState)
  996. mocker.patch.object(app, "postprocess", AsyncMock())
  997. event = Event(
  998. token=token, name="gen_state.go", payload={"c": 5}, router_data=router_data
  999. )
  1000. async for _update in process(app, event, "mock_sid", {}, "127.0.0.1"):
  1001. pass
  1002. assert (await app.state_manager.get_state(event.substate_token)).value == 5
  1003. assert app.postprocess.call_count == 6
  1004. if isinstance(app.state_manager, StateManagerRedis):
  1005. await app.state_manager.close()
  1006. @pytest.mark.parametrize(
  1007. ("state", "overlay_component", "exp_page_child"),
  1008. [
  1009. (None, default_overlay_component, None),
  1010. (None, None, None),
  1011. (None, Text.create("foo"), Text),
  1012. (State, default_overlay_component, Fragment),
  1013. (State, None, None),
  1014. (State, Text.create("foo"), Text),
  1015. (State, lambda: Text.create("foo"), Text),
  1016. ],
  1017. )
  1018. def test_overlay_component(
  1019. state: State | None,
  1020. overlay_component: Component | ComponentCallable | None,
  1021. exp_page_child: Type[Component] | None,
  1022. ):
  1023. """Test that the overlay component is set correctly.
  1024. Args:
  1025. state: The state class to pass to App.
  1026. overlay_component: The overlay_component to pass to App.
  1027. exp_page_child: The type of the expected child in the page fragment.
  1028. """
  1029. app = App(state=state, overlay_component=overlay_component)
  1030. app._setup_overlay_component()
  1031. if exp_page_child is None:
  1032. assert app.overlay_component is None
  1033. elif isinstance(exp_page_child, OverlayFragment):
  1034. assert app.overlay_component is not None
  1035. generated_component = app._generate_component(app.overlay_component) # type: ignore
  1036. assert isinstance(generated_component, OverlayFragment)
  1037. assert isinstance(
  1038. generated_component.children[0],
  1039. Cond, # ConnectionModal is a Cond under the hood
  1040. )
  1041. else:
  1042. assert app.overlay_component is not None
  1043. assert isinstance(
  1044. app._generate_component(app.overlay_component), # type: ignore
  1045. exp_page_child,
  1046. )
  1047. app.add_page(rx.box("Index"), route="/test")
  1048. # overlay components are wrapped during compile only
  1049. app._setup_overlay_component()
  1050. page = app.pages["test"]
  1051. if exp_page_child is not None:
  1052. assert len(page.children) == 3
  1053. children_types = (type(child) for child in page.children)
  1054. assert exp_page_child in children_types
  1055. else:
  1056. assert len(page.children) == 2
  1057. @pytest.fixture
  1058. def compilable_app(tmp_path) -> Generator[tuple[App, Path], None, None]:
  1059. """Fixture for an app that can be compiled.
  1060. Args:
  1061. tmp_path: Temporary path.
  1062. Yields:
  1063. Tuple containing (app instance, Path to ".web" directory)
  1064. The working directory is set to the app dir (parent of .web),
  1065. allowing app.compile() to be called.
  1066. """
  1067. app_path = tmp_path / "app"
  1068. web_dir = app_path / ".web"
  1069. web_dir.mkdir(parents=True)
  1070. (web_dir / "package.json").touch()
  1071. app = App(theme=None)
  1072. app.get_frontend_packages = unittest.mock.Mock()
  1073. with chdir(app_path):
  1074. yield app, web_dir
  1075. def test_app_wrap_compile_theme(compilable_app):
  1076. """Test that the radix theme component wraps the app.
  1077. Args:
  1078. compilable_app: compilable_app fixture.
  1079. """
  1080. app, web_dir = compilable_app
  1081. app.theme = rx.theme(accent_color="plum")
  1082. app.compile_()
  1083. app_js_contents = (web_dir / "pages" / "_app.js").read_text()
  1084. app_js_lines = [
  1085. line.strip() for line in app_js_contents.splitlines() if line.strip()
  1086. ]
  1087. assert (
  1088. "function AppWrap({children}) {"
  1089. "return ("
  1090. "<RadixThemesColorModeProvider>"
  1091. "<RadixThemesTheme accentColor={`plum`} css={{...theme.styles.global[':root'], ...theme.styles.global.body}}>"
  1092. "<Fragment>"
  1093. "{children}"
  1094. "</Fragment>"
  1095. "</RadixThemesTheme>"
  1096. "</RadixThemesColorModeProvider>"
  1097. ")"
  1098. "}"
  1099. ) in "".join(app_js_lines)
  1100. def test_app_wrap_priority(compilable_app):
  1101. """Test that the app wrap components are wrapped in the correct order.
  1102. Args:
  1103. compilable_app: compilable_app fixture.
  1104. """
  1105. app, web_dir = compilable_app
  1106. class Fragment1(Component):
  1107. tag = "Fragment1"
  1108. def _get_app_wrap_components(self) -> dict[tuple[int, str], Component]:
  1109. return {(99, "Box"): rx.chakra.box()}
  1110. class Fragment2(Component):
  1111. tag = "Fragment2"
  1112. def _get_app_wrap_components(self) -> dict[tuple[int, str], Component]:
  1113. return {(50, "Text"): rx.chakra.text()}
  1114. class Fragment3(Component):
  1115. tag = "Fragment3"
  1116. def _get_app_wrap_components(self) -> dict[tuple[int, str], Component]:
  1117. return {(10, "Fragment2"): Fragment2.create()}
  1118. def page():
  1119. return Fragment1.create(Fragment3.create())
  1120. app.add_page(page)
  1121. app.compile_()
  1122. app_js_contents = (web_dir / "pages" / "_app.js").read_text()
  1123. app_js_lines = [
  1124. line.strip() for line in app_js_contents.splitlines() if line.strip()
  1125. ]
  1126. assert (
  1127. "function AppWrap({children}) {"
  1128. "return ("
  1129. "<Box>"
  1130. "<ChakraProvider theme={extendTheme(theme)}>"
  1131. "<ChakraColorModeProvider>"
  1132. "<Text>"
  1133. "<Fragment2>"
  1134. "<Fragment>"
  1135. "{children}"
  1136. "</Fragment>"
  1137. "</Fragment2>"
  1138. "</Text>"
  1139. "</ChakraColorModeProvider>"
  1140. "</ChakraProvider>"
  1141. "</Box>"
  1142. ")"
  1143. "}"
  1144. ) in "".join(app_js_lines)
  1145. def test_app_state_determination():
  1146. """Test that the stateless status of an app is determined correctly."""
  1147. a1 = App()
  1148. assert a1.state is None
  1149. # No state, no router, no event handlers.
  1150. a1.add_page(rx.box("Index"), route="/")
  1151. assert a1.state is None
  1152. # Add a page with `on_load` enables state.
  1153. a1.add_page(rx.box("About"), route="/about", on_load=rx.console_log(""))
  1154. assert a1.state is not None
  1155. a2 = App()
  1156. assert a2.state is None
  1157. # Referencing a state Var enables state.
  1158. a2.add_page(rx.box(rx.text(GenState.value)), route="/")
  1159. assert a2.state is not None
  1160. a3 = App()
  1161. assert a3.state is None
  1162. # Referencing router enables state.
  1163. a3.add_page(rx.box(rx.text(State.router.page.full_path)), route="/")
  1164. assert a3.state is not None
  1165. a4 = App()
  1166. assert a4.state is None
  1167. # Referencing an event handler enables state.
  1168. a4.add_page(rx.box(rx.button("Click", on_click=rx.console_log(""))), route="/")
  1169. assert a4.state is not None
  1170. # for coverage
  1171. def test_raise_on_connect_error():
  1172. """Test that the connect_error function is called."""
  1173. with pytest.raises(ValueError):
  1174. App(connect_error_component="Foo")
  1175. def test_raise_on_state():
  1176. """Test that the state is set."""
  1177. # state kwargs is deprecated, we just make sure the app is created anyway.
  1178. _app = App(state=State)
  1179. print(_app.state)
  1180. assert issubclass(_app.state, State)
  1181. def test_call_app():
  1182. """Test that the app can be called."""
  1183. app = App()
  1184. api = app()
  1185. assert isinstance(api, FastAPI)
  1186. def test_app_with_optional_endpoints():
  1187. from reflex.components.core.upload import Upload
  1188. app = App()
  1189. Upload.is_used = True
  1190. app.add_optional_endpoints()
  1191. # TODO: verify the availability of the endpoints in app.api
  1192. def test_app_state_manager():
  1193. app = App()
  1194. with pytest.raises(ValueError):
  1195. app.state_manager
  1196. app.enable_state()
  1197. assert app.state_manager is not None
  1198. assert isinstance(app.state_manager, (StateManagerMemory, StateManagerRedis))
  1199. def test_generate_component():
  1200. def index():
  1201. return rx.box("Index")
  1202. def index_mismatch():
  1203. return rx.match(
  1204. 1,
  1205. (1, rx.box("Index")),
  1206. (2, "About"),
  1207. "Bar",
  1208. )
  1209. comp = App._generate_component(index) # type: ignore
  1210. assert isinstance(comp, Component)
  1211. with pytest.raises(exceptions.MatchTypeError):
  1212. App._generate_component(index_mismatch) # type: ignore
  1213. def test_add_page_component_returning_tuple():
  1214. """Test that a component or render method returning a
  1215. tuple is unpacked in a Fragment.
  1216. """
  1217. app = App()
  1218. def index():
  1219. return rx.text("first"), rx.text("second")
  1220. def page2():
  1221. return (rx.text("third"),)
  1222. app.add_page(index) # type: ignore
  1223. app.add_page(page2) # type: ignore
  1224. assert isinstance((fragment_wrapper := app.pages["index"].children[0]), Fragment)
  1225. assert isinstance((first_text := fragment_wrapper.children[0]), Text)
  1226. assert str(first_text.children[0].contents) == "{`first`}" # type: ignore
  1227. assert isinstance((second_text := fragment_wrapper.children[1]), Text)
  1228. assert str(second_text.children[0].contents) == "{`second`}" # type: ignore
  1229. # Test page with trailing comma.
  1230. assert isinstance(
  1231. (page2_fragment_wrapper := app.pages["page2"].children[0]), Fragment
  1232. )
  1233. assert isinstance((third_text := page2_fragment_wrapper.children[0]), Text)
  1234. assert str(third_text.children[0].contents) == "{`third`}" # type: ignore
  1235. @pytest.mark.parametrize("export", (True, False))
  1236. def test_app_with_transpile_packages(compilable_app, export):
  1237. class C1(rx.Component):
  1238. library = "foo@1.2.3"
  1239. tag = "Foo"
  1240. transpile_packages: List[str] = ["foo"]
  1241. class C2(rx.Component):
  1242. library = "bar@4.5.6"
  1243. tag = "Bar"
  1244. transpile_packages: List[str] = ["bar@4.5.6"]
  1245. class C3(rx.NoSSRComponent):
  1246. library = "baz@7.8.10"
  1247. tag = "Baz"
  1248. transpile_packages: List[str] = ["baz@7.8.9"]
  1249. class C4(rx.NoSSRComponent):
  1250. library = "quuc@2.3.4"
  1251. tag = "Quuc"
  1252. transpile_packages: List[str] = ["quuc"]
  1253. class C5(rx.Component):
  1254. library = "quuc"
  1255. tag = "Quuc"
  1256. app, web_dir = compilable_app
  1257. page = Fragment.create(
  1258. C1.create(), C2.create(), C3.create(), C4.create(), C5.create()
  1259. )
  1260. app.add_page(page, route="/")
  1261. app.compile_(export=export)
  1262. next_config = (web_dir / "next.config.js").read_text()
  1263. transpile_packages_match = re.search(r"transpilePackages: (\[.*?\])", next_config)
  1264. transpile_packages_json = transpile_packages_match.group(1) # type: ignore
  1265. transpile_packages = sorted(json.loads(transpile_packages_json))
  1266. assert transpile_packages == [
  1267. "bar",
  1268. "foo",
  1269. "quuc",
  1270. ]
  1271. if export:
  1272. assert 'output: "export"' in next_config
  1273. assert f'distDir: "{constants.Dirs.STATIC}"' in next_config
  1274. else:
  1275. assert 'output: "export"' not in next_config
  1276. assert f'distDir: "{constants.Dirs.STATIC}"' not in next_config