| 6 |
| import { expect, test, type Page } from '@playwright/test'; |
| 7 |
| 8 | async function open(page: Page, file: string) { |
| 9 | await page.goto(`/?file=compat/${file}`); |
| 10 | await page.waitForFunction(() => { const s = (window as any).__tw?.state; return s && (s.loaded || s.dead); }, null, { timeout: 60_000 }); |
| 11 | return page.frameLocator('#sandbox-wrap iframe'); |
| 12 | } |
| 13 | const tasks = (page: Page) => page.evaluate(() => (window as any).__tw.state.stats?.tasks as number); |
| 14 | const setVisible = (page: Page, v: boolean | null) => page.evaluate((v) => (window as any).__tw.setVisible(v), v); |
| 15 |
| 16 | test('hidden: animation frames stop; visible again: they resume', async ({ page }) => { |
| 17 | const f = await open(page, 'raf-animation.html'); |
| 18 | await page.waitForTimeout(500); |
| 19 | await setVisible(page, false); |
| 20 | await page.waitForTimeout(300); // let an in-flight frame land |
| 21 | const framesHidden = Number(await f.locator('#frames').textContent()); |
| 22 | const tasksHidden = await tasks(page); |
| 23 | expect(framesHidden).toBeGreaterThan(5); |
| 24 | await page.waitForTimeout(1200); |
| 25 | expect(Number(await f.locator('#frames').textContent())).toBe(framesHidden); |
| 26 | // Only the visibility task itself (and at most one straggler) may have run. |
| 27 | expect((await tasks(page)) - tasksHidden).toBeLessThanOrEqual(2); |
| 28 | const posHidden = await f.locator('#b1').evaluate((e) => (e as HTMLElement).style.transform); |
| 29 | await setVisible(page, true); |
| 30 | await page.waitForTimeout(600); |
| 31 | expect(Number(await f.locator('#frames').textContent())).toBeGreaterThan(framesHidden + 10); |
| 32 | expect(await f.locator('#b1').evaluate((e) => (e as HTMLElement).style.transform)).not.toBe(posHidden); |
| 33 | await expect(page.locator('#status')).toHaveText('● loaded'); |
| 34 | }); |
| 35 |
| 36 | test('hidden: a 1 s interval ticks at most once per second; visible: no ticks are lost', async ({ page }) => { |
| 37 | const f = await open(page, 'clock.html'); |
| 38 | await page.waitForTimeout(200); |
| 39 | await setVisible(page, false); |
| 40 | const t0 = Number((await f.locator('#ticks').textContent())!.split(' ')[0]); |
| 41 | await page.waitForTimeout(2600); |
| 42 | const t1 = Number((await f.locator('#ticks').textContent())!.split(' ')[0]); |
| 43 | expect(t1 - t0).toBeGreaterThanOrEqual(1); |
| 44 | expect(t1 - t0).toBeLessThanOrEqual(3); |
| 45 | await setVisible(page, true); |
| 46 | await page.waitForTimeout(2300); |
| 47 | const t2 = Number((await f.locator('#ticks').textContent())!.split(' ')[0]); |
| 48 | expect(t2 - t1).toBeGreaterThanOrEqual(2); |
| 49 | }); |
| 50 |
| 51 | test('the widget sees document.hidden and visibilitychange, like a background tab', async ({ page }) => { |
| 52 | await page.goto('/'); |
| 53 | await page.evaluate(() => (window as any).__tw.load(`<p id="s">?</p><p id="n">0</p><p id="a"></p><script>var n = 0, a = ''; function show() { document.getElementById('s').textContent = document.visibilityState + '/' + document.hidden; document.getElementById('n').textContent = ++n; } document.addEventListener('visibilitychange', show); show(); |
| 54 | TelegramWidget.onEvent('activated', function () { a += '+' + TelegramWidget.isActive; document.getElementById('a').textContent = a; }); |
| 55 | TelegramWidget.onEvent('deactivated', function () { a += '-' + TelegramWidget.isActive; document.getElementById('a').textContent = a; });</script>`, null)); |
| 56 | await page.waitForFunction(() => (window as any).__tw.state.loaded); |
| 57 | const f = page.frameLocator('#sandbox-wrap iframe'); |
| 58 | await expect(f.locator('#s')).toHaveText('visible/false'); |
| 59 | await setVisible(page, false); |
| 60 | await expect(f.locator('#s')).toHaveText('hidden/true'); |
| 61 | await expect(f.locator('#n')).toHaveText('2'); |
| 62 | await setVisible(page, true); |
| 63 | await expect(f.locator('#s')).toHaveText('visible/false'); |
| 64 | await expect(f.locator('#n')).toHaveText('3'); |
| 65 | await setVisible(page, false); |
| 66 | await setVisible(page, true); |
| 67 | await expect(f.locator('#n')).toHaveText('5'); |
| 68 | // SDK v1: isActive and activated / deactivated mirror the same state. |
| 69 | await expect(f.locator('#a')).toHaveText('-false+true-false+true'); |
| 70 | }); |
| 71 |
| 72 | test('hidden: the timers still run in the order of their delays (setTimeout 20, 10, 0 → 0, 10, 20)', async ({ page }) => { |
| 73 | await page.goto('/'); |
| 74 | await page.evaluate(() => (window as any).__tw.load(`<p id="o"></p><script>var o = []; document.addEventListener('visibilitychange', function () { if (!document.hidden) return; [20, 10, 0].forEach(function (d) { setTimeout(function () { o.push(d); document.getElementById('o').textContent = o.join(); }, d); }); });</script>`, null)); |
| 75 | await page.waitForFunction(() => (window as any).__tw.state.loaded); |
| 76 | await setVisible(page, false); |
| 77 | await page.waitForTimeout(1600); |
| 78 | await setVisible(page, true); |
| 79 | await expect(page.frameLocator('#sandbox-wrap iframe').locator('#o')).toHaveText('0,10,20'); |
| 80 | }); |
| 81 |
| 82 | // ───────── the triggers: the dev UI's IntersectionObserver and the page's own visibility ───────── |
| 83 |
| 84 | const frames = async (page: Page) => Number(await page.frameLocator('#sandbox-wrap iframe').locator('#frames').textContent()); |
| 85 |
| 86 | test('scrolling the bubble out of view pauses the widget; scrolling back resumes it', async ({ page }) => { |
| 87 | await open(page, 'raf-animation.html'); |
| 88 | await page.waitForTimeout(400); |
| 89 | expect(await frames(page)).toBeGreaterThan(5); |
| 90 | // Make the chat column tall and scroll the stage so the iframe leaves the viewport. |
| 91 | await page.evaluate(() => { |
| 92 | const spacer = document.createElement('div'); |
| 93 | spacer.id = 'spacer'; |
| 94 | spacer.style.height = '3000px'; |
| 95 | document.querySelector('#chat-sandbox')!.appendChild(spacer); |
| 96 | document.querySelector('.stage')!.scrollTop = 2500; |
| 97 | }); |
| 98 | await page.waitForTimeout(400); // observer callback + an in-flight frame |
| 99 | const atHidden = await frames(page); |
| 100 | await page.waitForTimeout(1000); |
| 101 | expect(await frames(page)).toBe(atHidden); |
| 102 | await page.evaluate(() => { document.querySelector('.stage')!.scrollTop = 0; }); |
| 103 | await page.waitForTimeout(600); |
| 104 | expect(await frames(page)).toBeGreaterThan(atHidden + 10); |
| 105 | }); |
| 106 |
| 107 | test('the page going hidden pauses the widget even while the bubble is in view', async ({ page }) => { |
| 108 | await open(page, 'raf-animation.html'); |
| 109 | await page.waitForTimeout(400); |
| 110 | // Playwright cannot background the tab; emulate what the browser does: document.hidden flips and visibilitychange fires. |
| 111 | await page.evaluate(() => { |
| 112 | Object.defineProperty(document, 'hidden', { configurable: true, get: () => true }); |
| 113 | document.dispatchEvent(new Event('visibilitychange')); |
| 114 | }); |
| 115 | await page.waitForTimeout(400); |
| 116 | const atHidden = await frames(page); |
| 117 | await page.waitForTimeout(1000); |
| 118 | expect(await frames(page)).toBe(atHidden); |
| 119 | await page.evaluate(() => { |
| 120 | delete (document as any).hidden; |
| 121 | document.dispatchEvent(new Event('visibilitychange')); |
| 122 | }); |
| 123 | await page.waitForTimeout(600); |
| 124 | expect(await frames(page)).toBeGreaterThan(atHidden + 10); |
| 125 | }); |
| 126 |