File size: 8,492 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* eslint-disable require-jsdoc */
import crypto from 'crypto';
import path from 'path';
import { getMag16Locales, getViewports } from './data-helper';
import { TEST_ACCOUNT_NAMES } from './secrets';
import { SupportedEnvVariables, JetpackTarget, AtomicVariation } from './types/env-variables.types';
import { TestAccountName } from '.';

class EnvVariables implements SupportedEnvVariables {
	private _defaultEnvVariables: SupportedEnvVariables = {
		VIEWPORT_NAME: 'desktop',
		TEST_LOCALES: [ ...getMag16Locales() ],
		HEADLESS: false,
		SLOW_MO: 0,
		TIMEOUT: 10000,
		GUTENBERG_EDGE: false,
		GUTENBERG_NIGHTLY: false,
		COBLOCKS_EDGE: false,
		AUTHENTICATE_ACCOUNTS: [],
		COOKIES_PATH: path.join( process.cwd(), 'cookies' ),
		ARTIFACTS_PATH: path.join( process.cwd(), 'results' ),
		TEST_ON_ATOMIC: false,
		ATOMIC_VARIATION: 'default',
		JETPACK_TARGET: 'wpcom-production',
		CALYPSO_BASE_URL: 'https://wordpress.com',
		BROWSER_NAME: 'chromium',
		ALLURE_RESULTS_PATH: '',
		RUN_ID: '',
		RETRY_COUNT: 0,
	};

	get VIEWPORT_NAME(): string {
		const value = process.env.VIEWPORT_NAME;
		if ( ! value ) {
			return this._defaultEnvVariables.VIEWPORT_NAME;
		}

		const supportedValues = getViewports() as ReadonlyArray< string >;
		if ( ! supportedValues.includes( value as string ) ) {
			throw new Error(
				`Unknown VIEWPORT_NAME value: ${ value }.\nSupported values: ${ supportedValues }`
			);
		}
		return value;
	}

	get TEST_LOCALES(): string[] {
		const value = process.env.TEST_LOCALES;
		if ( ! value ) {
			return this._defaultEnvVariables.TEST_LOCALES;
		}

		const parsedLocales = value.split( ',' );
		const supportedValues = getMag16Locales() as ReadonlyArray< string >;
		if ( ! parsedLocales.every( ( v ) => supportedValues.includes( v ) ) ) {
			throw new Error(
				`Unknown TEST_LOCALES value: ${ value }.\nSupported values: ${ supportedValues.join(
					' | '
				) }`
			);
		}
		return parsedLocales;
	}

	get HEADLESS(): boolean {
		const value = process.env.HEADLESS;
		return value ? castAsBoolean( 'HEADLESS', value ) : this._defaultEnvVariables.HEADLESS;
	}

	get SLOW_MO(): number {
		const value = process.env.SLOW_MO;
		return value ? castAsNumber( 'SLOW_MO', value ) : this._defaultEnvVariables.SLOW_MO;
	}

	get TIMEOUT(): number {
		const value = process.env.TIMEOUT;
		return value ? castAsNumber( 'TIMEOUT', value ) : this._defaultEnvVariables.TIMEOUT;
	}

	get GUTENBERG_EDGE(): boolean {
		const value = process.env.GUTENBERG_EDGE;
		return value
			? castAsBoolean( 'GUTENBERG_EDGE', value )
			: this._defaultEnvVariables.GUTENBERG_EDGE;
	}

	get GUTENBERG_NIGHTLY(): boolean {
		const value = process.env.GUTENBERG_NIGHTLY;
		return value
			? castAsBoolean( 'GUTENBERG_NIGHTLY', value )
			: this._defaultEnvVariables.GUTENBERG_NIGHTLY;
	}

	get COBLOCKS_EDGE(): boolean {
		const value = process.env.COBLOCKS_EDGE;
		return value
			? castAsBoolean( 'COBLOCKS_EDGE', value )
			: this._defaultEnvVariables.COBLOCKS_EDGE;
	}

	get AUTHENTICATE_ACCOUNTS(): TestAccountName[] {
		const value = process.env.AUTHENTICATE_ACCOUNTS;
		if ( ! value ) {
			return this._defaultEnvVariables.AUTHENTICATE_ACCOUNTS;
		}

		const parsedAccounts: TestAccountName[] = value.split( ',' ) as TestAccountName[];
		const supportedValues = new Set< TestAccountName >( TEST_ACCOUNT_NAMES );
		if ( ! parsedAccounts.every( ( account ) => supportedValues.has( account ) ) ) {
			throw new Error(
				`Unknown AUTHENTICATE_ACCOUNTS value: ${ value }.\nSupported values: ${ TEST_ACCOUNT_NAMES }`
			);
		}
		return parsedAccounts;
	}

	get COOKIES_PATH(): string {
		const value = process.env.COOKIES_PATH;
		return value ? value : this._defaultEnvVariables.COOKIES_PATH;
	}

	get ARTIFACTS_PATH(): string {
		const value = process.env.ARTIFACTS_PATH;
		return value ? value : this._defaultEnvVariables.ARTIFACTS_PATH;
	}

	get TEST_ON_ATOMIC(): boolean {
		const value = process.env.TEST_ON_ATOMIC;
		return value
			? castAsBoolean( 'TEST_ON_ATOMIC', value )
			: this._defaultEnvVariables.TEST_ON_ATOMIC;
	}

	get ATOMIC_VARIATION(): AtomicVariation {
		const value = process.env.ATOMIC_VARIATION;
		if ( ! value ) {
			return this._defaultEnvVariables.ATOMIC_VARIATION;
		}

		const supportedValues: AtomicVariation[] = [
			'default',
			'php-old',
			'php-new',
			'wp-beta',
			'wp-previous',
			'private',
			'ecomm-plan',
			'mixed',
		];
		if ( ! supportedValues.includes( value as AtomicVariation ) ) {
			throw new Error(
				`Unknown ATOMIC_VARIATION value: ${ value }.\nSupported values: ${ supportedValues.join(
					' | '
				) }`
			);
		}

		if ( value === 'mixed' ) {
			return getAtomicVariationInMixedRun();
		}

		return value as AtomicVariation;
	}

	get JETPACK_TARGET(): JetpackTarget {
		const value = process.env.JETPACK_TARGET;
		if ( ! value ) {
			return this._defaultEnvVariables.JETPACK_TARGET;
		}

		const supportedValues: JetpackTarget[] = [
			'remote-site',
			'wpcom-production',
			'wpcom-deployment',
		];
		if ( ! supportedValues.includes( value as JetpackTarget ) ) {
			throw new Error(
				`Unknown JETPACK_TARGET value: ${ value }.\nSupported values: ${ supportedValues.join(
					' | '
				) }`
			);
		}
		return value as JetpackTarget;
	}

	get CALYPSO_BASE_URL(): string {
		const value = process.env.CALYPSO_BASE_URL;
		if ( ! value ) {
			return this._defaultEnvVariables.CALYPSO_BASE_URL;
		}

		try {
			// Disabling eslint because this constructor is really the simplest way to validate a URL.
			// eslint-disable-next-line no-new
			new URL( value );
		} catch ( error ) {
			throw new Error(
				`Invalid CALYPSO_BASE_URL value: ${ value }.\nYou must provide a valid URL.`
			);
		}
		return value;
	}

	get BROWSER_NAME(): string {
		const value = process.env.BROWSER_NAME;
		return value ? value : this._defaultEnvVariables.BROWSER_NAME;
	}

	get ALLURE_RESULTS_PATH(): string {
		const value = process.env.ALLURE_RESULTS_PATH;
		return value ? value : this._defaultEnvVariables.ALLURE_RESULTS_PATH;
	}

	get RUN_ID(): string {
		const value = process.env.RUN_ID;
		// Support our Jetpack "mixed" atomic test strategy.
		// We still want to preserve test history as we randomly rotate through the variations.
		// And we won't know the variation at the command line to use as the run ID.
		if ( ! value && this.JETPACK_TARGET === 'wpcom-deployment' && this.TEST_ON_ATOMIC ) {
			return `Atomic: ${ this.ATOMIC_VARIATION }`;
		}
		return value ? value : this._defaultEnvVariables.RUN_ID;
	}

	get RETRY_COUNT(): number {
		const value = process.env.RETRY_COUNT;
		return value ? castAsNumber( 'RETRY_COUNT', value ) : this._defaultEnvVariables.RETRY_COUNT;
	}

	validate() {
		for ( const property in this._defaultEnvVariables ) {
			const envVarName = property as keyof SupportedEnvVariables;
			// Access each property
			// Any validation errors within the getter will throw an exception here.
			this[ envVarName ];
		}
	}
}

function getAtomicVariationInMixedRun() {
	const allVariations: AtomicVariation[] = [
		'default',
		'php-old',
		'php-new',
		'wp-beta',
		'wp-previous',
		'private',
		'ecomm-plan',
	];
	// The goal here is controlled randomness to include multiple variations within a single run.
	// By using the current day of the month and the test file name hash, we can get a
	// lot of variation throughout the week while also ensuring the same variation is used on a failed retry.
	const currentDayOfMonth = new Date().getDate();
	const currentTestFileName = global.testFileName || '';
	const fileHash = hashTestFileName( currentTestFileName );
	const variationIndex = ( currentDayOfMonth + fileHash ) % allVariations.length;
	return allVariations[ variationIndex ];
}

function hashTestFileName( testFileName: string ): number {
	return Math.abs( crypto.createHash( 'md5' ).update( testFileName ).digest().readInt8() );
}

function castAsNumber( name: string, value: string ): number {
	const output = Number( value );
	if ( Number.isNaN( output ) ) {
		throw new Error( `Incorrect type of the ${ name } variable - expecting number` );
	}
	return output;
}

function castAsBoolean( name: string, value: string ): boolean {
	const caseInsensitiveValue = value.toLowerCase();
	if ( caseInsensitiveValue === 'true' || caseInsensitiveValue === '1' ) {
		return true;
	}
	if ( caseInsensitiveValue === 'false' || caseInsensitiveValue === '0' ) {
		return false;
	}
	throw new Error( `Incorrect type of the ${ name } variable - expecting boolean` );
}

export default new EnvVariables();