-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathvariable-parser.test.ts
More file actions
345 lines (317 loc) · 11.6 KB
/
variable-parser.test.ts
File metadata and controls
345 lines (317 loc) · 11.6 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import * as fs from 'node:fs/promises';
import stringify from 'json-stringify-pretty-compact';
import { describe, expect, it, vi } from 'vitest';
import * as data from '../src/data';
import type { FontObjectVariableDirect } from '../src/types';
import {
addAndMergeAxesRange,
fetchAllCSS,
generateCSSLinks,
parseCSS,
parseVariable,
sortAxes,
} from '../src/variable-parser';
import { apiParseVariableHandlers, setupAPIServer } from './mocks/index';
import {
cssFixtureVariable,
dataFixture,
getFontResponse,
} from './utils/helpers';
vi.mock('node:fs/promises');
vi.mock('../src/data');
describe('Variable Parser', () => {
setupAPIServer(apiParseVariableHandlers);
const response = dataFixture('variable-response');
vi.spyOn(data, 'APIVariableDirect', 'get').mockReturnValue(response);
describe('Sort axes in alphabetic order', () => {
it('should sort lowercase', () => {
const axes = ['wght', 'ital', 'slnt'];
const sortedAxes = sortAxes(axes);
expect(sortedAxes).toEqual(['ital', 'slnt', 'wght']);
});
it('should sort uppercase', () => {
const axes = ['XTRA', 'YTUC', 'YTLC', 'FILL'];
const sortedAxes = sortAxes(axes);
expect(sortedAxes).toEqual(['FILL', 'XTRA', 'YTLC', 'YTUC']);
});
it('should sort both cases', () => {
const axes = ['XTRA', 'YTUC', 'YTLC', 'WONK', 'wght', 'ital', 'slnt'];
const sortedAxes = sortAxes(axes);
expect(sortedAxes).toEqual([
'ital',
'slnt',
'wght',
'WONK',
'XTRA',
'YTLC',
'YTUC',
]);
});
});
describe('Add, sort and merge axes ranges', () => {
const fraunces = getFontResponse(
response,
'fraunces',
) as FontObjectVariableDirect;
it('Returns tuple without ital', () => {
expect(
addAndMergeAxesRange(fraunces, ['wght', 'opsz'], ['SOFT']),
).toEqual(['opsz,wght,SOFT', '9..144,100..900,0..100']);
});
it('Returns tuple with existing ital', () => {
expect(
addAndMergeAxesRange(fraunces, ['wght', 'opsz', 'ital'], ['SOFT']),
).toEqual(['ital,opsz,wght,SOFT', '1,9..144,100..900,0..100']);
});
it('Returns tuple with new ital', () => {
expect(
addAndMergeAxesRange(fraunces, ['wght', 'opsz', 'SOFT'], ['ital']),
).toEqual(['ital,opsz,wght,SOFT', '1,9..144,100..900,0..100']);
});
it('Returns tuple with both wght and ital', () => {
expect(
addAndMergeAxesRange(fraunces, ['opsz', 'SOFT'], ['wght', 'ital']),
).toEqual(['ital,opsz,wght,SOFT', '1,9..144,100..900,0..100']);
});
});
describe('Generate all CSS links to scrape', () => {
it('Gets valid links only wght, no ital', () => {
const akshar = getFontResponse(
response,
'akshar',
) as FontObjectVariableDirect;
expect(generateCSSLinks(akshar)).toEqual({
'wght.normal':
'https://fonts.googleapis.com/css2?family=Akshar:wght@300..700',
});
});
it('Gets valid links wght and ital', () => {
const alegreya = getFontResponse(
response,
'alegreya',
) as FontObjectVariableDirect;
expect(generateCSSLinks(alegreya)).toEqual({
'wght.italic':
'https://fonts.googleapis.com/css2?family=Alegreya:ital,wght@1,400..900',
'wght.normal':
'https://fonts.googleapis.com/css2?family=Alegreya:wght@400..900',
});
});
// Edge case e.g. Ballet which only has opsz and no wght
it('Gets valid links with no wght axis', () => {
const ballet = getFontResponse(
response,
'ballet',
) as FontObjectVariableDirect;
expect(generateCSSLinks(ballet)).toEqual({
'opsz.normal':
'https://fonts.googleapis.com/css2?family=Ballet:opsz@16..72',
'standard.normal':
'https://fonts.googleapis.com/css2?family=Ballet:opsz@16..72',
});
});
it('Gets valid links additional axis GRAD, XTRA, YOPQ, YTAS, YTDE, YTFI, YTLC, YTUC, opsz, slnt, wdth', () => {
const robotoflex = getFontResponse(
response,
'roboto-flex',
) as FontObjectVariableDirect;
expect(generateCSSLinks(robotoflex)).toEqual({
'GRAD.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:wght,GRAD@100..1000,-200..150',
'XTRA.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:wght,XTRA@100..1000,323..603',
'YOPQ.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:wght,YOPQ@100..1000,25..135',
'YTAS.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:wght,YTAS@100..1000,649..854',
'YTDE.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:wght,YTDE@100..1000,-305..-98',
'YTFI.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:wght,YTFI@100..1000,560..788',
'YTLC.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:wght,YTLC@100..1000,416..570',
'YTUC.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:wght,YTUC@100..1000,528..760',
'full.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:opsz,slnt,wdth,wght,GRAD,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC@8..144,-10..0,25..151,100..1000,-200..150,323..603,25..135,649..854,-305..-98,560..788,416..570,528..760',
'opsz.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:opsz,wght@8..144,100..1000',
'slnt.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:slnt,wght@-10..0,100..1000',
'standard.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:opsz,slnt,wdth,wght@8..144,-10..0,25..151,100..1000',
'wdth.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:wdth,wght@25..151,100..1000',
'wght.normal':
'https://fonts.googleapis.com/css2?family=Roboto+Flex:wght@100..1000',
});
});
it('Gets valid links additional axis CASL, CRSV, MONO, slnt', () => {
const recursive = getFontResponse(
response,
'recursive',
) as FontObjectVariableDirect;
expect(generateCSSLinks(recursive)).toEqual({
'CASL.normal':
'https://fonts.googleapis.com/css2?family=Recursive:wght,CASL@300..1000,0..1',
'CRSV.normal':
'https://fonts.googleapis.com/css2?family=Recursive:wght,CRSV@300..1000,0..1',
'MONO.normal':
'https://fonts.googleapis.com/css2?family=Recursive:wght,MONO@300..1000,0..1',
'full.normal':
'https://fonts.googleapis.com/css2?family=Recursive:slnt,wght,CASL,CRSV,MONO@-15..0,300..1000,0..1,0..1,0..1',
'slnt.normal':
'https://fonts.googleapis.com/css2?family=Recursive:slnt,wght@-15..0,300..1000',
'standard.normal':
'https://fonts.googleapis.com/css2?family=Recursive:slnt,wght@-15..0,300..1000',
'wght.normal':
'https://fonts.googleapis.com/css2?family=Recursive:wght@300..1000',
});
});
it('Gets valid links additional axis with ital SOFT, WONK', () => {
const fraunces = getFontResponse(
response,
'fraunces',
) as FontObjectVariableDirect;
expect(generateCSSLinks(fraunces)).toEqual({
'SOFT.italic':
'https://fonts.googleapis.com/css2?family=Fraunces:ital,wght,SOFT@1,100..900,0..100',
'SOFT.normal':
'https://fonts.googleapis.com/css2?family=Fraunces:wght,SOFT@100..900,0..100',
'WONK.italic':
'https://fonts.googleapis.com/css2?family=Fraunces:ital,wght,WONK@1,100..900,0..1',
'WONK.normal':
'https://fonts.googleapis.com/css2?family=Fraunces:wght,WONK@100..900,0..1',
'full.italic':
'https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght,SOFT,WONK@1,9..144,100..900,0..100,0..1',
'full.normal':
'https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght,SOFT,WONK@9..144,100..900,0..100,0..1',
'opsz.italic':
'https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght@1,9..144,100..900',
'opsz.normal':
'https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,100..900',
'standard.italic':
'https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght@1,9..144,100..900',
'standard.normal':
'https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,100..900',
'wght.italic':
'https://fonts.googleapis.com/css2?family=Fraunces:ital,wght@1,100..900',
'wght.normal':
'https://fonts.googleapis.com/css2?family=Fraunces:wght@100..900',
});
});
});
describe('Fetch CSS', () => {
it('Returns all types of CSS together', async () => {
const fraunces = getFontResponse(
response,
'fraunces',
) as FontObjectVariableDirect;
const links = generateCSSLinks(fraunces);
const cssAll = await fetchAllCSS(links);
for (const [key, css] of cssAll) {
const [type, style] = key.split('.');
expect(css).toEqual(cssFixtureVariable('fraunces', type, style));
}
});
});
describe('Parse CSS with numbered subsets', () => {
it('Handles numbered subsets without comments (new format)', () => {
// Mock CSS data that mimics the new Google Fonts format without comments
// but with numbered subsets in the URL filenames
const cssWithoutComments: string[][] = [
[
'wght.normal',
`@font-face {
font-family: 'Noto Sans JP Variable';
font-style: normal;
font-weight: 100 900;
src: url(https://fonts.gstatic.com/s/notosansjp/v55/variable-font.0.woff2) format('woff2');
}
@font-face {
font-family: 'Noto Sans JP Variable';
font-style: normal;
font-weight: 100 900;
src: url(https://fonts.gstatic.com/s/notosansjp/v55/variable-font.1.woff2) format('woff2');
}
@font-face {
font-family: 'Noto Sans JP Variable';
font-style: normal;
font-weight: 100 900;
src: url(https://fonts.gstatic.com/s/notosansjp/v55/variable-font.119.woff2) format('woff2');
}`,
],
];
const result = parseCSS(cssWithoutComments);
// Should extract numbered subsets from URLs: [0], [1], and [119]
expect(result.wght.normal['[0]']).toBe(
'https://fonts.gstatic.com/s/notosansjp/v55/variable-font.0.woff2',
);
expect(result.wght.normal['[1]']).toBe(
'https://fonts.gstatic.com/s/notosansjp/v55/variable-font.1.woff2',
);
expect(result.wght.normal['[119]']).toBe(
'https://fonts.gstatic.com/s/notosansjp/v55/variable-font.119.woff2',
);
});
it('Handles numbered subsets with comments (old format)', () => {
// Mock CSS data with comments (old format)
const cssWithComments: string[][] = [
[
'wght.normal',
`/* [0] */
@font-face {
font-family: 'Noto Sans JP Variable';
font-style: normal;
font-weight: 100 900;
src: url(https://fonts.gstatic.com/s/notosansjp/v55/variable-font.0.woff2) format('woff2');
}
/* [1] */
@font-face {
font-family: 'Noto Sans JP Variable';
font-style: normal;
font-weight: 100 900;
src: url(https://fonts.gstatic.com/s/notosansjp/v55/variable-font.1.woff2) format('woff2');
}`,
],
];
const result = parseCSS(cssWithComments);
// Should use comment-based subset names [0] and [1]
expect(result.wght.normal['[0]']).toBe(
'https://fonts.gstatic.com/s/notosansjp/v55/variable-font.0.woff2',
);
expect(result.wght.normal['[1]']).toBe(
'https://fonts.gstatic.com/s/notosansjp/v55/variable-font.1.woff2',
);
});
it('Falls back to default subset for non-numbered URLs', () => {
// Mock CSS data with regular URLs (no numbered pattern)
const cssRegular: string[][] = [
[
'wght.normal',
`/* latin */
@font-face {
font-family: 'Roboto Flex';
font-style: normal;
font-weight: 100 1000;
src: url(https://fonts.gstatic.com/s/robotoflex/v30/regular.woff2) format('woff2');
}`,
],
];
const result = parseCSS(cssRegular);
// Should use comment-based subset name 'latin'
expect(result.wght.normal.latin).toBe(
'https://fonts.gstatic.com/s/robotoflex/v30/regular.woff2',
);
});
});
describe('Full parse and order', () => {
it('Parses successfully', async () => {
await parseVariable(false);
expect(vi.mocked(fs.writeFile)).toHaveBeenCalledWith(
expect.anything(),
stringify(dataFixture('variable')),
);
});
});
});