|
1 | 1 | import { expect, test } from "@playwright/test"; |
2 | 2 |
|
| 3 | +import { SYNTAX_THEMES } from "../../src/shared/theme/theme-loader"; |
3 | 4 | import { |
4 | 5 | installMockBridge, |
5 | 6 | openChannelBrowser, |
@@ -37,6 +38,85 @@ const DM_THREAD_AGENT_MENTION_ERROR_TEXT = |
37 | 38 | const DM_THREAD_MEMBERS_LOADING_ERROR_TEXT = |
38 | 39 | "Checking conversation members. Try again in a moment."; |
39 | 40 |
|
| 41 | +async function readRenderedContrast( |
| 42 | + locator: import("@playwright/test").Locator, |
| 43 | +) { |
| 44 | + return locator.evaluate((element) => { |
| 45 | + type Rgba = { r: number; g: number; b: number; a: number }; |
| 46 | + const parseColor = (value: string): Rgba => { |
| 47 | + if (value === "transparent") return { r: 0, g: 0, b: 0, a: 0 }; |
| 48 | + const canvas = document.createElement("canvas"); |
| 49 | + canvas.width = 1; |
| 50 | + canvas.height = 1; |
| 51 | + const context = canvas.getContext("2d"); |
| 52 | + if (!context) return { r: 0, g: 0, b: 0, a: 0 }; |
| 53 | + context.clearRect(0, 0, 1, 1); |
| 54 | + context.fillStyle = value; |
| 55 | + context.fillRect(0, 0, 1, 1); |
| 56 | + const [r, g, b, alpha] = context.getImageData(0, 0, 1, 1).data; |
| 57 | + return { r, g, b, a: alpha / 255 }; |
| 58 | + }; |
| 59 | + const composite = (foreground: Rgba, background: Rgba): Rgba => { |
| 60 | + const alpha = foreground.a + background.a * (1 - foreground.a); |
| 61 | + if (alpha === 0) return { r: 0, g: 0, b: 0, a: 0 }; |
| 62 | + return { |
| 63 | + r: |
| 64 | + (foreground.r * foreground.a + |
| 65 | + background.r * background.a * (1 - foreground.a)) / |
| 66 | + alpha, |
| 67 | + g: |
| 68 | + (foreground.g * foreground.a + |
| 69 | + background.g * background.a * (1 - foreground.a)) / |
| 70 | + alpha, |
| 71 | + b: |
| 72 | + (foreground.b * foreground.a + |
| 73 | + background.b * background.a * (1 - foreground.a)) / |
| 74 | + alpha, |
| 75 | + a: alpha, |
| 76 | + }; |
| 77 | + }; |
| 78 | + const backgrounds: Rgba[] = []; |
| 79 | + for (let node: Element | null = element; node; node = node.parentElement) { |
| 80 | + backgrounds.push(parseColor(getComputedStyle(node).backgroundColor)); |
| 81 | + } |
| 82 | + const background = backgrounds |
| 83 | + .reverse() |
| 84 | + .reduce((result, layer) => composite(layer, result), { |
| 85 | + r: 0, |
| 86 | + g: 0, |
| 87 | + b: 0, |
| 88 | + a: 0, |
| 89 | + }); |
| 90 | + const foreground = composite( |
| 91 | + parseColor(getComputedStyle(element).color), |
| 92 | + background, |
| 93 | + ); |
| 94 | + const luminance = (color: Rgba) => |
| 95 | + [color.r, color.g, color.b] |
| 96 | + .map((channel) => { |
| 97 | + const value = channel / 255; |
| 98 | + return value <= 0.04045 |
| 99 | + ? value / 12.92 |
| 100 | + : ((value + 0.055) / 1.055) ** 2.4; |
| 101 | + }) |
| 102 | + .reduce( |
| 103 | + (sum, channel, index) => |
| 104 | + sum + channel * [0.2126, 0.7152, 0.0722][index], |
| 105 | + 0, |
| 106 | + ); |
| 107 | + const foregroundLuminance = luminance(foreground); |
| 108 | + const backgroundLuminance = luminance(background); |
| 109 | + return { |
| 110 | + background: getComputedStyle(element).backgroundColor, |
| 111 | + backgroundAlpha: parseColor(getComputedStyle(element).backgroundColor).a, |
| 112 | + contrast: |
| 113 | + (Math.max(foregroundLuminance, backgroundLuminance) + 0.05) / |
| 114 | + (Math.min(foregroundLuminance, backgroundLuminance) + 0.05), |
| 115 | + foreground: getComputedStyle(element).color, |
| 116 | + }; |
| 117 | + }); |
| 118 | +} |
| 119 | + |
40 | 120 | /** Locator scoped to the mention autocomplete dropdown inside the composer. */ |
41 | 121 | function autocomplete(page: import("@playwright/test").Page) { |
42 | 122 | return page |
@@ -2734,6 +2814,48 @@ test("mention text is highlighted in sent messages", async ({ page }) => { |
2734 | 2814 | await expect(mentionChip).toHaveClass(/inline-chip-icon-human/); |
2735 | 2815 | }); |
2736 | 2816 |
|
| 2817 | +for (const theme of SYNTAX_THEMES) { |
| 2818 | + test(`mention chip keeps AA contrast and theme accent in ${theme}`, async ({ |
| 2819 | + page, |
| 2820 | + }) => { |
| 2821 | + await page.addInitScript((themeName) => { |
| 2822 | + window.localStorage.setItem("buzz-theme", themeName); |
| 2823 | + }, theme); |
| 2824 | + await page.goto("/"); |
| 2825 | + await page.getByTestId("channel-general").click(); |
| 2826 | + await expect(page.getByTestId("chat-title")).toHaveText("general"); |
| 2827 | + await waitForMockLiveSubscription(page, "general"); |
| 2828 | + |
| 2829 | + await emitMockMessage(page, "general", `Ask @bob in ${theme}`, { |
| 2830 | + mentionPubkeys: [TEST_IDENTITIES.bob.pubkey], |
| 2831 | + }); |
| 2832 | + await waitForTimelineSettled(page); |
| 2833 | + |
| 2834 | + const mentionChip = page |
| 2835 | + .getByTestId("message-row") |
| 2836 | + .filter({ hasText: `Ask bob in ${theme}` }) |
| 2837 | + .locator("[data-mention]", { hasText: "bob" }); |
| 2838 | + await expect(mentionChip).toBeVisible(); |
| 2839 | + |
| 2840 | + const resting = await readRenderedContrast(mentionChip); |
| 2841 | + expect(resting.contrast).toBeGreaterThanOrEqual(4.5); |
| 2842 | + expect(resting.backgroundAlpha).toBeLessThan(1); |
| 2843 | + |
| 2844 | + await mentionChip.hover(); |
| 2845 | + await expect |
| 2846 | + .poll(() => |
| 2847 | + mentionChip.evaluate( |
| 2848 | + (element) => getComputedStyle(element).backgroundColor, |
| 2849 | + ), |
| 2850 | + ) |
| 2851 | + .not.toBe(resting.background); |
| 2852 | + const hovered = await readRenderedContrast(mentionChip); |
| 2853 | + expect(hovered.foreground).toBe(resting.foreground); |
| 2854 | + expect(hovered.contrast).toBeGreaterThanOrEqual(4.5); |
| 2855 | + expect(hovered.backgroundAlpha).toBeLessThan(1); |
| 2856 | + }); |
| 2857 | +} |
| 2858 | + |
2737 | 2859 | test("clicking author name opens user profile panel", async ({ page }) => { |
2738 | 2860 | await page.goto("/"); |
2739 | 2861 | await page.getByTestId("channel-general").click(); |
|
0 commit comments