query-navigation.spec.ts (5591B)
1 import { test, expect } from '@playwright/test'; 2 import { createStaticHarness } from '../integration/harness/static'; 3 4 test('real public query navigation normalizes copies and restores Back/Forward state', async ({ 5 browser 6 }) => { 7 const server = await createStaticHarness(); 8 const context = await browser.newContext(); 9 const external: string[] = []; 10 try { 11 await context.route('**/*', (route) => { 12 if (new URL(route.request().url()).origin === server.url) 13 return route.continue(); 14 external.push(route.request().url()); 15 return route.abort(); 16 }); 17 const page = await context.newPage(); 18 await page.goto(server.url); 19 await page.getByRole('button', { name: 'Search', exact: true }).click(); 20 await expect(page).toHaveURL(server.url + '/search'); 21 const input = page.getByRole('textbox', { 22 name: 'What are you looking for?' 23 }); 24 await input.fill(' CARROTS\nVICTORIA 菜 '); 25 await page.getByRole('button', { name: 'Search', exact: true }).click(); 26 await expect(page).toHaveURL( 27 server.url + '/search?q=carrots+victoria+%E8%8F%9C' 28 ); 29 await expect(input).toHaveValue('carrots victoria 菜'); 30 await expect( 31 page.getByText('Search sources are unavailable.') 32 ).toBeVisible(); 33 await input.fill('Turnips'); 34 await page.getByRole('button', { name: 'Search', exact: true }).click(); 35 await expect(page).toHaveURL(server.url + '/search?q=turnips'); 36 await page.goBack(); 37 await expect(input).toHaveValue('carrots victoria 菜'); 38 await page.goForward(); 39 await expect(input).toHaveValue('turnips'); 40 expect([...new URL(page.url()).searchParams.keys()]).toEqual(['q']); 41 expect(external).toEqual([]); 42 expect(await page.evaluate(() => 'nostr' in window)).toBe(false); 43 } finally { 44 await context.close(); 45 await server.close(); 46 } 47 expect(context.pages()).toHaveLength(0); 48 expect(server.state()).toEqual({ listening: false, childProcesses: 0 }); 49 }); 50 51 test('real search preserves rejected input and does not navigate or execute excess queries', async ({ 52 browser 53 }) => { 54 const server = await createStaticHarness(); 55 const context = await browser.newContext(); 56 try { 57 await context.route('**/*', (route) => 58 new URL(route.request().url()).origin === server.url 59 ? route.continue() 60 : route.abort() 61 ); 62 const page = await context.newPage(); 63 await page.goto(server.url); 64 const input = page.getByRole('textbox', { 65 name: 'What are you looking for?' 66 }); 67 for (const [value, message] of [ 68 ['菜'.repeat(171), 'Use at most 512 UTF-8 bytes'], 69 [Array(13).fill('carrots').join(' '), 'Use at most 12 words'] 70 ] as const) { 71 await input.fill(value); 72 await page.getByRole('button', { name: 'Search', exact: true }).click(); 73 await expect(input).toHaveValue(value); 74 await expect(input).toHaveAttribute('aria-invalid', 'true'); 75 await expect(page.getByText(message, { exact: false })).toBeVisible(); 76 expect(new URL(page.url()).pathname).toBe('/'); 77 expect(new URL(page.url()).search).toBe(''); 78 } 79 await page.goto(server.url + '/search?q=carrots&body=private'); 80 await expect( 81 page.getByText('Enter plain search words without private URL context.') 82 ).toBeVisible(); 83 await expect( 84 page.getByRole('textbox', { name: 'What are you looking for?' }) 85 ).toHaveValue(''); 86 } finally { 87 await context.close(); 88 await server.close(); 89 } 90 expect(server.state()).toEqual({ listening: false, childProcesses: 0 }); 91 }); 92 93 test('search editing waits for real hydration and query restoration when scripts are delayed', async ({ 94 browser 95 }) => { 96 const server = await createStaticHarness(); 97 const context = await browser.newContext(); 98 const external: string[] = []; 99 let releaseScripts!: () => void; 100 const scriptsReady = new Promise<void>((resolve) => { 101 releaseScripts = resolve; 102 }); 103 let delayedScripts = 0; 104 try { 105 await context.route('**/*', async (route) => { 106 if (new URL(route.request().url()).origin !== server.url) { 107 external.push(route.request().url()); 108 return route.abort(); 109 } 110 if (new URL(route.request().url()).pathname.endsWith('.js')) { 111 delayedScripts++; 112 await scriptsReady; 113 } 114 return route.continue(); 115 }); 116 const page = await context.newPage(); 117 await page.goto(server.url + '/search?q=turnips', { waitUntil: 'commit' }); 118 const input = page.getByRole('textbox', { 119 name: 'What are you looking for?' 120 }); 121 const submit = page.getByRole('button', { name: 'Search', exact: true }); 122 await expect( 123 page.getByRole('heading', { name: 'Search food' }) 124 ).toBeVisible(); 125 await expect.poll(() => delayedScripts).toBeGreaterThan(0); 126 await expect(input).toBeDisabled(); 127 await expect(submit).toBeDisabled(); 128 releaseScripts(); 129 await expect(input).toBeEnabled(); 130 await expect(input).toHaveValue('turnips'); 131 await expect(submit).toBeEnabled(); 132 await input.fill(' CARROTS\nVICTORIA 菜 '); 133 await submit.click(); 134 await expect(page).toHaveURL( 135 server.url + '/search?q=carrots+victoria+%E8%8F%9C' 136 ); 137 await expect(input).toBeEnabled(); 138 await expect(input).toHaveValue('carrots victoria 菜'); 139 expect(external).toEqual([]); 140 } finally { 141 releaseScripts(); 142 await context.close(); 143 await server.close(); 144 } 145 expect(context.pages()).toHaveLength(0); 146 expect(server.state()).toEqual({ listening: false, childProcesses: 0 }); 147 });