UNPKG

@case-contract-testing/case

Version:

Next-generation contract testing suite

72 lines (71 loc) 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReadingCaseContract = void 0; const async_mutex_1 = require("async-mutex"); const entities_1 = require("../entities"); const context_1 = require("../entities/context"); const contract_1 = require("../entities/contract"); const BaseCaseContract_1 = require("./BaseCaseContract"); const executeExample_1 = require("./executeExample"); class ReadingCaseContract extends BaseCaseContract_1.BaseCaseContract { mutex; makeBrokerService; links; status; constructor(contractFile, { resultPrinter, makeLogger, defaultConfig, makeBrokerService, }, config) { super(contractFile.description, { throwOnFail: false, testRunId: 'VERIFIER', ...config }, defaultConfig, resultPrinter, makeLogger); this.currentContract = contractFile; this.makeBrokerService = makeBrokerService; this.links = contractFile; this.status = 'UNKNOWN'; this.mutex = new async_mutex_1.Mutex(); } verifyContract({ stateHandlers = {}, triggers }, runTestCb) { this.currentContract.examples.forEach((example, index) => { if (example.result !== 'VERIFIED') { throw new entities_1.CaseCoreError(`Attempting to verify an example which was '${example.result}'. This should never happen in normal operation, and might be the result of a corrupted ContractCase file, a file that was not written by ContractCase, or a bug.`); } const names = (0, contract_1.exampleToNames)(example, `${index}`); runTestCb(names.mockName, () => this.mutex.runExclusive(() => (0, executeExample_1.executeExample)({ ...example, result: 'PENDING' }, { stateHandlers, names, triggers }, this, (0, context_1.applyNodeToContext)(example.mock, this.initialContext, { 'case:currentRun:context:testName': `${index}`, 'case:currentRun:context:contractMode': 'read', 'case:currentRun:context:location': [ 'verification', `mock[${index}]`, ], })))); }); runTestCb('Publishing verification results', () => { this.initialContext.logger.maintainerDebug('In test callback'); return this.endRecord(); }); } // eslint-disable-next-line class-methods-use-this recordExample(example, currentContext) { if (example.result === 'FAILED') { this.status = 'FAILED'; } currentContext.logger.maintainerDebug(`recordExample called with`, example); return example; } async endRecord() { const publishingContext = (0, context_1.addLocation)('PublishingResults', this.initialContext); if (this.status === 'UNKNOWN') { this.status = 'SUCCESS'; } if (this.status === 'FAILED') { // TODO: Print all failures this.initialContext.logger.maintainerDebug('Verification failed'); } else { this.initialContext.logger.maintainerDebug('Verification successful'); } if (!this.initialContext['case:currentRun:context:brokerCiAccessToken']) { this.initialContext.logger.warn('Not publishing verification results, as there is no brokerCiAccessToken set'); return; } this.initialContext.logger.maintainerDebug('Calling publishVerificationResults'); await this.makeBrokerService(publishingContext).publishVerificationResults(this.links, this.status === 'SUCCESS', (0, context_1.addLocation)('PublishingContract', this.initialContext)); } } exports.ReadingCaseContract = ReadingCaseContract;