-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathlocal-merge.test.ts
More file actions
288 lines (246 loc) Β· 8.2 KB
/
Copy pathlocal-merge.test.ts
File metadata and controls
288 lines (246 loc) Β· 8.2 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
import { by, device, element, waitFor } from 'detox';
import {
LOCAL_UPDATE_HASHES,
LOCAL_UPDATE_LABELS,
} from './localUpdateConfig.ts';
const RELOAD_TIMEOUT = 300000;
const RETRYABLE_RELOAD_TIMEOUT = 90000;
const MARK_SUCCESS_TIMEOUT = 30000;
const MARK_SUCCESS_SETTLE_MS = 1500;
const DOWNLOAD_SUCCESS_TIMEOUT = 120000;
const TRANSIENT_ERROR_TIMEOUT = 5000;
const MAX_CHECK_UPDATE_ATTEMPTS = 2;
function getDetoxLaunchArgs() {
if (device.getPlatform() !== 'android') {
return {};
}
return {
launchArgs: {
detoxEnableSynchronization: '0',
},
};
}
async function relaunchAppPreservingData() {
await device.launchApp({
newInstance: true,
...getDetoxLaunchArgs(),
});
}
async function tapCheckUpdate() {
await waitFor(element(by.id('check-update')))
.toBeVisible()
.withTimeout(30000);
await element(by.id('check-update')).tap();
}
async function waitForBundleLabel(
expectedLabel: string,
timeoutMs = RELOAD_TIMEOUT,
) {
await waitFor(element(by.id('bundle-label')))
.toHaveText(`bundleLabel: ${expectedLabel}`)
.withTimeout(timeoutMs);
}
async function matchesText(
testId: string,
text: string,
timeoutMs = TRANSIENT_ERROR_TIMEOUT,
) {
try {
await waitFor(element(by.id(testId)))
.toHaveText(text)
.withTimeout(timeoutMs);
return true;
} catch {
return false;
}
}
async function didHitTransientCheckError() {
const [hasCheckError, hasErrorEvent] = await Promise.all([
matchesText('last-check-status', 'lastCheckStatus: error'),
matchesText('last-event', 'lastEvent: errorChecking'),
]);
return hasCheckError && hasErrorEvent;
}
async function tapCheckUpdateAndWaitForBundleLabel(expectedLabel: string) {
let lastError: unknown;
for (let attempt = 1; attempt <= MAX_CHECK_UPDATE_ATTEMPTS; attempt++) {
await tapCheckUpdate();
try {
await waitForBundleLabel(
expectedLabel,
attempt < MAX_CHECK_UPDATE_ATTEMPTS
? RETRYABLE_RELOAD_TIMEOUT
: RELOAD_TIMEOUT,
);
return;
} catch (error) {
lastError = error;
const shouldRetry =
attempt < MAX_CHECK_UPDATE_ATTEMPTS &&
(await didHitTransientCheckError());
if (!shouldRetry) {
throw error;
}
await waitForReady();
}
}
throw lastError;
}
async function waitForHash(expectedHash: string) {
const visibleHash = expectedHash || '(empty)';
await waitFor(element(by.id('current-hash')))
.toHaveText(`currentHash: ${visibleHash}`)
.withTimeout(RELOAD_TIMEOUT);
}
async function waitForMarkSuccess() {
await waitFor(element(by.id('last-event')))
.toHaveText('lastEvent: markSuccess')
.withTimeout(MARK_SUCCESS_TIMEOUT);
await new Promise(resolve => setTimeout(resolve, MARK_SUCCESS_SETTLE_MS));
}
async function waitForDownloadSuccess(expectedHash: string) {
await waitFor(element(by.id('last-event')))
.toHaveText('lastEvent: downloadSuccess')
.withTimeout(DOWNLOAD_SUCCESS_TIMEOUT);
await waitFor(element(by.id('last-event-version')))
.toHaveText(`lastEventVersion: ${expectedHash}`)
.withTimeout(DOWNLOAD_SUCCESS_TIMEOUT);
}
async function waitForReady() {
await waitFor(element(by.id('check-update')))
.toBeVisible()
.withTimeout(30000);
}
async function waitForCheckState(
expectedStatus: string,
expectedResult: string,
) {
await waitFor(element(by.id('last-check-status')))
.toHaveText(`lastCheckStatus: ${expectedStatus}`)
.withTimeout(RELOAD_TIMEOUT);
await waitFor(element(by.id('last-check-result')))
.toHaveText(`lastCheckResult: ${expectedResult}`)
.withTimeout(RELOAD_TIMEOUT);
}
async function waitForStrategy(
expectedStrategy: 'silentAndNow' | 'silentAndLater',
) {
await waitFor(element(by.id('update-strategy')))
.toHaveText(`updateStrategy: ${expectedStrategy}`)
.withTimeout(10000);
}
async function selectStrategy(
testId: 'strategy-silent-now' | 'strategy-silent-later',
) {
await waitFor(element(by.id(testId)))
.toBeVisible()
.withTimeout(10000);
await element(by.id(testId)).tap();
}
describe('Local Update Merge E2E', () => {
beforeEach(async () => {
await device.launchApp({
newInstance: true,
delete: true,
...getDetoxLaunchArgs(),
});
});
it('covers local full update, diff merge, and package diff through checkUpdate + silentAndNow', async () => {
await waitForReady();
await waitForStrategy('silentAndNow');
await waitForBundleLabel(LOCAL_UPDATE_LABELS.base);
await tapCheckUpdateAndWaitForBundleLabel(LOCAL_UPDATE_LABELS.full);
await waitForHash(LOCAL_UPDATE_HASHES.full);
await waitForMarkSuccess();
await tapCheckUpdateAndWaitForBundleLabel(LOCAL_UPDATE_LABELS.ppkPatch);
await waitForHash(LOCAL_UPDATE_HASHES.ppkPatch);
await waitForMarkSuccess();
if (device.getPlatform() === 'android') {
await tapCheckUpdateAndWaitForBundleLabel(
LOCAL_UPDATE_LABELS.packagePatch,
);
await waitForHash(LOCAL_UPDATE_HASHES.packagePatch);
await waitForMarkSuccess();
}
const finalLabel =
device.getPlatform() === 'android'
? LOCAL_UPDATE_LABELS.packagePatch
: LOCAL_UPDATE_LABELS.ppkPatch;
const finalHash =
device.getPlatform() === 'android'
? LOCAL_UPDATE_HASHES.packagePatch
: LOCAL_UPDATE_HASHES.ppkPatch;
await relaunchAppPreservingData();
await waitForReady();
await waitForBundleLabel(finalLabel);
await waitForHash(finalHash);
await tapCheckUpdate();
await waitForCheckState('completed', 'upToDate');
await waitForBundleLabel(finalLabel);
await waitForHash(finalHash);
});
it('covers local full update, deferred install, and follow-up deferred patches through silentAndLater', async () => {
await waitForReady();
await waitForBundleLabel(LOCAL_UPDATE_LABELS.base);
await selectStrategy('strategy-silent-later');
await waitForStrategy('silentAndLater');
await tapCheckUpdate();
await waitForCheckState('completed', `update:${LOCAL_UPDATE_HASHES.full}`);
await waitForDownloadSuccess(LOCAL_UPDATE_HASHES.full);
await waitForBundleLabel(LOCAL_UPDATE_LABELS.base);
await waitForHash('');
await relaunchAppPreservingData();
await waitForReady();
await waitForBundleLabel(LOCAL_UPDATE_LABELS.full);
await waitForHash(LOCAL_UPDATE_HASHES.full);
await waitForMarkSuccess();
await selectStrategy('strategy-silent-later');
await waitForStrategy('silentAndLater');
await tapCheckUpdate();
await waitForCheckState(
'completed',
`update:${LOCAL_UPDATE_HASHES.ppkPatch}`,
);
await waitForDownloadSuccess(LOCAL_UPDATE_HASHES.ppkPatch);
await waitForBundleLabel(LOCAL_UPDATE_LABELS.full);
await waitForHash(LOCAL_UPDATE_HASHES.full);
await relaunchAppPreservingData();
await waitForReady();
await waitForBundleLabel(LOCAL_UPDATE_LABELS.ppkPatch);
await waitForHash(LOCAL_UPDATE_HASHES.ppkPatch);
await waitForMarkSuccess();
if (device.getPlatform() === 'android') {
await selectStrategy('strategy-silent-later');
await waitForStrategy('silentAndLater');
await tapCheckUpdate();
await waitForCheckState(
'completed',
`update:${LOCAL_UPDATE_HASHES.packagePatch}`,
);
await waitForDownloadSuccess(LOCAL_UPDATE_HASHES.packagePatch);
await waitForBundleLabel(LOCAL_UPDATE_LABELS.ppkPatch);
await waitForHash(LOCAL_UPDATE_HASHES.ppkPatch);
await relaunchAppPreservingData();
await waitForReady();
await waitForBundleLabel(LOCAL_UPDATE_LABELS.packagePatch);
await waitForHash(LOCAL_UPDATE_HASHES.packagePatch);
await waitForMarkSuccess();
}
const finalLabel =
device.getPlatform() === 'android'
? LOCAL_UPDATE_LABELS.packagePatch
: LOCAL_UPDATE_LABELS.ppkPatch;
const finalHash =
device.getPlatform() === 'android'
? LOCAL_UPDATE_HASHES.packagePatch
: LOCAL_UPDATE_HASHES.ppkPatch;
await relaunchAppPreservingData();
await waitForReady();
await waitForBundleLabel(finalLabel);
await waitForHash(finalHash);
await tapCheckUpdate();
await waitForCheckState('completed', 'upToDate');
await waitForBundleLabel(finalLabel);
await waitForHash(finalHash);
});
});