All files performTest.js

75% Statements 48/64
59.09% Branches 13/22
45.45% Functions 5/11
77.05% Lines 47/61

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    1x 1x 1x 1x 1x     2x 2x 2x 2x 2x   2x       3x 3x 2x   1x 1x                   3x   3x     3x                 2x                                       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  
'use strict';
 
const { ReferenceResolver } = require( './db.js' );
const { generateError } = require( './utils' );
const orchestrate = require( './orchestrate.js' );
const normalize = require( '../function-schemata/javascript/src/normalize.js' );
const { convertZListToItemArray, convertItemArrayToZList } = require( '../function-schemata/javascript/src/utils.js' );
 
async function resolveListOfReferences( listOfReferences, resolver ) {
	const ZIDs = convertZListToItemArray( listOfReferences ).map( ( Z9 ) => ( Z9.Z9K1 ) );
	const resolved = await resolver.dereference( ZIDs );
	const result = [];
	for ( const ZID of ZIDs ) {
		result.push( resolved[ ZID ].asJSON() );
	}
	return result;
}
 
function parse( str ) {
	try {
		const zobject = JSON.parse( str );
		return zobject;
	} catch ( err ) {
		const m = ( err.name === 'SyntaxError' ) ? err.message : err.name;
		return generateError( m );
	}
}
 
async function getTestResults( data, wikiUri, evaluatorUri ) {
	const {
		zfunction,
		zimplementations,
		ztesters,
		doValidate
	} = parse( data );
 
	const resolver = new ReferenceResolver( wikiUri );
 
	// Get ZFunction object
	const zFunction = zfunction.match( /^Z\d+$/ ) ?
		( await resolver.dereference( [ zfunction ] ) )[ zfunction ].asJSON() :
		( await normalize( JSON.parse( zfunction ),
			/* generically= */true, /* withVoid= */ true ) ).Z22K1;
 
	// Get ZImplementation objects
	// If list of implementations is provided, get those.
	// Otherwise, fetch all associated with ZFunction
	let implementations;
	Iif ( zimplementations && JSON.parse( zimplementations ).filter( ( item ) => !!item ).length ) {
		implementations = await Promise.all(
			// Parse the list of implementations
			JSON.parse( zimplementations ).map(
				async ( impl ) => {
					if ( typeof impl === 'object' ) {
						// If it's an object, normalize it
						return ( await normalize( impl,
							/* generically= */true, /* withVoid= */ true ) ).Z22K1;
					} else {
						// If it's a string, dereference it
						return resolver.dereference(
							[ impl ]
						).then( ( res ) => res[ impl ].asJSON() );
					}
				}
			)
		);
	} else {
		// Get the list of ZImplementations, then dereference them
		implementations = await resolveListOfReferences( zFunction.Z2K2.Z8K4, resolver );
	}
 
	// Get ZTester objects
	// If list of testers is provided, get those.
	// Otherwise, fetch all associated with ZFunction
	let testers;
	Iif ( ztesters && JSON.parse( ztesters ).filter( ( item ) => !!item ).length ) {
		testers = await Promise.all(
			// Parse the list of testers
			JSON.parse( ztesters ).map(
				async ( tester ) => {
					if ( typeof tester === 'object' ) {
						// If it's an object, normalize it
						return ( await normalize( tester,
							/* generically= */true, /* withVoid= */ true ) ).Z22K1;
						// If it's a string, dereference it
					} else {
						return resolver.dereference(
							[ tester ]
						).then( ( res ) => res[ tester ].asJSON() );
					}
				}
			)
		);
	} else {
		// Get the list of ZTesters, then dereference them
		testers = await resolveListOfReferences( zFunction.Z2K2.Z8K3, resolver );
	}
 
	async function performTest(
		zFunction,
		zImplementation,
		zTester
	) {
		const payload = {
			zFunctionId: zFunction.Z2K1.Z6K1,
			zImplementationId: zImplementation.Z2K1.Z6K1,
			zTesterId: zTester.Z2K1.Z6K1,
			validationResponse: null
		};
 
		const test = JSON.parse( JSON.stringify( zTester.Z2K2.Z20K2 ) );
		const implementation = JSON.parse( JSON.stringify( zImplementation.Z2K2 ) );
		const validator = JSON.parse( JSON.stringify( zTester.Z2K2.Z20K3 ) );
 
		test.Z7K1 = zFunction.Z2K2;
		test.Z7K1.Z8K4 = await convertItemArrayToZList( [ implementation ] );
 
		const testResponse = ( await normalize(
			await orchestrate( {
				zobject: test,
				evaluatorUri,
				wikiUri,
				doValidate
			} ), /* generically= */true, /* withVoid= */ true
		) ).Z22K1;
		const testResult = testResponse.Z22K1;
 
		Iif ( testResult === 'Z21' || testResult.Z9K1 === 'Z21' ) {
			payload.validationResponse = testResponse;
		} else {
			const validatorFn = validator.Z7K1.Z9K1;
			validator[ validatorFn + 'K1' ] = testResult;
 
			const start = Date.now();
 
			const validationResponse = await orchestrate( {
				zobject: validator,
				evaluatorUri,
				wikiUri,
				doValidate
			} );
 
			const end = Date.now();
 
			payload.duration = end - start;
 
			Iif ( validationResponse.Z22K1.Z40K1 === 'Z42' ) {
				const actual = testResult;
				const expected = validator[ validatorFn + 'K2' ];
 
				// FIXME (T311163): replace the hardcoded validationResponse.Z22K2
				// with makeMappedResultEnvelope and a specific error type
				// about test failure
				validationResponse.Z22K2 = {
					Z1K1: {
						Z1K1: 'Z9',
						Z9K1: 'Z5'
					},
					Z5K2: {
						Z1K1: {
							Z1K1: 'Z9',
							Z9K1: 'Z10'
						},
						Z10K1: expected,
						Z10K2: {
							Z1K1: {
								Z1K1: 'Z9',
								Z9K1: 'Z10'
							},
							Z10K1: actual
						}
					}
				};
			}
 
			payload.validationResponse = validationResponse;
		}
 
		return payload;
	}
 
	const tests = [];
 
	for ( const implementation of implementations ) {
		for ( const tester of testers ) {
			tests.push( performTest( zFunction, implementation, tester ) );
		}
	}
 
	return Promise.all( tests );
}
 
module.exports = getTestResults;