@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
267 lines • 11.8 kB
JavaScript
;
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 ActionFactory_1 = __importDefault(require("../../../features/ActionFactory"));
const SyncAction_1 = __importDefault(require("../../../features/permission/actions/SyncAction"));
const test_utility_1 = __importDefault(require("../../../tests/utilities/test.utility"));
const AbstractPermissionsTest_1 = __importDefault(require("./support/AbstractPermissionsTest"));
const generateShortAlphaId_1 = __importDefault(require("./support/generateShortAlphaId"));
class SyncingPermissionsTest extends AbstractPermissionsTest_1.default {
static syncAction;
static contractId1;
static contractId2;
static heartwoodSkill = {
id: (0, test_utils_1.generateId)(),
slug: 'heartwood',
dateCreated: 0,
name: 'Heartwood',
};
static async beforeAll() {
await super.beforeAll();
this.contractId1 = 'b-should-be-second' + (0, generateShortAlphaId_1.default)();
this.contractId2 = 'a-should-be-first' + (0, generateShortAlphaId_1.default)();
}
static async beforeEach() {
await super.beforeEach();
this.syncAction = this.Action('permission', 'sync');
mercury_client_1.MercuryClientFactory.setIsTestMode(true);
ExecuteTrackingAction.wasExecuteInvoked = false;
await this.eventFaker.fakeListPermissionContracts();
await this.eventFaker.fakeListSkills(() => {
return [this.heartwoodSkill];
});
}
static async generatesExpectedTypesFile() {
const results = await this.sync();
const expected = this.getTypesPath();
test_utility_1.default.assertFileByPathInGeneratedFiles(expected, results.files);
}
static async syncsNewPermissionsWhenMade() {
await this.createPermissionContract(this.contractId1);
await this.writeTestFileAndAssertValid(`testing-permissions.${this.contractId1}`);
}
static async upgradingSyncsPermissions() {
this.beginTrackingExecute();
await this.emitDidExecuteUpgrade();
test_utils_1.assert.isTrue(ExecuteTrackingAction.wasExecuteInvoked);
}
static async doesNotSyncIfNotInstalled() {
this.beginTrackingExecute();
this.featureInstaller.isInstalled = async (code) => code === 'node';
await this.emitDidExecuteUpgrade();
test_utils_1.assert.isFalse(ExecuteTrackingAction.wasExecuteInvoked);
}
static async generatesCombinedFile() {
test_utils_1.assert.isTrue(spruce_skill_utils_1.diskUtil.doesFileExist(this.getCombinedPath()));
}
static async combinedFileImportsAllPermissions() {
await this.createPermissionContract(this.contractId2);
const imported = await this.import();
test_utils_1.assert.isEqualDeep(imported, [
{
id: this.contractId2,
name: this.contractId2,
description: '',
requireAllPermissions: false,
permissions: [
{
id: 'can-high-five',
name: 'Can give high five',
description: 'Will this person be allowed to high five?',
defaults: { skill: false },
requireAllStatuses: false,
},
],
},
{
id: this.contractId1,
name: this.contractId1,
description: '',
requireAllPermissions: false,
permissions: [
{
id: 'can-high-five',
name: 'Can give high five',
description: 'Will this person be allowed to high five?',
defaults: { skill: false },
requireAllStatuses: false,
},
],
},
]);
}
static async canSyncCorePermissions() {
let wasHit = false;
let passedTarget;
await this.eventFaker.fakeListPermissionContracts((targetAndPayload) => {
passedTarget = targetAndPayload.target;
wasHit = true;
});
await this.sync({ shouldSyncCorePermissions: true });
test_utils_1.assert.isTrue(wasHit, 'Did not emit list-permission-contracts event');
test_utils_1.assert.isUndefined(passedTarget, 'Should not have passed a target to list-permission-contracts');
}
static async syncingEventsSyncsPermissions() {
this.beginTrackingExecute();
await this.emitter.emitAndFlattenResponses('feature.did-execute', {
actionCode: 'sync',
featureCode: 'event',
results: {},
});
test_utils_1.assert.isTrue(ExecuteTrackingAction.wasExecuteInvoked);
}
static async permsSentInAlphabeticalOrder() {
await this.addHeartwoodAsDependency();
await this.fakeHeartwoodPermContracts();
await this.sync();
const contents = this.readTypesFile();
const firstId = contents.indexOf('perk-a-should-be-first');
const secondId = contents.indexOf('perk-should-be-second');
const thirdId = contents.indexOf('perl-a-should-be-first');
const fourthId = contents.indexOf('perl-should-be-second');
const fifthId = contents.indexOf('perm-a-should-be-first');
const sixthId = contents.indexOf('perm-should-be-second');
test_utils_1.assert.isTrue(firstId < secondId, 'Permissions are not in alphabetical order');
test_utils_1.assert.isTrue(secondId < thirdId, 'Permissions are not in alphabetical order');
test_utils_1.assert.isTrue(thirdId < fourthId, 'Permissions are not in alphabetical order');
test_utils_1.assert.isTrue(fourthId < fifthId, 'Permissions are not in alphabetical order');
test_utils_1.assert.isTrue(fifthId < sixthId, 'Permissions are not in alphabetical order');
}
static async fakeHeartwoodPermContracts() {
await this.eventFaker.fakeListPermissionContracts(() => {
return [
{
id: 'ab-' + (0, generateShortAlphaId_1.default)(),
contract: {
id: 'ab-' + (0, generateShortAlphaId_1.default)(),
name: (0, test_utils_1.generateId)(),
permissions: [
{
id: 'perl-should-be-second',
name: 'Should be second',
},
{
id: 'perl-a-should-be-first',
name: 'Should be first',
},
],
},
},
{
id: 'ba-' + (0, generateShortAlphaId_1.default)(),
contract: {
id: 'ba-' + (0, generateShortAlphaId_1.default)(),
name: (0, test_utils_1.generateId)(),
permissions: [
{
id: 'perm-should-be-second',
name: 'Should be second',
},
{
id: 'perm-a-should-be-first',
name: 'Should be first',
},
],
},
},
{
id: 'aa-' + (0, generateShortAlphaId_1.default)(),
contract: {
id: 'aa-' + (0, generateShortAlphaId_1.default)(),
name: (0, test_utils_1.generateId)(),
permissions: [
{
id: 'perk-should-be-second',
name: 'Should be second',
},
{
id: 'perk-a-should-be-first',
name: 'Should be first',
},
],
},
},
];
});
}
static async import() {
return await this.Service('import').importDefault(this.getCombinedPath());
}
static async addHeartwoodAsDependency() {
const results = await this.Action('dependency', 'add').execute({
namespace: 'heartwood',
});
test_utils_1.assert.isFalsy(results.errors, 'Should not have errored');
}
static readTypesFile() {
return spruce_skill_utils_1.diskUtil.readFile(this.getTypesPath());
}
static getTypesPath() {
return this.resolveHashSprucePath(`permissions/permissions.types.ts`);
}
static getCombinedPath() {
return this.resolveHashSprucePath('permissions', 'permissions.ts');
}
static beginTrackingExecute() {
ActionFactory_1.default.setActionClass('permission', 'sync', ExecuteTrackingAction);
}
static async sync(options) {
const results = await this.syncAction.execute(options);
test_utils_1.assert.isFalsy(results.errors, 'Should not have errored when syncing permissions');
return results;
}
static async emitDidExecuteUpgrade() {
await this.emitter.emitAndFlattenResponses('feature.did-execute', {
actionCode: 'upgrade',
featureCode: 'node',
results: {},
});
}
}
exports.default = SyncingPermissionsTest;
__decorate([
(0, test_utils_1.test)()
], SyncingPermissionsTest, "generatesExpectedTypesFile", null);
__decorate([
(0, test_utils_1.test)()
], SyncingPermissionsTest, "syncsNewPermissionsWhenMade", null);
__decorate([
(0, test_utils_1.test)()
], SyncingPermissionsTest, "upgradingSyncsPermissions", null);
__decorate([
(0, test_utils_1.test)()
], SyncingPermissionsTest, "doesNotSyncIfNotInstalled", null);
__decorate([
(0, test_utils_1.test)()
], SyncingPermissionsTest, "generatesCombinedFile", null);
__decorate([
(0, test_utils_1.test)()
], SyncingPermissionsTest, "combinedFileImportsAllPermissions", null);
__decorate([
(0, test_utils_1.test)()
], SyncingPermissionsTest, "canSyncCorePermissions", null);
__decorate([
(0, test_utils_1.test)()
], SyncingPermissionsTest, "syncingEventsSyncsPermissions", null);
__decorate([
(0, test_utils_1.test)()
], SyncingPermissionsTest, "permsSentInAlphabeticalOrder", null);
class ExecuteTrackingAction extends SyncAction_1.default {
static wasExecuteInvoked = false;
async execute() {
ExecuteTrackingAction.wasExecuteInvoked = true;
return {};
}
}
//# sourceMappingURL=SyncingPermissions.test.js.map