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

7.5% Statements 6/80
0% Branches 0/16
4.76% Functions 1/21
7.59% Lines 6/79

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            2x 2x 2x 2x                           2x                                     2x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
/*!
 * WikiLambda Vuex code to manipulate the ZTesters of a ZFunction object.
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
var canonicalize = require( '../../mixins/schemata.js' ).methods.canonicalizeZObject,
	Constants = require( '../../Constants.js' ),
	saveZObject = require( '../../mixins/api.js' ).methods.saveZObject,
	typeUtils = require( '../../mixins/typeUtils.js' ).methods;
 
function filterOutPresentZids( rootState ) {
	return function ( zid ) {
		var zobject;
		for ( zobject in rootState.zobjectModule.zobject ) {
			if ( rootState.zobjectModule.zobject[ zobject ].value === zid ) {
				return false;
			}
		}
 
		return true;
	};
}
module.exports = exports = {
	state: {
		newTester: null,
		/**
		 * List of tester keys
		 */
		zTesters: []
	},
	getters: {
		getNewTesterId: function ( state ) {
			return state.newTester;
		},
		getNewTesterZObjects: function ( state, getters ) {
			return getters.getZObjectAsJsonById( state.newTester );
		},
		getUnattachedZTesters: function ( state, getters, rootState ) {
			return state.zTesters.filter( filterOutPresentZids( rootState ) );
		},
		getZTesters: function ( state ) {
			return state.zTesters;
		},
		getTestInputOutputByZIDs: function ( state, getters ) {
			/**
			 * @param {Array} zIDs
			 * @return {Array}
			 */
			return function ( zIDs ) {
				var inputOutput = [];
				for ( let index = 0; index < zIDs.length; index++ ) {
					const zid = zIDs[ index ];
					if ( getters.getZkeyLabels[ zid ] ) {
						const zObjectValue = getters.getZkeyLabels[ zid ].split( ' -> ' );
						inputOutput.push( {
							input: zObjectValue[ 0 ],
							output: zObjectValue[ 1 ]
						} );
					}
				}
 
				return inputOutput;
			};
		}
	},
	mutations: {
		addNewTester: function ( state, newTester ) {
			state.newTester = newTester;
		},
		removeNewTester: function ( state ) {
			state.newTester = null;
		},
		/**
		 * Set the zTesters in the store
		 *
		 * @param {Object} state
		 * @param {Object} zTesters
		 */
		setZTesters: function ( state, zTesters ) {
			state.zTesters = zTesters;
		}
	},
	actions: {
		/**
		 * Fetches function testers of a the specified zFunctionId.
		 * This methos will also fetch the zKeys in case of them are missing.
		 *
		 * @param {Object} context
		 * @param {string} zFunctionId
		 *
		 * @return {Promise}
		 */
		fetchZTesters: function ( context, zFunctionId ) {
			var api = new mw.Api();
 
			return api.get( {
				action: 'query',
				list: 'wikilambdafn_search',
				format: 'json',
				// eslint-disable-next-line camelcase
				wikilambdafn_zfunction_id: zFunctionId,
				// eslint-disable-next-line camelcase
				wikilambdafn_type: Constants.Z_TESTER
			} ).then( function ( response ) {
				var zidList = response.query.wikilambdafn_search.map( function ( zidItem ) {
					return zidItem.zid;
				} );
				context.commit( 'setZTesters', zidList );
				return context.dispatch( 'fetchZKeys', { zids: zidList } );
			} );
		},
		/**
		 * Create a new instance of a tester. This is NOT attached to the main object, and used
		 * by the UI to support the user in creating a new tester.
		 * The object is given a parent of -1 to indicate that it is not attached to the main zObject
		 *
		 * @param {Object} context
		 */
		createNewTester: function ( context ) {
			context.dispatch( 'initializeResultId', context.getters.getNextObjectId )
				.then( function ( resultId ) {
					var returnType = context.getters.getZObjectAsJson[
							Constants.Z_PERSISTENTOBJECT_VALUE ][
							Constants.Z_FUNCTION_RETURN_TYPE ][
							Constants.Z_REFERENCE_ID ],
						defaultTestValidator = '';
 
					if ( returnType === Constants.Z_STRING ) {
						defaultTestValidator = Constants.Z_FUNCTION_STRING_EQUALITY;
					} else if ( returnType === Constants.Z_BOOLEAN ) {
						defaultTestValidator = Constants.Z_FUNCTION_BOOLEAN_EQUALITY;
					}
 
					context.commit( 'addNewTester', resultId );
 
					context.dispatch( 'injectZObject', {
						zobject: {
							Z1K1: Constants.Z_PERSISTENTOBJECT,
							Z2K1: Constants.NEW_ZID_PLACEHOLDER,
							Z2K2: {
								Z1K1: Constants.Z_TESTER,
								Z20K1: context.getters.getCurrentZObjectId,
								Z20K2: {
									Z1K1: Constants.Z_FUNCTION_CALL,
									Z7K1: context.getters.getCurrentZObjectId
								},
								Z20K3: {
									Z1K1: Constants.Z_FUNCTION_CALL,
									Z7K1: defaultTestValidator
								}
							},
							Z2K3: {
								Z1K1: Constants.Z_MULTILINGUALSTRING,
								Z12K1: [
									Constants.Z_MONOLINGUALSTRING,
									{
										Z1K1: Constants.Z_MONOLINGUALSTRING,
										Z11K1: context.getters.getUserZlangZID,
										Z11K2: ''
									}
								]
							},
							Z2K4: {
								Z1K1: Constants.Z_MULTILINGUALSTRINGSET,
								Z32K1: [
									Constants.Z_MONOLINGUALSTRINGSET,
									{
										Z1K1: Constants.Z_MONOLINGUALSTRINGSET,
										Z31K1: Constants.Z_NATURAL_LANGUAGE_ENGLISH,
										Z31K2: []
									}
								]
							}
						},
						key: '',
						id: resultId,
						parent: -1
					} );
				} );
		},
		/**
		 * Generates the test summary label used in the UI.
		 * This string includes the input and expected output value.
		 * eg: [ input1, input2 ] -> true
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {number} payload.testerId
		 */
		updateTesterLabel: function ( context, payload ) {
			var testerJson = canonicalize( context.getters.getZObjectAsJsonById( payload.testerId ) ).Z2K2,
				testerLabels = context.getters.getZObjectChildrenById(
					context.getters.getNestedZObjectById( payload.testerId, [
						Constants.Z_PERSISTENTOBJECT_LABEL,
						Constants.Z_MULTILINGUALSTRING_VALUE
					] ).id ),
				call = testerJson.Z20K2,
				validator = testerJson.Z20K3,
				validatorZid = validator.Z7K1,
				callInputs = '',
				validatorInputs = '',
				key,
				value;
 
			delete call.Z1K1;
			delete call.Z7K1;
			delete validator.Z1K1;
			delete validator.Z7K1;
			delete validator[ validatorZid + 'K1' ];
 
			for ( key in call ) {
				value = call[ key ];
				if ( !value ) {
					continue;
				}
				if ( callInputs.length ) {
					callInputs += ', ' + typeUtils.zObjectToString( value ).toString();
				} else {
					callInputs = typeUtils.zObjectToString( value ).toString();
				}
			}
 
			for ( key in validator ) {
				value = validator[ key ];
				if ( !value ) {
					continue;
				}
				if ( validatorInputs.length ) {
					validatorInputs += ', ' + typeUtils.zObjectToString( value ).toString();
				} else {
					validatorInputs = typeUtils.zObjectToString( value ).toString();
				}
			}
 
			testerLabels.forEach( function ( labelObject ) {
				var labelString = context.getters.getNestedZObjectById( labelObject.id, [
					Constants.Z_MONOLINGUALSTRING_VALUE,
					Constants.Z_STRING_VALUE
				] );
 
				context.dispatch( 'setZObjectValue', {
					id: labelString.id,
					value: callInputs + ' -> ' + validatorInputs
				} );
			} );
		},
		/**
		 * This method will take the provisional testers (the one created using createNewTester )
		 * and create a new zObject from it. It then attached the newly create zId as a tester
		 * to the current zObject.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {number} payload.testerId
		 * @param {number} payload.nextTesterIndex
		 */
		saveNewTester: function ( context, payload ) {
			var zobject = canonicalize( context.getters.getZObjectAsJsonById( payload.testerId ) ),
				newZid;
 
			saveZObject( zobject ).then( function ( result ) {
				newZid = result.title;
				return context.dispatch( 'fetchZTesters', context.getters.getCurrentZObjectId );
			} ).then( function () {
				var nextId = context.getters.getNextObjectId;
 
				context.dispatch( 'addZObject', {
					key: payload.nextTesterIndex,
					value: 'object',
					parent: payload.parent
				} );
				context.dispatch( 'addZReference', {
					value: newZid,
					id: nextId
				} );
 
				context.dispatch( 'removeZObjectChildren', payload.testerId );
				context.dispatch( 'removeZObject', payload.testerId );
				context.commit( 'removeNewTester' );
			} );
		}
	}
};