|
| 1 | +/** |
| 2 | + * Feature assertion: the "Increase contrast" toggle in Settings → Appearance |
| 3 | + * actually applies and persists an app-wide high-contrast preference. |
| 4 | + * |
| 5 | + * Drives the real UI over CDP entirely in the draft/no-session state (no seeded |
| 6 | + * conversation, no backend connection): opens Settings → Appearance, flips the |
| 7 | + * toggle, and asserts the observable effects — the switch state, the |
| 8 | + * `data-high-contrast` attribute on <html>, the persisted localStorage value, |
| 9 | + * AND that the high-contrast CSS actually applies (the `--hc-enabled` custom |
| 10 | + * property computes to `1` on :root only while enabled). Toggling twice proves |
| 11 | + * it both applies and reverts, not merely renders. |
| 12 | + */ |
| 13 | + |
| 14 | +import type { Assertion } from '../runner'; |
| 15 | + |
| 16 | +const SETTINGS_NAV = '[data-testid="nav:settings"]'; |
| 17 | +const APPEARANCE_NAV = '[data-testid="settings-nav-appearance"]'; |
| 18 | +const TOGGLE = '[data-testid="high-contrast-toggle"]'; |
| 19 | +const STORAGE_KEY = 'craft-high-contrast'; |
| 20 | + |
| 21 | +/** Read the toggle's aria-checked ("true" | "false" | null). */ |
| 22 | +function ariaCheckedExpr(): string { |
| 23 | + return `(() => { |
| 24 | + const el = document.querySelector(${JSON.stringify(TOGGLE)}); |
| 25 | + return el ? el.getAttribute('aria-checked') : null; |
| 26 | + })()`; |
| 27 | +} |
| 28 | + |
| 29 | +/** True when <html> carries the high-contrast marker attribute. */ |
| 30 | +function htmlMarkedExpr(): string { |
| 31 | + return `document.documentElement.getAttribute('data-high-contrast') === 'true'`; |
| 32 | +} |
| 33 | + |
| 34 | +/** True when the high-contrast CSS block actually matched (marker custom prop). */ |
| 35 | +function cssAppliedExpr(): string { |
| 36 | + return `getComputedStyle(document.documentElement).getPropertyValue('--hc-enabled').trim() === '1'`; |
| 37 | +} |
| 38 | + |
| 39 | +/** The persisted localStorage value for the preference. */ |
| 40 | +function storedValueExpr(): string { |
| 41 | + return `window.localStorage.getItem(${JSON.stringify(STORAGE_KEY)})`; |
| 42 | +} |
| 43 | + |
| 44 | +const assertion: Assertion = { |
| 45 | + name: 'increase-contrast toggle applies and persists an app-wide preference', |
| 46 | + async run(app) { |
| 47 | + const { session } = app; |
| 48 | + |
| 49 | + // App fully mounted. |
| 50 | + await session.waitForFunction( |
| 51 | + '!document.getElementById("_loader") && (document.getElementById("root")?.childElementCount ?? 0) > 0', |
| 52 | + { timeoutMs: 30000, message: 'app did not mount' }, |
| 53 | + ); |
| 54 | + |
| 55 | + // Open Settings → Appearance (real user path). |
| 56 | + await session.click(SETTINGS_NAV, { timeoutMs: 15000 }); |
| 57 | + await session.click(APPEARANCE_NAV, { timeoutMs: 15000 }); |
| 58 | + |
| 59 | + // The toggle is the feature under test — its presence is the first signal. |
| 60 | + await session.waitForSelector(TOGGLE, { |
| 61 | + timeoutMs: 15000, |
| 62 | + message: 'increase-contrast toggle did not render', |
| 63 | + }); |
| 64 | + |
| 65 | + // Initial state: off, no marker on <html>, CSS not applied, not persisted true. |
| 66 | + const initialChecked = await session.evaluate<string | null>(ariaCheckedExpr()); |
| 67 | + if (initialChecked !== 'false') { |
| 68 | + throw new Error(`expected toggle off initially, saw aria-checked=${initialChecked}`); |
| 69 | + } |
| 70 | + if (await session.evaluate<boolean>(htmlMarkedExpr())) { |
| 71 | + throw new Error('expected no data-high-contrast attribute before enabling'); |
| 72 | + } |
| 73 | + if (await session.evaluate<boolean>(cssAppliedExpr())) { |
| 74 | + throw new Error('expected --hc-enabled to be unset before enabling'); |
| 75 | + } |
| 76 | + |
| 77 | + // Enable → toggle on, <html> marked, CSS applied, persisted true. |
| 78 | + await session.click(TOGGLE); |
| 79 | + await session.waitForFunction( |
| 80 | + `${ariaCheckedExpr()} === 'true'`, |
| 81 | + { timeoutMs: 5000, message: 'toggle did not switch on' }, |
| 82 | + ); |
| 83 | + await session.waitForFunction(htmlMarkedExpr(), { |
| 84 | + timeoutMs: 5000, |
| 85 | + message: 'data-high-contrast was not applied to <html> when enabled', |
| 86 | + }); |
| 87 | + await session.waitForFunction(cssAppliedExpr(), { |
| 88 | + timeoutMs: 5000, |
| 89 | + message: 'high-contrast CSS did not apply (--hc-enabled !== 1) when enabled', |
| 90 | + }); |
| 91 | + const storedOn = await session.evaluate<string | null>(storedValueExpr()); |
| 92 | + if (storedOn !== 'true') { |
| 93 | + throw new Error(`expected persisted "true" after enabling, saw ${JSON.stringify(storedOn)}`); |
| 94 | + } |
| 95 | + |
| 96 | + // Disable → toggle off, marker removed, CSS reverted, persisted false. |
| 97 | + await session.click(TOGGLE); |
| 98 | + await session.waitForFunction( |
| 99 | + `${ariaCheckedExpr()} === 'false'`, |
| 100 | + { timeoutMs: 5000, message: 'toggle did not switch off' }, |
| 101 | + ); |
| 102 | + await session.waitForFunction(`!(${htmlMarkedExpr()})`, { |
| 103 | + timeoutMs: 5000, |
| 104 | + message: 'data-high-contrast was not removed from <html> when disabled', |
| 105 | + }); |
| 106 | + await session.waitForFunction(`!(${cssAppliedExpr()})`, { |
| 107 | + timeoutMs: 5000, |
| 108 | + message: 'high-contrast CSS did not revert (--hc-enabled still 1) when disabled', |
| 109 | + }); |
| 110 | + const storedOff = await session.evaluate<string | null>(storedValueExpr()); |
| 111 | + if (storedOff !== 'false') { |
| 112 | + throw new Error(`expected persisted "false" after disabling, saw ${JSON.stringify(storedOff)}`); |
| 113 | + } |
| 114 | + }, |
| 115 | +}; |
| 116 | + |
| 117 | +export default assertion; |
0 commit comments