1515from pytest_homeassistant .async_mock import mock_open , patch
1616
1717from homeassistant import loader
18+ from homeassistant .const import EVENT_HOMEASSISTANT_STARTED , EVENT_HOMEASSISTANT_STOP
1819from homeassistant .setup import async_setup_component
1920
2021SECRET_KEY = b"0123456789abcdef"
@@ -232,9 +233,10 @@ async def shell_msg(sock, msg_type, msg_content, execute=False):
232233
233234
234235async def test_jupyter_kernel_msgs (hass , caplog ):
235- """Test Jupyter kernel."""
236+ """Test Jupyter kernel messages ."""
236237 sock , _ = await setup_script (hass , [dt (2020 , 7 , 1 , 11 , 0 , 0 , 0 )], "" )
237238
239+ hass .bus .async_fire (EVENT_HOMEASSISTANT_STARTED )
238240 #
239241 # test the heartbeat loopback with some long and short messages
240242 # also send messages to stdin and iopub, which ignore them
@@ -264,11 +266,13 @@ async def test_jupyter_kernel_msgs(hass, caplog):
264266 #
265267 # test completions
266268 #
267- reply = await shell_msg (sock , "complete_request" , {"code" : "whi" , "cursor_pos" : 3 })
269+ code = "whi"
270+ reply = await shell_msg (sock , "complete_request" , {"code" : code , "cursor_pos" : len (code )})
268271 assert reply ["header" ]["msg_type" ] == "complete_reply"
269272 assert reply ["content" ]["matches" ] == ["while" ]
270273
271- reply = await shell_msg (sock , "complete_request" , {"code" : "pyscr" , "cursor_pos" : 5 })
274+ code = "pyscr"
275+ reply = await shell_msg (sock , "complete_request" , {"code" : code , "cursor_pos" : len (code )})
272276 assert reply ["header" ]["msg_type" ] == "complete_reply"
273277 assert reply ["content" ]["matches" ] == [
274278 "pyscript" ,
@@ -313,36 +317,47 @@ async def test_jupyter_kernel_msgs(hass, caplog):
313317 #
314318 # test code execution
315319 #
316- reply = await shell_msg (sock , "execute_request" , {"code" : "1 + 2" }, execute = True )
317- assert reply ["content" ]["data" ]["text/plain" ] == "3"
320+ reply = await shell_msg (sock , "execute_request" , {"code" : "x = 123; x + 1 + 2" }, execute = True )
321+ assert reply ["content" ]["data" ]["text/plain" ] == "126"
322+
323+ reply = await shell_msg (sock , "execute_request" , {"code" : "import math; x + 5" }, execute = True )
324+ assert reply ["content" ]["data" ]["text/plain" ] == "128"
325+
326+ #
327+ # do a reload to make sure our global context is preserved
328+ #
329+ await hass .services .async_call ("pyscript" , "reload" , {}, blocking = True )
330+
331+ reply = await shell_msg (sock , "execute_request" , {"code" : "x + 10" }, execute = True )
332+ assert reply ["content" ]["data" ]["text/plain" ] == "133"
333+
334+ #
335+ # test completion of object attribute now that we've loaded math above
336+ #
337+ code = "import math; math.sq"
338+ reply = await shell_msg (sock , "complete_request" , {"code" : code , "cursor_pos" : len (code )})
339+ assert reply ["header" ]["msg_type" ] == "complete_reply"
340+ assert reply ["content" ]["matches" ] == ["math.sqrt" ]
318341
319342 #
320343 # run-time error
321344 #
322- reply = await shell_msg (sock , "execute_request" , {"code" : "x " }, execute = True )
323- assert reply ["content" ]["evalue" ] == "name 'x ' is not defined"
345+ reply = await shell_msg (sock , "execute_request" , {"code" : "xyz " }, execute = True )
346+ assert reply ["content" ]["evalue" ] == "name 'xyz ' is not defined"
324347
325348 #
326349 # syntax error
327350 #
328351 reply = await shell_msg (sock , "execute_request" , {"code" : "1 + " }, execute = True )
329352 assert reply ["content" ]["evalue" ] == "invalid syntax (jupyter_0, line 1)"
330353
331- #
332- # test stdout; doesn't work yet since iopub messages can be OoO
333- # doesn't work yet...
334- #
335- reply = await shell_msg (sock , "execute_request" , {"code" : "print('hello'); 123" }, execute = True )
336- assert reply ["header" ]["msg_type" ] == "execute_result"
337- assert reply ["content" ]["data" ]["text/plain" ] == "123"
338- # stdout_msg = await get_iopub_msg(sock, "stream")
339- # assert reply["content"]["text"] == "hello"
354+ hass .bus .async_fire (EVENT_HOMEASSISTANT_STOP )
340355
341356 await shutdown (sock )
342357
343358
344359async def test_jupyter_kernel_port_close (hass , caplog ):
345- """Test Jupyter kernel."""
360+ """Test Jupyter kernel closing ports ."""
346361 sock , port_nums = await setup_script (hass , [dt (2020 , 7 , 1 , 11 , 0 , 0 , 0 )], "" )
347362
348363 #
@@ -382,11 +397,12 @@ async def test_jupyter_kernel_port_close(hass, caplog):
382397 await sock ["stdin_port" ].send (msg )
383398 assert await sock ["hb_port" ].recv () == msg
384399
400+ hass .bus .async_fire (EVENT_HOMEASSISTANT_STARTED )
385401 await shutdown (sock )
386402
387403
388404async def test_jupyter_kernel_redefine_func (hass , caplog ):
389- """Test Jupyter kernel."""
405+ """Test Jupyter kernel redefining trigger function ."""
390406 sock , _ = await setup_script (hass , [dt (2020 , 7 , 1 , 11 , 0 , 0 , 0 )], "" )
391407
392408 reply = await shell_msg (
@@ -427,7 +443,7 @@ def func():
427443
428444
429445async def test_jupyter_kernel_global_ctx_func (hass , caplog ):
430- """Test Jupyter kernel."""
446+ """Test Jupyter kernel global_ctx functions ."""
431447 sock , _ = await setup_script (hass , [dt (2020 , 7 , 1 , 11 , 0 , 0 , 0 )], "" )
432448
433449 reply = await shell_msg (sock , "execute_request" , {"code" : "pyscript.get_global_ctx()" }, execute = True )
@@ -445,3 +461,16 @@ async def test_jupyter_kernel_global_ctx_func(hass, caplog):
445461 assert literal_eval (reply ["content" ]["data" ]["text/plain" ]) == 456
446462
447463 await shutdown (sock )
464+
465+
466+ async def test_jupyter_kernel_stdout (hass , caplog ):
467+ """Test Jupyter kernel stdout."""
468+ sock , _ = await setup_script (hass , [dt (2020 , 7 , 1 , 11 , 0 , 0 , 0 )], "" )
469+
470+ reply = await shell_msg (sock , "execute_request" , {"code" : "log.info('hello'); 123" }, execute = True )
471+ assert reply ["header" ]["msg_type" ] == "execute_result"
472+ assert reply ["content" ]["data" ]["text/plain" ] == "123"
473+ stdout_msg = await get_iopub_msg (sock , "stream" )
474+ assert stdout_msg ["content" ]["text" ] == "hello\n "
475+
476+ await shutdown (sock )
0 commit comments