Repository: pyscript/pyscript
File: pyscript.core/tests/integration/test_py_terminal.py (Python)

Code before:
import time

import pytest
from playwright.sync_api import expect

from .support import PageErrors, PyScriptTest, only_worker, skip_worker


class TestPyTerminal(PyScriptTest):
    def test_multiple_terminals(self):
        """
        Multiple terminals are not currently supported
        """
        self.pyscript_run(
            """
            <script type="py" terminal></script>
            <script type="py" terminal></script>
            """,
            wait_for_pyscript=False,
            check_js_errors=False,
        )
        assert self.assert_banner_message("Unable to address multiple terminals")

        with pytest.raises(PageErrors) as exc:
            self.check_js_errors()
        assert "Unable to address multiple terminals" in str(exc.value)

    @only_worker
    def test_py_terminal_input(self):
        """
        Only worker py-terminal accepts an input
        """
        self.pyscript_run(
            """
            <script type="py" terminal></script>
            """,
            wait_for_pyscript=False,
        )
        self.page.get_by_text(">>> ", exact=True).wait_for()
        self.page.keyboard.type("'the answer is ' + str(6 * 7)")
        self.page.keyboard.press("Enter")
        self.page.get_by_text("the answer is 42").wait_for()

    def test_py_terminal(self):
        """
        1. <py-terminal> should redirect stdout and stderr to the DOM

        2. they also go to the console as usual
        """
        self.pyscript_run(
            """

Review comment (suggestion):
nitpick/suggestion: you can use `pytest.raises(..., match="...")`:
```
with pytest.raises(PageErrors, match="Unable to address multiple terminals"):
    self.check_js_errors()
```

I learnt this only recently, that's why I never used this pattern in the other tests :)

Code after:
import time

import pytest
from playwright.sync_api import expect

from .support import PageErrors, PyScriptTest, only_worker, skip_worker


class TestPyTerminal(PyScriptTest):
    def test_multiple_terminals(self):
        """
        Multiple terminals are not currently supported
        """
        self.pyscript_run(
            """
            <script type="py" terminal></script>
            <script type="py" terminal></script>
            """,
            wait_for_pyscript=False,
            check_js_errors=False,
        )
        assert self.assert_banner_message("You can use at most 1 terminal")

        with pytest.raises(PageErrors, match="You can use at most 1 terminal"):
            self.check_js_errors()

    @only_worker
    def test_py_terminal_input(self):
        """
        Only worker py-terminal accepts an input
        """
        self.pyscript_run(
            """
            <script type="py" terminal></script>
            """,
            wait_for_pyscript=False,
        )
        self.page.get_by_text(">>> ", exact=True).wait_for()
        self.page.keyboard.type("'the answer is ' + str(6 * 7)")
        self.page.keyboard.press("Enter")
        self.page.get_by_text("the answer is 42").wait_for()

    def test_py_terminal(self):
        """
        1. <py-terminal> should redirect stdout and stderr to the DOM

        2. they also go to the console as usual
        """
        self.pyscript_run(
            """
            <script type="py" terminal>