-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathscrapeVersions.test.ts
More file actions
498 lines (430 loc) · 13.8 KB
/
Copy pathscrapeVersions.test.ts
File metadata and controls
498 lines (430 loc) · 13.8 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import { describe, it, expect, vi, beforeEach } from 'vitest';
import {
scrapeLatestOfficialUnityVersion,
scrapeVersions,
scrapeRecentOfficialUnityVersions,
} from '../src/logic/ingestUnityVersions/scrapeVersions';
import { SearchMode } from 'unity-changeset';
import fetch from 'node-fetch';
// Mock the unity-changeset module
vi.mock('unity-changeset', async () => {
const actual = await vi.importActual('unity-changeset');
return {
...actual,
searchChangesets: vi.fn(),
};
});
vi.mock('node-fetch', () => ({
default: vi.fn(),
}));
const { searchChangesets } = await import('unity-changeset');
const mockedFetch = fetch as unknown as vi.MockedFunction<typeof fetch>;
const mockOfficialUnityRelease = (
html = '<h1>Unity 6000.4.10f1</h1><p>Changeset: feeafc12a938</p>',
) => {
mockedFetch.mockResolvedValue({
ok: true,
status: 200,
text: async () => html,
} as any);
};
describe('scrapeVersions', () => {
beforeEach(() => {
vi.clearAllMocks();
mockOfficialUnityRelease('');
});
it('should fetch both default and XLTS versions', async () => {
// Mock return values for both Default and XLTS search modes
const mockDefaultVersions = [
{
version: '2022.3.20f1',
changeset: 'abc123def456',
},
{
version: '2023.2.10f1',
changeset: '234567ab8cd9',
},
];
const mockXltsVersions = [
{
version: '2022.3.21f1', // XLTS versions might have same format as regular versions
changeset: '789abcdef012',
},
{
version: '2021.3.25f1',
changeset: '345cdef67890',
},
];
(searchChangesets as vi.MockedFunction<any>).mockImplementation(async (mode: SearchMode) => {
if (mode === SearchMode.Default) {
return mockDefaultVersions;
} else if (mode === SearchMode.XLTS) {
return mockXltsVersions;
}
return [];
});
const result = await scrapeVersions();
// Verify that both Default and XLTS methods were called
expect(searchChangesets).toHaveBeenCalledWith(SearchMode.Default);
expect(searchChangesets).toHaveBeenCalledWith(SearchMode.XLTS);
// Verify the result includes both default and XLTS versions
expect(result).toHaveLength(4); // 2 default + 2 XLTS (assuming no overlap)
// Check that the default versions are present
expect(result).toContainEqual(
expect.objectContaining({
version: '2022.3.20f1',
changeSet: 'abc123def456',
major: 2022,
minor: 3,
patch: '20',
}),
);
expect(result).toContainEqual(
expect.objectContaining({
version: '2023.2.10f1',
changeSet: '234567ab8cd9',
major: 2023,
minor: 2,
patch: '10',
}),
);
// Check that the XLTS versions are present
expect(result).toContainEqual(
expect.objectContaining({
version: '2022.3.21f1',
changeSet: '789abcdef012',
major: 2022,
minor: 3,
patch: '21',
}),
);
expect(result).toContainEqual(
expect.objectContaining({
version: '2021.3.25f1',
changeSet: '345cdef67890',
major: 2021,
minor: 3,
patch: '25',
}),
);
});
it('should merge the latest official Unity release when unity-changeset is stale', async () => {
mockOfficialUnityRelease();
const mockDefaultVersions = [
{
version: '6000.4.7f1',
changeset: 'f3c3c4248748',
},
];
(searchChangesets as vi.MockedFunction<any>).mockImplementation(async (mode: SearchMode) => {
if (mode === SearchMode.Default) {
return mockDefaultVersions;
}
return [];
});
const result = await scrapeVersions();
expect(result).toContainEqual(
expect.objectContaining({
version: '6000.4.10f1',
changeSet: 'feeafc12a938',
major: 6000,
minor: 4,
patch: '10',
}),
);
});
it('should parse the latest official Unity release page', async () => {
mockOfficialUnityRelease('<h1>Unity 6000.4.10f1</h1><div>Changeset: feeafc12a938</div>');
await expect(scrapeLatestOfficialUnityVersion()).resolves.toEqual(
expect.objectContaining({
version: '6000.4.10f1',
changeSet: 'feeafc12a938',
major: 6000,
minor: 4,
patch: '10',
}),
);
});
it('should parse the Unity Hub install URL from the official release page', async () => {
mockOfficialUnityRelease(
'<h1>Unity 6000.4.10f1</h1><a href="unityhub://6000.4.10f1/feeafc12a938">Install</a>',
);
await expect(scrapeLatestOfficialUnityVersion()).resolves.toEqual(
expect.objectContaining({
version: '6000.4.10f1',
changeSet: 'feeafc12a938',
}),
);
});
it('should not duplicate versions when XLTS versions overlap with default versions', async () => {
// Mock return values where one XLTS version is the same as a default version
const mockDefaultVersions = [
{
version: '2022.3.20f1',
changeset: 'abc123def456',
},
];
const mockXltsVersions = [
{
version: '2022.3.20f1', // Duplicate version
changeset: 'abc123def456',
},
{
version: '2022.3.21f1',
changeset: '789abcdef012',
},
];
(searchChangesets as vi.MockedFunction<any>).mockImplementation(async (_mode: SearchMode) => {
if (_mode === SearchMode.Default) {
return mockDefaultVersions;
} else if (_mode === SearchMode.XLTS) {
return mockXltsVersions;
}
return [];
});
const result = await scrapeVersions();
// Verify that duplicate was filtered out
expect(result).toHaveLength(2); // Only 2 unique versions
expect(result.some((v) => v.version === '2022.3.20f1')).toBe(true);
expect(result.some((v) => v.version === '2022.3.21f1')).toBe(true);
});
it('should filter out non-final versions (not containing "f")', async () => {
const mockDefaultVersions = [
{
version: '2022.3.20f1', // Final version - should be included
changeset: 'abc123def456',
},
{
version: '2022.3.20a1', // Alpha version - should be excluded
changeset: '234567ab8cd9',
},
];
const mockXltsVersions = [
{
version: '2021.3.25f1', // Final version - should be included
changeset: '789abcdef012',
},
{
version: '2020.3.15a2', // Alpha version - should be excluded
changeset: '345cdef67890',
},
];
(searchChangesets as vi.MockedFunction<any>).mockImplementation(async (_mode: SearchMode) => {
if (_mode === SearchMode.Default) {
return mockDefaultVersions;
} else if (_mode === SearchMode.XLTS) {
return mockXltsVersions;
}
return [];
});
const result = await scrapeVersions();
// Should only contain the final versions
expect(result).toHaveLength(2);
expect(result).toContainEqual(
expect.objectContaining({
version: '2022.3.20f1',
changeSet: 'abc123def456',
major: 2022,
minor: 3,
patch: '20',
}),
);
expect(result).toContainEqual(
expect.objectContaining({
version: '2021.3.25f1',
changeSet: '789abcdef012',
major: 2021,
minor: 3,
patch: '25',
}),
);
// Alpha versions should be excluded
expect(result.some((v) => v.version.includes('a1'))).toBe(false);
expect(result.some((v) => v.version.includes('a2'))).toBe(false);
});
it('should filter out versions with major number less than 2017', async () => {
const mockDefaultVersions = [
{
version: '2022.3.20f1', // Should be included
changeset: 'abc123def456',
},
{
version: '5.6.7f1', // Should be excluded (major < 2017)
changeset: '234567ab8cd9',
},
];
const mockXltsVersions = [
{
version: '2021.3.25f1', // Should be included
changeset: '789abcdef012',
},
];
(searchChangesets as vi.MockedFunction<any>).mockImplementation(async (mode: SearchMode) => {
if (mode === SearchMode.Default) {
return mockDefaultVersions;
} else if (mode === SearchMode.XLTS) {
return mockXltsVersions;
}
return [];
});
const result = await scrapeVersions();
// Should only contain versions with major >= 2017
expect(result).toHaveLength(2);
expect(result).toContainEqual(
expect.objectContaining({
version: '2022.3.20f1',
changeSet: 'abc123def456',
major: 2022,
minor: 3,
patch: '20',
}),
);
expect(result).toContainEqual(
expect.objectContaining({
version: '2021.3.25f1',
changeSet: '789abcdef012',
major: 2021,
minor: 3,
patch: '25',
}),
);
// Old version should be excluded
expect(result.some((v) => v.major < 2017)).toBe(false);
});
it('should throw an error when no Unity versions are found', async () => {
(searchChangesets as vi.MockedFunction<any>).mockImplementation(async (_mode: SearchMode) => {
return [];
});
await expect(scrapeVersions()).rejects.toThrow('No Unity versions found!');
});
});
describe('scrapeRecentOfficialUnityVersions', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('should discover multiple recent versions from the releases page', async () => {
const html = `
<h1>Unity 6000.4.10f1</h1>
<a href="unityhub://6000.4.10f1/feeafc12a938">Install</a>
<h2>Unity 6000.3.17f1</h2>
<p>Changeset: abc123def456</p>
<h2>Unity 6000.2.5f1</h2>
<a href="unityhub://6000.2.5f1/deadbeef0123">Download</a>
`;
mockedFetch.mockResolvedValue({
ok: true,
status: 200,
text: async () => html,
} as any);
const result = await scrapeRecentOfficialUnityVersions();
expect(result).toHaveLength(3);
expect(result.map((v) => v.version)).toContain('6000.4.10f1');
expect(result.map((v) => v.version)).toContain('6000.3.17f1');
expect(result.map((v) => v.version)).toContain('6000.2.5f1');
});
it('should extract changesets from unityhub:// URLs', async () => {
const html = `
<a href="unityhub://6000.4.10f1/feeafc12a938">Install</a>
<a href="unityhub://6000.3.17f1/abc123def456">Install</a>
`;
mockedFetch.mockResolvedValue({
ok: true,
status: 200,
text: async () => html,
} as any);
const result = await scrapeRecentOfficialUnityVersions();
expect(result).toContainEqual(
expect.objectContaining({
version: '6000.4.10f1',
changeSet: 'feeafc12a938',
}),
);
expect(result).toContainEqual(
expect.objectContaining({
version: '6000.3.17f1',
changeSet: 'abc123def456',
}),
);
});
it('should extract changesets from context near the version', async () => {
const html = `
<h2>Unity 6000.4.10f1</h2>
<p>Changeset: feeafc12a938</p>
<h2>Unity 6000.3.17f1</h2>
<p>Changeset: abc123def456 is the commit hash</p>
`;
mockedFetch.mockResolvedValue({
ok: true,
status: 200,
text: async () => html,
} as any);
const result = await scrapeRecentOfficialUnityVersions();
// Both should be found - implementation uses context-window search
expect(result.length).toBeGreaterThanOrEqual(2);
expect(result.some((v) => v.version === '6000.4.10f1')).toBe(true);
expect(result.some((v) => v.version === '6000.3.17f1')).toBe(true);
});
it('should skip versions without valid changesets nearby', async () => {
const html = `
<h2>Unity 6000.4.10f1</h2>
<a href="unityhub://6000.4.10f1/feeafc12a938">Install</a>
<h2>Unity 6000.2.5f1</h2>
<p>This version has no changeset information</p>
`;
mockedFetch.mockResolvedValue({
ok: true,
status: 200,
text: async () => html,
} as any);
const result = await scrapeRecentOfficialUnityVersions();
// Only 6000.4.10f1 should be found with a valid changeset
expect(result.length).toBeGreaterThanOrEqual(1);
expect(result.map((v) => v.version)).toContain('6000.4.10f1');
expect(result.map((v) => v.version)).not.toContain('6000.2.5f1');
});
it('should deduplicate versions found multiple times on the page', async () => {
const html = `
<h2>Unity 6000.4.10f1</h2>
<a href="unityhub://6000.4.10f1/feeafc12a938">Install</a>
<p>Latest version: 6000.4.10f1</p>
<a href="unityhub://6000.4.10f1/feeafc12a938">Download</a>
`;
mockedFetch.mockResolvedValue({
ok: true,
status: 200,
text: async () => html,
} as any);
const result = await scrapeRecentOfficialUnityVersions();
expect(result).toHaveLength(1);
expect(result[0].version).toBe('6000.4.10f1');
});
it('should return empty array if page returns error', async () => {
mockedFetch.mockResolvedValue({
ok: false,
status: 404,
} as any);
await expect(scrapeRecentOfficialUnityVersions()).rejects.toThrow(
'Unity release page returned 404',
);
});
it('should filter out non-final versions', async () => {
const html = `
<h2>Unity 6000.4.10f1</h2>
<a href="unityhub://6000.4.10f1/feeafc12a938">Install</a>
<h2>Unity 6000.4.10a1</h2>
<a href="unityhub://6000.4.10a1/abc1234567ab">Install</a>
<h2>Unity 6000.4.9f1</h2>
<a href="unityhub://6000.4.9f1/def1234567cd">Install</a>
`;
mockedFetch.mockResolvedValue({
ok: true,
status: 200,
text: async () => html,
} as any);
const result = await scrapeRecentOfficialUnityVersions();
expect(result.length).toBeGreaterThanOrEqual(2);
expect(result.map((v) => v.version)).toContain('6000.4.10f1');
expect(result.map((v) => v.version)).toContain('6000.4.9f1');
expect(result.map((v) => v.version)).not.toContain('6000.4.10a1');
});
});