| 1 | // Who gets a press (docs/sdk-v1.md §5.5, packages/core/src/shared/gesture-policy.ts): the dev app's bubble plays a |
| 2 | // phone client by default (a mouse arbitrates as a finger), so a hold on an `auto` element ends for the widget with |
| 3 | // pointercancel (the message menu) and a horizontal drag is swipe-to-reply, while `touch-action: none` gives the widget |
| 4 | // holds (`contextmenu`) and horizontal drags; a vertical drag is always the chat's (v0.1.3); a double tap is always the |
| // widget's (`dblclick`, on every element); the leading 20 px take no claim. `?gestures=desktop` plays a desktop |
| 6 | // client, `off` no bubble. Iframe and Shadow DOM renderer. |
| 7 | // Also the dev app's other agent tooling: the bubble-width control and the size row (zlib as the server counts it). |
| 8 |
| 9 | import { expect, test, type Locator, type Page } from '@playwright/test'; |
| 10 |
| 11 | const WIDGET = `<style>body{margin:0}.t{height:40px;margin:0 0 6px 40px;background:#ddd;font:12px sans-serif}</style> |
| 12 | <div class="t" id="auto">auto</div> |
| 13 | <div class="t" id="none" style="touch-action:none">none</div> |
| 14 | <div class="t" id="pany" style="touch-action:pan-y">pan-y</div> |
| 15 | <div class="t" id="panx" style="touch-action:pan-x">pan-x</div> |
| 16 | <div class="t" id="edge" style="touch-action:none;margin-left:0">none from the edge</div> |
| 17 | <p id="log"></p><p id="cs"></p> |
| 18 | <script> |
| 19 | var log = []; |
| 20 | document.getElementById('cs').textContent = ['auto', 'none', 'pany', 'panx'].map(function (id) { return getComputedStyle(document.getElementById(id)).touchAction; }).join(','); |
| 21 | ['pointerdown', 'pointerup', 'pointercancel', 'click', 'dblclick', 'contextmenu'].forEach(function (t) { |
| 22 | document.addEventListener(t, function (e) { log.push(e.target.id + ':' + t); document.getElementById('log').textContent = log.join(' '); }); |
| 23 | }); |
| 24 | </script>`; |
| 25 |
| 26 | async function load(page: Page, shadow: boolean, gestures?: 'desktop' | 'off'): Promise<Locator> { |
| 27 | const q = new URLSearchParams(); |
| 28 | if (shadow) q.set('shadow', '1'); |
| 29 | if (gestures) q.set('gestures', gestures); |
| 30 | await page.goto('/?' + q.toString()); |
| 31 | await page.evaluate((h) => (window as any).__tw.load(h, null), WIDGET); |
| 32 | await page.waitForFunction(() => (window as any).__tw.state.loaded); |
| 33 | await page.waitForTimeout(100); |
| 34 | return shadow ? page.locator('#sandbox-wrap .widget-shadow') : page.frameLocator('#sandbox-wrap iframe').locator(':root'); |
| 35 | } |
| 36 |
| 37 | /** What the widget logged since `from` entries, once it has settled. */ |
| 38 | async function events(root: Locator, from: number): Promise<string[]> { |
| 39 | let last = ''; |
| 40 | for (let i = 0; i < 20; i++) { |
| 41 | await root.page().waitForTimeout(100); |
| 42 | const now = (await root.locator('#log').textContent()) ?? ''; |
| 43 | if (now === last && i > 2) break; |
| 44 | last = now; |
| 45 | } |
| 46 | return last ? last.split(' ').slice(from) : []; |
| 47 | } |
| 48 | const count = async (root: Locator) => ((await root.locator('#log').textContent()) || '').split(' ').filter(Boolean).length; |
| 49 | const bubble = (page: Page) => page.evaluate(() => (window as any).__tw.state.bubbleGestures.map((g: any) => g.action + (g.edge ? '@edge' : ''))); |
| 50 | const claims = (page: Page) => page.evaluate(() => (window as any).__tw.state.gestures); |
| 51 |
| 52 | async function at(root: Locator, id: string, dx = 0.5): Promise<[number, number]> { |
| 53 | const b = (await root.locator('#' + id).boundingBox())!; |
| 54 | return [b.x + (dx < 1 ? b.width * dx : dx), b.y + b.height / 2]; |
| 55 | } |
| 56 | async function hold(page: Page, [x, y]: [number, number], ms = 700) { |
| 57 | await page.mouse.move(x, y); |
| 58 | await page.mouse.down(); |
| 59 | await page.waitForTimeout(ms); |
| 60 | await page.mouse.up(); |
| 61 | } |
| 62 | async function drag(page: Page, [x, y]: [number, number], dx: number, dy: number) { |
| 63 | await page.mouse.move(x, y); |
| 64 | await page.mouse.down(); |
| 65 | await page.mouse.move(x + dx, y + dy, { steps: 8 }); |
| 66 | await page.mouse.up(); |
| 67 | } |
| 68 |
| 69 | for (const shadow of [false, true]) { |
| 70 | const r = shadow ? 'shadow' : 'iframe'; |
| 71 |
| 72 | test(`${r}: the claim of each press comes from touch-action, the hold only from none`, async ({ page }) => { |
| 73 | const root = await load(page, shadow); |
| 74 | for (const id of ['auto', 'none', 'pany']) await root.locator('#' + id).click(); |
| 75 | await expect.poll(async () => (await claims(page)).length).toBe(3); |
| 76 | expect(await claims(page)).toEqual([ |
| 77 | { x: false, y: false, hold: false }, |
| 78 | { x: true, y: false, hold: true }, |
| 79 | { x: true, y: false, hold: false }, |
| 80 | ]); |
| 81 | }); |
| 82 |
| 83 | test(`${r}: a vertical drag is the chat's on none and pan-x too; a drag that starts sideways on none is the widget's on both axes`, async ({ page }) => { |
| 84 | const root = await load(page, shadow); |
| 85 | await drag(page, await at(root, 'none', 20), 2, 30); |
| 86 | expect(await events(root, 0)).toEqual(['none:pointerdown', 'none:pointercancel']); |
| 87 | await drag(page, await at(root, 'panx', 20), 2, 30); |
| 88 | expect(await events(root, 2)).toEqual(['panx:pointerdown', 'panx:pointercancel']); |
| 89 | // pan-x claims nothing: a horizontal drag there is swipe-to-reply. |
| 90 | await drag(page, await at(root, 'panx', 20), 120, 4); |
| 91 | expect(await events(root, 4)).toEqual(['panx:pointerdown', 'panx:pointercancel']); |
| 92 | // The axis locks at the first move past the slop: sideways first, then down, stays the widget's. |
| 93 | const [x, y] = await at(root, 'none', 20); |
| 94 | await page.mouse.move(x, y); |
| 95 | await page.mouse.down(); |
| 96 | await page.mouse.move(x + 30, y, { steps: 4 }); |
| 97 | await page.mouse.move(x + 40, y + 16, { steps: 6 }); |
| 98 | await page.mouse.up(); |
| 99 | expect(await events(root, 6)).toEqual(['none:pointerdown', 'none:pointerup']); |
| 100 | expect(await bubble(page)).toEqual(['scroll', 'scroll', 'swipe']); |
| 101 | expect(await claims(page)).toContainEqual({ x: false, y: false, hold: false }); |
| 102 | }); |
| 103 |
| 104 | test(`${r}: the engine always allows vertical panning (none → pan-y), the widget and the claim see its own touch-action`, async ({ page }) => { |
| 105 | const root = await load(page, shadow); |
| 106 | const engine = await root.locator('#none').evaluate((el) => { |
| 107 | const r = el.getRootNode() as Document | ShadowRoot; |
| 108 | return ['auto', 'none', 'pany', 'panx'].map((id) => getComputedStyle(r.getElementById(id)!).touchAction).join(','); |
| 109 | }); |
| 110 | expect(engine).toBe('auto,pan-y,pan-y,pan-x pan-y'); |
| 111 | await expect(root.locator('#cs')).toHaveText('auto,none,pan-y,pan-x'); |
| 112 | }); |
| 113 |
| 114 | test(`${r}: a hold on auto is the message menu: the widget gets pointercancel, no pointerup, no click`, async ({ page }) => { |
| 115 | const root = await load(page, shadow); |
| 116 | await hold(page, await at(root, 'auto')); |
| 117 | expect(await events(root, 0)).toEqual(['auto:pointerdown', 'auto:pointercancel']); |
| 118 | expect(await bubble(page)).toEqual(['menu']); |
| 119 | // A tap still is the widget's. |
| 120 | await root.locator('#auto').click(); |
| 121 | expect(await events(root, 2)).toEqual(['auto:pointerdown', 'auto:pointerup', 'auto:click']); |
| 122 | }); |
| 123 |
| 124 | test(`${r}: a hold on touch-action: none is the widget's, with its own contextmenu`, async ({ page }) => { |
| 125 | const root = await load(page, shadow); |
| 126 | await hold(page, await at(root, 'none')); |
| 127 | const ev = await events(root, 0); |
| 128 | // A finger makes no click after a hold: the phone mode drops the mouse's. |
| 129 | expect(ev).toEqual(['none:pointerdown', 'none:contextmenu', 'none:pointerup']); |
| 130 | expect(await bubble(page)).toEqual([]); |
| 131 | }); |
| 132 |
| 133 | test(`${r}: a double tap on auto is the widget's too: both clicks and one dblclick, no reaction`, async ({ page }) => { |
| 134 | const root = await load(page, shadow); |
| 135 | const [x, y] = await at(root, 'auto'); |
| 136 | await page.mouse.dblclick(x, y); |
| 137 | expect(await events(root, 0)).toEqual(['auto:pointerdown', 'auto:pointerup', 'auto:click', 'auto:pointerdown', 'auto:pointerup', 'auto:click', 'auto:dblclick']); |
| 138 | expect(await bubble(page)).toEqual([]); |
| 139 | }); |
| 140 |
| 141 | test(`${r}: a double tap on touch-action: none gets both clicks and one dblclick`, async ({ page }) => { |
| 142 | const root = await load(page, shadow); |
| 143 | const [x, y] = await at(root, 'none'); |
| 144 | await page.mouse.dblclick(x, y); |
| 145 | expect(await events(root, 0)).toEqual(['none:pointerdown', 'none:pointerup', 'none:click', 'none:pointerdown', 'none:pointerup', 'none:click', 'none:dblclick']); |
| 146 | expect(await bubble(page)).toEqual([]); |
| 147 | }); |
| 148 |
| 149 | test(`${r}: a horizontal drag on auto is swipe-to-reply, on pan-y the widget's; a vertical one on pan-y scrolls`, async ({ page }) => { |
| 150 | const root = await load(page, shadow); |
| 151 | await drag(page, await at(root, 'auto', 20), 120, 4); |
| 152 | expect(await events(root, 0)).toEqual(['auto:pointerdown', 'auto:pointercancel']); |
| 153 | await drag(page, await at(root, 'pany', 20), 120, 4); |
| 154 | expect(await events(root, 2)).toEqual(['pany:pointerdown', 'pany:pointerup']); |
| 155 | await drag(page, await at(root, 'pany', 20), 2, 30); |
| 156 | expect(await events(root, 4)).toEqual(['pany:pointerdown', 'pany:pointercancel']); |
| 157 | expect(await bubble(page)).toEqual(['swipe', 'scroll']); |
| 158 | }); |
| 159 |
| 160 | test(`${r}: the leading 20 px take no claim: a drag there on none is the back gesture`, async ({ page }) => { |
| 161 | const root = await load(page, shadow); |
| 162 | await drag(page, await at(root, 'edge', 6), 120, 0); |
| 163 | expect(await events(root, 0)).toEqual(['edge:pointerdown', 'edge:pointercancel']); |
| 164 | expect(await bubble(page)).toEqual(['back@edge']); |
| 165 | await drag(page, await at(root, 'edge', 30), 120, 0); |
| 166 | expect(await events(root, 2)).toEqual(['edge:pointerdown', 'edge:pointerup']); |
| 167 | }); |
| 168 |
| 169 | test(`${r}: desktop: a right click is the menu on auto and contextmenu on none; holds, drags and double clicks are the widget's`, async ({ page }) => { |
| 170 | const root = await load(page, shadow, 'desktop'); |
| 171 | const [ax, ay] = await at(root, 'auto'); |
| 172 | await page.mouse.click(ax, ay, { button: 'right' }); |
| 173 | expect(await events(root, 0)).toEqual(['auto:pointerdown', 'auto:pointercancel']); |
| 174 | const [nx, ny] = await at(root, 'none'); |
| 175 | await page.mouse.click(nx, ny, { button: 'right' }); |
| 176 | expect(await events(root, 2)).toEqual(['none:pointerdown', 'none:contextmenu', 'none:pointerup']); |
| 177 | await hold(page, [ax, ay]); |
| 178 | expect(await events(root, 5)).toEqual(['auto:pointerdown', 'auto:pointerup', 'auto:click']); |
| 179 | await drag(page, await at(root, 'auto', 20), 120, 4); |
| 180 | expect(await events(root, 8)).toEqual(['auto:pointerdown', 'auto:pointerup', 'auto:click']); |
| 181 | await page.waitForTimeout(350); |
| 182 | await page.mouse.dblclick(ax, ay); |
| 183 | expect(await events(root, 11)).toEqual(['auto:pointerdown', 'auto:pointerup', 'auto:click', 'auto:pointerdown', 'auto:pointerup', 'auto:click', 'auto:dblclick']); |
| 184 | expect(await bubble(page)).toEqual(['menu']); |
| 185 | }); |
| 186 |
| 187 | test(`${r}: off: no bubble, the widget gets every press`, async ({ page }) => { |
| 188 | const root = await load(page, shadow, 'off'); |
| 189 | await hold(page, await at(root, 'auto')); |
| 190 | expect(await events(root, 0)).toEqual(['auto:pointerdown', 'auto:pointerup', 'auto:click']); |
| 191 | expect(await bubble(page)).toEqual([]); |
| 192 | }); |
| 193 | } |
| 194 |
| 195 | for (const shadow of [false, true]) { |
| 196 | test(`${shadow ? 'shadow' : 'iframe'}: touch-action on a container covers its children, as in CSS`, async ({ page }) => { |
| 197 | await page.goto(shadow ? '/?shadow=1' : '/'); |
| 198 | await page.evaluate(() => (window as any).__tw.load(`<style>body{margin:0}#pad{touch-action:none;padding:10px 10px 10px 40px}#t{height:60px;background:#ddd}</style><div id="pad"><div id="t"></div></div><p id="log"></p><script> |
| 199 | var log = []; |
| 200 | ['pointerdown', 'pointerup', 'pointercancel', 'contextmenu'].forEach(function (t) { document.getElementById('t').addEventListener(t, function () { log.push(t); document.getElementById('log').textContent = log.join(' '); }); }); |
| 201 | </script>`, null)); |
| 202 | await page.waitForFunction(() => (window as any).__tw.state.loaded); |
| 203 | await page.waitForTimeout(200); |
| 204 | const root = shadow ? page.locator('#sandbox-wrap .widget-shadow') : page.frameLocator('#sandbox-wrap iframe').locator(':root'); |
| 205 | // #t says nothing itself: its container's touch-action: none claims the hold. |
| 206 | await root.locator('#t').click(); |
| 207 | await expect.poll(async () => (await claims(page)).length).toBe(1); |
| 208 | expect(await claims(page)).toEqual([{ x: true, y: false, hold: true }]); |
| 209 | await page.waitForTimeout(400); |
| 210 | await hold(page, await at(root, 't')); |
| 211 | await expect(root.locator('#log')).toHaveText('pointerdown pointerup pointerdown contextmenu pointerup'); |
| 212 | }); |
| 213 | } |
| 214 |
| 215 | for (const [mode, type] of [['phone', 'touch'], ['desktop', 'mouse']] as const) { |
| 216 | test(`${mode}: the widget sees the press as pointerType ${type}, a hover move stays the mouse's`, async ({ page }) => { |
| 217 | await page.goto(mode === 'phone' ? '/' : '/?gestures=desktop'); |
| 218 | await page.evaluate(() => (window as any).__tw.load(`<div id="t" style="height:60px;margin-left:40px;background:#ddd"></div><p id="log"></p><script> |
| 219 | var log = []; |
| 220 | ['pointerdown', 'pointermove', 'pointerup', 'click'].forEach(function (t) { document.addEventListener(t, function (e) { if (t !== 'pointermove' || !log.length || log[log.length - 1].indexOf('pointermove') < 0) log.push(t + ':' + e.pointerType); document.getElementById('log').textContent = log.join(' '); }); }); |
| 221 | </script>`, null)); |
| 222 | await page.waitForFunction(() => (window as any).__tw.state.loaded); |
| 223 | const root = page.frameLocator('#sandbox-wrap iframe').locator(':root'); |
| 224 | const b = (await root.locator('#t').boundingBox())!; |
| 225 | await page.mouse.move(b.x + 100, b.y + 30, { steps: 3 }); |
| 226 | await page.waitForTimeout(150); |
| 227 | await page.mouse.click(b.x + 100, b.y + 30); |
| 228 | await expect(root.locator('#log')).toHaveText(`pointermove:mouse pointerdown:${type} pointerup:${type} click:${type}`); |
| 229 | }); |
| 230 | } |
| 231 |
| 232 | test('the width control lays the bubble out at a preset or a free width', async ({ page }) => { |
| 233 | await page.goto('/?width=360'); |
| 234 | await page.evaluate(() => (window as any).__tw.load('<div id="w"></div><script>function w() { document.getElementById("w").textContent = innerWidth; } w(); addEventListener("resize", w);</script>', null)); |
| 235 | await page.waitForFunction(() => (window as any).__tw.state.loaded); |
| 236 | const frame = page.frameLocator('#sandbox-wrap iframe'); |
| 237 | await expect(frame.locator('#w')).toHaveText('360'); |
| 238 | await page.evaluate(() => (window as any).__tw.setWidth(200)); |
| 239 | await expect(frame.locator('#w')).toHaveText('200'); |
| 240 | expect(new URL(page.url()).searchParams.get('width')).toBe('200'); |
| 241 | await page.selectOption('#width', 'free'); |
| 242 | expect(new URL(page.url()).searchParams.get('width')).toBe('free'); |
| 243 | await expect(page.locator('#chat-sandbox')).toHaveCSS('resize', 'horizontal'); |
| 244 | }); |
| 245 |
| 246 | test('the stats panel shows the size as the server counts it, with a warning and an error past the limits', async ({ page }) => { |
| 247 | await page.goto('/'); |
| 248 | const load = async (html: string) => { |
| 249 | await page.evaluate((h) => (window as any).__tw.load(h, null), html); |
| 250 | await page.waitForFunction(() => (window as any).__tw.state.size); |
| 251 | return page.evaluate(() => (window as any).__tw.state.size as { zlib: number; raw: number }); |
| 252 | }; |
| 253 | // Incompressible text: a deterministic pseudo-random string of n characters (xorshift32; 5150 → 3888 B, 6000 → 4525 B). |
| 254 | const noise = (n: number) => { let x = 2463534242, t = ''; for (let i = 0; i < n; i++) { x ^= x << 13; x >>>= 0; x ^= x >>> 17; x ^= x << 5; x >>>= 0; t += 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'[(x >>> 8) % 62]; } return t; }; |
| 255 | const small = await load('<p>hello</p>'); |
| 256 | expect(small.raw).toBe(12); |
| 257 | expect(small.zlib).toBeGreaterThan(10); |
| 258 | await expect(page.locator('#stats tr.size-ok')).toContainText('B zlib / 0.0 KB raw (limit 4096 / 32768)'); |
| 259 | const near = await load('<!--' + noise(5150) + '-->'); |
| 260 | expect(near.zlib).toBeGreaterThan(3800); |
| 261 | expect(near.zlib).toBeLessThanOrEqual(4096); |
| 262 | await expect(page.locator('#stats tr.size-warning')).toContainText('aim for 3800 B or less'); |
| 263 | await load('<!--' + noise(6000) + '-->'); |
| 264 | await expect(page.locator('#stats tr.size-error')).toContainText('INPUT_MEDIA_WIDGET_TOO_LARGE'); |
| 265 | }); |
| 266 |