| 6 |
| 7 | import { expect, test, type Page } from '@playwright/test'; |
| 8 |
| 9 | test.skip(process.env.VITE_TW_POLICY === 'compat', 'the gesture gate belongs to the strict policy'); |
| 10 |
| 11 | const WIDGET = `<button id="b">open</button> |
| 12 | <script> |
| 13 | TelegramWidget.ready(); |
| 14 | setTimeout(function () { TelegramWidget.openLink('https://example.com/timer'); }, 10); |
| 15 | document.getElementById('b').addEventListener('click', function () { |
| 16 | TelegramWidget.openLink('https://example.com/clicked'); |
| 17 | TelegramWidget.openTelegramLink('https://t.me/second_in_the_same_gesture'); |
| 18 | setTimeout(function () { TelegramWidget.openLink('https://example.com/later'); }, 0); |
| 19 | }); |
| 20 | document.addEventListener('keydown', function () { TelegramWidget.openLink('https://example.com/key'); }); |
| 21 | setTimeout(function () { document.getElementById('b').click(); }, 50); |
| 22 | </script>`; |
| 23 |
| 24 | async function load(page: Page) { |
| 25 | await page.goto('/'); |
| 26 | await page.evaluate((html) => (window as any).__tw.load(html, null), WIDGET); |
| 27 | await page.waitForFunction(() => (window as any).__tw.state.loaded); |
| 28 | return page.frameLocator('#sandbox-wrap iframe'); |
| 29 | } |
| 30 | const calls = (page: Page) => page.evaluate(() => (window as any).__tw.state.bridge.map((b: any) => `${b.method}:${JSON.stringify(b.args)}`) as string[]); |
| 31 | const blocked = (page: Page) => page.evaluate(() => (window as any).__tw.state.diags['activation-blocked']?.count ?? 0); |
| 32 |
| 33 | test('timers and scripted clicks reach no privileged bridge method', async ({ page }) => { |
| 34 | await load(page); |
| 35 | await page.waitForTimeout(500); |
| 36 | expect(await calls(page)).toEqual(['ready:[]']); |
| 37 | expect(await blocked(page)).toBeGreaterThanOrEqual(3); |
| 38 | }); |
| 39 |
| 40 | test('one real click unlocks exactly one call, made in its own handler', async ({ page }) => { |
| 41 | const f = await load(page); |
| 42 | await page.waitForTimeout(300); |
| 43 | await f.locator('#b').click(); |
| 44 | await expect.poll(() => calls(page)).toContain('openLink:["https://example.com/clicked"]'); |
| 45 | await page.waitForTimeout(300); |
| 46 | expect(await calls(page)).toEqual(['ready:[]', 'openLink:["https://example.com/clicked"]']); |
| 47 | await f.locator('#b').click(); |
| 48 | await expect.poll(async () => (await calls(page)).length).toBe(3); |
| 49 | }); |
| 50 |
| 51 | test('a real key press reaches nothing: no key event, no Enter or Space click (widgets get no keyboard)', async ({ page }) => { |
| 52 | const f = await load(page); |
| 53 | await page.waitForTimeout(300); |
| 54 | const before = await blocked(page); |
| 55 | await f.locator('#b').press('Enter'); |
| 56 | await f.locator('#b').press(' '); |
| 57 | await f.locator('#b').press('a'); |
| 58 | await page.waitForTimeout(500); |
| 59 | // Neither the keydown handler nor the button's click handler ran, so nothing was even attempted. |
| 60 | expect(await calls(page)).toEqual(['ready:[]']); |
| 61 | expect(await blocked(page)).toBe(before); |
| 62 | }); |
| 63 |
| 64 | test('a hidden widget cannot use a gesture', async ({ page }) => { |
| 65 | const f = await load(page); |
| 66 | await page.evaluate(() => (window as any).__tw.setVisible(false)); |
| 67 | await f.locator('#b').click({ force: true }); |
| 68 | await page.waitForTimeout(500); |
| 69 | expect(await calls(page)).toEqual(['ready:[]']); |
| 70 | }); |
| 71 |