@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
211 lines • 9.55 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const spruce_event_utils_1 = require("@sprucelabs/spruce-event-utils");
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const test_utils_1 = require("@sprucelabs/test-utils");
const test_utils_2 = require("@sprucelabs/test-utils");
const AbstractCliTest_1 = __importDefault(require("../../tests/AbstractCliTest"));
const test_utility_1 = __importDefault(require("../../tests/utilities/test.utility"));
class DeployingToSandboxTest extends AbstractCliTest_1.default {
static sandboxDemoNumber = process.env.SANDBOX_DEMO_NUMBER;
static async beforeAll() {
await super.beforeAll();
if (!this.sandboxDemoNumber) {
test_utils_1.assert.fail('You gotta have a SANDBOX_DEMO_NUMBER set in your ENV for the Deploying to Sandbox test pass.');
}
}
static async beforeEach() {
await super.beforeEach();
const personFixture = this.people;
await personFixture.loginAsDemoPerson(this.sandboxDemoNumber);
}
static async afterEach() {
await this.getSkillFixture().clearAllSkills();
await super.afterEach();
}
static async hasSetupSandboxAction() {
test_utils_1.assert.isFunction(this.Action('sandbox', 'setup').execute);
}
static async writesWillBootListener() {
await this.FeatureFixture().installCachedFeatures('sandbox');
const results = await this.Action('sandbox', 'setup').execute({});
test_utils_1.assert.isFalsy(results.errors);
const version = spruce_skill_utils_1.versionUtil.generateVersion().dirValue;
test_utility_1.default.assertFileByNameInGeneratedFiles(`will-boot.${version}.listener.ts`, results.files);
}
static async throwsHelpfulErrorWhenMissingParams() {
await this.installAndSetupForSandbox();
const skill = await this.getSkillFixture().registerCurrentSkill({
name: 'My new skill',
});
await this.unregisterCurrentSkill();
const env = this.Service('env');
env.set('SKILL_ID', skill.id);
env.unset('SKILL_NAME');
env.unset('SKILL_SLUG');
const results = await this.boot();
test_utils_1.assert.isTruthy(results.errors);
test_utils_2.errorAssert.assertError(results.errors[0], 'MISSING_PARAMETERS', {
parameters: ['env.SKILL_NAME'],
});
}
static async doesNotTryToRegisterIfNeverRegisteredBefore() {
const { client } = await this.installAndSetupForSandbox();
const expected = await this.getTotalSkills(client);
await test_utils_1.assert.doesThrowAsync(() => this.bootAndKill(), "don't have access");
const actual = await this.getTotalSkills(client);
test_utils_1.assert.isEqual(expected, actual);
}
static async skipsAlreadyRegisteredSkill() {
const { client } = await this.installAndSetupForSandbox();
const registered = await this.getSkillFixture().registerCurrentSkill({
name: 'My new skill',
});
await this.bootAndKill();
const skills = await this.fetchSkills(client);
test_utils_1.assert.isLength(skills, 1);
test_utils_1.assert.isTruthy(skills[0]);
test_utils_1.assert.isEqual(skills[0].id, registered.id);
}
static async registersSkillAgain() {
const { client } = await this.installAndSetupForSandbox();
const skill = await this.registerCurrentSkill('My new skill');
await this.unregisterCurrentSkill();
this.updateEnv(skill);
await this.bootAndKill();
const skills = await this.fetchSkills(client);
test_utils_1.assert.isLength(skills, 1);
test_utils_1.assert.isTruthy(skills[0]);
test_utils_1.assert.isEqual(skills[0].slug, skill.slug);
test_utils_1.assert.isNotEqual(skills[0].id, skill.id);
}
static async registersSkillAndCanBootAgain() {
await this.installAndSetupForSandbox();
const skill = await this.registerCurrentSkill('My new skill');
await this.unregisterCurrentSkill();
this.updateEnv(skill);
await this.bootAndKill();
await this.bootAndKill();
}
static async canReRegisterAndThenRegisterConversationsWithoutCrash() {
await this.installAndSetupForSandbox('conversation-with-sandbox');
const skill = await this.registerCurrentSkill('Conversation test');
await this.unregisterCurrentSkill();
this.updateEnv(skill);
await this.Action('conversation', 'create').execute({
nameReadable: 'book an appointment',
nameCamel: 'bookAnAppointment',
});
await this.bootAndKill();
}
static async logsInSkillIfAlreadyRegisteredButMissingEnv() {
await this.installAndSetupForSandbox();
await this.registerCurrentSkill('Login if already registered');
const env = this.Service('env');
const originalSkillId = env.get('SKILL_ID');
const orginalSkillApiKey = env.get('SKILL_API_KEY');
env.set('SKILL_ID', 'this is garbage');
await this.bootAndKill();
delete process.env.SKILL_ID;
delete process.env.SKILL_API_KEY;
const skillId = env.get('SKILL_ID');
const apiKey = env.get('SKILL_API_KEY');
test_utils_1.assert.isEqual(skillId, originalSkillId, 'It logged in as the wrong skill!');
test_utils_1.assert.isEqual(apiKey, orginalSkillApiKey, 'It logged in with the wrong api key!');
}
static updateEnv(skill) {
const env = this.Service('env');
env.set('SKILL_ID', skill.id);
env.set('SKILL_NAME', skill.name);
}
static async bootAndKill() {
const boot = await this.boot();
test_utils_1.assert.isFalsy(boot.errors);
boot.meta?.kill();
}
static async boot() {
return await this.Action('skill', 'boot').execute({ local: true });
}
static async installAndSetupForSandbox(cacheKey = 'sandbox') {
const client = await this.getMercuryFixture().connectToApi();
const cli = await this.FeatureFixture().installCachedFeatures(cacheKey);
await this.Action('sandbox', 'setup').execute({});
const env = this.Service('env');
env.set('SANDBOX_DEMO_NUMBER', this.sandboxDemoNumber);
return { cli, client };
}
static async fetchSkills(client) {
const results = await client.emit(`list-skills::v2020_12_25`, {
payload: {
shouldOnlyShowMine: true,
},
});
const { skills } = spruce_event_utils_1.eventResponseUtil.getFirstResponseOrThrow(results);
return skills;
}
static async registerCurrentSkill(name) {
return await this.getSkillFixture().registerCurrentSkill({
name,
});
}
static async unregisterCurrentSkill() {
const isInstalled = this.Service('settings').isMarkedAsInstalled('skill');
if (!isInstalled) {
return;
}
const skills = this.Store('skill');
const isRegistered = await skills.isCurrentSkillRegistered();
if (isRegistered) {
const skill = await skills.loadCurrentSkill();
if (skill.id) {
await skills.unregisterSkill(skill.id);
}
}
}
static async getTotalSkills(client) {
const results2 = await client.emit(`list-skills::v2020_12_25`, {
payload: { shouldOnlyShowMine: true },
});
const { skills: skills2 } = spruce_event_utils_1.eventResponseUtil.getFirstResponseOrThrow(results2);
const total = skills2.length;
return total;
}
}
exports.default = DeployingToSandboxTest;
__decorate([
(0, test_utils_1.test)()
], DeployingToSandboxTest, "hasSetupSandboxAction", null);
__decorate([
(0, test_utils_1.test)()
], DeployingToSandboxTest, "writesWillBootListener", null);
__decorate([
(0, test_utils_1.test)()
], DeployingToSandboxTest, "throwsHelpfulErrorWhenMissingParams", null);
__decorate([
(0, test_utils_1.test)()
], DeployingToSandboxTest, "doesNotTryToRegisterIfNeverRegisteredBefore", null);
__decorate([
(0, test_utils_1.test)()
], DeployingToSandboxTest, "skipsAlreadyRegisteredSkill", null);
__decorate([
(0, test_utils_1.test)()
], DeployingToSandboxTest, "registersSkillAgain", null);
__decorate([
(0, test_utils_1.test)()
], DeployingToSandboxTest, "registersSkillAndCanBootAgain", null);
__decorate([
(0, test_utils_1.test)()
], DeployingToSandboxTest, "canReRegisterAndThenRegisterConversationsWithoutCrash", null);
__decorate([
(0, test_utils_1.test)()
], DeployingToSandboxTest, "logsInSkillIfAlreadyRegisteredButMissingEnv", null);
//# sourceMappingURL=DeployingToSandbox.test.js.map