-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Updates.e2e.ts
755 lines (676 loc) · 28.3 KB
/
Updates.e2e.ts
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
import { by, device, element, waitFor } from 'detox';
import jestExpect from 'expect';
import path from 'path';
import { setTimeout } from 'timers/promises';
import Server from './utils/server';
import Update from './utils/update';
const projectRoot = process.env.PROJECT_ROOT || process.cwd();
const platform = device.getPlatform();
const protocolVersion = 1;
const TIMEOUT_BIAS = process.env.CI ? 10 : 1;
const checkNumAssetsAsync = async () => {
await element(by.id('readAssetFiles')).tap();
await setTimeout(20 * TIMEOUT_BIAS);
await waitFor(element(by.id('activity')))
.not.toBeVisible()
.withTimeout(2000);
const attributes: any = await element(by.id('numAssetFiles')).getAttributes();
return parseInt(attributes?.text || -1, 10);
};
const clearNumAssetsAsync = async () => {
await element(by.id('clearAssetFiles')).tap();
await setTimeout(20 * TIMEOUT_BIAS);
await waitFor(element(by.id('activity')))
.not.toBeVisible()
.withTimeout(2000);
};
const testElementValueAsync = async (testID: string) => {
const attributes: any = await element(by.id(testID)).getAttributes();
return attributes?.text || '';
};
const pressTestButtonAsync = async (testID: string) => await element(by.id(testID)).tap();
const readLogEntriesAsync = async () => {
await element(by.id('readLogEntries')).tap();
await setTimeout(20 * TIMEOUT_BIAS);
await waitFor(element(by.id('activity')))
.not.toBeVisible()
.withTimeout(2000);
const attributes: any = await element(by.id('logEntries')).getAttributes();
try {
return JSON.parse(attributes?.text) || [];
} catch (e) {
console.warn(`Error in parsing logs: ${e}`);
return [];
}
};
const waitForAppToBecomeVisible = async () => {
await waitFor(element(by.id('updateString')))
.toBeVisible()
.withTimeout(2000);
};
describe('Basic tests', () => {
afterEach(async () => {
await device.uninstallApp();
Server.stop();
});
it('starts app, stops, and starts again', async () => {
console.warn(`Platform = ${platform}`);
jest.setTimeout(300000 * TIMEOUT_BIAS);
Server.start(Update.serverPort, protocolVersion);
await device.installApp();
await device.launchApp({
newInstance: true,
});
await waitForAppToBecomeVisible();
const message = await testElementValueAsync('updateString');
jestExpect(message).toBe('test');
await device.terminateApp();
await device.launchApp();
await waitForAppToBecomeVisible();
const message2 = await testElementValueAsync('updateString');
jestExpect(message2).toBe('test');
await device.terminateApp();
});
it('initial request includes correct update-id headers', async () => {
jest.setTimeout(300000 * TIMEOUT_BIAS);
Server.start(Update.serverPort);
await device.installApp();
await device.launchApp({
newInstance: true,
});
const request = await Server.waitForUpdateRequest(10000 * TIMEOUT_BIAS);
jestExpect(request.headers['expo-embedded-update-id'] || null).toBeDefined();
jestExpect(request.headers['expo-current-update-id']).toBeDefined();
// before any updates, the current update ID and embedded update ID should be the same
jestExpect(request.headers['expo-current-update-id']).toEqual(
request.headers['expo-embedded-update-id']
);
});
it('downloads and runs update, and updates current-update-id header', async () => {
jest.setTimeout(300000 * TIMEOUT_BIAS);
const bundleFilename = 'bundle1.js';
const newNotifyString = 'test-update-1';
const hash = await Update.copyBundleToStaticFolder(
projectRoot,
bundleFilename,
newNotifyString,
platform
);
const manifest = Update.getUpdateManifestForBundleFilename(
new Date(),
hash,
'test-update-1-key',
bundleFilename,
[]
);
Server.start(Update.serverPort, protocolVersion);
await Server.serveSignedManifest(manifest, projectRoot);
await device.installApp();
await device.launchApp({
newInstance: true,
});
const firstRequest = await Server.waitForUpdateRequest(10000 * TIMEOUT_BIAS);
await waitForAppToBecomeVisible();
const message = await testElementValueAsync('updateString');
jestExpect(message).toBe('test');
// give the app time to load the new update in the background
jestExpect(Server.consumeRequestedStaticFiles().length).toBe(1);
// restart the app so it will launch the new update
await device.terminateApp();
await device.launchApp();
const secondRequest = await Server.waitForUpdateRequest(10000 * TIMEOUT_BIAS);
await waitForAppToBecomeVisible();
const updatedMessage = await testElementValueAsync('updateString');
jestExpect(updatedMessage).toBe(newNotifyString);
jestExpect(secondRequest.headers['expo-embedded-update-id']).toBeDefined();
jestExpect(secondRequest.headers['expo-embedded-update-id']).toEqual(
firstRequest.headers['expo-embedded-update-id']
);
jestExpect(secondRequest.headers['expo-current-update-id']).toBeDefined();
jestExpect(secondRequest.headers['expo-current-update-id']).toEqual(manifest.id);
});
it('does not run update with incorrect hash', async () => {
jest.setTimeout(300000 * TIMEOUT_BIAS);
const bundleFilename = 'bundle-invalid-hash.js';
const newNotifyString = 'test-update-invalid-hash';
await Update.copyBundleToStaticFolder(projectRoot, bundleFilename, newNotifyString, platform);
const hash = 'invalid-hash';
const manifest = Update.getUpdateManifestForBundleFilename(
new Date(),
hash,
'test-update-1-key',
bundleFilename,
[]
);
Server.start(Update.serverPort, protocolVersion);
await Server.serveSignedManifest(manifest, projectRoot);
await device.installApp();
await device.launchApp({
newInstance: true,
});
await Server.waitForUpdateRequest(10000 * TIMEOUT_BIAS);
await waitForAppToBecomeVisible();
const message = await testElementValueAsync('updateString');
jestExpect(message).toBe('test');
// give the app time to load the new update in the background
jestExpect(Server.consumeRequestedStaticFiles().length).toBe(1);
// restart the app to verify the new update isn't used
await device.terminateApp();
await device.launchApp();
await Server.waitForUpdateRequest(10000 * TIMEOUT_BIAS);
const updatedMessage = await testElementValueAsync('updateString');
jestExpect(updatedMessage).toBe('test');
});
it('update with bad asset hash yields expected log entry', async () => {
jest.setTimeout(300000 * TIMEOUT_BIAS);
const bundleFilename = 'bundle2.js';
const newNotifyString = 'test-update-2';
const hash = await Update.copyBundleToStaticFolder(
projectRoot,
bundleFilename,
newNotifyString,
platform
);
const assets = await Promise.all(
[
'lubo-minar-j2RgHfqKhCM-unsplash.jpg',
'niklas-liniger-zuPiCN7xekM-unsplash.jpg',
'patrick-untersee-XJjsuuDwWas-unsplash.jpg',
].map(async (sourceFilename, index) => {
const destinationFilename = `asset${index}.jpg`;
const hash = await Update.copyAssetToStaticFolder(
path.join(__dirname, 'assets', sourceFilename),
destinationFilename
);
return {
hash:
index === 0 ? hash.substring(1, 2) + hash.substring(0, 1) + hash.substring(2) : hash,
key: `asset${index}`,
contentType: 'image/jpg',
fileExtension: '.jpg',
url: `http://${Update.serverHost}:${Update.serverPort}/static/${destinationFilename}`,
};
})
);
const manifest = Update.getUpdateManifestForBundleFilename(
new Date(),
hash,
'test-update-2-key',
bundleFilename,
assets
);
Server.start(Update.serverPort, protocolVersion);
await Server.serveSignedManifest(manifest, projectRoot);
await device.installApp();
await device.launchApp({
newInstance: true,
});
// give the app time to load the new update in the background
await waitForAppToBecomeVisible();
const message = await testElementValueAsync('updateString');
jestExpect(message).toBe('test');
jestExpect(Server.consumeRequestedStaticFiles().length).toBe(4);
// restart the app so it will launch the new update
await device.terminateApp();
await device.launchApp();
await waitForAppToBecomeVisible();
await setTimeout(2000 * TIMEOUT_BIAS);
const updatedMessage = await testElementValueAsync('updateString');
// Because of the mismatch, the new update will not load, so updatedMessage will still be 'test'
jestExpect(updatedMessage).toBe('test');
// Check readLogEntriesAsync
const logEntries: any[] = await readLogEntriesAsync();
console.warn(
'Total number of log entries = ' +
logEntries.length +
'\n' +
JSON.stringify(logEntries, null, 2)
);
// Should have at least one message
jestExpect(logEntries.length > 0).toBe(true);
// Check for message that hash is mismatched, with expected error code
jestExpect(logEntries.map((entry) => entry.code)).toEqual(
jestExpect.arrayContaining(['AssetsFailedToLoad'])
);
});
it('downloads and runs update with multiple assets', async () => {
jest.setTimeout(300000 * TIMEOUT_BIAS);
const bundleFilename = 'bundle2.js';
const newNotifyString = 'test-update-2';
const hash = await Update.copyBundleToStaticFolder(
projectRoot,
bundleFilename,
newNotifyString,
platform
);
const assets = await Promise.all(
[
'lubo-minar-j2RgHfqKhCM-unsplash.jpg',
'niklas-liniger-zuPiCN7xekM-unsplash.jpg',
'patrick-untersee-XJjsuuDwWas-unsplash.jpg',
].map(async (sourceFilename, index) => {
const destinationFilename: string = `asset${index}.jpg`;
const hash = await Update.copyAssetToStaticFolder(
path.join(__dirname, 'assets', sourceFilename),
destinationFilename
);
return {
hash,
key: `asset${index}`,
contentType: 'image/jpg',
fileExtension: '.jpg',
url: `http://${Update.serverHost}:${Update.serverPort}/static/${destinationFilename}`,
};
})
);
const manifest = Update.getUpdateManifestForBundleFilename(
new Date(),
hash,
'test-update-2-key',
bundleFilename,
assets
);
Server.start(Update.serverPort, protocolVersion);
await Server.serveSignedManifest(manifest, projectRoot);
await device.installApp();
await device.launchApp({
newInstance: true,
});
await waitForAppToBecomeVisible();
const message = await testElementValueAsync('updateString');
jestExpect(message).toBe('test');
// give the app time to load the new update in the background
jestExpect(Server.consumeRequestedStaticFiles().length).toBe(4);
// restart the app so it will launch the new update
await device.terminateApp();
await device.launchApp();
const updatedMessage = await testElementValueAsync('updateString');
jestExpect(updatedMessage).toBe(newNotifyString);
});
// important for usage accuracy
it('does not download any assets for an older update', async () => {
jest.setTimeout(300000 * TIMEOUT_BIAS);
const bundleFilename = 'bundle-old.js';
const hash = await Update.copyBundleToStaticFolder(
projectRoot,
bundleFilename,
'test-update-older',
platform
);
const manifest = Update.getUpdateManifestForBundleFilename(
new Date(Date.now() - 1000 * 60 * 60 * 24),
hash,
'test-update-old-key',
bundleFilename,
[]
);
Server.start(Update.serverPort, protocolVersion);
await Server.serveSignedManifest(manifest, projectRoot);
await device.installApp();
await device.launchApp({
newInstance: true,
});
await Server.waitForUpdateRequest(10000 * TIMEOUT_BIAS);
await waitForAppToBecomeVisible();
const firstMessage = await testElementValueAsync('updateString');
jestExpect(firstMessage).toBe('test');
// give the app time to load the new update in the background (i.e. to make sure it doesn't)
jestExpect(Server.consumeRequestedStaticFiles().length).toBe(0);
// restart the app and make sure it's still running the initial update
await device.terminateApp();
await device.launchApp();
const secondMessage = await testElementValueAsync('updateString');
jestExpect(secondMessage).toBe('test');
});
it('supports rollbacks', async () => {
jest.setTimeout(300000 * TIMEOUT_BIAS);
const bundleFilename = 'bundle1.js';
const newNotifyString = 'test-update-3';
const hash = await Update.copyBundleToStaticFolder(
projectRoot,
bundleFilename,
newNotifyString,
platform
);
const manifest = Update.getUpdateManifestForBundleFilename(
new Date(),
hash,
'test-update-3-key',
bundleFilename,
[]
);
Server.start(Update.serverPort, protocolVersion);
await Server.serveSignedManifest(manifest, projectRoot);
await device.installApp();
await device.launchApp({
newInstance: true,
});
const firstRequest = await Server.waitForUpdateRequest(10000 * TIMEOUT_BIAS);
await waitForAppToBecomeVisible();
const message = await testElementValueAsync('updateString');
jestExpect(message).toBe('test');
// give the app time to load the new update in the background
jestExpect(Server.consumeRequestedStaticFiles().length).toBe(1);
// restart the app so it will launch the new update
await device.terminateApp();
await device.launchApp();
const secondRequest = await Server.waitForUpdateRequest(10000 * TIMEOUT_BIAS);
await waitForAppToBecomeVisible();
const updatedMessage = await testElementValueAsync('updateString');
jestExpect(updatedMessage).toBe(newNotifyString);
// serve a rollback now
const rollbackDirective = Update.getRollbackDirective(new Date());
await Server.serveSignedDirective(rollbackDirective, projectRoot);
// restart the app so it will fetch the rollback
await device.terminateApp();
await device.launchApp();
const thirdRequest = await Server.waitForUpdateRequest(10000 * TIMEOUT_BIAS);
// Restart the app so it will launch the rollback
await device.terminateApp();
await device.launchApp();
const fourthRequest = await Server.waitForUpdateRequest(10000 * TIMEOUT_BIAS);
await waitForAppToBecomeVisible();
const rolledBackMessage = await testElementValueAsync('updateString');
jestExpect(rolledBackMessage).toBe('test');
jestExpect(secondRequest.headers['expo-embedded-update-id']).toBeDefined();
jestExpect(secondRequest.headers['expo-embedded-update-id']).toEqual(
firstRequest.headers['expo-embedded-update-id']
);
jestExpect(thirdRequest.headers['expo-embedded-update-id']).toEqual(
firstRequest.headers['expo-embedded-update-id']
);
jestExpect(fourthRequest.headers['expo-embedded-update-id']).toEqual(
firstRequest.headers['expo-embedded-update-id']
);
jestExpect(firstRequest.headers['expo-current-update-id']).toEqual(
firstRequest.headers['expo-embedded-update-id']
);
jestExpect(secondRequest.headers['expo-current-update-id']).toEqual(manifest.id);
jestExpect(thirdRequest.headers['expo-current-update-id']).toEqual(manifest.id);
jestExpect(fourthRequest.headers['expo-current-update-id']).toEqual(
firstRequest.headers['expo-embedded-update-id']
);
});
});
describe('JS API tests', () => {
afterEach(async () => {
await device.uninstallApp();
Server.stop();
});
it('downloads and runs update with JS API', async () => {
jest.setTimeout(300000 * TIMEOUT_BIAS);
const bundleFilename = 'bundle1.js';
const newNotifyString = 'test-update-1';
const hash = await Update.copyBundleToStaticFolder(
projectRoot,
bundleFilename,
newNotifyString,
platform
);
const manifest = Update.getUpdateManifestForBundleFilename(
new Date(),
hash,
'test-update-1-key',
bundleFilename,
[]
);
await device.installApp();
await device.launchApp({
newInstance: true,
});
await waitForAppToBecomeVisible();
const message = await testElementValueAsync('updateString');
jestExpect(message).toEqual('test');
const isEmbedded = await testElementValueAsync('isEmbeddedLaunch');
jestExpect(isEmbedded).toEqual('true');
const checkAutomatically = await testElementValueAsync('checkAutomatically');
jestExpect(checkAutomatically).toEqual('ON_LOAD');
Server.start(Update.serverPort, protocolVersion);
await Server.serveSignedManifest(manifest, projectRoot);
await pressTestButtonAsync('checkForUpdate');
const availableUpdateID = await testElementValueAsync('availableUpdateID');
jestExpect(availableUpdateID).toEqual(manifest.id);
await pressTestButtonAsync('downloadUpdate');
await setTimeout(2000);
Server.stop();
await device.terminateApp();
await device.launchApp();
await waitForAppToBecomeVisible();
const runningUpdateID = await testElementValueAsync('updateID');
jestExpect(runningUpdateID).toEqual(manifest.id);
const isEmbeddedAfterUpdate = await testElementValueAsync('isEmbeddedLaunch');
jestExpect(isEmbeddedAfterUpdate).toEqual('false');
});
it('Receives expected events when update available on start', async () => {
jest.setTimeout(300000 * TIMEOUT_BIAS);
const bundleFilename = 'bundle1.js';
const newNotifyString = 'test-update-1';
const hash = await Update.copyBundleToStaticFolder(
projectRoot,
bundleFilename,
newNotifyString,
platform
);
const manifest = Update.getUpdateManifestForBundleFilename(
new Date(),
hash,
'test-update-1-key',
bundleFilename,
[]
);
// Launch app
await device.installApp();
await device.launchApp({
newInstance: true,
});
await waitForAppToBecomeVisible();
const lastUpdateEventType = await testElementValueAsync('lastUpdateEventType');
// Server is not running, so error received
jestExpect(lastUpdateEventType).toEqual('error');
// Start server with no update available directive,
// then restart app, we should get "No update available" event
if (protocolVersion === 1) {
Server.start(Update.serverPort, protocolVersion);
const directive = Update.getNoUpdateAvailableDirective();
await Server.serveSignedDirective(directive, projectRoot);
await device.terminateApp();
await device.launchApp();
await waitForAppToBecomeVisible();
const lastUpdateEventType2 = await testElementValueAsync('lastUpdateEventType');
jestExpect(lastUpdateEventType2).toEqual('noUpdateAvailable');
Server.stop();
}
// Relaunch app after server has an update,
// we should get the 'update available' event
Server.start(Update.serverPort, protocolVersion);
await Server.serveSignedManifest(manifest, projectRoot);
await device.terminateApp();
await device.launchApp();
await waitForAppToBecomeVisible();
const lastUpdateEventType3 = await testElementValueAsync('lastUpdateEventType');
jestExpect(lastUpdateEventType3).toEqual('updateAvailable');
});
});
// The tests in this suite install an app with multiple assets, then clear all the assets from
// .expo-internal storage (but not SQLite). This simulates scenarios such as: a bug in our code that
// deletes assets unintentionally; OS deleting files from app storage if it runs out of memory; etc.
//
// Recovery code for this situation exists in the DatabaseLauncher, these are the main tests that
// ensure that logic doesn't regress.
//
// These tests all make use of the additional UpdatesE2ETestModule, which provides methods for
// clearing and reading the .expo-internal folder.
describe('Asset deletion recovery tests', () => {
afterEach(async () => {
await device.uninstallApp();
Server.stop();
});
it('embedded assets deleted from internal storage should be re-copied', async () => {
// Simplest scenario; only one update (embedded) is loaded, then assets are cleared from
// internal storage. The app is then relaunched with the same embedded update.
// DatabaseLauncher should copy all the missing assets and run the update as normal.
jest.setTimeout(300000 * TIMEOUT_BIAS);
Server.start(Update.serverPort, protocolVersion);
// Install the app and immediately send it a message to clear internal storage. Verify storage
// has been cleared properly.
await device.installApp();
await device.launchApp({
newInstance: true,
});
await waitForAppToBecomeVisible();
// Check that we are running the embedded update
const isEmbedded = await testElementValueAsync('isEmbeddedLaunch');
jestExpect(isEmbedded).toEqual('true');
// Check that asset files are present
let numAssets = await checkNumAssetsAsync();
jestExpect(numAssets).toBeGreaterThan(2);
// Get current update ID
const updateID = await testElementValueAsync('updateID');
// Clear assets and check that number of assets is now 0
await clearNumAssetsAsync();
numAssets = await checkNumAssetsAsync();
jestExpect(numAssets).toBe(0);
// Stop and then restart app.
await device.terminateApp();
await device.launchApp();
await waitForAppToBecomeVisible();
// Check that assets are restored from DB
numAssets = await checkNumAssetsAsync();
jestExpect(numAssets).toBeGreaterThan(2);
// Check that update ID is the same
const updateID2 = await testElementValueAsync('updateID');
jestExpect(updateID2).toEqual(updateID);
// Check for log messages
const logEntries = await readLogEntriesAsync();
console.warn(
'Total number of log entries = ' +
logEntries.length +
'\n' +
JSON.stringify(logEntries, null, 2)
);
jestExpect(logEntries.length).toBeGreaterThan(0);
});
it('embedded assets deleted from internal storage should be re-copied from a new embedded update', async () => {
// This test ensures that when trying to launch a NEW update that includes some OLD assets we
// already have (according to SQLite), even if those assets are actually missing from disk
// (but included in the embedded update) DatabaseLauncher can recover.
//
// To create this scenario, we load a single (embedded) update, then clear assets from
// internal storage. Then we install a NEW build with a NEW embedded update but that includes
// some of the same assets. When we launch this new build, DatabaseLauncher should still copy
// the missing assets and run the update as normal.
jest.setTimeout(300000 * TIMEOUT_BIAS);
Server.start(Update.serverPort, protocolVersion);
// Install the app and immediately send it a message to clear internal storage. Verify storage
// has been cleared properly.
await device.installApp();
await device.launchApp({
newInstance: true,
});
await waitForAppToBecomeVisible();
// Save the number of assets in storage
const numAssetsSaved = await checkNumAssetsAsync();
jestExpect(numAssetsSaved).toBeGreaterThan(0);
// Clear assets and check that number of assets is now 0
await clearNumAssetsAsync();
let numAssets = await checkNumAssetsAsync();
jestExpect(numAssets).toBe(0);
// Stop the app and install a newer build on top of it. The newer build has a different
// embedded update (different updateId) but still includes some of the same assets. Now SQLite
// thinks we already have these assets, but we actually just deleted them from internal
// storage.
await device.terminateApp();
await device.installApp();
// Start the new build, and immediately send it a message to read internal storage.
await device.launchApp({
newInstance: true,
});
await waitForAppToBecomeVisible();
// Verify all the assets that were deleted have been re-copied back into internal storage, and
// that we are running a DIFFERENT update than before -- otherwise this test is no different
// from the previous one.
numAssets = await checkNumAssetsAsync();
jestExpect(numAssets).toEqual(numAssetsSaved);
// TODO: develop a way to modify the embedded update used by the build in a Detox test environment,
// so that we can actually do this test with a real modified update. Until then, disable the line below
// to allow the test to pass.
//jestExpect(readAssetsMessage.updateId).not.toEqual(clearAssetsMessage.updateId);
});
it('assets in a downloaded update deleted from internal storage should be re-copied or re-downloaded', async () => {
// This test ensures we can (or at least try to) recover missing assets that originated from a
// downloaded update, as opposed to assets originally copied from an embedded update (which
// the previous 2 tests concern).
//
// To create this scenario, we launch an app, download an update with multiple assets
// (including at least one -- the bundle -- not part of the embedded update), make sure the
// update runs, then clear assets from internal storage. When we relaunch the app,
// DatabaseLauncher should re-download the missing assets and run the update as normal.
jest.setTimeout(300000 * TIMEOUT_BIAS);
// Prepare to host update manifest and assets from the test runner
const bundleFilename = 'bundle-assets.js';
const newNotifyString = 'test-assets-1';
const bundleHash = await Update.copyBundleToStaticFolder(
projectRoot,
bundleFilename,
newNotifyString,
platform
);
const bundledAssets = Update.findAssets(projectRoot, platform);
const assets = await Promise.all(
bundledAssets.map(async (asset: { path: string; ext: string }) => {
const filename = path.basename(asset.path);
const mimeType = asset.ext === 'ttf' ? 'font/ttf' : 'image/png';
const key = filename.replace('asset_', '').replace(/\.[^/.]+$/, '');
const hash = await Update.copyAssetToStaticFolder(asset.path, filename);
return {
hash,
key,
contentType: mimeType,
fileExtension: asset.ext,
url: `http://${Update.serverHost}:${Update.serverPort}/static/${filename}`,
};
})
);
const manifest = Update.getUpdateManifestForBundleFilename(
new Date(),
bundleHash,
'test-assets-bundle',
bundleFilename,
assets
);
// Install the app and launch it so that it downloads the new update we're hosting
Server.start(Update.serverPort, protocolVersion);
await Server.serveSignedManifest(manifest, projectRoot);
await device.installApp();
await device.launchApp({ newInstance: true });
await Server.waitForUpdateRequest(10000 * TIMEOUT_BIAS);
// give the app time to load the new update in the background
jestExpect(Server.consumeRequestedStaticFiles().length).toBe(1); // only the bundle should be new
// Stop and restart the app so it will launch the new update. Immediately send it a message to
// clear internal storage while also verifying the new update is running.
await device.terminateApp();
await device.launchApp({ newInstance: true });
await waitForAppToBecomeVisible();
const updateString = await testElementValueAsync('updateString');
jestExpect(updateString).toEqual(newNotifyString);
await clearNumAssetsAsync();
// Verify that the assets were cleared correctly.
let numAssets = await checkNumAssetsAsync();
jestExpect(numAssets).toBe(0);
let updateID = await testElementValueAsync('updateID');
jestExpect(updateID).toEqual(manifest.id);
// Stop and restart the app and immediately send it a message to read internal storage. Verify
// that the new update is running (again).
await device.terminateApp();
await device.launchApp({ newInstance: true });
await waitForAppToBecomeVisible();
// Verify all the assets -- including the JS bundle from the update (which wasn't in the
// embedded update) -- have been restored. Additionally verify from the server side that the
// updated bundle was re-downloaded.
numAssets = await checkNumAssetsAsync();
jestExpect(numAssets).toBe(manifest.assets.length + 1);
updateID = await testElementValueAsync('updateID');
jestExpect(updateID).toEqual(manifest.id);
jestExpect(Server.consumeRequestedStaticFiles().length).toBe(1); // should have re-downloaded only the JS bundle; the rest should have been copied from the app binary
});
});