All files / ext.wikilambda.app/components/widgets/function-report FunctionReport.vue

95.78% Statements 341/356
86.53% Branches 45/52
90.47% Functions 19/21
95.78% Lines 341/356

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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 3571x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 78x 1x 1x 1x 1x 1x 1x 1x 1x 80x 1x 1x 1x 1x 1x 1x 1x 1x 12x 1x 1x 1x 1x 1x 1x 1x 1x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 109x 109x 31x 31x 78x 109x 41x 109x 37x 37x 37x 37x 37x 37x 1x 1x 1x 1x 1x 1x 1x 1x 1x 109x 109x 31x 31x 78x 109x 37x 109x 41x 41x 41x 41x 41x 41x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x       12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 6x 2x 2x 6x 1x 1x 1x 1x 95x       1x 1x 95x       95x 1x 1x 14x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<!--
	WikiLambda Vue component for displaying the results of running
	a function's testers against its implementations or a funciton's
	implementations against its testers.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<wl-widget-base class="ext-wikilambda-app-function-report-widget" data-testid="function-report-widget">
		<!-- Widget header -->
		<template #header>
			{{ title }}
		</template>
		<template #header-action>
			<cdx-button
				v-if="hasItems"
				weight="quiet"
				:aria-label="reloadLabel"
				@click="runTesters"
			>
				<cdx-icon :icon="reloadIcon"></cdx-icon>
			</cdx-button>
		</template>
 
		<!-- Widget main -->
		<template #main>
			<div v-if="hasItems">
				<div
					v-for="item in zIds"
					:key="item"
					class="ext-wikilambda-app-function-report-widget__items"
				>
					<wl-function-report-item
						class="ext-wikilambda-app-function-report-widget__result"
						:z-function-id="zFunctionId"
						:z-implementation-id="isImplementationReport ? zImplementationId : item"
						:z-tester-id="isTesterReport ? zTesterId : item"
						:fetching="fetching"
						:report-type="reportType"
						@set-keys="openMetricsDialog"
					></wl-function-report-item>
				</div>
				<wl-function-metadata-dialog
					:open="showMetrics"
					:header-text="activeImplementationLabelData"
					:metadata="metadata"
					:error-id="errorId"
					@close-dialog="closeMetricsDialog"
				></wl-function-metadata-dialog>
			</div>
			<div v-else>
				<p> {{ $i18n( 'wikilambda-tester-no-results' ).text() }} </p>
			</div>
		</template>
	</wl-widget-base>
</template>
 
<script>
const { CdxButton, CdxIcon } = require( '@wikimedia/codex' );
const { defineComponent } = require( 'vue' );
const Constants = require( '../../../Constants.js' ),
	typeUtils = require( '../../../mixins/typeUtils.js' ),
	utilsMixins = require( '../../../mixins/utilsMixins.js' ),
	{ mapActions, mapGetters } = require( 'vuex' ),
	icons = require( '../../../../lib/icons.json' ),
	WidgetBase = require( '../../base/WidgetBase.vue' ),
	FunctionMetadataDialog = require( '../function-evaluator/FunctionMetadataDialog.vue' ),
	FunctionReportItem = require( './FunctionReportItem.vue' );
 
module.exports = exports = defineComponent( {
	name: 'wl-function-report-widget',
	components: {
		'wl-function-report-item': FunctionReportItem,
		'wl-function-metadata-dialog': FunctionMetadataDialog,
		'wl-widget-base': WidgetBase,
		'cdx-button': CdxButton,
		'cdx-icon': CdxIcon
	},
	mixins: [ typeUtils, utilsMixins ],
	inject: {
		viewmode: { default: false }
	},
	props: {
		reportType: {
			type: String,
			required: true
		},
		zFunctionId: {
			type: String,
			required: true
		},
		rootZid: {
			type: String,
			required: true
		}
	},
	data: function () {
		return {
			activeZImplementationId: null,
			activeZTesterId: null,
			errorId: Constants.errorIds.TEST_RESULTS,
			showMetrics: false,
			fetching: false
		};
	},
	computed: Object.assign( mapGetters( [
		'getLabelData',
		'getStoredObject',
		'getZTesterMetadata'
	] ), {
		/**
		 * Returns the items that must be tested. If we are in an implementation page
		 * returns the tester zids. If we are in a tester page, returns the implementation
		 * zids.
		 *
		 * @return {Array}
		 */
		zIds: function () {
			return this.isTesterReport ? this.implementations : this.testers;
		},
 
		/**
		 * Returns whether there are any items to test.
		 *
		 * @return {boolean}
		 */
		hasItems: function () {
			return ( this.zFunctionId && ( this.zIds.length > 0 ) );
		},
 
		/**
		 * Whether it is a report for an implementation page.
		 *
		 * @return {boolean}
		 */
		isImplementationReport: function () {
			return this.reportType === Constants.Z_IMPLEMENTATION;
		},
 
		/**
		 * Whether it is a report for a tester page.
		 *
		 * @return {boolean}
		 */
		isTesterReport: function () {
			return this.reportType === Constants.Z_TESTER;
		},
 
		/**
		 * Returns the selected Implementation zid if we are in an implementation
		 * page; else returns null.
		 *
		 * @return {string|null}
		 */
		zImplementationId: function () {
			return this.isImplementationReport ? this.rootZid : null;
		},
 
		/**
		 * Returns the selected Tester zid if we are in a tester page;
		 * else returns null.
		 *
		 * @return {string|null}
		 */
		zTesterId: function () {
			return this.isTesterReport ? this.rootZid : null;
		},
 
		/**
		 * Returns the list of implementation zids in the persisted
		 * selected function.
		 *
		 * @return {Array}
		 */
		implementations: function () {
			const functionObject = this.getStoredObject( this.zFunctionId );
			if ( !this.zFunctionId || !functionObject ) {
				return [];
			}
 
			if ( this.zImplementationId ) {
				return [ this.zImplementationId ];
			} else {
				const fetched = functionObject[
					Constants.Z_PERSISTENTOBJECT_VALUE ][
					Constants.Z_FUNCTION_IMPLEMENTATIONS ];
				// Slice off the first item in the canonical form array; this is a string representing the type.
				return Array.isArray( fetched ) ? fetched.slice( 1 ) : [];
			}
		},
 
		/**
		 * Returns the list of tester zids in the persisted
		 * selected function.
		 *
		 * @return {Array}
		 */
		testers: function () {
			const functionObject = this.getStoredObject( this.zFunctionId );
			if ( !this.zFunctionId || !functionObject ) {
				return [];
			}
 
			if ( this.zTesterId ) {
				return [ this.zTesterId ];
			} else {
				const fetched = functionObject[
					Constants.Z_PERSISTENTOBJECT_VALUE ][
					Constants.Z_FUNCTION_TESTERS ];
				// Slice off the first item in the canonical form array; this is a string representing the type.
				return Array.isArray( fetched ) ? fetched.slice( 1 ) : [];
			}
		},
 
		/**
		 * Returns the metadata object for the current open metrics dialog;
		 * else, returns null
		 *
		 * @return {Object | null}
		 */
		metadata: function () {
			return ( this.activeZTesterId && this.activeZImplementationId ) ?
				this.getZTesterMetadata(
					this.zFunctionId,
					this.activeZTesterId,
					this.activeZImplementationId ) :
				null;
		},
 
		/**
		 * Returns the label data of the implementation for the current open metrics dialog
		 *
		 * @return {LabelData|undefined}
		 */
		activeImplementationLabelData: function () {
			return this.activeZImplementationId ?
				this.getLabelData( this.activeZImplementationId ) :
				undefined;
		},
 
		/**
		 * Returns the icon for the top right corner of the widget,
		 * depending on the running state.
		 *
		 * @return {string}
		 */
		reloadIcon: function () {
			return this.fetching ? icons.cdxIconCancel : icons.cdxIconReload;
		},
 
		/**
		 * Returns the title of the widget, depending on the page type
		 *
		 * @return {string}
		 */
		title: function () {
			return this.isTesterReport ?
				this.$i18n( 'wikilambda-function-implementation-table-header' ).text() :
				this.$i18n( 'wikilambda-function-test-cases-table-header' ).text();
		},
 
		/**
		 * Returns the label of the reload button
		 *
		 * @return {string}
		 */
		reloadLabel: function () {
			return this.fetching ?
				this.$i18n( 'wikilambda-tester-status-cancel' ).text() :
				this.$i18n( 'wikilambda-tester-status-run' ).text();
		}
	} ),
	methods: Object.assign( mapActions( [
		'fetchZids',
		'getTestResults'
	] ), {
		/**
		 * Sets the target zids and opens the metrics dialog
		 *
		 * @param {Object} keys
		 */
		openMetricsDialog: function ( keys ) {
			this.activeZImplementationId = keys.zImplementationId;
			this.activeZTesterId = keys.zTesterId;
			this.showMetrics = true;
		},
		/**
		 * Closes the metrics dialog
		 */
		closeMetricsDialog: function () {
			this.activeZImplementationId = null;
			this.activeZTesterId = null;
			this.showMetrics = false;
		},
		/**
		 * Calls the run function API with the required tester and implementation zids.
		 */
		runTesters: function () {
			this.fetching = true;
			this.getTestResults( {
				zFunctionId: this.zFunctionId,
				zImplementations: this.implementations,
				zTesters: this.testers,
				clearPreviousResults: true
			} ).then( () => {
				this.fetching = false;
			} );
		},
		/**
		 * Run the initial call only when we are in a view or edit page
		 * but not when we are in a new implementation or test page
		 */
		runInitialCall: function () {
			if ( this.rootZid && this.rootZid !== Constants.NEW_ZID_PLACEHOLDER ) {
				this.runTesters();
			}
		}
 
	} ),
	watch: {
		implementations: function ( newValue, oldValue ) {
			if ( !this.arraysAreEqual( oldValue, newValue ) ) {
				this.fetchZids( { zids: this.implementations } );
				this.runTesters();
			}
		},
		testers: function ( newValue, oldValue ) {
			if ( !this.arraysAreEqual( oldValue, newValue ) ) {
				this.fetchZids( { zids: this.testers } );
				this.runTesters();
			}
		}
	},
	mounted: function () {
		this.fetchZids( { zids: this.implementations.concat( this.testers ) } )
			.then( () => {
				setTimeout( this.runInitialCall, 1000 );
			} );
	}
} );
</script>
 
<style lang="less">
@import '../../../ext.wikilambda.app.variables.less';
 
.ext-wikilambda-app-function-report-widget {
	.ext-wikilambda-app-function-report-widget__items {
		margin-bottom: @spacing-50;
 
		&:last-child {
			margin-bottom: 0;
		}
	}
}
</style>