blob: 3057f692504e13565de0aee406b99202ac0c4fbc [file]
const { test, expect } = require('./artifact-test');
const presets = ['paper', 'slate', 'github', 'ink', 'news', 'terminal'];
const versions = ['latest', '1.7', '1.5', '1.3', '1.0'];
const included = new Set((process.env.EXPECTED_VERSIONS || versions.join(',')).split(','));
const helpTrigger = '.td-site-header .td-shell-keyboard__trigger';
async function insideViewport(locator) {
expect(await locator.evaluate(node => {
const box = node.getBoundingClientRect();
return box.left >= -1 && box.right <= innerWidth + 1 && box.top >= -1 && box.bottom <= innerHeight + 1;
})).toBe(true);
}
// All six palettes in both modes, sampled on English desktop and Chinese mobile.
// The existing field suite supplies deeper content/search/Print coverage.
for (const preset of presets) {
for (const mode of ['light', 'dark']) {
test(`${preset} ${mode} keeps native reading colors and the purple brand`, async ({ page }) => {
const mobile = mode === 'dark';
await page.setViewportSize({ width: mobile ? 320 : 1440, height: 900 });
await page.addInitScript(({ preset, mode }) => {
localStorage.setItem('td-preset', preset);
localStorage.setItem('td-color-theme', mode);
}, { preset, mode });
await page.goto(`${mobile ? '/cn' : ''}/docs/config/config-option/`);
await expect(page.locator('html')).toHaveAttribute('data-td-preset', preset);
await expect(page.locator('html')).toHaveAttribute('data-bs-theme', mode);
await page.evaluate(() => document.fonts.ready);
const colors = await page.evaluate(() => {
const probe = document.createElement('span');
document.body.append(probe);
const color = value => {
probe.style.color = value;
return getComputedStyle(probe).color;
};
const result = {
header: getComputedStyle(document.querySelector('.td-site-header')).backgroundColor,
accent: color('var(--td-accent)'),
native: color('var(--td-preset-accent, var(--bs-link-color))'),
brand: color('#532fc9'),
};
probe.remove();
return result;
});
expect(colors.header).toBe(colors.brand);
expect(colors.accent).toBe(colors.native);
expect(colors.accent).not.toBe(colors.brand);
const field = page.locator('[data-td-search-entry="restserver.connection_max_requests"]');
await field.scrollIntoViewIfNeeded();
await expect(field.locator('.td-field__name')).toBeVisible();
await expect(field.locator('.td-field__description')).not.toBeEmpty();
expect(await field.evaluate(node => node.scrollWidth <= node.clientWidth + 1)).toBe(true);
expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1)).toBe(true);
});
}
}
for (const locale of ['en', 'cn']) {
const prefix = locale === 'cn' ? '/cn' : '';
test(`${locale} top help keeps native groups, focus, shortcut preference and IME`, async ({ page }) => {
await page.setViewportSize({ width: 1440, height: 900 });
await page.goto(`${prefix}/docs/config/config-option/`);
const trigger = page.locator(helpTrigger);
await expect(trigger).toHaveCount(1);
await trigger.hover();
const preview = page.locator('.td-site-header .td-shell-keyboard-help');
await expect(preview).toBeVisible();
await expect(preview.locator('.td-shell-keyboard-help__section')).toHaveCount(3);
await expect(preview.locator('[data-td-shortcut]')).toHaveCount(29);
await insideViewport(preview);
expect(await preview.locator('.td-shell-keyboard-help__body').evaluate(node =>
getComputedStyle(node).gridTemplateColumns.split(' ').length)).toBe(3);
const overlaps = await preview.locator('.td-shell-keyboard-help__row').evaluateAll(rows => rows.filter(row => {
const keys = row.querySelector('dt').getBoundingClientRect();
const text = document.createTreeWalker(row.querySelector('dd'), NodeFilter.SHOW_TEXT);
while (text.nextNode()) {
// Native unavailable-state notes are clipped for sighted users; Range
// still reports their unclipped text, so measure only visible labels.
if (text.currentNode.parentElement.closest('.visually-hidden[data-td-shortcut-state]')) continue;
const label = document.createRange();
label.selectNodeContents(text.currentNode);
if (Array.from(label.getClientRects()).some(rect => rect.right > keys.left - 4)) return true;
}
return false;
}).map(row => row.dataset.tdShortcut));
expect(overlaps, 'Shortcut labels must not overlap their keycaps').toEqual([]);
await trigger.click();
const dialog = page.locator('dialog[data-td-keyboard-dialog]');
await expect(dialog).toBeVisible();
await expect(dialog.locator('[data-td-shortcut]')).toHaveCount(29);
for (const action of ['chatgpt', 'claude']) {
await expect(dialog.locator(`[data-td-shortcut="${action}"]`)).toHaveClass(/td-is-unavailable/);
}
await dialog.locator('[data-td-shortcut-preference] input').uncheck();
await page.keyboard.press('Escape');
await expect(dialog).toBeHidden();
await expect(trigger).toBeFocused();
await expect(preview).toBeHidden();
await expect(page.locator('html')).toHaveAttribute('data-td-shortcuts-off', '');
await page.keyboard.press('t');
await expect(page.locator('html')).toHaveAttribute('data-td-preset', 'slate');
await page.reload();
await expect(page.locator('html')).toHaveAttribute('data-td-shortcuts-off', '');
// Ctrl+K and Escape remain available even with single-key shortcuts off.
await page.keyboard.press('Control+k');
const search = page.locator('#td-shell-search');
const input = search.locator('.td-shell-search__input');
await expect(input).toBeFocused();
await input.evaluate(node => {
node.dispatchEvent(new CompositionEvent('compositionstart', { bubbles: true }));
node.value = '配置';
node.dispatchEvent(new InputEvent('input', { bubbles: true, data: '配置', isComposing: true }));
node.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', code: 'Escape', bubbles: true, isComposing: true }));
});
await expect(search).toBeVisible();
await expect(input).toHaveValue('配置');
await input.evaluate(node => {
node.dispatchEvent(new CompositionEvent('compositionend', { bubbles: true, data: '配置' }));
node.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', code: 'Escape', keyCode: 229, bubbles: true }));
});
await expect(search).toBeVisible();
await page.keyboard.press('Escape');
await expect(search).toBeHidden();
await page.setViewportSize({ width: 320, height: 844 });
await trigger.click();
await expect(dialog).toBeVisible();
await insideViewport(dialog);
await expect(dialog.locator('.td-shell-keyboard-help__section')).toHaveCount(3);
await dialog.locator('[data-td-shortcut-preference] input').check();
await page.keyboard.press('Escape');
await expect(trigger).toBeFocused();
await expect(page.locator('html')).not.toHaveAttribute('data-td-shortcuts-off');
await page.keyboard.press("'");
await expect(dialog).toBeVisible();
});
}
test('native reading width persists, fits mobile and returns to the authored width', async ({ page }) => {
await page.setViewportSize({ width: 1920, height: 1000 });
await page.goto('/docs/config/config-option/');
const article = page.locator('.td-shell-layout .td-shell-article');
const width = () => article.evaluate(node => node.getBoundingClientRect().width);
const original = await width();
expect(original).toBeGreaterThan(700);
await page.keyboard.press('-');
await expect.poll(width).toBeCloseTo(original - 100, 0);
const saved = await page.evaluate(() => localStorage.getItem('td-shell-article-w'));
expect(Number(saved)).toBeCloseTo(original - 100, 0);
await page.reload();
await expect.poll(width).toBeCloseTo(original - 100, 0);
await page.setViewportSize({ width: 320, height: 844 });
expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1)).toBe(true);
await page.setViewportSize({ width: 1920, height: 1000 });
await page.keyboard.press('=');
await expect.poll(width).toBeCloseTo(original, 0);
expect(await page.evaluate(() => localStorage.getItem('td-shell-article-w'))).toBeNull();
});
for (const version of versions) {
test(`${version} local history is scoped, removable and excludes Duo panes`, async ({ page }) => {
test.skip(!included.has(version), 'version absent from this artifact');
const mount = version === 'latest' ? '' : `/versions/${version}`;
const scope = mount + '/';
const key = 'td-history:' + scope;
await page.goto(`${mount}/docs/`);
await page.goto(`${mount}/cn/docs/?reading-test=1#top`);
await expect(page.locator('#td-shell-history')).toHaveAttribute('data-td-history-scope', scope);
await expect(page.locator('[data-td-shortcut-preference]').first()).toHaveAttribute('data-td-shortcut-preference', scope);
const entries = await page.evaluate(key => JSON.parse(localStorage.getItem(key)), key);
expect(entries.map(entry => entry.url)).toEqual([`${mount}/cn/docs/?reading-test=1`, `${mount}/docs/`]);
expect(await page.evaluate(() => Object.keys(localStorage).filter(key => key.startsWith('td-history:')))).toEqual([key]);
await page.keyboard.press('h');
const history = page.locator('#td-shell-history');
const input = history.locator('.td-shell-search__input');
await expect(input).toBeFocused();
await expect(history.getByRole('option')).toHaveCount(1);
await input.fill('this-page-does-not-exist');
await expect(history.getByRole('option')).toHaveCount(0);
await input.fill('');
await expect(history.getByRole('option')).toHaveCount(1);
await input.press('Shift+Delete');
await expect(history.getByRole('option')).toHaveCount(0);
expect(await page.evaluate(key => JSON.parse(localStorage.getItem(key)).length, key)).toBe(1);
await history.locator('[data-td-history-clear]').click();
expect(await page.evaluate(key => localStorage.getItem(key), key)).toBeNull();
await page.keyboard.press('Escape');
await expect(history).toBeHidden();
await page.locator('[data-td-compare-open]').click();
const frames = page.locator('dialog[data-td-compare] .td-compare__body:not([aria-busy]) > iframe[data-td-compare-frame]');
await expect(frames).toHaveCount(2, { timeout: 15_000 });
for (const locator of await frames.all()) {
const frame = await (await locator.elementHandle()).contentFrame();
await frame.waitForFunction(() => window.OinkComparePane === true && document.readyState === 'complete');
expect(await frame.evaluate(() => typeof window.OinkHistory)).toBe('undefined');
}
expect(await page.evaluate(key => localStorage.getItem(key), key)).toBeNull();
await page.locator('[data-td-compare-close]').click();
});
}
for (const locale of ['en', 'cn']) {
test(`${locale} rich copy preserves selected configuration text without page controls`, async ({ page, browserName, headless }) => {
await page.addInitScript(() => localStorage.setItem('td-color-theme', 'dark'));
await page.goto(`${locale === 'cn' ? '/cn' : ''}/docs/config/config-option/`);
const field = page.locator('[data-td-search-entry="restserver.connection_max_requests"]');
// Give the browser content real input focus before selecting and copying.
await field.locator('.td-field__description').click();
// Observe a native copy after the document handler. Read
// clipboardData during dispatch: Firefox does not reuse a DataTransfer
// passed to a synthetic ClipboardEvent, and event data may expire later.
const text = await field.evaluate(node => {
const selection = getSelection();
const range = document.createRange();
range.selectNodeContents(node);
selection.removeAllRanges();
selection.addRange(range);
window.__readingTestCopy = null;
window.addEventListener('copy', event => {
window.__readingTestCopy = {
trusted: event.isTrusted,
handled: event.defaultPrevented,
plain: event.clipboardData.getData('text/plain'),
html: event.clipboardData.getData('text/html')
};
}, { once: true });
return selection.toString();
});
if (browserName === 'webkit' && process.platform === 'linux' && headless) {
// Headless Linux WebKit uses WPE, whose keyboard editor returns before
// Copy for non-editable content. Invoke the native command directly;
// the trusted event and complete clipboard payload remain required.
expect(await page.evaluate(() => document.execCommand('copy'))).toBe(true);
} else {
await page.keyboard.press('ControlOrMeta+c');
}
await expect.poll(() => page.evaluate(() => window.__readingTestCopy)).not.toBeNull();
const copied = await page.evaluate(() => window.__readingTestCopy);
expect(copied.trusted).toBe(true);
expect(copied.handled).toBe(true);
expect(copied.plain).toBe(text);
expect(copied.html).toContain('restserver.connection_max_requests');
expect(copied.html).toContain('256');
expect(copied.html).toMatch(/<code[ >]/);
expect(copied.html).not.toMatch(/td-heading-self-link|data-td-|<button|<script|<style|javascript:/i);
expect(copied.html).not.toContain('background-color:');
});
}