|
| 1 | +"""Regression coverage for the mobile swipe-to-open-sidebar gesture guard. |
| 2 | +
|
| 3 | +The mobile "swipe from the edge to open the sidebar" gesture lives in |
| 4 | +``_initChatSwipeToOpenSidebar`` in ``static/js/sidebar-layout.js``. It used to |
| 5 | +arm for any horizontal drag that started anywhere over the chat content, so |
| 6 | +dragging a finger to extend a text selection in a message (to copy it) was |
| 7 | +captured as an edge-swipe and yanked the drawer open. |
| 8 | +
|
| 9 | +These tests load the real browser module under Node, stub just enough of the |
| 10 | +DOM/BOM for the gesture handlers to run, dispatch synthetic ``touchstart`` / |
| 11 | +``touchmove`` events, and assert whether the drawer opened. They follow the same |
| 12 | +Node-subprocess pattern as ``test_markdown_rendering_js.py``. |
| 13 | +""" |
| 14 | + |
| 15 | +import json |
| 16 | +import shutil |
| 17 | +import subprocess |
| 18 | +import textwrap |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +_REPO = Path(__file__).resolve().parent.parent |
| 24 | +_HAS_NODE = shutil.which("node") is not None |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(scope="module") |
| 28 | +def node_available(): |
| 29 | + if not _HAS_NODE: |
| 30 | + pytest.skip("node binary not on PATH") |
| 31 | + |
| 32 | + |
| 33 | +# One synthetic gesture, driven entirely by a small scenario dict: |
| 34 | +# startX - clientX of the touchstart (px from the left edge) |
| 35 | +# moveX - clientX of the touchmove |
| 36 | +# selectionAtStart - is a text selection already active at touchstart? |
| 37 | +# selectionAtMove - does a selection become/stay active by the touchmove? |
| 38 | +# The harness reports whether the swipe opened the drawer, which side, whether |
| 39 | +# the sidebar stayed hidden, and how much text remained selected. |
| 40 | +_HARNESS = r""" |
| 41 | +import fs from 'node:fs'; |
| 42 | +
|
| 43 | +const S = JSON.parse(process.argv[1]); |
| 44 | +
|
| 45 | +// Records the listeners the module wires on `document`. |
| 46 | +const listeners = {}; |
| 47 | +
|
| 48 | +function classList(initial) { |
| 49 | + const set = new Set(initial || []); |
| 50 | + return { |
| 51 | + contains: (c) => set.has(c), |
| 52 | + add: (c) => set.add(c), |
| 53 | + remove: (c) => set.delete(c), |
| 54 | + }; |
| 55 | +} |
| 56 | +
|
| 57 | +const sidebarEl = { classList: classList(['hidden']) }; // drawer starts closed |
| 58 | +const chatContainerEl = { classList: classList([]) }; // not compare-active |
| 59 | +
|
| 60 | +// The selection text the page would report. Mutable so a scenario can make a |
| 61 | +// selection appear between touchstart and touchmove. |
| 62 | +let selectionText = S.selectionAtStart ? 'The quick brown fox jumps over the lazy dog.' : ''; |
| 63 | +
|
| 64 | +let opened = false; |
| 65 | +let openedSide = null; |
| 66 | +
|
| 67 | +globalThis.window = { |
| 68 | + innerWidth: 390, // mobile width (< 768 mobile gate) |
| 69 | + _chipDragging: false, |
| 70 | + getSelection: () => ({ |
| 71 | + isCollapsed: selectionText.length === 0, |
| 72 | + toString: () => selectionText, |
| 73 | + }), |
| 74 | + _odyOpenSidebar: (side) => { |
| 75 | + opened = true; |
| 76 | + openedSide = side; |
| 77 | + sidebarEl.classList.remove('hidden'); |
| 78 | + }, |
| 79 | +}; |
| 80 | +
|
| 81 | +globalThis.getComputedStyle = () => ({ display: 'block' }); |
| 82 | +
|
| 83 | +globalThis.document = { |
| 84 | + readyState: 'complete', // so the module auto-wires on import |
| 85 | + body: { classList: classList([]) }, |
| 86 | + addEventListener: (type, handler) => { |
| 87 | + (listeners[type] = listeners[type] || []).push(handler); |
| 88 | + }, |
| 89 | + getElementById: (id) => { |
| 90 | + if (id === 'sidebar') return sidebarEl; |
| 91 | + if (id === 'chat-container') return chatContainerEl; |
| 92 | + return null; |
| 93 | + }, |
| 94 | + querySelectorAll: () => [], // no open modals |
| 95 | +}; |
| 96 | +
|
| 97 | +// A touch that lands on a message body: inside #chat-container, but not inside |
| 98 | +// any of the EXCLUDE regions the gesture already ignores. |
| 99 | +const messageBody = { |
| 100 | + closest: (sel) => (sel === '#chat-container' ? chatContainerEl : null), |
| 101 | +}; |
| 102 | +
|
| 103 | +const source = fs.readFileSync('./static/js/sidebar-layout.js', 'utf8'); |
| 104 | +const moduleUrl = 'data:text/javascript;base64,' + Buffer.from(source).toString('base64'); |
| 105 | +await import(moduleUrl); |
| 106 | +
|
| 107 | +const fire = (type, ev) => (listeners[type] || []).forEach((h) => h(ev)); |
| 108 | +
|
| 109 | +fire('touchstart', { |
| 110 | + target: messageBody, |
| 111 | + touches: [{ clientX: S.startX, clientY: 300 }], |
| 112 | +}); |
| 113 | +
|
| 114 | +if (S.selectionAtMove) { |
| 115 | + selectionText = 'The quick brown fox jumps over the lazy dog.'; |
| 116 | +} |
| 117 | +
|
| 118 | +fire('touchmove', { |
| 119 | + cancelable: true, |
| 120 | + preventDefault() {}, |
| 121 | + touches: [{ clientX: S.moveX, clientY: 300 }], |
| 122 | +}); |
| 123 | +
|
| 124 | +console.log(JSON.stringify({ |
| 125 | + opened, |
| 126 | + openedSide, |
| 127 | + drawerHidden: sidebarEl.classList.contains('hidden'), |
| 128 | + selectionLen: (window.getSelection().toString() || '').length, |
| 129 | +})); |
| 130 | +""" |
| 131 | + |
| 132 | + |
| 133 | +def _run_gesture(**scenario): |
| 134 | + result = subprocess.run( |
| 135 | + ["node", "--input-type=module", "-e", textwrap.dedent(_HARNESS), json.dumps(scenario)], |
| 136 | + cwd=_REPO, |
| 137 | + capture_output=True, |
| 138 | + timeout=15, |
| 139 | + text=True, |
| 140 | + ) |
| 141 | + if result.returncode != 0: |
| 142 | + raise AssertionError(f"node failed:\nSTDERR:\n{result.stderr}\nSTDOUT:\n{result.stdout}") |
| 143 | + return json.loads(result.stdout.splitlines()[-1]) |
| 144 | + |
| 145 | + |
| 146 | +def test_drag_to_select_in_message_does_not_open_drawer(node_available): |
| 147 | + # The core bug: a horizontal drag that STARTS over a message body (to grow a |
| 148 | + # text selection) must not be captured as an edge-swipe. |
| 149 | + r = _run_gesture(startX=195, moveX=335, selectionAtStart=True, selectionAtMove=True) |
| 150 | + assert r["opened"] is False |
| 151 | + assert r["drawerHidden"] is True |
| 152 | + # The native selection is left untouched (the gesture never fought it). |
| 153 | + assert r["selectionLen"] > 0 |
| 154 | + |
| 155 | + |
| 156 | +def test_active_selection_blocks_even_an_edge_start_swipe(node_available): |
| 157 | + # Starting right at the left edge would normally arm the gesture, but a live |
| 158 | + # selection means the user is copying text — the selection guard must refuse. |
| 159 | + r = _run_gesture(startX=8, moveX=148, selectionAtStart=True, selectionAtMove=True) |
| 160 | + assert r["opened"] is False |
| 161 | + assert r["drawerHidden"] is True |
| 162 | + |
| 163 | + |
| 164 | +def test_selection_growing_mid_drag_bails_out(node_available): |
| 165 | + # No selection at touchstart (so the swipe arms), but a selection appears |
| 166 | + # during the drag: the touchmove guard must bail before opening. |
| 167 | + r = _run_gesture(startX=8, moveX=148, selectionAtStart=False, selectionAtMove=True) |
| 168 | + assert r["opened"] is False |
| 169 | + assert r["drawerHidden"] is True |
| 170 | + |
| 171 | + |
| 172 | +def test_genuine_left_edge_swipe_still_opens_drawer(node_available): |
| 173 | + # A real edge-swipe with no selection active must still open the drawer. |
| 174 | + r = _run_gesture(startX=8, moveX=160, selectionAtStart=False, selectionAtMove=False) |
| 175 | + assert r["opened"] is True |
| 176 | + assert r["drawerHidden"] is False |
| 177 | + assert r["openedSide"] == "left" |
| 178 | + |
| 179 | + |
| 180 | +def test_genuine_right_edge_swipe_still_opens_drawer(node_available): |
| 181 | + # The mirror gesture from the right edge is preserved too (both sides arm). |
| 182 | + r = _run_gesture(startX=382, moveX=230, selectionAtStart=False, selectionAtMove=False) |
| 183 | + assert r["opened"] is True |
| 184 | + assert r["openedSide"] == "right" |
0 commit comments