@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
210 lines (208 loc) • 10.7 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 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 AbstractEventTest_1 = __importDefault(require("../../../tests/AbstractEventTest"));
const constants_1 = require("../../../tests/constants");
const test_utility_1 = __importDefault(require("../../../tests/utilities/test.utility"));
const uiAssert_utility_1 = __importDefault(require("../../../tests/utilities/uiAssert.utility"));
const action_utility_1 = __importDefault(require("../../../utilities/action.utility"));
const EVENT_NAME_READABLE = 'my fantastically amazing event';
const EVENT_NAME = 'my-fantastically-amazing-event';
const EVENT_CAMEL = 'myFantasticallyAmazingEvent';
class CreatingAnEventTest extends AbstractEventTest_1.default {
static expectedVersion = spruce_skill_utils_1.versionUtil.generateVersion().constValue;
static async hasCreateAction() {
test_utils_1.assert.isFunction(this.Action('event', 'create').execute);
}
static async cantCreateEventWithoutBeingRegistered() {
await this.FeatureFixture().installCachedFeatures('events');
const results = await this.Action('event', 'create').execute({
nameReadable: EVENT_NAME_READABLE,
nameKebab: EVENT_NAME,
nameCamel: EVENT_CAMEL,
version: this.expectedVersion,
});
test_utils_1.assert.isArray(results.errors);
test_utils_2.errorAssert.assertError(results.errors[0], 'SKILL_NOT_REGISTERED');
}
static async createsPayloadBuildersAndSchemas() {
const { results, skill } = await this.createEvent();
const filesThatShouldExist = [
new RegExp(`${spruce_skill_utils_1.namesUtil.toCamel(skill.slug)}.*?${this.expectedVersion}.*?myFantasticallyAmazingEventResponsePayload`, 'gis'),
new RegExp(`${spruce_skill_utils_1.namesUtil.toCamel(skill.slug)}.*?${this.expectedVersion}.*?myFantasticallyAmazingEventEmitTargetAndPayload`, 'gis'),
new RegExp(`${spruce_skill_utils_1.namesUtil.toCamel(skill.slug)}.*?${this.expectedVersion}.*?myFantasticallyAmazingEventEmitPayload`, 'gis'),
'emitPayload.builder.ts',
'emitTarget.builder.ts',
];
const checker = this.Service('typeChecker');
for (const name of filesThatShouldExist) {
const match = test_utility_1.default.assertFileByPathInGeneratedFiles(name, results.files);
await checker.check(match);
}
}
static async createsEventWithPayloadsPermissionsAndOptions() {
const { results, cli, skill } = await this.createEvent();
test_utils_1.assert.isFalsy(results.errors);
await this.copyEventBuildersAndPermissions(EVENT_NAME);
const syncResults = await this.Action('event', 'sync').execute({});
test_utils_1.assert.isFalsy(syncResults.errors);
const mixedResults = action_utility_1.default.mergeActionResults(results, syncResults);
await this.assertExpectedTargetAndPayload(mixedResults, skill);
await this.assertExpectedPayloadSchemas(mixedResults);
await this.assertReturnsEventFromHealthCheck(cli, skill);
await this.assertCreatesOptionsFile(mixedResults);
}
static async createdEventsAreTypedCorrectly() {
const { results } = await this.createEvent();
test_utils_1.assert.isFalsy(results.errors);
const { fqen } = results.meta ?? {};
const testFileContents = spruce_skill_utils_1.diskUtil
.readFile(this.resolveTestPath('client-test.ts.hbs'))
.replace('{{fqen}}', fqen);
const testFile = this.resolvePath('src', 'test-client.ts');
spruce_skill_utils_1.diskUtil.writeFile(testFile, testFileContents);
await this.Service('typeChecker').check(testFile);
}
static async canReferenceSchemaFromOtherModule() {
const { results } = await this.createEvent();
await this.Service('pkg').install('@sprucelabs/heartwood-view-controllers');
const emitPayloadFile = test_utility_1.default.assertFileByNameInGeneratedFiles('emitPayload.builder.ts', results.files);
const newContents = `import { formBuilderImportExportObjectSchema } from '@sprucelabs/heartwood-view-controllers'
import { buildSchema } from '@sprucelabs/schema'
const createFormEmitPayloadBuilder = buildSchema({
id: 'myFantasticallyAmazingEventEmitPayload',
fields: {
...formBuilderImportExportObjectSchema.fields,
},
})
export default createFormEmitPayloadBuilder
`;
spruce_skill_utils_1.diskUtil.writeFile(emitPayloadFile, newContents);
const syncResults = await this.Action('event', 'sync').execute({});
test_utils_1.assert.isFalsy(syncResults.errors);
}
static async asksForVersionIfPreviousVersionExistsOnDifferentDay() {
await this.createEvent({
version: spruce_skill_utils_1.versionUtil.generateVersion('2020_01_10').constValue,
});
void this.Action('event', 'create').execute({
nameReadable: EVENT_NAME_READABLE,
nameKebab: EVENT_NAME,
nameCamel: EVENT_CAMEL,
});
await uiAssert_utility_1.default.assertRendersSelect(this.ui);
this.ui.reset();
}
static async createEvent(options) {
const cli = await this.FeatureFixture().installCachedFeatures('events');
const skill = await this.registerCurrentSkill();
const results = await this.Action('event', 'create').execute({
nameReadable: EVENT_NAME_READABLE,
nameKebab: EVENT_NAME,
nameCamel: EVENT_CAMEL,
version: this.expectedVersion,
...options,
});
return { results, cli, skill };
}
static async registerCurrentSkill() {
return await this.getSkillFixture().registerCurrentSkill({
name: 'my new skill',
}, {
phone: constants_1.DEMO_NUMBER_CREATING_AN_EVENT,
});
}
static async assertCreatesOptionsFile(results) {
const optionsFile = test_utility_1.default.assertFileByNameInGeneratedFiles('event.options.ts', results.files);
const imported = await this.Service('import').importDefault(optionsFile);
test_utils_1.assert.isEqualDeep(imported, { isGlobal: false });
}
static async assertExpectedTargetAndPayload(results, skill) {
const match = test_utility_1.default.assertFileByPathInGeneratedFiles(new RegExp(`${spruce_skill_utils_1.namesUtil.toCamel(skill.slug)}.*?${this.expectedVersion}.*?myFantasticallyAmazingEventEmitTargetAndPayload`, 'gis'), results.files);
const schema = await this.Service('schema').importSchema(match);
test_utils_1.assert.isEqual(schema.id, 'myFantasticallyAmazingEventEmitTargetAndPayload');
test_utils_1.assert.isTruthy(schema.fields?.payload);
test_utils_1.assert.isTruthy(schema.fields?.target);
}
static async assertReturnsEventFromHealthCheck(cli, skill) {
const health = await cli.checkHealth();
test_utils_1.assert.isTruthy(health.event);
test_utils_1.assert.isLength(health.event.events, 1);
const eventName = EVENT_NAME;
const eventNamespace = spruce_skill_utils_1.namesUtil.toKebab(skill.slug);
test_utils_1.assert.doesInclude(health.event.events, {
eventName,
eventNamespace,
version: this.expectedVersion,
});
test_utils_1.assert.doesInclude(health.event.contracts, {
fullyQualifiedEventName: spruce_event_utils_1.eventNameUtil.join({
eventName,
eventNamespace,
version: this.expectedVersion,
}),
});
}
static async assertExpectedPayloadSchemas(results) {
const payloadSchemas = [
{
fileName: 'emitPayload.builder.ts',
expectedId: 'myFantasticallyAmazingEventEmitPayload',
},
{
fileName: 'emitTarget.builder.ts',
expectedId: 'myFantasticallyAmazingEventEmitTarget',
},
{
fileName: 'responsePayload.builder.ts',
expectedId: 'myFantasticallyAmazingEventResponsePayload',
},
];
const schemas = this.Service('schema');
for (const payload of payloadSchemas) {
const match = test_utility_1.default.assertFileByNameInGeneratedFiles(payload.fileName, results.files);
test_utils_1.assert.isEqual(match, spruce_event_utils_1.eventDiskUtil.resolveEventPath(this.cwd + '/src/events', {
fileName: payload.fileName,
eventName: EVENT_NAME,
version: this.expectedVersion,
}));
const imported = await schemas.importSchema(match);
test_utils_1.assert.isEqual(imported.id, payload.expectedId);
}
}
}
exports.default = CreatingAnEventTest;
__decorate([
(0, test_utils_1.test)()
], CreatingAnEventTest, "hasCreateAction", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnEventTest, "cantCreateEventWithoutBeingRegistered", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnEventTest, "createsPayloadBuildersAndSchemas", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnEventTest, "createsEventWithPayloadsPermissionsAndOptions", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnEventTest, "createdEventsAreTypedCorrectly", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnEventTest, "canReferenceSchemaFromOtherModule", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnEventTest, "asksForVersionIfPreviousVersionExistsOnDifferentDay", null);
//# sourceMappingURL=CreatingAnEvent.test.js.map