@sprucelabs/mercury-client
Version:
The simple way to interact with the Spruce Experience Platform
214 lines (213 loc) • 9.06 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { coreEventContracts } from '@sprucelabs/mercury-core-events';
import { SchemaError } from '@sprucelabs/schema';
import { eventResponseUtil } from '@sprucelabs/spruce-event-utils';
import AbstractSpruceTest from '@sprucelabs/test';
import { assert } from '@sprucelabs/test-utils';
import MercuryClientFactory from '../clients/MercuryClientFactory.js';
import MutableContractClient from '../clients/MutableContractClient.js';
import { TEST_HOST } from './constants.js';
class AbstractClientTest extends AbstractSpruceTest {
static afterEach() {
const _super = Object.create(null, {
afterEach: { get: () => super.afterEach }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.afterEach.call(this);
for (const client of this.clients) {
yield client.disconnect();
}
this.clients = [];
});
}
static beforeEach() {
const _super = Object.create(null, {
beforeEach: { get: () => super.beforeEach }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.beforeEach.call(this);
MercuryClientFactory.reset();
});
}
static beforeAll() {
const _super = Object.create(null, {
beforeAll: { get: () => super.beforeAll }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.beforeAll.call(this);
MercuryClientFactory.setDefaultTimeoutMs(2 * 60 * 1000);
});
}
static afterAll() {
const _super = Object.create(null, {
afterAll: { get: () => super.afterAll }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.afterAll.call(this);
for (const client of this.clients) {
yield client.disconnect();
}
this.clients = [];
MercuryClientFactory.reset();
});
}
static connectToApi(options) {
return __awaiter(this, void 0, void 0, function* () {
const _a = options || {}, { host = TEST_HOST } = _a, rest = __rest(_a, ["host"]);
const client = yield MercuryClientFactory.Client(Object.assign({ host, contracts: coreEventContracts, reconnectDelayMs: 10, emitTimeoutMs: 10000 }, rest));
this.clients.push(client);
return client;
});
}
static loginAsDemoPerson() {
return __awaiter(this, arguments, void 0, function* (phone = process.env.DEMO_PHONE) {
const client = yield this.connectToApi();
if (!phone) {
throw new SchemaError({
code: 'MISSING_PARAMETERS',
parameters: ['env.DEMO_PHONE'],
});
}
const requestPinResults = yield client.emit('request-pin::v2020_12_25', {
payload: {
phone,
},
});
const { challenge } = eventResponseUtil.getFirstResponseOrThrow(requestPinResults);
assert.isTruthy(challenge);
const confirmPinResults = yield client.emit('confirm-pin::v2020_12_25', {
payload: {
challenge,
pin: phone.substr(-4),
},
});
const { person, token } = eventResponseUtil.getFirstResponseOrThrow(confirmPinResults);
assert.isTruthy(person, 'Failed to login!');
return { person, client, token };
});
}
static seedDummyOrg(client) {
return __awaiter(this, void 0, void 0, function* () {
const orgName = `Dummy org ${new Date().getTime()}`;
const orgResults = yield client.emit('create-organization::v2020_12_25', {
payload: {
name: orgName,
},
});
const { organization } = eventResponseUtil.getFirstResponseOrThrow(orgResults);
assert.isTruthy(organization);
return organization;
});
}
static seedInstallAndLoginAsSkill(client, orgId) {
return __awaiter(this, void 0, void 0, function* () {
const skill = yield this.seedAndInstallDummySkill(client, orgId);
const skillClient = yield this.connectToApi();
yield skillClient.authenticate({
skillId: skill.id,
apiKey: skill.apiKey,
});
return { skill, client: skillClient };
});
}
static seedAndInstallDummySkill(client, orgId) {
return __awaiter(this, void 0, void 0, function* () {
const skill = yield this.seedDemoSkill(client);
const installResults = yield client.emit('install-skill::v2020_12_25', {
target: {
organizationId: orgId,
},
payload: { skillId: skill.id },
});
assert.isEqual(installResults.totalErrors, 0);
return skill;
});
}
static seedDemoSkill(client) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const skill1Results = yield client.emit('register-skill::v2020_12_25', {
payload: {
name: `${this.skillName} ${++this.dummySkillCount} ${new Date().getTime() * Math.random()}`,
},
});
const skill = (_a = skill1Results.responses[0].payload) === null || _a === void 0 ? void 0 : _a.skill;
assert.isTruthy(skill);
return skill;
});
}
static registerEvent(namespace, client) {
return __awaiter(this, void 0, void 0, function* () {
const sig = this.generateWillSendVipEventSignature(namespace);
MutableContractClient.mixinContract(sig);
const registerResults = yield client.emit('register-events::v2020_12_25', {
payload: {
contract: this.generateWillSendVipEventSignature(),
},
});
eventResponseUtil.getFirstResponseOrThrow(registerResults);
return sig;
});
}
static generateWillSendVipEventSignature(slug) {
const contract = {
eventSignatures: {
[`${slug ? `${slug}.` : ''}will-send-vip::v1`]: {
emitPayloadSchema: {
id: 'willSendVipTargetAndPayload',
fields: {
target: {
type: 'schema',
isRequired: true,
options: {
schema: {
id: 'willSendVipTarget',
fields: {
organizationId: {
type: 'text',
},
},
},
},
},
},
},
responsePayloadSchema: {
id: 'testEventResponsePayload',
fields: {
messages: {
type: 'text',
isArray: true,
isRequired: true,
},
},
},
},
},
};
return contract;
}
}
AbstractClientTest.dummySkillCount = 0;
AbstractClientTest.clients = [];
AbstractClientTest.skillName = 'Dummy skill';
export default AbstractClientTest;