| import re |
| from playwright.sync_api import Page, expect |
| from conftest import wait_for_sync_idle |
|
|
| def test_sync_engine_stress(authenticated_page: Page, context): |
| """Scenario A, B, C, D: Rapid Typing, Offline Mode, Multi-tab, Refresh mid-sync.""" |
| page = authenticated_page |
| |
| editor = page.locator("#editor-container") |
| editor.wait_for(state="visible") |
| |
| |
| editor.click() |
| editor.focus() |
| editor.evaluate("el => { el.innerHTML = ''; el.dispatchEvent(new Event('input', {bubbles: true})); }") |
| |
| |
| editor.press_sequentially("Testing debounce sync engine... ", delay=10) |
| page.wait_for_timeout(500) |
| editor.focus() |
| editor.press_sequentially("Should not trigger save immediately. ", delay=10) |
| |
| |
| wait_for_sync_idle(page) |
| |
| |
| context.set_offline(True) |
| page.evaluate("window.dispatchEvent(new Event('offline'))") |
| |
| editor.focus() |
| editor.press_sequentially("This is typed while offline. ", delay=10) |
| |
| |
| save_btn = page.locator("#doc-save-btn") |
| expect(save_btn).to_have_attribute("title", re.compile(r"محفوظ محلياً|حفظ"), timeout=10000) |
| |
| context.set_offline(False) |
| page.evaluate("window.dispatchEvent(new Event('online'))") |
| |
| |
| wait_for_sync_idle(page) |
| |
| |
| editor.focus() |
| editor.press_sequentially("Typing and immediately refreshing! ", delay=10) |
| |
| page.reload() |
| page.wait_for_load_state("networkidle") |
| |
| |
| editor = page.locator("#editor-container") |
| editor.wait_for(state="visible") |
| expect(editor).to_contain_text("Typing and immediately refreshing!", timeout=10000) |
| |
| |
| page2 = context.new_page() |
| page2.goto("http://localhost:5050") |
| page2.wait_for_load_state("networkidle") |
| |
| |
| try: |
| guest_btn = page2.locator("#auth-guest-btn") |
| guest_btn.wait_for(state="visible", timeout=2000) |
| guest_btn.click(force=True) |
| except: |
| pass |
| |
| |
| page2.evaluate("if(typeof showPage==='function') showPage('editor')") |
| |
| page2_editor = page2.locator("#editor-container") |
| page2_editor.wait_for(state="visible") |
| |
| |
| page2_editor.click() |
| page2_editor.focus() |
| page2_editor.press_sequentially(" Tab 2 edit.", delay=10) |
| |
| wait_for_sync_idle(page2) |
| |
| |
| page.reload() |
| page.wait_for_load_state("networkidle") |
| |
| editor1 = page.locator("#editor-container") |
| editor1.wait_for(state="visible") |
| expect(editor1).to_contain_text("Tab 2 edit.", timeout=10000) |
| |
| page2.close() |
|
|