// Browser web data settings: cookie and history lists, filters, removal, profile isolation, stale-response guards, and narrow layouts. import assert from 'node:assert/strict'; import path from 'node:path'; import { mockTauri, outputDir, runSmoke, settle as settleFrames, smokeUrl } from '../_harness/index.mjs'; await runSmoke('browser/webData', async ({ page }) => { await mockTauri(page, { label: 'settings-main', commands: test => { const snapshot = { profiles: [{ id: 'user', name: 'Personal' }, { id: 'work', name: 'Work' }], tabs: [{ id: 'open-tab', profileId: 'user', url: 'https://example.com', title: 'Keep open', loading: false, blocked: 0, canBack: false, canForward: false }], bookmarks: [], settings: { search: 'duckduckgo', adblock: true, linkDestination: 'internal', personalRules: '', homepage: '', filterLists: [] }, }; const longUrl = 'https://very-long-domain.example/' + 'long-path-segment-'.repeat(30); const data = { user: { profileId: 'user', cookies: [ { id: 'shared-cookie', name: 'session', domain: 'personal.example', path: '/account' }, { id: 'second-cookie', name: '', domain: 'another.example', path: '/settings' }, { id: 'long-cookie', name: 'Long cookie ' + 'value'.repeat(30), domain: 'long.example', path: '/' + 'path'.repeat(30) }, ], history: [ { id: 'shared-visit', title: 'Personal guide', url: 'https://personal.example/guide', at: 1700000000000 }, { id: 'second-visit', title: '', url: 'https://another.example/document', at: 1700000001000 }, { id: 'long-visit', title: 'Long visit ' + 'title'.repeat(30), url: longUrl, at: 1700000002000 }, ], }, work: { profileId: 'work', cookies: [{ id: 'shared-cookie', name: 'session', domain: 'work.example', path: '/account' }], history: [{ id: 'shared-visit', title: 'Work guide', url: 'https://work.example/guide', at: 1700000000000 }], }, }; // `requests` records browser requests; `hold` keys delay a request until the test releases it. const check = Object.assign(test, { data, snapshot, requests: [], hold: [], pending: [], failNext: {} }); return { browser_connect: () => 'web-data-session', plugins_snapshot: () => ({ root: 'test', plugins: [] }), 'plugin:window|is_maximized': () => false, browser_request: async ({ session, command: operation, args: details = {} }) => { if (session !== 'web-data-session') throw Error('Missing browser session'); const key = `${operation}:${details.profileId}`; check.requests.push({ operation, ...details }); if (check.failNext[key]) { const message = check.failNext[key]; delete check.failNext[key]; throw Error(message); } const nextData = operation === 'webData' ? structuredClone(data[details.profileId]) : null; if (check.hold.includes(key)) await new Promise((resolve, reject) => check.pending.push({ key, resolve, reject })); if (operation === 'webData') return nextData; if (operation === 'removeCookie') data[details.profileId].cookies = data[details.profileId].cookies.filter(item => item.id !== details.id); else if (operation === 'removeHistory') data[details.profileId].history = data[details.profileId].history.filter(item => item.id !== details.id); else if (!['state', 'settings'].includes(operation)) throw Error('Unhandled browser operation: ' + operation); return structuredClone(snapshot); }, }; }, }); const output = await outputDir('browser/webData'); const section = page.getByRole('region', { name: 'Individual web data', exact: true }); const profile = page.getByRole('button', { name: 'Profile', exact: true }); async function selectProfile(id) { await profile.click(); await page.getByRole('menuitemradio', { name: id === 'work' ? 'Work' : 'Personal', exact: true }).click(); } const kind = section.getByRole('button', { name: 'Web data type', exact: true }); async function selectKind(value) { await kind.click(); await page.getByRole('menuitemradio', { name: value === 'cookies' ? 'Cookies' : 'History', exact: true }).click(); } const filter = section.getByRole('searchbox', { name: 'Filter web data', exact: true }); const refresh = section.getByRole('button', { name: 'Refresh', exact: true }); const personalCookie = () => section.getByRole('button', { name: 'Remove cookie session from personal.example at /account', exact: true }); const workCookie = () => section.getByRole('button', { name: 'Remove cookie session from work.example at /account', exact: true }); const settle = () => section.locator('.entries[aria-busy="false"]').waitFor(); async function hold(key) { await page.evaluate(key => { window.__TEST__.hold.push(key); }, key); } async function pending(key) { await page.waitForFunction(key => window.__TEST__.pending.some(item => item.key === key), key); } async function release(key, error) { await page.evaluate(({ key, error }) => { const check = window.__TEST__; check.hold = check.hold.filter(item => item !== key); const index = check.pending.findIndex(item => item.key === key), item = check.pending.splice(index, 1)[0]; if (!item) throw Error('No pending request for ' + key); if (error) item.reject(Error(error)); else item.resolve(); }, { key, error }); await settleFrames(page); } try { await page.goto(smokeUrl + '/?settingsWindow=true'); await page.getByRole('button', { name: 'Browser', exact: true }).click(); await personalCookie().waitFor(); assert.equal(await section.locator('.entry').count(), 3); assert.equal(await section.getByText('(unnamed cookie)', { exact: true }).count(), 1); await filter.fill('PERSONAL.EXAMPLE'); assert.equal(await section.locator('.entry').count(), 1); await filter.fill('/settings'); assert.equal(await section.locator('.entry').count(), 1); await filter.fill('no matches'); await section.getByText('No matching cookies.', { exact: true }).waitFor(); await filter.fill('session'); await personalCookie().click(); await settle(); await section.getByText('No matching cookies.', { exact: true }).waitFor(); assert.equal(await page.evaluate(() => window.__TEST__.data.work.cookies.length), 1, 'Removing a cookie preserves another profile with the same ID'); await selectKind('history'); await filter.fill('GUIDE'); await section.getByRole('button', { name: 'Remove history entry Personal guide', exact: true }).click(); await settle(); await section.getByText('No matching history.', { exact: true }).waitFor(); assert.equal(await page.evaluate(() => window.__TEST__.data.work.history.length), 1); await filter.fill('another.example/document'); await section.getByRole('button', { name: 'Remove history entry https://another.example/document', exact: true }).waitFor(); assert.equal(await section.locator('.entry').count(), 1); await selectProfile('work'); await settle(); assert.equal(await filter.inputValue(), '', 'Profile changes clear the prior filter'); await section.getByText('Work guide', { exact: true }).waitFor(); await selectKind('cookies'); await workCookie().waitFor(); // A delayed successful response from the previous profile must not replace the current rows. await hold('webData:work'); await refresh.click(); await pending('webData:work'); assert.equal(await workCookie().isDisabled(), true); await selectProfile('user'); await settle(); await release('webData:work'); assert.equal(await workCookie().count(), 0); assert.equal(await section.locator('.entry').count(), 2); // The same guard must ignore an old request error. await hold('webData:user'); await refresh.click(); await pending('webData:user'); await selectProfile('work'); await workCookie().waitFor(); await settle(); await release('webData:user', 'Stale personal error'); assert.equal(await section.getByRole('alert').count(), 0); assert.equal(await refresh.isEnabled(), true); // Removal finishes for its original profile without refreshing the selected profile. await hold('removeCookie:work'); await workCookie().click(); await pending('removeCookie:work'); await selectProfile('user'); await settle(); const readsBefore = await page.evaluate(() => window.__TEST__.requests.filter(item => item.operation === 'webData').length); await release('removeCookie:work'); assert.equal(await profile.innerText(), 'Personal'); assert.equal(await section.locator('.entry').count(), 2); assert.equal(await page.evaluate(() => window.__TEST__.requests.filter(item => item.operation === 'webData').length), readsBefore); assert.equal(await page.evaluate(() => window.__TEST__.data.work.cookies.length), 0); await page.evaluate(() => { window.__TEST__.failNext['webData:user'] = 'Cannot read personal data'; }); await refresh.click(); await section.getByRole('alert').filter({ hasText: 'Cannot read personal data' }).waitFor(); assert.equal(await refresh.isEnabled(), true); await refresh.click(); await settle(); assert.equal(await section.getByRole('alert').count(), 0); await page.evaluate(() => { window.__TEST__.failNext['removeCookie:user'] = 'Cannot remove this cookie'; }); const unnamed = section.getByRole('button', { name: 'Remove cookie from another.example at /settings', exact: true }); await unnamed.click(); await section.getByRole('alert').filter({ hasText: 'Cannot remove this cookie' }).waitFor(); assert.equal(await unnamed.isEnabled(), true); await unnamed.click(); await settle(); assert.equal(await unnamed.count(), 0); assert.equal(await section.getByRole('alert').count(), 0); assert.deepEqual(await page.evaluate(() => window.__TEST__.snapshot.tabs.map(tab => tab.id)), ['open-tab']); assert.equal(await page.evaluate(() => window.__TEST__.requests.some(item => ['closeTab', 'clearData'].includes(item.operation))), false); await page.setViewportSize({ width: 360, height: 800 }); for (const theme of ['dark', 'light']) { await page.evaluate(async mode => { const { saveAppearance } = await import('/src/lib/theme/appearance.ts'); saveAppearance({ theme: 'default', mode }); }, theme); assert.equal(await page.evaluate(() => getComputedStyle(document.documentElement).getPropertyValue('--rs-bg').trim()), theme === 'light' ? '#fdfdfa' : '#151617'); for (const dataType of ['cookies', 'history']) { await selectKind(dataType); await section.scrollIntoViewIfNeeded(); await page.screenshot({ path: path.join(output, `browser-web-data-${dataType}-${theme}.png`) }); const overflow = await section.evaluate(node => { const elements = [node, node.querySelector('.entries'), ...node.querySelectorAll('input, select, button')]; return elements.filter(element => element.scrollWidth > element.clientWidth + 1).map(element => ({ tag: element.tagName, className: element.className, width: element.clientWidth, scrollWidth: element.scrollWidth })); }); assert.deepEqual(overflow, [], `${dataType} fits the narrow ${theme} settings view`); assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false); const controls = await section.locator('input,select,button').evaluateAll(nodes => nodes.map(node => { const rect = node.getBoundingClientRect(), style = getComputedStyle(node); return { width: rect.width, height: rect.height, radius: parseFloat(style.borderRadius) }; })); assert.ok(controls.every(control => control.height >= 28 && control.radius > 0)); await filter.focus(); await page.keyboard.press('Tab'); assert.equal(await refresh.evaluate(node => node === document.activeElement), true); assert.notEqual(await refresh.evaluate(node => getComputedStyle(node).outlineStyle), 'none'); } } } catch (error) { console.error(await page.locator('body').innerText()); throw error; } console.log('PASS: cookie/history lists and removal, filters, profile isolation, stale success/error/removal guards, recovery, open-tab preservation, and 360px controls in both themes.'); }, { viewport: { width: 1100, height: 850 } });