All files / ext.wikilambda.edit/store/modules zTesterResults.js

69.51% Statements 57/82
57.14% Branches 36/63
81.81% Functions 18/22
69.51% Lines 57/82

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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              2x 2x 2x 2x                       10x   20x             20x           20x   20x       2x                                     7x 7x   7x 1x     6x 6x                                                                                                                                                                       4x 4x 13x   4x 4x 12x 12x       4x   4x                                     20x 20x                 11x               2x 2x                 7x 7x                           1x   1x                                         6x   6x 1x     5x 5x 5x 1x     5x       5x           5x                           4x 4x                         4x 16x 16x 16x     16x             4x     1x   1x 1x       1x 1x          
/*!
 * WikiLambda Vue editor: Module for storing, retrieving, and running test runners
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
 
var Vue = require( 'vue' ),
	Constants = require( '../../Constants.js' ),
	canonicalize = require( '../../mixins/schemata.js' ).methods.canonicalizeZObject,
	getValueFromCanonicalZMap = require( '../../mixins/schemata.js' ).methods.getValueFromCanonicalZMap;
 
/**
 * Loop through a given array and replace the current Object or the placeholder object with a full object
 * This is required to be able to see proper test result whil changin implementations and testers.
 *
 * @param {Object} context
 * @param {Array} items - List of implementations or testers
 * @param {Object} newItemZObject - Full zObject of the implementation or tester
 * @return {Array}
 */
function replaceCurrentObjectWithFullJSONObject( context, items, newItemZObject ) {
	return ( items || [] ).map( function ( item ) {
		// if the item is the current object replace it
		Iif ( !context.getters.getViewMode && item === context.getters.getCurrentZObjectId ) {
			return canonicalize(
				JSON.parse( JSON.stringify( context.getters.getZObjectAsJson ) )
			);
		}
 
		// if the item has a placeholder value, it means that it is a new implementation/tester, replace it
		Iif ( item === Constants.NEW_ZID_PLACEHOLDER && newItemZObject ) {
			return canonicalize(
				JSON.parse( JSON.stringify( newItemZObject ) )
			);
		}
 
		return item;
	} ).filter( function ( item ) {
		return !!item;
	} );
}
 
module.exports = exports = {
	state: {
		zTesterResults: {},
		zTesterMetadata: {},
		fetchingTestResults: false,
		errorState: false,
		errorMessage: ''
	},
	getters: {
		getZTesterResults: function ( state ) {
			/**
			 * Retrieve the result value zTester a specific set of Function, tester and implementation.
			 *
			 * @param {string} zFunctionId
			 * @param {string} zTesterId
			 * @param {string} zImplementationId
			 *
			 * @return {boolean}
			 */
			return function ( zFunctionId, zTesterId, zImplementationId ) {
				var key = zFunctionId + ':' + zTesterId + ':' + zImplementationId;
 
				if ( state.errorState ) {
					return false;
				}
 
				var result = state.zTesterResults[ key ];
				return result &&
					( result === Constants.Z_BOOLEAN_TRUE ||
						( typeof result === 'object' && result[ Constants.Z_BOOLEAN_IDENTITY ] === Constants.Z_BOOLEAN_TRUE ) );
			};
		},
		getZTesterFailReason: function ( state ) {
			/**
			 * Retrieve the failed reason for a specific test.
			 * Test are identified by a zFunctionId, zTesterId, and zImplementationId.
			 *
			 * @param {string} zFunctionId
			 * @param {string} zTesterId
			 * @param {string} zImplementationId
			 *
			 * @return {string} Error Value
			 */
			return function ( zFunctionId, zTesterId, zImplementationId ) {
				var key = zFunctionId + ':' + zTesterId + ':' + zImplementationId;
 
				if ( state.errorState ) {
					return state.errorMessage;
				}
				var metadata = state.zTesterMetadata[ key ];
				if ( !metadata || metadata === Constants.Z_VOID ) {
					return '';
				}
 
				var errorResponse;
				// Allow for an error object here, for backwards compatibility
				if ( metadata[ Constants.Z_OBJECT_TYPE ] === Constants.Z_ERROR ) {
					errorResponse = metadata;
				} else {
					errorResponse = getValueFromCanonicalZMap( metadata, 'errors' );
				}
				if ( !errorResponse ) {
					return '';
				}
 
				// FIXME: Could use messageForError, from portray.js, for what's below.  But first check if
				//  getZTesterFailReason might go away; see T314079.
				if ( errorResponse[ Constants.Z_ERROR_VALUE ] ) {
					return errorResponse[ Constants.Z_ERROR_VALUE ];
				}
 
				var errorResponseType = errorResponse[ Constants.Z_ERROR_TYPE ][ Constants.Z_OBJECT_TYPE ];
				if ( errorResponse[ Constants.Z_ERROR_TYPE ][
					errorResponseType + Constants.Z_TYPED_OBJECT_ELEMENT_1
				] ) {
					return errorResponse[ Constants.Z_ERROR_TYPE ][
						errorResponseType + Constants.Z_TYPED_OBJECT_ELEMENT_1
					];
				}
 
				return 'No error returned!';
			};
		},
		getZTesterMetadata: function ( state ) {
			/**
			 * Retrieve metadata ( eg. duration ), for a specific test.
			 * Test are identified by a zFunctionId, zTesterId, and zImplementationId.
			 *
			 * @param {string} zFunctionId
			 * @param {string} zTesterId
			 * @param {string} zImplementationId
			 *
			 * @return {Object} metadata
			 */
			return function ( zFunctionId, zTesterId, zImplementationId ) {
				var key = zFunctionId + ':' + zTesterId + ':' + zImplementationId;
 
				// TODO(T314267): Check for and handle state.errorState = true
 
				return state.zTesterMetadata[ key ];
			};
		},
		getZTesterPercentage: function ( state ) {
			/**
			 * Retrieve percentage of test that passed.
			 * This getter takes into consideration all the tests of a specific zId
			 *
			 * @param {string} zid
			 *
			 * @return {Object}
			 */
			return function ( zid ) {
				var results = Object.keys( state.zTesterResults ).filter( function ( key ) {
						return key.indexOf( zid ) !== -1 && state.zTesterResults[ key ] !== undefined;
					} ),
					total = results.length,
					passing = results.filter( function ( key ) {
						var result = state.zTesterResults[ key ];
						return result &&
							( result === Constants.Z_BOOLEAN_TRUE ||
								( typeof result === 'object' && result[ Constants.Z_BOOLEAN_IDENTITY ] === Constants.Z_BOOLEAN_TRUE ) );
					} ).length,
					percentage = Math.round( ( passing / total ) * 100 ) || 0;
 
				return {
					total: total,
					passing: passing,
					percentage: percentage
				};
			};
		}
	},
	mutations: {
		/**
		 * Set the result of a specific test
		 *
		 * @param {Object} state
		 * @param {Object} result
		 * @param {string} result.key
		 * @param {boolean} result.result
		 * @param {Object} result.metadata
		 */
		setZTesterResult: function ( state, result ) {
			Vue.set( state.zTesterResults, result.key, result.result );
			Vue.set( state.zTesterMetadata, result.key, result.metadata );
		},
		/**
		 * Set the fetching state of the test results
		 *
		 * @param {Object} state
		 * @param {boolean} fetching
		 */
		setFetchingTestResults: function ( state, fetching ) {
			state.fetchingTestResults = fetching;
		},
		/**
		 * Clear all the test results and metadata
		 *
		 * @param {Object} state
		 */
		clearZTesterResults: function ( state ) {
			state.zTesterResults = {};
			state.zTesterMetadata = {};
		},
		/**
		 * Set the error state and message
		 *
		 * @param {Object} state
		 * @param {Object} error
		 */
		setErrorState: function ( state, error ) {
			state.errorState = !!error;
			state.errorMessage = error.toString();
		}
	},
	actions: {
		/**
		 * Reset the test results, using payload to create the required test keys
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {string} payload.zFunctionId
		 * @param {string} payload.zTesterId
		 * @param {string} payload.zImplementationId
		 */
		resetTestResult: function ( context, payload ) {
			var key = payload.zFunctionId + ':' + payload.zTesterId + ':' + payload.zImplementationId;
 
			context.commit( 'setZTesterResult', {
				key: key,
				result: undefined
			} );
		},
		/**
		 * Triggers a test API call and updates the test results, handles the test pending state
		 * and also define the error state and meessage if test returns errors
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {string} payload.zFunctionId The ZID of the Function we're for which running these
		 * @param {string[]} payload.zTesters The ZIDs (or if unsaved the full ZObjects) of the Testers to run
		 * @param {string[]} payload.zImplementations The ZIDs (or if unsaved the full ZObjects) of the
		 *   Implementations to run
		 * @param {boolean} payload.nocache Whether to tell the Orchestrator to cache these results
		 * @param {boolean} payload.clearPreviousResults Whether to clear the previous results from the Vuex store
		 *
		 * @return {Promise}
		 */
		getTestResults: function ( context, payload ) {
			var api = new mw.Api();
 
			if ( context.state.fetchingTestResults ) {
				return;
			}
 
			context.commit( 'setFetchingTestResults', true );
			context.commit( 'setErrorState', false );
			if ( payload.clearPreviousResults ) {
				context.commit( 'clearZTesterResults' );
			}
 
			var implementations = replaceCurrentObjectWithFullJSONObject(
				context,
				payload.zImplementations
			);
			var testers = replaceCurrentObjectWithFullJSONObject(
				context,
				payload.zTesters,
				context.getters.getNewTesterZObjects
			);
 
			return api.get( {
				action: 'wikilambda_perform_test',
				// eslint-disable-next-line camelcase
				wikilambda_perform_test_zfunction:
					!context.getters.getViewMode && payload.zFunctionId === context.getters.getCurrentZObjectId ?
						JSON.stringify( context.getters.getZkeys[ payload.zFunctionId ] ) :
						payload.zFunctionId,
				// eslint-disable-next-line camelcase
				wikilambda_perform_test_zimplementations: implementations.join( '|' ),
				// eslint-disable-next-line camelcase
				wikilambda_perform_test_ztesters: testers.join( '|' ),
				// eslint-disable-next-line camelcase
				wikilambda_perform_test_nocache: payload.nocache || false
			} ).then( function ( data ) {
				var results = data.query.wikilambda_perform_test;
				Iif ( !Array.isArray( results ) &&
						results[ Constants.Z_RESPONSEENVELOPE_METADATA ] !== Constants.Z_NOTHING ) {
					throw new Error(
						results[
							Constants.Z_RESPONSEENVELOPE_METADATA
						][
							Constants.Z_ERROR_VALUE
						][
							Constants.Z_STRING_VALUE
						]
					);
				}
 
				results.forEach( function ( testResult ) {
					var status = canonicalize( JSON.parse( testResult.validateStatus ) ),
						metadata = canonicalize( JSON.parse( testResult.testMetadata ) ),
						key = ( testResult.zFunctionId || Constants.NEW_ZID_PLACEHOLDER ) + ':' + ( testResult.zTesterId || Constants.NEW_ZID_PLACEHOLDER ) + ':' + ( testResult.zImplementationId || Constants.NEW_ZID_PLACEHOLDER );
 
					// Store result
					context.commit( 'setZTesterResult', {
						key: key,
						result: status,
						metadata: metadata
					} );
				} );
 
				context.commit( 'setFetchingTestResults', false );
			} )
				.catch( function ( error, message ) {
					mw.log.error( 'Tester API call was nothing: ' + error );
 
					var errorMessage = error;
					Iif ( message && message.error && message.error.info ) {
						errorMessage = message.error.info;
					}
 
					context.commit( 'setErrorState', errorMessage );
					context.commit( 'setFetchingTestResults', false );
				} );
		}
	}
};