| 1 | import { test, expect } from './_fixtures'; |
| 2 | |
| 3 | // Wait for SW to claim the page |
| 4 | async function waitForSw(page: any) { |
| 5 | await page.evaluate(async () => { |
| 6 | if (!('serviceWorker' in navigator)) return; |
| 7 | await navigator.serviceWorker.ready; |
| 8 | const deadline = Date.now() + 10_000; |
| 9 | while (!navigator.serviceWorker.controller && Date.now() < deadline) |
| 10 | await new Promise(r => setTimeout(r, 50)); |
| 11 | }); |
| 12 | } |
| 13 | |
| 14 | // Wait for demo content to appear (supports both #demo-content and .demo-container) |
| 15 | async function waitForDemo(page: any) { |
| 16 | await expect(page.locator('#demo-content > *, .demo-container > *').first()).toBeVisible({ timeout: 15_000 }); |
| 17 | } |
| 18 | |
| 19 | // Inject a boosted link and click it to trigger morph navigation |
| 20 | async function morphViaLink(page: any, url: string) { |
| 21 | const id = 'test-morph-link-' + Math.random().toString(36).slice(2, 8); |
| 22 | await page.evaluate(({ url, id }) => { |
| 23 | const a = document.createElement('a'); |
| 24 | a.href = url; |
| 25 | a.textContent = 'Test nav'; |
| 26 | a.id = id; |
| 27 | a.style.cssText = 'position:fixed;top:0;left:0;z-index:9999;padding:10px;background:red;color:white'; |
| 28 | document.body.appendChild(a); |
| 29 | if ((window as any).htmx) (window as any).htmx.process(a); |
| 30 | }, { url, id }); |
| 31 | await page.click(`#${id}`); |
| 32 | await expect(page).toHaveURL(url, { timeout: 10_000 }); |
| 33 | } |
| 34 | |
| 35 | // Locate demo content in either #demo-content or .demo-container |
| 36 | function demo(page: any, selector: string) { |
| 37 | return page.locator(`#demo-content ${selector}, .demo-container ${selector}`); |
| 38 | } |
| 39 | |
| 40 | // Serial: SW state leaks between parallel workers |
| 41 | test.describe.serial('Pattern demo pages', () => { |
| 42 | test.setTimeout(30_000); |
| 43 | |
| 44 | // ============================================= |
| 45 | // Loading |
| 46 | // ============================================= |
| 47 | |
| 48 | test('click-to-load: renders and loads more', async ({ page }) => { |
| 49 | await page.goto('/patterns/loading/click-to-load'); |
| 50 | await waitForSw(page); |
| 51 | await waitForDemo(page); |
| 52 | |
| 53 | await expect(demo(page, '#comments')).toBeVisible(); |
| 54 | const initialCount = await demo(page, '#comments > div').count(); |
| 55 | expect(initialCount).toBeGreaterThan(0); |
| 56 | |
| 57 | const showMore = demo(page, 'button').filter({ hasText: /show more/i }); |
| 58 | if (await showMore.isVisible()) { |
| 59 | await showMore.click(); |
| 60 | await page.waitForTimeout(1000); |
| 61 | const newCount = await demo(page, '#comments > div').count(); |
| 62 | expect(newCount).toBeGreaterThan(initialCount); |
| 63 | } |
| 64 | }); |
| 65 | |
| 66 | test('click-to-load: works after morph navigation', async ({ page }) => { |
| 67 | await page.goto('/patterns/loading/infinite-scroll'); |
| 68 | await waitForSw(page); |
| 69 | await waitForDemo(page); |
| 70 | |
| 71 | await morphViaLink(page, '/patterns/loading/click-to-load'); |
| 72 | await waitForDemo(page); |
| 73 | |
| 74 | const showMore = demo(page, 'button').filter({ hasText: /show more/i }); |
| 75 | await expect(showMore).toBeVisible({ timeout: 15_000 }); |
| 76 | |
| 77 | const initialCount = await demo(page, '#comments > div').count(); |
| 78 | await showMore.click(); |
| 79 | await page.waitForTimeout(1500); |
| 80 | const newCount = await demo(page, '#comments > div').count(); |
| 81 | expect(newCount).toBeGreaterThan(initialCount); |
| 82 | }); |
| 83 | |
| 84 | test('infinite-scroll: renders table with rows', async ({ page }) => { |
| 85 | await page.goto('/patterns/loading/infinite-scroll'); |
| 86 | await waitForSw(page); |
| 87 | await waitForDemo(page); |
| 88 | |
| 89 | await expect(demo(page, 'table')).toBeVisible(); |
| 90 | const initialRows = await demo(page, 'tbody tr').count(); |
| 91 | expect(initialRows).toBeGreaterThan(0); |
| 92 | }); |
| 93 | |
| 94 | test('lazy-load: renders weather card after delay', async ({ page }) => { |
| 95 | await page.goto('/patterns/loading/lazy-load'); |
| 96 | await waitForSw(page); |
| 97 | await waitForDemo(page); |
| 98 | |
| 99 | await expect(page.locator('.demo-container', { hasText: '5-Day Forecast' })).toBeVisible({ timeout: 10_000 }); |
| 100 | }); |
| 101 | |
| 102 | test('lazy-load: works after morph navigation', async ({ page }) => { |
| 103 | await page.goto('/patterns/loading/click-to-load'); |
| 104 | await waitForSw(page); |
| 105 | await waitForDemo(page); |
| 106 | |
| 107 | await page.click('a[href="/patterns/loading/lazy-load"]'); |
| 108 | await expect(page).toHaveURL('/patterns/loading/lazy-load', { timeout: 10_000 }); |
| 109 | |
| 110 | await expect(page.locator('.demo-container', { hasText: '5-Day Forecast' })).toBeVisible({ timeout: 15_000 }); |
| 111 | }); |
| 112 | |
| 113 | test('progress-bar: renders and starts job', async ({ page }) => { |
| 114 | await page.goto('/patterns/loading/progress-bar'); |
| 115 | await waitForSw(page); |
| 116 | await waitForDemo(page); |
| 117 | |
| 118 | const startBtn = demo(page, 'button').filter({ hasText: /start/i }); |
| 119 | await expect(startBtn).toBeVisible(); |
| 120 | await startBtn.click(); |
| 121 | |
| 122 | // Progress element should appear |
| 123 | await expect(demo(page, '[role="progressbar"]').first()) |
| 124 | .toBeVisible({ timeout: 5_000 }); |
| 125 | }); |
| 126 | |
| 127 | // ============================================= |
| 128 | // Forms |
| 129 | // ============================================= |
| 130 | |
| 131 | test('active-search: renders and filters results', async ({ page }) => { |
| 132 | await page.goto('/patterns/forms/active-search'); |
| 133 | await waitForSw(page); |
| 134 | await waitForDemo(page); |
| 135 | |
| 136 | const input = demo(page, 'input[type="search"]'); |
| 137 | await expect(input).toBeVisible(); |
| 138 | |
| 139 | // Initial load should populate results |
| 140 | await expect(demo(page, 'table').first()).toBeVisible({ timeout: 5_000 }); |
| 141 | await expect(demo(page, 'tbody tr').first()).toBeVisible({ timeout: 5_000 }); |
| 142 | |
| 143 | // Type a search term and verify filtering |
| 144 | await input.fill('Venus'); |
| 145 | await page.waitForTimeout(1000); |
| 146 | const results = await demo(page, 'tbody tr').count(); |
| 147 | expect(results).toBeGreaterThan(0); |
| 148 | }); |
| 149 | |
| 150 | test('active-search: works after morph navigation', async ({ page }) => { |
| 151 | await page.goto('/patterns/loading/click-to-load'); |
| 152 | await waitForSw(page); |
| 153 | await waitForDemo(page); |
| 154 | |
| 155 | await morphViaLink(page, '/patterns/forms/active-search'); |
| 156 | await waitForDemo(page); |
| 157 | |
| 158 | const input = demo(page, 'input[type="search"]'); |
| 159 | await expect(input).toBeVisible({ timeout: 15_000 }); |
| 160 | |
| 161 | await expect(demo(page, 'tbody tr').first()).toBeVisible({ timeout: 5_000 }); |
| 162 | }); |
| 163 | |
| 164 | test('active-validation: renders and validates username', async ({ page }) => { |
| 165 | await page.goto('/patterns/forms/active-validation'); |
| 166 | await waitForSw(page); |
| 167 | await waitForDemo(page); |
| 168 | |
| 169 | const input = demo(page, 'input[name="username"]'); |
| 170 | await expect(input).toBeVisible(); |
| 171 | |
| 172 | // Type a taken username |
| 173 | await input.fill('admin'); |
| 174 | await page.waitForTimeout(600); |
| 175 | await expect(page.locator('text=already taken')).toBeVisible({ timeout: 3_000 }); |
| 176 | |
| 177 | // Type an available username |
| 178 | await input.fill('newuser123'); |
| 179 | await page.waitForTimeout(600); |
| 180 | await expect(page.locator('text=is available')).toBeVisible({ timeout: 3_000 }); |
| 181 | }); |
| 182 | |
| 183 | test('linked-selects: renders and updates models', async ({ page }) => { |
| 184 | await page.goto('/patterns/forms/linked-selects'); |
| 185 | await waitForSw(page); |
| 186 | await waitForDemo(page); |
| 187 | |
| 188 | const makeSelect = demo(page, 'select[name="make"]'); |
| 189 | await expect(makeSelect).toBeVisible(); |
| 190 | |
| 191 | // Change make and verify model options update |
| 192 | await makeSelect.selectOption('toyota'); |
| 193 | await page.waitForTimeout(1000); |
| 194 | const modelSelect = demo(page, 'select[name="model"], #models'); |
| 195 | const modelText = await modelSelect.textContent(); |
| 196 | expect(modelText).toContain('Tacoma'); |
| 197 | }); |
| 198 | |
| 199 | test('linked-selects: detail card updates on model change', async ({ page }) => { |
| 200 | await page.goto('/patterns/forms/linked-selects'); |
| 201 | await waitForSw(page); |
| 202 | await waitForDemo(page); |
| 203 | |
| 204 | // Initial detail card should show first Audi model |
| 205 | await expect(demo(page, '#detail').filter({ hasText: 'A4' })).toBeVisible({ timeout: 3_000 }); |
| 206 | |
| 207 | // Change model to Q5 and verify detail card updates |
| 208 | const modelSelect = demo(page, '#models'); |
| 209 | await expect(modelSelect).toBeVisible(); |
| 210 | await modelSelect.selectOption('Q5'); |
| 211 | await page.waitForTimeout(1000); |
| 212 | await expect(demo(page, '#detail').filter({ hasText: 'Q5' })).toBeVisible({ timeout: 3_000 }); |
| 213 | }); |
| 214 | |
| 215 | test('reset-on-submit: sends message and clears input', async ({ page }) => { |
| 216 | await page.goto('/patterns/forms/reset-on-submit'); |
| 217 | await waitForSw(page); |
| 218 | await waitForDemo(page); |
| 219 | |
| 220 | const input = demo(page, 'input[name="message"]'); |
| 221 | await expect(input).toBeVisible(); |
| 222 | |
| 223 | const sendBtn = demo(page, 'button').filter({ hasText: /send/i }); |
| 224 | await expect(sendBtn).toBeVisible(); |
| 225 | |
| 226 | // Send a message |
| 227 | await input.fill('Hello there'); |
| 228 | await sendBtn.click(); |
| 229 | await page.waitForTimeout(1000); |
| 230 | |
| 231 | // Message should appear in the chat |
| 232 | await expect(demo(page, '#messages').filter({ hasText: 'Hello there' })).toBeVisible({ timeout: 3_000 }); |
| 233 | |
| 234 | // AI reply should also appear |
| 235 | await expect(demo(page, '#messages').filter({ hasText: 'returning HTML' })).toBeVisible({ timeout: 3_000 }); |
| 236 | |
| 237 | // Input should be cleared (form reset) |
| 238 | await expect(input).toHaveValue('', { timeout: 3_000 }); |
| 239 | }); |
| 240 | |
| 241 | test('reset-on-submit: messages append in order', async ({ page }) => { |
| 242 | await page.goto('/patterns/forms/reset-on-submit'); |
| 243 | await waitForSw(page); |
| 244 | await waitForDemo(page); |
| 245 | |
| 246 | const input = demo(page, 'input[name="message"]'); |
| 247 | const sendBtn = demo(page, 'button').filter({ hasText: /send/i }); |
| 248 | |
| 249 | // Send two messages |
| 250 | await input.fill('First message'); |
| 251 | await sendBtn.click(); |
| 252 | await page.waitForTimeout(500); |
| 253 | |
| 254 | await input.fill('Second message'); |
| 255 | await sendBtn.click(); |
| 256 | await page.waitForTimeout(500); |
| 257 | |
| 258 | // 2 user messages + 2 AI replies = 4 top-level divs |
| 259 | const messages = demo(page, '#messages > div'); |
| 260 | await expect(messages).toHaveCount(4, { timeout: 3_000 }); |
| 261 | |
| 262 | // Messages should be in chronological order (beforeend) |
| 263 | await expect(messages.nth(0)).toContainText('First message'); |
| 264 | await expect(messages.nth(2)).toContainText('Second message'); |
| 265 | }); |
| 266 | |
| 267 | // ============================================= |
| 268 | // Records |
| 269 | // ============================================= |
| 270 | |
| 271 | test('bulk-actions: renders table with checkboxes', async ({ page }) => { |
| 272 | await page.goto('/patterns/records/bulk-actions'); |
| 273 | await waitForSw(page); |
| 274 | await waitForDemo(page); |
| 275 | |
| 276 | await expect(demo(page, 'table')).toBeVisible(); |
| 277 | const checkboxes = await demo(page, 'input[type="checkbox"]').count(); |
| 278 | expect(checkboxes).toBeGreaterThan(0); |
| 279 | }); |
| 280 | |
| 281 | test('bulk-actions: activate action works', async ({ page }) => { |
| 282 | await page.goto('/patterns/records/bulk-actions'); |
| 283 | await waitForSw(page); |
| 284 | await waitForDemo(page); |
| 285 | |
| 286 | // Check a checkbox and click Activate |
| 287 | const firstCheckbox = demo(page, 'input[type="checkbox"]').first(); |
| 288 | await firstCheckbox.check(); |
| 289 | |
| 290 | const activateBtn = demo(page, 'button').filter({ hasText: /^activate$/i }); |
| 291 | if (await activateBtn.isVisible()) { |
| 292 | await activateBtn.click(); |
| 293 | await page.waitForTimeout(1000); |
| 294 | } |
| 295 | }); |
| 296 | |
| 297 | // delete-in-place: disabled/unfinished — skipped |
| 298 | |
| 299 | test('drag-to-reorder: renders sortable list', async ({ page }) => { |
| 300 | await page.goto('/patterns/records/drag-to-reorder'); |
| 301 | await waitForSw(page); |
| 302 | await waitForDemo(page); |
| 303 | |
| 304 | // Should have draggable items (inputs or divs with content) |
| 305 | const items = await demo(page, 'input[name="item"], [data-sortable-item], .sortable > *').count(); |
| 306 | expect(items).toBeGreaterThan(0); |
| 307 | }); |
| 308 | |
| 309 | test('edit-in-place: renders and toggles edit mode', async ({ page }) => { |
| 310 | await page.goto('/patterns/records/edit-in-place'); |
| 311 | await waitForSw(page); |
| 312 | await waitForDemo(page); |
| 313 | |
| 314 | // Should show view mode with Edit button |
| 315 | const editBtn = page.locator('#demo-content button', { hasText: /edit/i }); |
| 316 | await expect(editBtn).toBeVisible(); |
| 317 | |
| 318 | // Click edit, verify form inputs appear |
| 319 | await editBtn.click(); |
| 320 | await expect(page.locator('#demo-content input[name="name"]')) |
| 321 | .toBeVisible({ timeout: 5_000 }); |
| 322 | |
| 323 | // Should have Save and Cancel buttons |
| 324 | await expect(page.locator('#demo-content button', { hasText: 'Save' })).toBeVisible({ timeout: 5_000 }); |
| 325 | await expect(page.locator('#demo-content button', { hasText: 'Cancel' })).toBeVisible({ timeout: 5_000 }); |
| 326 | }); |
| 327 | |
| 328 | test('edit-in-place: works after morph navigation', async ({ page }) => { |
| 329 | await page.goto('/patterns/records/bulk-actions'); |
| 330 | await waitForSw(page); |
| 331 | await waitForDemo(page); |
| 332 | |
| 333 | await morphViaLink(page, '/patterns/records/edit-in-place'); |
| 334 | await waitForDemo(page); |
| 335 | |
| 336 | const editBtn = page.locator('#demo-content button', { hasText: /edit/i }); |
| 337 | await expect(editBtn).toBeVisible({ timeout: 15_000 }); |
| 338 | |
| 339 | await editBtn.click(); |
| 340 | await expect(page.locator('#demo-content input[name="name"]')) |
| 341 | .toBeVisible({ timeout: 5_000 }); |
| 342 | }); |
| 343 | |
| 344 | // ============================================= |
| 345 | // Display |
| 346 | // ============================================= |
| 347 | |
| 348 | test('dialogs: renders button and opens modal', async ({ page }) => { |
| 349 | await page.goto('/patterns/display/dialogs'); |
| 350 | await waitForSw(page); |
| 351 | await waitForDemo(page); |
| 352 | |
| 353 | const openBtn = demo(page, 'button').filter({ hasText: /open.*modal/i }); |
| 354 | await expect(openBtn).toBeVisible(); |
| 355 | |
| 356 | // Click and verify modal appears |
| 357 | await openBtn.click(); |
| 358 | await expect(page.locator('#modal, [role="dialog"], .modal-underlay').first()) |
| 359 | .toBeVisible({ timeout: 5_000 }); |
| 360 | }); |
| 361 | |
| 362 | test('dialogs: works after morph navigation', async ({ page }) => { |
| 363 | await page.goto('/patterns/loading/click-to-load'); |
| 364 | await waitForSw(page); |
| 365 | await waitForDemo(page); |
| 366 | |
| 367 | await morphViaLink(page, '/patterns/display/dialogs'); |
| 368 | await waitForDemo(page); |
| 369 | |
| 370 | const openBtn = demo(page, 'button').filter({ hasText: /open.*modal/i }); |
| 371 | await expect(openBtn).toBeVisible({ timeout: 15_000 }); |
| 372 | }); |
| 373 | |
| 374 | // ============================================= |
| 375 | // Advanced |
| 376 | // ============================================= |
| 377 | |
| 378 | test('keyboard-shortcuts: renders and responds to click', async ({ page }) => { |
| 379 | await page.goto('/patterns/advanced/keyboard-shortcuts'); |
| 380 | await waitForSw(page); |
| 381 | await waitForDemo(page); |
| 382 | |
| 383 | const btn = demo(page, 'button').filter({ hasText: /do it/i }); |
| 384 | await expect(btn).toBeVisible(); |
| 385 | |
| 386 | // Click the button and verify response appears |
| 387 | await btn.click(); |
| 388 | await page.waitForTimeout(1000); |
| 389 | // Some result should appear after clicking |
| 390 | await expect(demo(page, '*').first()).toBeVisible(); |
| 391 | }); |
| 392 | }); |