All files / ext.wikilambda.edit/components/widgets FunctionReport.vue

85.52% Statements 65/76
77.58% Branches 45/58
84.37% Functions 27/32
85.52% Lines 65/76

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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408    16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x                                                           14x                               78x               80x               12x               14x                 12x                 12x                 109x 109x 31x   78x 41x   37x   37x                   109x 109x 31x   78x 37x   41x   41x                   12x                               12x                 20x               14x               20x                                                   4x 4x           4x               6x 2x                                                   95x             95x               14x     14x       16x   16x     16x 16x 16x     16x       14x 14x 14x 14x 14x 14x       88x     88x             40x               88x 209x                                         16x                                                                                                                                  
<!--
	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-function-report">
		<!-- 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-function-report__items"
				>
					<wl-function-report-item
						class="ext-wikilambda-function-report__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="activeImplementationLabel"
					:metadata="metadata"
					@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 { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	typeUtils = require( '../../mixins/typeUtils.js' ),
	mapGetters = require( 'vuex' ).mapGetters,
	mapActions = require( 'vuex' ).mapActions,
	CdxButton = require( '@wikimedia/codex' ).CdxButton,
	CdxIcon = require( '@wikimedia/codex' ).CdxIcon,
	icons = require( '../../../lib/icons.json' ),
	WidgetBase = require( '../base/WidgetBase.vue' ),
	FunctionMetadataDialog = require( './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 ],
	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,
			showMetrics: false,
			fetching: false
		};
	},
	computed: Object.assign( mapGetters( [
		'getLabel',
		'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 of the tester for the current open metrics dialog
		 *
		 * @return {string}
		 */
		activeTesterLabel: function () {
			return this.activeZTesterId ? this.getLabel( this.activeZTesterId ) : '';
		},
 
		/**
		 * Returns the label of the implementation for the current open metrics dialog
		 *
		 * @return {string}
		 */
		activeImplementationLabel: function () {
			return this.activeZImplementationId ? this.getLabel( this.activeZImplementationId ) : '';
		},
 
		/**
		 * 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;
		},
 
		/**I
		 * Returns the title of the widget, depending on the page type
		 *
		 * @return {string}
		 */
		title: function () {
			return this.isTesterReport ?
				thIis.$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();
			}
		},
		/**
		 * Returns the label of the given implementation zid or
		 * a default message if the implementation has no name.
		 *
		 * @param {string} implementation
		 * @return {string}
		 */
		implementationLabel: function ( implementation ) {
			return this.zImplementationId ?
				this.$i18n( 'wikilambda-tester-results-current-implementation' ).text() :
				this.getLabel( implementation );
		},
		/**
		 * Returns the label of the given tester zid or
		 * a default message if the tester has no name.
		 *
		 * @param {string} test
		 * @return {string}
		 */
		testLabel: function ( test ) {
			return this.zTesterId ?
				this.$i18n( 'wikilambda-tester-results-current-test' ).text() :
				this.getLabel( test );
		}
	} ),
	watch: {
		implementations: function ( newValue, oldValue ) {
			if ( newValue.length !== oldValue.length ) {
				this.fetchZids( { zids: this.implementations } );
			}
		},
		testers: function ( newValue, oldValue ) {
			if ( newValue.length !== oldValue.length ) {
				this.fetchZids( { zids: this.testers } );
			}
		}
	},
	mounted: function () {
		this.fetchZids( { zids: this.implementations.concat( this.testers ) } )
			.then( () => {
				setTimeout( this.runInitialCall, 1000 );
			} );
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-function-report {
	&__items {
		margin-bottom: @spacing-50;
 
		&:last-child {
			margin-bottom: 0;
		}
	}
 
	.cdx-card__text__description {
		.ext-wikilambda-function-report-item-status {
			&__ready {
				color: @color-disabled;
			}
 
			&__canceled {
				color: @color-subtle;
			}
 
			&__passed {
				color: @color-success;
			}
 
			&__failed {
				color: @color-error;
			}
 
			&__running {
				color: @color-warning;
			}
		}
	}
}
</style>