| 1 | import { test, expect } from './_fixtures'; |
| 2 | |
| 3 | // Helper to open search modal (hyperscript-driven, so we call showModal directly) |
| 4 | async function openSearch(page: any) { |
| 5 | await page.evaluate(() => { |
| 6 | const dialog = document.querySelector('dialog#search-modal') as HTMLDialogElement; |
| 7 | dialog?.showModal(); |
| 8 | }); |
| 9 | await expect(page.locator('dialog#search-modal')).toBeVisible(); |
| 10 | } |
| 11 | |
| 12 | test.describe('Search', () => { |
| 13 | test('search modal can be opened', async ({ page }) => { |
| 14 | await page.goto('/'); |
| 15 | const dialog = page.locator('dialog#search-modal'); |
| 16 | await expect(dialog).not.toBeVisible(); |
| 17 | |
| 18 | await openSearch(page); |
| 19 | await expect(dialog).toBeVisible(); |
| 20 | }); |
| 21 | |
| 22 | test('search modal can be closed with Escape', async ({ page }) => { |
| 23 | await page.goto('/'); |
| 24 | await openSearch(page); |
| 25 | |
| 26 | await page.keyboard.press('Escape'); |
| 27 | await expect(page.locator('dialog#search-modal')).not.toBeVisible(); |
| 28 | }); |
| 29 | |
| 30 | test('search input exists and is interactive', async ({ page }) => { |
| 31 | await page.goto('/'); |
| 32 | await openSearch(page); |
| 33 | |
| 34 | const input = page.locator('#search-input'); |
| 35 | await expect(input).toBeVisible(); |
| 36 | await input.fill('hx-get'); |
| 37 | await expect(input).toHaveValue('hx-get'); |
| 38 | }); |
| 39 | |
| 40 | test('search results container exists', async ({ page }) => { |
| 41 | await page.goto('/'); |
| 42 | await openSearch(page); |
| 43 | await expect(page.locator('#search-results')).toBeAttached(); |
| 44 | }); |
| 45 | |
| 46 | test('search dialog has correct structure', async ({ page }) => { |
| 47 | await page.goto('/'); |
| 48 | const dialog = page.locator('dialog#search-modal'); |
| 49 | await expect(dialog).toHaveAttribute('role', 'alertdialog'); |
| 50 | await expect(dialog).toHaveAttribute('aria-modal', 'true'); |
| 51 | await expect(dialog).toHaveAttribute('aria-label', 'Search documentation'); |
| 52 | }); |
| 53 | |
| 54 | test('search-index.json is valid and populated', async ({ request }) => { |
| 55 | const response = await request.get('/search-index.json'); |
| 56 | expect(response.status()).toBe(200); |
| 57 | const data = await response.json(); |
| 58 | expect(data.results.length).toBeGreaterThan(50); |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | /** |
| 63 | * Search ranking tests — verifies the first result for common queries. |
| 64 | * |
| 65 | * Each entry: [queries, expected title of the first result] |
| 66 | * - queries can be a single string or an array of strings |
| 67 | * - each query in the array generates a separate test |
| 68 | * |
| 69 | * We assert on the TITLE (what the user sees), not the URL. This means: |
| 70 | * - Pages can be moved/reorganized without breaking tests |
| 71 | * - Tests read naturally: "searching X should show X first" |
| 72 | * - Case matters: "HX-Trigger" (header) vs "hx-trigger" (attribute) |
| 73 | */ |
| 74 | const SEARCH_RANKING: [string | string[], string][] = [ |
| 75 | // ── Attributes (lowercase hx-*) ── |
| 76 | ['hx-get', 'hx-get'], |
| 77 | ['hx-post', 'hx-post'], |
| 78 | ['hx-swap', 'hx-swap'], |
| 79 | ['hx-target', 'hx-target'], |
| 80 | ['hx-trigger', 'hx-trigger'], |
| 81 | ['hx-boost', 'hx-boost'], |
| 82 | ['hx-push-url', 'hx-push-url'], |
| 83 | ['hx-select', 'hx-select'], |
| 84 | ['hx-swap-oob', 'hx-swap-oob'], |
| 85 | ['hx-on', 'hx-on'], |
| 86 | ['hx-include', 'hx-include'], |
| 87 | ['hx-vals', 'hx-vals'], |
| 88 | ['hx-confirm', 'hx-confirm'], |
| 89 | ['hx-indicator', 'hx-indicator'], |
| 90 | ['hx-sync', 'hx-sync'], |
| 91 | ['hx-headers', 'hx-headers'], |
| 92 | ['hx-delete', 'hx-delete'], |
| 93 | ['hx-preserve', 'hx-preserve'], |
| 94 | |
| 95 | // ── Headers (uppercase HX-*) — must beat same-name attributes ── |
| 96 | ['headers', 'Headers'], |
| 97 | ['HX-Trigger', 'HX-Trigger'], |
| 98 | ['HX-Target', 'HX-Target'], |
| 99 | ['HX-Redirect', 'HX-Redirect'], |
| 100 | ['HX-Location', 'HX-Location'], |
| 101 | ['HX-Request', 'HX-Request'], |
| 102 | ['HX-Refresh', 'HX-Refresh'], |
| 103 | ['HX-Boosted', 'HX-Boosted'], |
| 104 | |
| 105 | // ── Reference index pages ── |
| 106 | ['attributes', 'Attributes'], |
| 107 | ['events', 'Events'], |
| 108 | |
| 109 | // ── Docs (with keyword aliases) ── |
| 110 | [['installation', 'install', 'cdn', 'npm', 'getting started', 'quick start'], 'Installation'], |
| 111 | [['boosting', 'boost', 'progressive enhancement'], 'Boosting'], |
| 112 | [['history', 'back button', 'pushState'], 'History'], |
| 113 | ['security', 'Security'], |
| 114 | ['extensions', 'Extensions'], |
| 115 | [['websockets', 'ws'], 'hx-ws'], |
| 116 | [['server-sent events', 'event stream', 'streaming'], 'hx-sse'], |
| 117 | ['validation', 'Validation'], |
| 118 | ['inheritance', 'Attribute Inheritance'], |
| 119 | [['synchronization', 'sync', 'debounce', 'throttle', 'race condition'], 'Synchronization'], |
| 120 | [['CSS Transitions', 'fade'], 'CSS Transitions'], |
| 121 | [['oob', 'out of band'], 'Multi-Target Updates'], |
| 122 | [['javascript', 'scripting', 'hyperscript'], 'Client-Side Scripting'], |
| 123 | ['alpine', 'hx-alpine-compat'], |
| 124 | ['caching', 'Caching'], |
| 125 | ['cache', 'hx-history-cache'], |
| 126 | ['etag', 'hx-ptag'], |
| 127 | [['configuration', 'settings', 'meta tag'], 'Configuration'], |
| 128 | ['config', 'Config'], |
| 129 | [['debugging', 'debug', 'devtools', 'logAll'], 'Debugging'], |
| 130 | ['troubleshoot', 'Troubleshoot'], |
| 131 | ['XHR', 'Requests & Responses'], |
| 132 | |
| 133 | // ── Patterns ── |
| 134 | ['infinite scroll', 'Infinite Scroll'], |
| 135 | ['lazy load', 'Lazy Load'], |
| 136 | ['active search', 'Active Search'], |
| 137 | ['file upload', 'File Upload'], |
| 138 | ['delete in place', 'Delete in Place'], |
| 139 | ['dialogs', 'Dialogs'], |
| 140 | ['tabs', 'Tabs'], |
| 141 | ['polling', 'Polling'], |
| 142 | |
| 143 | // ── Methods ── |
| 144 | ['htmx.ajax', 'htmx.ajax()'], |
| 145 | |
| 146 | // ── Essays ── |
| 147 | ['HATEOAS', 'HATEOAS'], |
| 148 | ['locality of behaviour', 'Locality of Behaviour (LoB)'], |
| 149 | ]; |
| 150 | |
| 151 | test.describe('Search ranking', () => { |
| 152 | test('first result matches expected title for each query', async ({ page }) => { |
| 153 | await page.goto('/', { waitUntil: 'networkidle' }); |
| 154 | await openSearch(page); |
| 155 | |
| 156 | await page.evaluate(() => customElements.whenDefined('search-index')); |
| 157 | await page.evaluate(() => |
| 158 | (document.querySelector('search-index') as any)?.load() |
| 159 | ); |
| 160 | |
| 161 | const input = page.locator('#search-input'); |
| 162 | const firstResult = page.locator('.result').first(); |
| 163 | |
| 164 | for (const [queries, expectedTitle] of SEARCH_RANKING) { |
| 165 | for (const query of Array.isArray(queries) ? queries : [queries]) { |
| 166 | await test.step(`"${query}" → ${expectedTitle}`, async () => { |
| 167 | await input.fill(''); |
| 168 | await input.fill(query); |
| 169 | await expect(firstResult).toBeAttached({ timeout: 2000 }); |
| 170 | |
| 171 | const titleEl = firstResult.locator('~ article .font-chicago'); |
| 172 | await expect(titleEl).toHaveText(expectedTitle, { timeout: 2000 }); |
| 173 | }); |
| 174 | } |
| 175 | } |
| 176 | }); |
| 177 | }); |