File size: 7,335 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {
	colorizeFaviconSvg,
	padFaviconSvg,
	writeThemeFavicons
} from '../../scripts/favicon-colorize';
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

const SOURCE_SVG = [
	'<svg xmlns="http://www.w3.org/2000/svg">',
	'  <path d="M0 0" fill="currentColor"/>',
	'  <path d="M1 1" fill="#ff00aa"/>',
	'  <circle fill="currentColor"/>',
	'</svg>'
].join('\n');

describe('colorizeFaviconSvg', () => {
	it('substitutes every currentColor occurrence for the light variant', () => {
		const { light } = colorizeFaviconSvg(SOURCE_SVG, '#111111', '#fafafa');

		expect(light.match(/currentColor/g)).toBeNull();
		expect(light).toContain('fill="#111111"');
		expect(light).toContain('<circle fill="#111111"/>');
	});

	it('substitutes every currentColor occurrence for the dark variant', () => {
		const { dark } = colorizeFaviconSvg(SOURCE_SVG, '#111111', '#fafafa');

		expect(dark.match(/currentColor/g)).toBeNull();
		expect(dark).toContain('fill="#fafafa"');
		expect(dark).toContain('<circle fill="#fafafa"/>');
	});

	it('leaves non-currentColor colors untouched in both variants', () => {
		const { dark, light } = colorizeFaviconSvg(SOURCE_SVG, '#111111', '#fafafa');

		expect(light).toContain('fill="#ff00aa"');
		expect(dark).toContain('fill="#ff00aa"');
	});

	it('does not alter any other part of the SVG', () => {
		const { dark, light } = colorizeFaviconSvg(SOURCE_SVG, '#111111', '#fafafa');
		const stripColors = (s: string) =>
			s.replaceAll('#111111', '').replaceAll('#fafafa', '').replaceAll('currentColor', '');
		const expected = stripColors(SOURCE_SVG);

		expect(stripColors(light)).toBe(expected);
		expect(stripColors(dark)).toBe(expected);
	});

	it('returns the same SVG for light and dark when called with the same color', () => {
		const result = colorizeFaviconSvg(SOURCE_SVG, '#abcdef', '#abcdef');

		expect(result.light).toBe(result.dark);
	});

	it('returns the source unchanged when given a color that does not appear (no currentColor in source)', () => {
		const plain = '<svg><path fill="#000"/></svg>';
		const { dark, light } = colorizeFaviconSvg(plain, '#111111', '#fafafa');

		expect(light).toBe(plain);
		expect(dark).toBe(plain);
	});
});

describe('padFaviconSvg', () => {
	const SIZED_SVG =
		'<svg width="512" height="512" viewBox="0 0 512 512" fill="none" xmlns="http://www.w3.org/2000/svg">' +
		'<path d="M244.95 8L388.923 8Z" fill="currentColor"/>' +
		'</svg>';

	it('wraps inner content in a translate-then-scale group that matches padding', () => {
		const padded = padFaviconSvg(SIZED_SVG, 0.05);

		// scale = 1 - 0.05 = 0.95
		// translate = (0.05 * 512) / 2 = 12.8 on each axis
		expect(padded).toContain('transform="translate(12.8 12.8) scale(0.95)"');
		expect(padded).toContain('<g transform="translate(12.8 12.8) scale(0.95)">');
		expect(padded).toContain('<path d="M244.95 8L388.923 8Z" fill="currentColor"/>');
		expect(padded.endsWith('</g></svg>')).toBe(true);
	});

	it('preserves the outer <svg> tag attributes', () => {
		const padded = padFaviconSvg(SIZED_SVG, 0.1);

		expect(padded.startsWith('<svg width="512" height="512" viewBox="0 0 512 512"')).toBe(true);
	});

	it('returns the input unchanged for zero or negative padding', () => {
		expect(padFaviconSvg(SIZED_SVG, 0)).toBe(SIZED_SVG);
		expect(padFaviconSvg(SIZED_SVG, -0.1)).toBe(SIZED_SVG);
	});

	it('returns the input unchanged when padding would fully collapse the icon (>= 1)', () => {
		expect(padFaviconSvg(SIZED_SVG, 1)).toBe(SIZED_SVG);
		expect(padFaviconSvg(SIZED_SVG, 1.5)).toBe(SIZED_SVG);
	});

	it('returns the input unchanged when no viewBox is present', () => {
		const noViewBox = '<svg width="32" height="32"><path d="M0 0Z"/></svg>';

		expect(padFaviconSvg(noViewBox, 0.1)).toBe(noViewBox);
	});

	it('returns the input unchanged when viewBox values are not finite numbers', () => {
		const bad = '<svg viewBox="auto auto 0 0"><path/></svg>';

		expect(padFaviconSvg(bad, 0.1)).toBe(bad);
	});

	it('tolerates a non-square viewBox', () => {
		const wide = '<svg viewBox="0 0 100 50"><rect/></svg>';
		const padded = padFaviconSvg(wide, 0.1);

		// scale 0.9, translate (5, 2.5)
		expect(padded).toContain('transform="translate(5 2.5) scale(0.9)"');
	});
});

describe('writeThemeFavicons', () => {
	const LOGO =
		'<svg width="512" height="512" viewBox="0 0 512 512" fill="none" xmlns="http://www.w3.org/2000/svg">' +
		'<path d="M244.95 8L388.923 8Z" fill="currentColor"/>' +
		'</svg>';

	let tmpDir: string;

	beforeEach(() => {
		tmpDir = mkdtempSync(join(tmpdir(), 'favicon-'));
	});

	afterEach(() => {
		rmSync(tmpDir, { force: true, recursive: true });
	});

	function setupSource() {
		const sourcePath = join(tmpDir, 'logo.svg');

		writeFileSync(sourcePath, LOGO);

		return {
			darkPath: join(tmpDir, 'favicon-dark.svg'),
			lightPath: join(tmpDir, 'favicon.svg'),
			sourcePath
		};
	}

	it('writes colorized, un-padded favicons without modifying the source', () => {
		const { darkPath, lightPath, sourcePath } = setupSource();
		const before = readFileSync(sourcePath, 'utf-8');

		writeThemeFavicons('#abcdef', '#012345', {
			darkOutPath: darkPath,
			lightOutPath: lightPath,
			sourcePath
		});

		const lightOut = readFileSync(lightPath, 'utf-8');
		const darkOut = readFileSync(darkPath, 'utf-8');

		// currentColor swapped to the requested palette in both files
		expect(lightOut).toContain('fill="#abcdef"');
		expect(lightOut).not.toContain('currentColor');
		expect(darkOut).toContain('fill="#012345"');
		expect(darkOut).not.toContain('currentColor');

		// default padding (0) keeps the wrapper off the output
		expect(lightOut).not.toContain('<g ');
		expect(darkOut).not.toContain('<g ');

		// source file is unchanged after the call
		expect(readFileSync(sourcePath, 'utf-8')).toBe(before);
	});

	it('writes colorized favicons wrapped in a padding <g transform>...</g>', () => {
		const { darkPath, lightPath, sourcePath } = setupSource();
		// mirror the production wiring: PWA_ASSET_GENERATOR.FAVICON_PADDING
		const padding = 0.04;
		// scale = 1 - 0.04 = 0.96; translate = (0.04 * 512) / 2 = 10.24
		const expectedTransform = 'transform="translate(10.24 10.24) scale(0.96)"';

		writeThemeFavicons('#111111', '#fafafa', {
			darkOutPath: darkPath,
			lightOutPath: lightPath,
			padding,
			sourcePath
		});

		const lightOut = readFileSync(lightPath, 'utf-8');
		const darkOut = readFileSync(darkPath, 'utf-8');

		// both files get the same padding wrapper derived from the viewBox
		expect(lightOut).toContain(`<g ${expectedTransform}>`);
		expect(lightOut.endsWith('</g></svg>')).toBe(true);
		expect(darkOut).toContain(`<g ${expectedTransform}>`);
		expect(darkOut.endsWith('</g></svg>')).toBe(true);

		// colorization still happens inside the wrapped group
		expect(lightOut).toContain('fill="#111111"');
		expect(lightOut).not.toContain('currentColor');
		expect(darkOut).toContain('fill="#fafafa"');
		expect(darkOut).not.toContain('currentColor');
		// light palette colour must not leak into dark output
		expect(darkOut).not.toContain('#111111');

		// source file is not modified by the padding step
		expect(readFileSync(sourcePath, 'utf-8')).toBe(LOGO);
	});
});