Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerChipPresentation.test.ts

109 lines3.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { contextChipAccent, contextChipPresentation } from "./composerChipPresentation";
4import {
5 flattenThemeColor,
6 getMobileThemeVariables,
7 MOBILE_THEME_IDS,
8 type MobileThemeVariables,
9} from "./mobileTheme";
10​
11const CHIP_ACCENTS = [
12 ...new Set(
13 [
14 ...[
15 "image",
16 "video",
17 "file",
18 "mention",
19 "terminal",
20 "element",
21 "review-comment",
22 "pull-request",
23 "skill",
24 "thread",
25 ].map((kind) => contextChipPresentation(kind)),
26 ...[
27 { state: "open", isDraft: false },
28 { state: "open", isDraft: true },
29 { state: "merged", isDraft: false },
30 { state: "closed", isDraft: false },
31 ].map((pullRequest) => contextChipPresentation("pull-request", { pullRequest })),
32 ].flatMap(({ accent }) => (accent ? [accent] : [])),
33 ),
34];
35​
36function relativeLuminance(hex: string): number {
37 const [red = 0, green = 0, blue = 0] = [1, 3, 5].map((offset) => {
38 const value = Number.parseInt(hex.slice(offset, offset + 2), 16) / 255;
39 return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
40 });
41 return 0.2126 * red + 0.7152 * green + 0.0722 * blue;
43​
44function contrastRatio(first: string, second: string): number {
45 const [darker = 0, lighter = 0] = [relativeLuminance(first), relativeLuminance(second)].sort(
46 (a, b) => a - b,
47 );
48 return (lighter + 0.05) / (darker + 0.05);
50​
51function chipSurfaces(theme: MobileThemeVariables) {
52 const screen = theme["--color-screen"];
53 return (
54 [
55 ["--color-user-bubble", "--color-user-bubble-foreground"],
56 ["--color-thread-canvas", "--color-md-body"],
57 ["--color-composer-surface", "--color-foreground"],
58 ] as const
59 ).map(([surface, text]) => ({
60 surface: flattenThemeColor(theme[surface], screen).slice(0, 7),
61 text: theme[text],
62 }));
64​
65const BUILT_IN_THEMES = MOBILE_THEME_IDS.filter((id) => id !== "material-you").flatMap((id) =>
66 (["light", "dark"] as const).map((appearance) => ({
67 name: `${id} ${appearance}`,
68 theme: getMobileThemeVariables(id, appearance),
69 })),
70);
71​
72describe("contextChipAccent", () => {
73 it("draws one color per theme that reads at 5:1 in messages, replies and the composer", () => {
74 for (const { name, theme } of BUILT_IN_THEMES) {
75 for (const accent of CHIP_ACCENTS) {
76 const shown = contextChipAccent(accent, theme, theme["--color-md-body"])!;
77 for (const { surface, text } of chipSurfaces(theme)) {
78 expect(contextChipAccent(accent, theme, text)).toBe(shown);
79 expect(
80 contrastRatio(shown, surface),
81 `${name}: ${accent} → ${shown} on ${surface}`,
82 ).toBeGreaterThanOrEqual(5);
83 }
84 }
85 }
86 });
87​
88 it("reads on both a saturated Material You bubble and the light canvas beside it", () => {
89 const theme = {
90 ...getMobileThemeVariables("t3-code", "light"),
91 "--color-user-bubble": "#6750a4",
92 "--color-user-bubble-foreground": "#ffffff",
93 };
94 for (const { surface, text } of chipSurfaces(theme)) {
95 for (const accent of CHIP_ACCENTS) {
96 const shown = contextChipAccent(accent, theme, text)!;
97 expect(
98 contrastRatio(shown, surface),
99 `${accent} → ${shown} on ${surface}`,
100 ).toBeGreaterThanOrEqual(5);
101 }
102 }
103 });
104​
105 it("keeps an accent that already reads everywhere", () => {
106 const theme = getMobileThemeVariables("t3-code", "dark");
107 expect(contextChipAccent("#009f6e", theme, theme["--color-md-body"])).toBe("#009f6e");
108 });
109});