diff --git a/.github/workflows/process-flowsheet-studio.yml b/.github/workflows/process-flowsheet-studio.yml index 6350980..d240412 100644 --- a/.github/workflows/process-flowsheet-studio.yml +++ b/.github/workflows/process-flowsheet-studio.yml @@ -27,6 +27,7 @@ on: - "tests/test_process_chat_studio_context.py" - "tests/test_studio_navigation.py" - "tests/test_app_navigation.py" + - "tests/test_experimental_links.py" - "tests/test_studio_results_context.py" - "tests/test_studio_results_shell.py" - "tests/test_studio_study_context.py" @@ -121,6 +122,7 @@ jobs: tests/test_subflowsheet_schema.py - name: Run Studio workspace navigation regressions run: | + python tests/test_experimental_links.py -v python tests/test_app_navigation.py -v python tests/test_studio_navigation.py -v - name: Run shared Studio case lifecycle regressions diff --git a/README.md b/README.md index 9e61ec0..f8a2e0b 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,12 @@ Studio card is also hidden from the front page. Enable **Experimental mode** with the toggle directly below the sidebar menu to register and show the wider set of developing models and interfaces, including NeqSim Studio. The selection is retained in Streamlit session state while the -user moves between pages and defaults to off for each new session. +user moves between pages. New sessions start in normal mode unless opened through +an experimental page's direct URL, such as +[CO₂ Mechanistic Model](https://neqsim.streamlit.app/CO2_mechanisticModel). +These links automatically enable Experimental mode before routing to the page; +no query parameter or prior visit to the home page is needed. The toggle can +still be turned off afterward. ## Pipeline Hydraulics diff --git a/app_navigation.py b/app_navigation.py index ce341d6..74dde8e 100644 --- a/app_navigation.py +++ b/app_navigation.py @@ -5,6 +5,8 @@ import re from dataclasses import dataclass from pathlib import Path +from collections.abc import Iterable +from urllib.parse import unquote, urlsplit APP_ROOT = Path(__file__).resolve().parent @@ -90,3 +92,11 @@ def experimental_page_specs() -> tuple[PageSpec, ...]: for path in discover_page_paths() if path not in STABLE_PAGE_PATHS ) + + +def is_experimental_url(url: str | None, experimental_paths: Iterable[str]) -> bool: + """Recognize a registered experimental URL, including a deployment base path.""" + if not url: + return False + pathname = unquote(urlsplit(url).path).rstrip("/").rsplit("/", 1)[-1] + return bool(pathname) and pathname in experimental_paths diff --git a/home.py b/home.py index f7e7b32..96d7916 100644 --- a/home.py +++ b/home.py @@ -93,7 +93,8 @@ ### Getting Started Use the left-hand menu to select a calculation. Normal mode contains TP Flash, Phase Envelope, Gas Hydrate, Hydrogen, and EOS-CG. Enable **Experimental mode** -below the menu to add the wider set of developing tools. +below the menu to add the wider set of developing tools. Opening a direct link +to an experimental page automatically enables Experimental mode for that session. ### Documentation & Tutorials - [NeqSim Documentation](https://equinor.github.io/neqsim/) diff --git a/tests/test_app_navigation.py b/tests/test_app_navigation.py index 48e923d..7ca95fd 100644 --- a/tests/test_app_navigation.py +++ b/tests/test_app_navigation.py @@ -4,6 +4,10 @@ from pathlib import Path import unittest +from unittest.mock import PropertyMock, patch + +from streamlit.runtime.context import ContextProxy +from streamlit.navigation.page import StreamlitPage from streamlit.testing.v1 import AppTest @@ -89,7 +93,34 @@ def test_studio_front_page_action_tracks_experimental_mode(self): self.assertFalse(app.exception) self.assertNotIn("Open NeqSim Studio", [button.label for button in app.button]) - def test_studio_url_is_unavailable_in_normal_mode(self): + + def test_direct_link_registers_experimental_pages_and_toggle_can_disable(self): + app_path = Path(__file__).resolve().parents[1] / "welcome.py" + for spec in experimental_page_specs(): + with self.subTest(page=spec.path): + # Page execution is isolated: this test exercises the real router + # without importing unrelated Java models or optional AI clients. + with patch.object(StreamlitPage, "run"), patch.object( + ContextProxy, "url", new_callable=PropertyMock, + return_value="https://neqsim.streamlit.app/" + + spec.path.split("/", 1)[1].split("_", 1)[1][:-3].replace(" ", "_"), + ): + app = AppTest.from_file(str(app_path)).run(timeout=30) + self.assertFalse(app.exception) + self.assertTrue(app.session_state["experimental_mode"]) + toggle = next( + item for item in app.sidebar.toggle + if item.label == "Experimental mode" + ) + self.assertTrue(toggle.value) + toggle.set_value(False) + app.run(timeout=30) + self.assertFalse(app.exception) + self.assertFalse(app.session_state["experimental_mode"]) + app.run(timeout=30) + self.assertFalse(app.session_state["experimental_mode"]) + + def test_programmatic_studio_switch_is_unavailable_without_opt_in(self): app_path = Path(__file__).resolve().parents[1] / "welcome.py" app = AppTest.from_file(str(app_path)).run(timeout=30) diff --git a/tests/test_experimental_links.py b/tests/test_experimental_links.py new file mode 100644 index 0000000..3833b23 --- /dev/null +++ b/tests/test_experimental_links.py @@ -0,0 +1,38 @@ +"""Direct-link policy tests; no simulation or Streamlit runtime required.""" + +import unittest + +from app_navigation import is_experimental_url + + +class ExperimentalLinkTest(unittest.TestCase): + def test_experimental_links(self): + for url in ( + "https://neqsim.streamlit.app/CO2_mechanisticModel", + "https://neqsim.streamlit.app/CO2_mechanisticModel?embed=true#results", + "https://example.org/neqsim/CO2_mechanisticModel/", + "https://example.org/neqsim/%43O2_mechanisticModel", + ): + with self.subTest(url=url): + self.assertTrue(is_experimental_url(url, ["CO2_mechanisticModel"])) + + def test_home_stable_and_unknown_links_do_not_enable_experimental(self): + for url in ( + None, "", "https://neqsim.streamlit.app/", + "https://neqsim.streamlit.app/TP_flash", + "https://neqsim.streamlit.app/unknown", + "https://neqsim.streamlit.app/?page=CO2_mechanisticModel", + "https://neqsim.streamlit.app/CO2_mechanisticModel_extra", + ): + with self.subTest(url=url): + self.assertFalse(is_experimental_url(url, ["CO2_mechanisticModel"])) + + def test_any_registered_experimental_route_is_supported(self): + self.assertTrue(is_experimental_url( + "https://neqsim.streamlit.app/NeqSim_Studio", + iter(["CO2_mechanisticModel", "NeqSim_Studio"]), + )) + + +if __name__ == "__main__": + unittest.main() diff --git a/welcome.py b/welcome.py index 6bc0a77..9a8aa9a 100644 --- a/welcome.py +++ b/welcome.py @@ -16,6 +16,7 @@ from app_navigation import ( experimental_page_specs, + is_experimental_url, stable_page_specs, ) @@ -66,8 +67,12 @@ def persist_experimental_mode(): ) +# Build page metadata before navigation so direct links can opt a new session in. +experimental_pages = [create_page(spec) for spec in experimental_page_specs()] if EXPERIMENTAL_MODE_KEY not in st.session_state: - st.session_state[EXPERIMENTAL_MODE_KEY] = False + st.session_state[EXPERIMENTAL_MODE_KEY] = is_experimental_url( + st.context.url, (page.url_path for page in experimental_pages) + ) if EXPERIMENTAL_MODE_WIDGET_KEY not in st.session_state: st.session_state[EXPERIMENTAL_MODE_WIDGET_KEY] = st.session_state[ EXPERIMENTAL_MODE_KEY @@ -79,9 +84,7 @@ def persist_experimental_mode(): "Stable tools": [create_page(spec) for spec in stable_page_specs()], } if experimental_mode: - navigation_pages["Experimental"] = [ - create_page(spec) for spec in experimental_page_specs() - ] + navigation_pages["Experimental"] = experimental_pages selected_page = st.navigation(navigation_pages)