UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

243 lines (241 loc) • 9.5 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 mercury_client_1 = require("@sprucelabs/mercury-client"); const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils"); const test_utils_1 = require("@sprucelabs/test-utils"); const AbstractPermissionsTest_1 = __importDefault(require("./support/AbstractPermissionsTest")); const generateShortAlphaId_1 = __importDefault(require("./support/generateShortAlphaId")); const sortPermissionContracts_1 = require("./support/sortPermissionContracts"); class PermissionStoreTest extends AbstractPermissionsTest_1.default { static permissions; static contractName1; static contractName2; static fqid1; static fqid2; static namespace; static async beforeAll() { await super.beforeAll(); const namespace = await this.Service('pkg').getSkillNamespace(); this.namespace = namespace; this.contractName1 = (0, generateShortAlphaId_1.default)(); this.fqid1 = `${namespace}.${this.contractName1}`; this.contractName2 = (0, generateShortAlphaId_1.default)(); const contractId = this.contractName2; this.fqid2 = buildPermissionContractId(contractId, namespace); } static async beforeEach() { await super.beforeEach(); mercury_client_1.MercuryClientFactory.setIsTestMode(true); this.permissions = this.Store('permission'); await this.eventFaker.fakeListPermissionContracts(() => { }); } static async loadsNoLocalByDefault() { const permissions = await this.loadLocalPermissions(); test_utils_1.assert.isEqualDeep(permissions, []); } static async loadsOneContract() { await this.createPermissionContract(this.contractName1); await this.assertLocalPermissionsEqual([ { id: this.fqid1, permissions: ['can-high-five'], path: this.resolvePath(`src/permissions/${this.contractName1}.permissions.ts`), }, ]); } static async loadsSecondContract() { await this.createPermissionContract(this.contractName2); await this.assertLocalPermissionsEqual([ { id: this.fqid2, permissions: ['can-high-five'], path: this.resolvePath(`src/permissions/${this.contractName2}.permissions.ts`), }, { id: this.fqid1, permissions: ['can-high-five'], path: this.resolvePath(`src/permissions/${this.contractName1}.permissions.ts`), }, ]); } static async mixesInAllPermissions() { const contractId = 'oeu-aoeuao'; const perm1Id = 'what-the'; const perm2Id = 'go-dogs'; this.updateFirstContractBuilder(contractId, perm1Id, perm2Id); await this.assertLocalPermissionsEqual([ { id: this.fqid2, permissions: ['can-high-five'], path: this.resolvePath(`src/permissions/${this.contractName2}.permissions.ts`), }, { id: buildPermissionContractId(contractId, this.namespace), permissions: [perm1Id, perm2Id], path: this.resolvePath(`src/permissions/${this.contractName1}.permissions.ts`), }, ]); } static async passesThroughDependentSkills() { const namespace = this.addRandomDependency(); let passedTarget; await this.eventFaker.fakeListPermissionContracts(({ target }) => { passedTarget = target; }); await this.fetchContracts(); test_utils_1.assert.isEqualDeep(passedTarget, { namespaces: [namespace], }); } static async returnsDependencyMapFromRemoteContracts() { const perm = this.generatePermValues(); const perm2 = this.generatePermValues(); const perm3 = this.generatePermValues(); const { contract, contractId } = this.generateContractRowValues([ perm, perm2, ]); const { contract: contract2, contractId: contractId2 } = this.generateContractRowValues([perm3]); await this.eventFaker.fakeListPermissionContracts(() => { return [contract, contract2]; }); const map = await this.fetchContracts(); //@ts-ignore test_utils_1.assert.isEqualDeep(map, { [contractId]: [perm.id, perm2.id], [contractId2]: [perm3.id], [this.fqid2]: ['can-high-five'], [buildPermissionContractId('oeu-aoeuao', this.namespace)]: [ 'what-the', 'go-dogs', ], }); } static async connectsAsSkill() { let passedOptions; //@ts-ignore const old = this.permissions.connectToApi.bind(this.permissions); //@ts-ignore this.permissions.connectToApi = (options) => { passedOptions = options; return old(passedOptions); }; await this.fetchContracts(); test_utils_1.assert.isEqualDeep(passedOptions, { shouldAuthAsCurrentSkill: true }); } static updateFirstContractBuilder(contractId, perm1Id, perm2Id) { const file = this.resolvePath('src', 'permissions', `${this.contractName1}.permissions.ts`); spruce_skill_utils_1.diskUtil.writeFile(file, generateContractBuilder(contractId, perm1Id, perm2Id)); } static generateContractRowValues(permissions) { const contractId = (0, test_utils_1.generateId)(); const contract = { id: (0, test_utils_1.generateId)(), contract: { id: contractId, name: (0, test_utils_1.generateId)(), permissions, }, }; return { contract, contractId }; } static generatePermValues() { const permissionId = (0, test_utils_1.generateId)(); const perm = { id: permissionId, name: (0, test_utils_1.generateId)(), defaults: {}, }; return perm; } static addRandomDependency() { const dep = this.Service('dependency'); const namespace = (0, test_utils_1.generateId)(); dep.add({ id: (0, test_utils_1.generateId)(), namespace, }); return namespace; } static async fetchContracts() { return this.permissions.fetchContracts(); } static async assertLocalPermissionsEqual(expected) { const perms = await this.loadLocalPermissions(); perms.sort(sortPermissionContracts_1.sortPermissionContracts); expected.sort(sortPermissionContracts_1.sortPermissionContracts); test_utils_1.assert.isEqualDeep(perms, expected); } static async loadLocalPermissions() { //@ts-ignore return await this.permissions.loadLocalPermissions(); } } exports.default = PermissionStoreTest; __decorate([ (0, test_utils_1.test)() ], PermissionStoreTest, "loadsNoLocalByDefault", null); __decorate([ (0, test_utils_1.test)() ], PermissionStoreTest, "loadsOneContract", null); __decorate([ (0, test_utils_1.test)() ], PermissionStoreTest, "loadsSecondContract", null); __decorate([ (0, test_utils_1.test)() ], PermissionStoreTest, "mixesInAllPermissions", null); __decorate([ (0, test_utils_1.test)() ], PermissionStoreTest, "passesThroughDependentSkills", null); __decorate([ (0, test_utils_1.test)() ], PermissionStoreTest, "returnsDependencyMapFromRemoteContracts", null); __decorate([ (0, test_utils_1.test)() ], PermissionStoreTest, "connectsAsSkill", null); function buildPermissionContractId(contractId, namespace) { return `${namespace}.${contractId}`; } function generateContractBuilder(contractId = 'oeu-aoeuao', perm1Id = 'what-the', perm2Id = 'go-dogs') { return `import { buildPermissionContract } from '@sprucelabs/mercury-types' const debeePermissions = buildPermissionContract({ id: '${contractId}', name: 'debee', description: '', requireAllPermissions: false, permissions: [ { id: '${perm1Id}', name: 'Can give high five', description: 'Will this person be allowed to high five?', defaults: { skill: false, }, requireAllStatuses: false, }, { id: '${perm2Id}', name: 'Can give high five', description: 'Will this person be allowed to high five?', defaults: { skill: false, }, requireAllStatuses: false, } ] }) export default debeePermissions `; } //# sourceMappingURL=PermissionStore.test.js.map