UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

151 lines • 7.16 kB
"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_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")); class SkillStoreTest extends AbstractCliTest_1.default { static store; static async beforeEach() { await super.beforeEach(); this.store = this.SkillStore(); } static async hasRegisterMethod() { test_utils_1.assert.isFunction(this.store.register); } static async cantRegisterIfNotInSkill() { const err = await test_utils_1.assert.doesThrowAsync(() => this.store.register({ name: 'awesome skill', slug: 'awesome-skill', })); test_utils_2.errorAssert.assertError(err, 'DIRECTORY_NOT_SKILL'); } static async cantLoadcurrentSkillIfNotInSkill() { const err = await test_utils_1.assert.doesThrowAsync(() => this.store.loadCurrentSkill()); test_utils_2.errorAssert.assertError(err, 'DIRECTORY_NOT_SKILL'); } static async cantCheckIfSkillIsRegisteredNotInSkill() { const err = await test_utils_1.assert.doesThrowAsync(() => this.store.isCurrentSkillRegistered()); test_utils_2.errorAssert.assertError(err, 'DIRECTORY_NOT_SKILL'); } static async canGetNamespace() { await this.FeatureFixture().installCachedFeatures('skills'); const namespace = await this.store.loadCurrentSkillsNamespace(); test_utils_1.assert.isEqual(namespace, 'TestSkill'); } static async canSetNamespace() { await this.FeatureFixture().installCachedFeatures('skills'); let namespace = await this.store.loadCurrentSkillsNamespace(); await this.store.setCurrentSkillsNamespace('new-namespace'); namespace = await this.store.loadCurrentSkillsNamespace(); test_utils_1.assert.isEqual(namespace, 'NewNamespace'); } static async canRegister() { await this.FeatureFixture().installCachedFeatures('skills'); const slug = `awesome-skill-${new Date().getTime()}`; await this.people.loginAsDemoPerson(); let isRegistered = await this.store.isCurrentSkillRegistered(); test_utils_1.assert.isFalse(isRegistered); const skill = await this.store.register({ name: 'awesome skill', slug, }); test_utils_1.assert.isTruthy(skill); test_utils_1.assert.isEqual(skill.name, 'awesome skill'); test_utils_1.assert.isEqual(skill.slug, slug); test_utils_1.assert.isString(skill.apiKey); test_utils_1.assert.isString(skill.id); const client = await this.connectToApi(); const results = await client.authenticate({ skillId: skill.id, apiKey: skill.apiKey, }); test_utils_1.assert.isEqual(results.skill?.id, skill.id); isRegistered = await this.store.isCurrentSkillRegistered(); test_utils_1.assert.isTrue(isRegistered); const currentSkill = await this.store.loadCurrentSkill(); test_utils_1.assert.isEqual(currentSkill.id, skill.id); test_utils_1.assert.isTrue(currentSkill.isRegistered); test_utils_1.assert.isEqual(currentSkill.name, 'awesome skill'); test_utils_1.assert.isEqual(currentSkill.slug, slug); test_utils_1.assert.isEqual(currentSkill.apiKey, skill.apiKey); const env = this.Service('env'); test_utils_1.assert.isEqual(env.get('SKILL_ID'), skill.id); test_utils_1.assert.isEqual(env.get('SKILL_API_KEY'), skill.apiKey); const err = await test_utils_1.assert.doesThrowAsync(() => this.store.setCurrentSkillsNamespace('test')); test_utils_2.errorAssert.assertError(err, 'GENERIC'); const namespace = this.Service('pkg').getSkillNamespace(); test_utils_1.assert.isEqual(namespace, slug); } static async returnsExpectedModuleInGoProject() { const name = await this.initRandomGoProject(); await this.assertCurrentNamespaceEquals(name); } static async canFindGoModuleInSubdirectory() { const name = await this.initRandomGoProject(); const newCwd = this.resolvePath(this.cwd, (0, test_utils_1.generateId)()); spruce_skill_utils_1.diskUtil.createDir(newCwd); this.setCwd(newCwd); this.store = this.SkillStore(); await this.assertCurrentNamespaceEquals(name); } static async throwsNotInGoModuleErrorIfNotInGoProject() { const err = await test_utils_1.assert.doesThrowAsync(() => this.store.getGoModuleName()); test_utils_2.errorAssert.assertError(err, 'DIRECTORY_NOT_GO_MODULE', { cwd: this.cwd, }); } static async assertCurrentNamespaceEquals(name) { const actual = await this.store.loadCurrentSkillsNamespace(); test_utils_1.assert.isEqual(actual, spruce_skill_utils_1.namesUtil.toPascal(name), 'Expected namespace to match go module name'); } static async initRandomGoProject() { const name = spruce_skill_utils_1.randomUtil.rand(['my-skill', 'superSkill', 'AwesomeSkill']); await this.go.initGoProject(name); return name; } static SkillStore() { return this.Store('skill', {}); } } exports.default = SkillStoreTest; __decorate([ (0, test_utils_1.test)() ], SkillStoreTest, "hasRegisterMethod", null); __decorate([ (0, test_utils_1.test)() ], SkillStoreTest, "cantRegisterIfNotInSkill", null); __decorate([ (0, test_utils_1.test)() ], SkillStoreTest, "cantLoadcurrentSkillIfNotInSkill", null); __decorate([ (0, test_utils_1.test)() ], SkillStoreTest, "cantCheckIfSkillIsRegisteredNotInSkill", null); __decorate([ (0, test_utils_1.test)() ], SkillStoreTest, "canGetNamespace", null); __decorate([ (0, test_utils_1.test)() ], SkillStoreTest, "canSetNamespace", null); __decorate([ (0, test_utils_1.test)() ], SkillStoreTest, "canRegister", null); __decorate([ (0, test_utils_1.test)() ], SkillStoreTest, "returnsExpectedModuleInGoProject", null); __decorate([ (0, test_utils_1.test)() ], SkillStoreTest, "canFindGoModuleInSubdirectory", null); __decorate([ (0, test_utils_1.test)() ], SkillStoreTest, "throwsNotInGoModuleErrorIfNotInGoProject", null); //# sourceMappingURL=SkillStore.test.js.map