|
| 1 | +import { expect, it } from "vitest"; |
| 2 | +import { installMockGateway } from "../test-helpers/control-ui-e2e.ts"; |
| 3 | +import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts"; |
| 4 | + |
| 5 | +const suite = createControlUiE2eSuite({ |
| 6 | + name: "Control UI chat composer focus", |
| 7 | +}); |
| 8 | + |
| 9 | +suite.define(() => { |
| 10 | + it("uses a visible neutral focus treatment in dark and light themes", async () => { |
| 11 | + await suite.withPage({ viewport: { width: 1440, height: 900 } }, async ({ page }) => { |
| 12 | + const gateway = await installMockGateway(page); |
| 13 | + await page.goto(`${suite.server.baseUrl}chat`); |
| 14 | + await gateway.waitForRequest("chat.startup"); |
| 15 | + |
| 16 | + const composer = page.locator(".agent-chat__input"); |
| 17 | + const textarea = composer.locator("textarea"); |
| 18 | + await composer.waitFor({ state: "visible" }); |
| 19 | + |
| 20 | + for (const theme of ["dark", "light"] as const) { |
| 21 | + await page.evaluate((nextTheme) => { |
| 22 | + document.documentElement.dataset.theme = "claw"; |
| 23 | + document.documentElement.dataset.themeMode = nextTheme; |
| 24 | + }, theme); |
| 25 | + await textarea.evaluate((element) => element.blur()); |
| 26 | + await page.waitForTimeout(150); |
| 27 | + const unfocused = await composer.evaluate((element) => { |
| 28 | + const style = getComputedStyle(element); |
| 29 | + return { borderColor: style.borderColor, boxShadow: style.boxShadow }; |
| 30 | + }); |
| 31 | + |
| 32 | + await textarea.focus(); |
| 33 | + await page.waitForTimeout(150); |
| 34 | + const focused = await composer.evaluate((element) => { |
| 35 | + const style = getComputedStyle(element); |
| 36 | + const parseRgb = (color: string): [number, number, number] => { |
| 37 | + const values = color |
| 38 | + .match(/[\d.]+/g) |
| 39 | + ?.slice(0, 3) |
| 40 | + .map(Number); |
| 41 | + if (!values || values.length !== 3) { |
| 42 | + throw new Error(`expected an RGB color, received ${color}`); |
| 43 | + } |
| 44 | + return ( |
| 45 | + color.startsWith("color(srgb") ? values.map((value) => value * 255) : values |
| 46 | + ) as [number, number, number]; |
| 47 | + }; |
| 48 | + const luminance = (color: string): number => { |
| 49 | + const linearize = (channel: number) => { |
| 50 | + const normalized = channel / 255; |
| 51 | + return normalized <= 0.04045 |
| 52 | + ? normalized / 12.92 |
| 53 | + : ((normalized + 0.055) / 1.055) ** 2.4; |
| 54 | + }; |
| 55 | + const channels = parseRgb(color); |
| 56 | + return ( |
| 57 | + 0.2126 * linearize(channels[0]) + |
| 58 | + 0.7152 * linearize(channels[1]) + |
| 59 | + 0.0722 * linearize(channels[2]) |
| 60 | + ); |
| 61 | + }; |
| 62 | + const borderLuminance = luminance(style.borderColor); |
| 63 | + const surfaceLuminance = luminance(style.backgroundColor); |
| 64 | + const borderChannels = parseRgb(style.borderColor); |
| 65 | + const contrast = |
| 66 | + (Math.max(borderLuminance, surfaceLuminance) + 0.05) / |
| 67 | + (Math.min(borderLuminance, surfaceLuminance) + 0.05); |
| 68 | + |
| 69 | + return { |
| 70 | + borderColor: style.borderColor, |
| 71 | + boxShadow: style.boxShadow, |
| 72 | + contrast, |
| 73 | + neutralChannelSpread: Math.max(...borderChannels) - Math.min(...borderChannels), |
| 74 | + }; |
| 75 | + }); |
| 76 | + |
| 77 | + expect(focused.borderColor).not.toBe(unfocused.borderColor); |
| 78 | + expect(focused.boxShadow).not.toBe(unfocused.boxShadow); |
| 79 | + expect(focused.neutralChannelSpread).toBeLessThanOrEqual(24); |
| 80 | + expect(focused.contrast).toBeGreaterThanOrEqual(3); |
| 81 | + } |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments