Repository: Textualize/textual

Issue:
Add a way to simulate the terminal resizing to Pilot
We could use a way of simulating the terminal sizing in tests.

The API should be something like the following:

```python
class Pilot:
    async def resize_terminal(self, width, height):
        ...
```

This should send the appropriate resize events to the App.


Discussion:

Resolving patch:
diff --git a/src/textual/pilot.py b/src/textual/pilot.py
--- a/src/textual/pilot.py
+++ b/src/textual/pilot.py
@@ -15,8 +15,9 @@
 
 from ._wait import wait_for_idle
 from .app import App, ReturnType
-from .events import Click, MouseDown, MouseEvent, MouseMove, MouseUp
-from .geometry import Offset
+from .drivers.headless_driver import HeadlessDriver
+from .events import Click, MouseDown, MouseEvent, MouseMove, MouseUp, Resize
+from .geometry import Offset, Size
 from .widget import Widget
 
 
@@ -81,6 +82,20 @@ async def press(self, *keys: str) -> None:
             await self._app._press_keys(keys)
             await self._wait_for_screen()
 
+    async def resize_terminal(self, width: int, height: int) -> None:
+        """Resize the terminal to the given dimensions.
+
+        Args:
+            width: The new width of the terminal.
+            height: The new height of the terminal.
+        """
+        size = Size(width, height)
+        # If we're running with the headless driver, update the inherent app size.
+        if isinstance(self.app._driver, HeadlessDriver):
+            self.app._driver._size = size
+        self.app.post_message(Resize(size, size))
+        await self.pause()
+
     async def mouse_down(
         self,
         selector: type[Widget] | str | None = None,


Test patch:
diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr
--- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr
+++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr
@@ -27131,6 +27131,105 @@
   
   '''
 # ---
+# name: test_pilot_resize_terminal
+  '''
+  <svg class="rich-terminal" viewBox="0 0 262 294.0" xmlns="http://www.w3.org/2000/svg">
+      <!-- Generated with Rich https://www.textualize.io -->
+      <style>
+  
+      @font-face {
+          font-family: "Fira Code";
+          src: local("FiraCode-Regular"),
+                  url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
+                  url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
+          font-style: normal;
+          font-weight: 400;
+      }
+      @font-face {
+          font-family: "Fira Code";
+          src: local("FiraCode-Bold"),
+                  url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
+                  url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
+          font-style: bold;
+          font-weight: 700;
+      }
+  
+      .terminal-2126838016-matrix {
+          font-family: Fira Code, monospace;
+          font-size: 20px;
+          line-height: 24.4px;
+          font-variant-east-asian: full-width;
+      }
+  
+      .terminal-2126838016-title {
+          font-size: 18px;
+          font-weight: bold;
+          font-family: arial;
+      }
+  
+      .terminal-2126838016-r1 { fill: #e1e1e1 }
+  .terminal-2126838016-r2 { fill: #c5c8c6 }
+      </style>
+  
+      <defs>
+      <clipPath id="terminal-2126838016-clip-terminal">
+        <rect x="0" y="0" width="243.0" height="243.0" />
+      </clipPath>
+      <clipPath id="terminal-2126838016-line-0">
+      <rect x="0" y="1.5" width="244" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-2126838016-line-1">
+      <rect x="0" y="25.9" width="244" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-2126838016-line-2">
+      <rect x="0" y="50.3" width="244" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-2126838016-line-3">
+      <rect x="0" y="74.7" width="244" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-2126838016-line-4">
+      <rect x="0" y="99.1" width="244" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-2126838016-line-5">
+      <rect x="0" y="123.5" width="244" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-2126838016-line-6">
+      <rect x="0" y="147.9" width="244" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-2126838016-line-7">
+      <rect x="0" y="172.3" width="244" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-2126838016-line-8">
+      <rect x="0" y="196.7" width="244" height="24.65"/>
+              </clipPath>
+      </defs>
+  
+      <rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="260" height="292" rx="8"/><text class="terminal-2126838016-title" fill="#c5c8c6" text-anchor="middle" x="130" y="27">SingleLabelApp</text>
+              <g transform="translate(26,22)">
+              <circle cx="0" cy="0" r="7" fill="#ff5f57"/>
+              <circle cx="22" cy="0" r="7" fill="#febc2e"/>
+              <circle cx="44" cy="0" r="7" fill="#28c840"/>
+              </g>
+          
+      <g transform="translate(9, 41)" clip-path="url(#terminal-2126838016-clip-terminal)">
+      <rect fill="#1e1e1e" x="0" y="1.5" width="244" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="25.9" width="244" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="50.3" width="244" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="74.7" width="244" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="99.1" width="244" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="123.5" width="244" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="147.9" width="244" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="172.3" width="244" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="196.7" width="244" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="221.1" width="244" height="24.65" shape-rendering="crispEdges"/>
+      <g class="terminal-2126838016-matrix">
+      <text class="terminal-2126838016-r1" x="0" y="20" textLength="244" clip-path="url(#terminal-2126838016-line-0)">12345678901234567890</text><text class="terminal-2126838016-r2" x="244" y="20" textLength="12.2" clip-path="url(#terminal-2126838016-line-0)">
+  </text><text class="terminal-2126838016-r1" x="0" y="44.4" textLength="244" clip-path="url(#terminal-2126838016-line-1)">12345678901234567890</text><text class="terminal-2126838016-r2" x="244" y="44.4" textLength="12.2" clip-path="url(#terminal-2126838016-line-1)">
+  </text><text class="terminal-2126838016-r1" x="0" y="68.8" textLength="244" clip-path="url(#terminal-2126838016-line-2)">12345678901234567890</text><text class="terminal-2126838016-r2" x="244" y="68.8" textLength="12.2" clip-path="url(#terminal-2126838016-line-2)">
+  </text><text class="terminal-2126838016-r1" x="0" y="93.2" textLength="244" clip-path="url(#terminal-2126838016-line-3)">12345678901234567890</text><text class="terminal-2126838016-r2" x="244" y="93.2" textLength="12.2" clip-path="url(#terminal-2126838016-line-3)">
+  </text><text class="terminal-2126838016-r1" x="0" y="117.6" textLength="244" clip-path="url(#terminal-2126838016-line-4)">12345678901234567890</text><text class="terminal-2126838016-r2" x="244" y="117.6" textLength="12.2" clip-path="url(#terminal-2126838016-line-4)">
+  </text><text class="terminal-2126838016-r1" x="0" y="142" textLength="244" clip-path="url(#terminal-2126838016-line-5)">12345678901234567890</text><text class="terminal-2126838016-r2" x="244" y="142" textLength="12.2" clip-path="url(#terminal-2126838016-line-5)">
+  </text><text class="terminal-2126838016-r1" x="0" y="166.4" textLength="244" clip-path="url(#terminal-2126838016-line-6)">12345678901234567890</text><text class="terminal-2126838016-r2" x="244" y="166.4" textLength="12.2" clip-path="url(#terminal-2126838016-line-6)">
+  </text><text class="terminal-2126838016-r1" x="0" y="190.8" textLength="244" clip-path="url(#terminal-2126838016-line-7)">12345678901234567890</text><text class="terminal-2126838016-r2" x="244" y="190.8" textLength="12.2" clip-path="url(#terminal-2126838016-line-7)">
+  </text><text class="terminal-2126838016-r1" x="0" y="215.2" textLength="244" clip-path="url(#terminal-2126838016-line-8)">12345678901234567890</text><text class="terminal-2126838016-r2" x="244" y="215.2" textLength="12.2" clip-path="url(#terminal-2126838016-line-8)">
+  </text><text class="terminal-2126838016-r1" x="0" y="239.6" textLength="244" clip-path="url(#terminal-2126838016-line-9)">12345678901234567890</text>
+      </g>
+      </g>
+  </svg>
+  
+  '''
+# ---
 # name: test_placeholder_render
   '''
   <svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
diff --git a/tests/snapshot_tests/snapshot_apps/pilot_resize_terminal.py b/tests/snapshot_tests/snapshot_apps/pilot_resize_terminal.py
new file mode 100644
--- /dev/null
+++ b/tests/snapshot_tests/snapshot_apps/pilot_resize_terminal.py
@@ -0,0 +1,13 @@
+from textual.app import App, ComposeResult
+from textual.widgets import Label
+
+
+class SingleLabelApp(App[None]):
+    """An app with a single label that's 20 x 10."""
+
+    def compose(self) -> ComposeResult:
+        yield Label(("12345678901234567890\n" * 10).strip())
+
+
+if __name__ == "__main__":
+    SingleLabelApp().run()
diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py
--- a/tests/snapshot_tests/test_snapshots.py
+++ b/tests/snapshot_tests/test_snapshots.py
@@ -583,6 +583,18 @@ def test_scrollbar_thumb_height(snap_compare):
     )
 
 
+def test_pilot_resize_terminal(snap_compare):
+    async def run_before(pilot):
+        await pilot.resize_terminal(35, 20)
+        await pilot.resize_terminal(20, 10)
+
+    assert snap_compare(
+        SNAPSHOT_APPS_DIR / "pilot_resize_terminal.py",
+        run_before=run_before,
+        terminal_size=(80, 25),
+    )
+
+
 def test_css_hot_reloading(snap_compare, monkeypatch):
     """Regression test for https://github.com/Textualize/textual/issues/2063."""
 
@@ -762,7 +774,9 @@ async def run_before(pilot) -> None:
         pilot.app.screen.query_one(Input).cursor_blink = False
         await pilot.app.screen.workers.wait_for_complete()
 
-    assert snap_compare(SNAPSHOT_APPS_DIR / "command_palette_discovery.py", run_before=run_before)
+    assert snap_compare(
+        SNAPSHOT_APPS_DIR / "command_palette_discovery.py", run_before=run_before
+    )
 
 
 # --- textual-dev library preview tests ---
diff --git a/tests/test_pilot.py b/tests/test_pilot.py
--- a/tests/test_pilot.py
+++ b/tests/test_pilot.py
@@ -6,7 +6,6 @@
 from textual.app import App, ComposeResult
 from textual.binding import Binding
 from textual.containers import Center, Middle
-from textual.events import MouseDown, MouseUp
 from textual.pilot import OutOfBounds
 from textual.widgets import Button, Label
 
@@ -352,3 +351,15 @@ async def test_pilot_target_screen_always_true(method, offset):
     async with app.run_test(size=(80, 24)) as pilot:
         pilot_method = getattr(pilot, method)
         assert (await pilot_method(offset=offset)) is True
+
+
+async def test_pilot_resize_terminal():
+    app = App()
+    async with app.run_test(size=(80, 24)) as pilot:
+        # Sanity checks.
+        assert app.size == (80, 24)
+        assert app.screen.size == (80, 24)
+        await pilot.resize_terminal(27, 15)
+        await pilot.pause()
+        assert app.size == (27, 15)
+        assert app.screen.size == (27, 15)