diff --git a/workflowai/core/domain/run_test.py b/workflowai/core/domain/run_test.py index ac0aae7..25d4f94 100644 --- a/workflowai/core/domain/run_test.py +++ b/workflowai/core/domain/run_test.py @@ -137,8 +137,16 @@ def test_format_output_no_cost_latency(): class TestRunURL: + # The @patch decorator from unittest.mock temporarily replaces the value of an attribute + # during the execution of the decorated test function. The original value is restored + # after the test completes. + + # To check what happens in different environemnt configurations, see env_test.py + + # Here we patch WORKFLOWAI_APP_URL to test the direct app URL case @patch("workflowai.env.WORKFLOWAI_APP_URL", "https://workflowai.hello") def test_run_url(self, run1: Run[_TestOutput]): + # The patched value is only active during this test method assert run1.run_url == "https://workflowai.hello/_/agents/agent-id/runs/run-id" diff --git a/workflowai/env.py b/workflowai/env.py index 46cfc4b..4aa7591 100644 --- a/workflowai/env.py +++ b/workflowai/env.py @@ -4,7 +4,7 @@ WORKFLOWAI_DEFAULT_MODEL = os.getenv("WORKFLOWAI_DEFAULT_MODEL", "gemini-1.5-pro-latest") -WORKFLOWAI_API_URL = os.getenv("WORKFLOWAI_API_URL") +WORKFLOWAI_API_URL = os.getenv("WORKFLOWAI_API_URL", "https://run.workflowai.com") def _default_app_url(): diff --git a/workflowai/env_test.py b/workflowai/env_test.py index c20824b..a1878f8 100644 --- a/workflowai/env_test.py +++ b/workflowai/env_test.py @@ -1,9 +1,40 @@ +import importlib +import os from typing import Optional from unittest.mock import patch import pytest -from .env import _default_app_url # pyright: ignore[reportPrivateUsage] +from workflowai import env + + +# Check what happens when the environment is fully empty +@patch.dict(os.environ, clear=True) +def test_default_app_url_clear_env(): + # We need to reload the env module so that the environment variables are read again + importlib.reload(env) + assert env.WORKFLOWAI_API_URL == "https://run.workflowai.com" + assert env.WORKFLOWAI_APP_URL == "https://workflowai.com" + + +# Check with the default app url when an api url is provided +@patch.dict(os.environ, {"WORKFLOWAI_API_URL": "https://run.workflowai.dev"}, clear=True) +def test_default_app_url_dev_url(): + importlib.reload(env) + assert env.WORKFLOWAI_API_URL == "https://run.workflowai.dev" + assert env.WORKFLOWAI_APP_URL == "https://workflowai.dev" + + +# Check the app url when both api url and app url are provided +@patch.dict( + os.environ, + {"WORKFLOWAI_API_URL": "https://run.workflowai.dev", "WORKFLOWAI_APP_URL": "https://workflowai.app"}, + clear=True, +) +def test_with_app_url(): + importlib.reload(env) + assert env.WORKFLOWAI_API_URL == "https://run.workflowai.dev" + assert env.WORKFLOWAI_APP_URL == "https://workflowai.app" @pytest.mark.parametrize( @@ -16,5 +47,6 @@ ], ) def test_default_app_url(api_url: Optional[str], expected: str): + # Importing here to avoid setting the environment variables before the test with patch("workflowai.env.WORKFLOWAI_API_URL", api_url): - assert _default_app_url() == expected + assert env._default_app_url() == expected # pyright: ignore[reportPrivateUsage]