@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 148 lines 8.24 KB
Raw
1 import { describe, expect, it } from "vite-plus/test";
2
3 import { resolveProjectFaviconColorScheme } from "./projectFaviconColorScheme";
4
5 const svgDataUrl = (svg: string) =>
6 `data:image/svg+xml;base64,${Buffer.from(svg, "utf8").toString("base64")}`;
7 const decode = (dataUrl: string) =>
8 Buffer.from(dataUrl.slice(dataUrl.indexOf(",") + 1), "base64").toString("utf8");
9
10 const PATH = `<path d="M 40 220 L 220 40 L 400 220 L 220 400 Z"/>`;
11 // Strokes with currentColor and switches the root color per scheme with @media.
12 const ISSUE_ICON = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 440 440" fill="none" stroke="currentColor" stroke-width="9"><style>:root{color:#111827}@media(prefers-color-scheme:dark){:root{color:#F8FAFC}}</style>${PATH}</svg>`;
13
14 describe("resolveProjectFaviconColorScheme", () => {
15 it("gives the root the dark color the stylesheet picks in dark mode", () => {
16 const resolved = decode(resolveProjectFaviconColorScheme(svgDataUrl(ISSUE_ICON), "dark"));
17
18 expect(resolved).toBe(
19 `<svg color="#F8FAFC" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 440 440" fill="none" stroke="currentColor" stroke-width="9"><style>:root{color:#111827}:root{color:#F8FAFC}</style>${PATH}</svg>`,
20 );
21 });
22
23 it("gives the root the light color in light mode", () => {
24 const resolved = decode(resolveProjectFaviconColorScheme(svgDataUrl(ISSUE_ICON), "light"));
25
26 expect(resolved).toBe(
27 `<svg color="#111827" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 440 440" fill="none" stroke="currentColor" stroke-width="9"><style>:root{color:#111827}</style>${PATH}</svg>`,
28 );
29 });
30
31 it("keeps the rules of the matching color-scheme block and drops the other", () => {
32 const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>.a{fill:#000} @media (prefers-color-scheme: dark) { .a{fill:#fff} .b{stroke:#eee} } @media (prefers-color-scheme: light) { .b{stroke:#111} }</style></svg>`;
33
34 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
35 `<svg xmlns="http://www.w3.org/2000/svg"><style>.a{fill:#000} .a{fill:#fff} .b{stroke:#eee} </style></svg>`,
36 );
37 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "light"))).toBe(
38 `<svg xmlns="http://www.w3.org/2000/svg"><style>.a{fill:#000} .b{stroke:#111} </style></svg>`,
39 );
40 });
41
42 it("replaces an existing root color attribute", () => {
43 const icon = `<svg xmlns="http://www.w3.org/2000/svg" color='red' stroke="currentColor"><style>svg{color:blue}</style></svg>`;
44
45 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "light"))).toBe(
46 `<svg color="blue" xmlns="http://www.w3.org/2000/svg" stroke="currentColor"><style>svg{color:blue}</style></svg>`,
47 );
48 });
49
50 it("takes the last root color declaration outside other at-rules", () => {
51 const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:red;color:blue}@media print{:root{color:green}}</style></svg>`;
52
53 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "light"))).toBe(
54 `<svg color="blue" xmlns="http://www.w3.org/2000/svg"><style>:root{color:red;color:blue}@media print{:root{color:green}}</style></svg>`,
55 );
56 const variable = icon.replace("color:blue", "color:var(--icon)");
57 expect(resolveProjectFaviconColorScheme(svgDataUrl(variable), "dark")).toBe(
58 svgDataUrl(variable),
59 );
60 const media = `<svg><style>:root{color:blue}</style><style media="(prefers-color-scheme: dark)">:root{color:white}.a{fill:white}</style><style media="print">:root{color:red}</style></svg>`;
61 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(media), "light"))).toBe(
62 `<svg color="blue"><style>:root{color:blue}</style><style media="print">:root{color:red}</style></svg>`,
63 );
64 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(media), "dark"))).toBe(
65 `<svg color="white"><style>:root{color:blue}</style><style>:root{color:white}.a{fill:white}</style><style media="print">:root{color:red}</style></svg>`,
66 );
67 });
68
69 it("lets an important root color beat later normal ones", () => {
70 const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:#111 !important}@media (prefers-color-scheme: dark){:root{color:#fff!important}}:root{color:red}</style></svg>`;
71
72 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
73 `<svg color="#fff" xmlns="http://www.w3.org/2000/svg"><style>:root{color:#111 !important}:root{color:#fff!important}:root{color:red}</style></svg>`,
74 );
75 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "light"))).toBe(
76 `<svg color="#111" xmlns="http://www.w3.org/2000/svg"><style>:root{color:#111 !important}:root{color:red}</style></svg>`,
77 );
78 });
79
80 it("keeps an important root color across style elements", () => {
81 const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:#fff !important}</style><style>:root{color:#000}</style></svg>`;
82
83 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
84 `<svg color="#fff" xmlns="http://www.w3.org/2000/svg"><style>:root{color:#fff !important}</style><style>:root{color:#000}</style></svg>`,
85 );
86 });
87
88 it("ignores braces and color-scheme blocks inside CSS comments", () => {
89 const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>/* @media (prefers-color-scheme: dark) { */.a{fill:#000}/* } */@media (prefers-color-scheme: dark){/* } */.a{fill:#fff}}</style></svg>`;
90
91 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "light"))).toBe(
92 `<svg xmlns="http://www.w3.org/2000/svg"><style>.a{fill:#000}</style></svg>`,
93 );
94 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
95 `<svg xmlns="http://www.w3.org/2000/svg"><style>.a{fill:#000}.a{fill:#fff}</style></svg>`,
96 );
97 });
98
99 it("colors the root element, not an svg tag in an earlier comment", () => {
100 const icon = `<!-- <svg width="1"> --><SVG xmlns="http://www.w3.org/2000/svg"><style>:root{color:#fff}</style></SVG>`;
101
102 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
103 `<!-- <svg width="1"> --><SVG color="#fff" xmlns="http://www.w3.org/2000/svg"><style>:root{color:#fff}</style></SVG>`,
104 );
105 });
106
107 it("leaves other media queries for the renderer", () => {
108 const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>@media screen and (prefers-color-scheme: dark){.a{fill:#fff}}@media not (prefers-color-scheme: dark){.a{fill:#000}}</style></svg>`;
109
110 expect(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark")).toBe(svgDataUrl(icon));
111 });
112
113 it("ignores a root color that is not a plain color value", () => {
114 const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:red$'}</style></svg>`;
115
116 expect(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark")).toBe(svgDataUrl(icon));
117 const padded = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:x${" \t".repeat(3000)}!bad}</style></svg>`;
118 expect(resolveProjectFaviconColorScheme(svgDataUrl(padded), "dark")).toBe(svgDataUrl(padded));
119 });
120
121 it("ignores style elements inside XML comments", () => {
122 const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:blue}</style><!-- <style>:root{color:red!important}</style> --></svg>`;
123
124 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
125 icon.replace("<svg ", '<svg color="blue" '),
126 );
127 });
128
129 it("keeps root selector specificity across style elements", () => {
130 const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:blue}</style><style>svg{color:red}</style></svg>`;
131
132 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
133 icon.replace("<svg ", '<svg color="blue" '),
134 );
135 const important = icon.replace("color:red", "color:red!important");
136 expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(important), "dark"))).toBe(
137 important.replace("<svg ", '<svg color="red" '),
138 );
139 });
140
141 it("returns non-SVG and remote URLs unchanged", () => {
142 const png = "data:image/png;base64,iVBORw0KGgo=";
143 const remote = "https://host/api/assets/token/icon.svg";
144
145 expect(resolveProjectFaviconColorScheme(png, "dark")).toBe(png);
146 expect(resolveProjectFaviconColorScheme(remote, "dark")).toBe(remote);
147 });
148 });