import { v4 as uuid } from 'uuid'; import { Web } from '@contour/fet/lib/elements'; import { DataTable } from '@cucumber/cucumber'; import PARTY from '../../src/enum/party'; import envConfig from '../../src/env/env-config'; import { getDefaultNodeProfile, IDENTITY_SEPARATOR } from '../../src/env/nodeProfile'; import { IScenarioContext } from '../@types/scenarioContext'; import { generateRadomEmail } from './stringUtil'; export default function scenarioContext(): ScenarioContext { if (!global.scenarioContext) { initScenarioContext(); } return global.scenarioContext; } export function initScenarioContext() { global.scenarioContext = new ScenarioContext(); } export async function initScenarioContextForDefaultParty() { global.scenarioContext = new ScenarioContext(); const nodeProfile = getDefaultNodeProfile(); scenarioContext().currentLoggedInCompany = envConfig.defaultParty.applicant; await Web.url(nodeProfile.baseUrl); } export function setupCustomScenarioContext(dataTable: DataTable) { initScenarioContext(); const data = dataTable.hashes(); data.forEach((row: { partyRole: string; party: string; identity?: string }) => { scenarioContext().scenario[row.partyRole] = row.identity ? row.party + IDENTITY_SEPARATOR + row.identity : row.party; }); } class ScenarioContext implements IScenarioContext { scenario: Record; currentLoggedInCompany: string | undefined; track: string | undefined; appVersion: string; dc: { track?: string; uuid?: string; appRefId?: string; randomDate?: string; attachments?: any[]; currentDraftId?: string; documentSourceJson?: any[]; locAmendId?: any[]; otherDocumentsJson?: any[]; concludedAmendmentSize?: number; }; amendment: { amendInProgressJson?: any; amendAcceptedJson?: any; amendPos?: number; }; p2p: P2PContext; variableMap: object | undefined; currentParty: string; userDtl: { email?: string; password?: string; userName?: string }; dataStore: DataStoreActions; constructor() { this.scenario; this.dc = {}; this.p2p = { recipients: [], attachments: [], replies: [] }; this.amendment = {}; this.userDtl = {}; this.dataStore = new DataStore({}); } initVariableMap() { this.variableMap = { '#RANDOM_STRING#': uuid().substring(8), RANDOM_EMAIL: generateRadomEmail() }; } mapContextVariable(str: string): string { if (this.variableMap) { const matchingValues = str.match(/#[^#]+#/g); matchingValues && matchingValues.forEach((e: string) => { if (this.variableMap[e]) { str = str.replace(e, this.variableMap[e]); } }); } return str; } isPartyInScenarioContext(party: string): boolean { if (this.scenario) { this.scenario[party] ? true : false; } return false; } isScenarioPartyAChild(party: string): boolean { return this.scenario[party].includes(IDENTITY_SEPARATOR); } isPartyScenariosPresent(): boolean { return this.scenario ? Object.keys(this.scenario).length > 0 : false; } getScenarioNode(party: string): string { if (!this.isScenarioPartyAChild(party)) { return this.scenario[party]; } return this.scenario[party].split(IDENTITY_SEPARATOR)[0]; } getNodeWithRoleInScenario(role: PARTY, scenario: string): string { return this.scenario[role] ?? envConfig.scenario[scenario][role]; } getPartyNode(scenario: string, party: string) { return this.isPartyScenariosPresent() ? this.getScenarioNode(party) : envConfig.scenario[scenario][party]; } } interface DataStoreActions { get(desc: string): any; set(desc: string, value: any): any; } class DataStore implements DataStoreActions { _ = require('lodash'); private dataStoreObj: object; constructor(obj: object) { this.dataStoreObj = obj; } public get(desc: string): any { return this._.get(this.dataStoreObj, desc); } public set(desc: string, value: any): any { this._.set(this.dataStoreObj, desc, value); return value; } }