from inspect import ( BoundArguments, signature, ) import pytest import trio from piker.ui._app import _async_main from piker.ui._display import display_symbol_data from piker.ui._widget import GodWidget from piker.ui.order_mode import open_order_mode @pytest.mark.parametrize('exec_mode', ['', 'Paper', 'dark']) def test_chart_rejects_invalid_ems_mode(exec_mode: str) -> None: ''' Reject misspelled execution modes before touching chart startup. EMS treats non-paper mode as live when a backend supports trades. A chart caller must not accidentally request that path with a typo or the order-trigger mode 'dark'. Invoke the actual startup coroutine with no widget or backend: ValueError, rather than a widget access or runtime error, proves rejection precedes chart initialization and any tasks that could acquire brokerd. This is a direct input-boundary test, not an EMS integration test. ''' with pytest.raises(ValueError, match='Invalid EMS execution mode'): trio.run( _async_main, None, [], {}, 'warning', exec_mode, ) def test_chart_ems_mode_call_contract() -> None: ''' Preserve the shipped CLI argument shape while enabling paper EMS. `_main()` passes only widget, symbols, brokers and loglevel to `_async_main()`. Bind that concrete call shape and apply defaults to verify it remains live; bind an explicit paper argument to verify integration callers need no CLI or runtime adapter changes. Check the widget and lower entrypoint defaults as well, since omitted arguments must not silently change shipped execution. These are direct API-contract assertions. They do not claim to exercise forwarding through a running chart or broker selection. ''' cli_call: BoundArguments = signature(_async_main).bind( None, ['xbtusd.kraken'], {}, 'warning', ) cli_call.apply_defaults() assert cli_call.arguments['exec_mode'] == 'live' paper_call: BoundArguments = signature(_async_main).bind( *cli_call.args[:-1], exec_mode='paper', ) assert paper_call.arguments['exec_mode'] == 'paper' assert GodWidget.exec_mode == 'live' assert signature(display_symbol_data).parameters[ 'exec_mode' ].default == 'live' assert signature(open_order_mode).parameters[ 'exec_mode' ].default == 'live'