Open History to see the last commit
1import { mFallback, type Msg } from "gt-react-native";
2import { describe, expect, it } from "vite-plus/test";
3
4import type { MessageResolver } from "../i18n/messages";
5
6import {
7 composerChipSizeSuffix,
8 contextChipAccent,
9 contextChipPresentation,
10} from "./composerChipPresentation";
11import {
12 flattenThemeColor,
13 getMobileThemeVariables,
14 MOBILE_THEME_IDS,
15 type MobileThemeVariables,
16} from "./mobileTheme";
17
18describe("composerChipSizeSuffix", () => {
19 it("shows a file chip's size in the app language", () => {
20 // A catalogue keyed by the message, as the real one is: only a message with a `size` value matches.
21 const units: Record<string, string> = {
22 [JSON.stringify([["size"], " KB"])]: " Ko",
23 [JSON.stringify([["size"], " MB"])]: " Mo",
24 };
25 const french = ((message: string | Msg) => {
26 const unit = typeof message === "string" ? undefined : units[JSON.stringify(message.tree)];
27 return unit === undefined || typeof message === "string"
28 ? mFallback(message)
29 : `${String(message.values.size)}${unit}`;
30 }) as MessageResolver;
31 expect(composerChipSizeSuffix({ kind: "file", sizeBytes: 12 * 1024 }, french)).toBe("12 Ko");
32 expect(composerChipSizeSuffix({ kind: "image", sizeBytes: 3 * 1024 * 1024 }, french)).toBe(
33 "3.0 Mo",
34 );
35 });
36});
37
38const CHIP_ACCENTS = [
39 ...new Set(
40 [
41 ...[
42 "image",
43 "video",
44 "file",
45 "mention",
46 "terminal",
47 "element",
48 "review-comment",
49 "pull-request",
50 "skill",
51 "thread",
52 "environment",
53 ].map((kind) => contextChipPresentation(kind)),
54 ...[
55 { state: "open", isDraft: false },
56 { state: "open", isDraft: true },
57 { state: "merged", isDraft: false },
58 { state: "closed", isDraft: false },
59 ].map((pullRequest) => contextChipPresentation("pull-request", { pullRequest })),
60 ].flatMap(({ accent }) => (accent ? [accent] : [])),
61 ),
62];
63
64function relativeLuminance(hex: string): number {
65 const [red = 0, green = 0, blue = 0] = [1, 3, 5].map((offset) => {
66 const value = Number.parseInt(hex.slice(offset, offset + 2), 16) / 255;
67 return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
68 });
69 return 0.2126 * red + 0.7152 * green + 0.0722 * blue;
70}
71
72function contrastRatio(first: string, second: string): number {
73 const [darker = 0, lighter = 0] = [relativeLuminance(first), relativeLuminance(second)].sort(
74 (a, b) => a - b,
75 );
76 return (lighter + 0.05) / (darker + 0.05);
77}
78
79function chipSurfaces(theme: MobileThemeVariables) {
80 const screen = theme["--color-screen"];
81 return (
82 [
83 ["--color-user-bubble", "--color-user-bubble-foreground"],
84 ["--color-thread-canvas", "--color-md-body"],
85 ["--color-composer-surface", "--color-foreground"],
86 ] as const
87 ).map(([surface, text]) => ({
88 surface: flattenThemeColor(theme[surface], screen).slice(0, 7),
89 text: theme[text],
90 }));
91}
92
93const BUILT_IN_THEMES = MOBILE_THEME_IDS.filter((id) => id !== "material-you").flatMap((id) =>
94 (["light", "dark"] as const).map((appearance) => ({
95 name: `${id} ${appearance}`,
96 theme: getMobileThemeVariables(id, appearance),
97 })),
98);
99
100describe("contextChipAccent", () => {
101 it("draws one color per theme that reads at 5:1 in messages, replies and the composer", () => {
102 for (const { name, theme } of BUILT_IN_THEMES) {
103 for (const accent of CHIP_ACCENTS) {
104 const shown = contextChipAccent(accent, theme, theme["--color-md-body"])!;
105 for (const { surface, text } of chipSurfaces(theme)) {
106 expect(contextChipAccent(accent, theme, text)).toBe(shown);
107 expect(
108 contrastRatio(shown, surface),
109 `${name}: ${accent} → ${shown} on ${surface}`,
110 ).toBeGreaterThanOrEqual(5);
111 }
112 }
113 }
114 });
115
116 it("reads on both a saturated Material You bubble and the light canvas beside it", () => {
117 const theme = {
118 ...getMobileThemeVariables("t3-code", "light"),
119 "--color-user-bubble": "#6750a4",
120 "--color-user-bubble-foreground": "#ffffff",
121 };
122 for (const { surface, text } of chipSurfaces(theme)) {
123 for (const accent of CHIP_ACCENTS) {
124 const shown = contextChipAccent(accent, theme, text)!;
125 expect(
126 contrastRatio(shown, surface),
127 `${accent} → ${shown} on ${surface}`,
128 ).toBeGreaterThanOrEqual(5);
129 }
130 }
131 });
132
133 it("keeps an accent that already reads everywhere", () => {
134 const theme = getMobileThemeVariables("t3-code", "dark");
135 expect(contextChipAccent("#009f6e", theme, theme["--color-md-body"])).toBe("#009f6e");
136 });
137});
138
139describe("contextChipPresentation", () => {
140 it("draws a mentioned machine as a machine, not the generic file chip", () => {
141 expect(contextChipPresentation("environment")).toEqual({ accent: "#3f9a4f", icon: "server" });
142 });
143});