@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
195 lines • 9.5 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_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const spruce_skill_utils_2 = require("@sprucelabs/spruce-skill-utils");
const test_utils_1 = require("@sprucelabs/test-utils");
const test_utils_2 = require("@sprucelabs/test-utils");
const AbstractSchemaTest_1 = __importDefault(require("../../../tests/AbstractSchemaTest"));
const test_utility_1 = __importDefault(require("../../../tests/utilities/test.utility"));
class CreatingANewSchemaBuilderTest extends AbstractSchemaTest_1.default {
static async failsWhenASkillIsNotInstalled() {
await this.Cli();
const err = await test_utils_1.assert.doesThrowAsync(() => this.Action('schema', 'create').execute({
nameReadable: 'Test schema!',
namePascal: 'AnotherTest',
nameCamel: 'anotherTest',
description: 'this is so great!',
}));
test_utils_2.errorAssert.assertError(err, 'FEATURE_NOT_INSTALLED');
}
static async canBuildFileWithoutCrashing() {
const createSchema = await this.syncSchemasAndGetCreateAction('schemas');
const response = await createSchema.execute({
nameReadable: 'Test schema!',
namePascal: 'Test',
nameCamel: 'test',
description: 'this is so great!',
});
test_utils_1.assert.isFalsy(response.errors);
const expectedDestination = spruce_skill_utils_1.versionUtil.resolveNewLatestPath(this.cwd, 'src', 'schemas', '{{@latest}}', 'test.builder.ts');
test_utils_1.assert.isEqual(response.files?.[0].path, expectedDestination);
}
static async syncSchemasAndGetCreateAction(cacheKey) {
await this.syncSchemas(cacheKey);
const createSchema = this.Action('schema', 'create');
return createSchema;
}
static async builderAndSchemaFilesValidate() {
const response = await this.buildTestSchema();
const checker = this.Service('typeChecker');
await checker.check(response.files?.[0].path ?? '');
await checker.check(this.schemaTypesFile);
const builderMatch = test_utility_1.default.assertFileByNameInGeneratedFiles('anotherTest.builder.ts', response.files);
await checker.check(builderMatch);
const schemaMatch = test_utility_1.default.assertFileByNameInGeneratedFiles('anotherTest.schema.ts', response.files);
await checker.check(schemaMatch);
const schemaContents = spruce_skill_utils_2.diskUtil.readFile(schemaMatch);
test_utils_1.assert.doesInclude(schemaContents, `namespace: 'TestingSchemas'`);
}
static async errorsWithBadVersion() {
const action = await this.syncSchemasAndGetCreateAction('schemas');
const results = await action.execute({
nameReadable: 'Bad schema version!',
namePascal: 'BadSchemaVersion',
nameCamel: 'badSchemaVersion',
version: 'v1',
});
test_utils_1.assert.isTruthy(results.errors);
test_utils_1.assert.doesInclude(results.errors[0].message, /must be in the form/i);
}
static async canBuild2SpecificVersions() {
const action = await this.syncSchemasAndGetCreateAction('schemas');
const firstResponse = await action.execute({
nameReadable: 'First schema!',
namePascal: 'FirstSchema',
nameCamel: 'firstSchema',
version: 'v2020_01_10',
});
this.validateSchemaFiles(firstResponse, 'firstSchema.schema.ts', 'v2020_01_10', 'FirstSchema');
const secondResponse = await action.execute({
nameReadable: 'Second schema!',
namePascal: 'secondSchema',
nameCamel: 'secondSchema',
version: 'v2020_01_11',
});
this.validateSchemaFiles(secondResponse, 'secondSchema.schema.ts', 'v2020_01_11', 'SecondSchema');
this.validateSchemaFiles(secondResponse, 'firstSchema.schema.ts', 'v2020_01_10', 'FirstSchema');
}
static async asksForVersionOnSecondSchema() {
const action = await this.syncSchemasAndGetCreateAction('schemas');
const newVersion = spruce_skill_utils_1.versionUtil.generateVersion();
// create old version first
await action.execute({
nameReadable: 'First schema!',
namePascal: 'FirstSchema',
nameCamel: 'firstSchema',
version: 'v2020_01_01',
});
await this.assertAnswersFirstTime(action, newVersion);
await this.assertAnswersSecondTime(action, newVersion);
}
// should ask if we want to use the old version we created above or a New Version
static async assertAnswersFirstTime(action, newVersion) {
const createPromise = action.execute({
nameReadable: 'Second schema!',
namePascal: 'SecondSchema',
nameCamel: 'secondSchema',
});
await this.wait(1000);
const last = this.ui.invocations[this.ui.invocations.length - 1];
test_utils_1.assert.isEqual(last?.command, 'prompt');
test_utils_1.assert.isLength(last?.options?.options?.choices, 2);
test_utils_1.assert.isEqualDeep(last?.options?.options?.choices, [
{
value: newVersion.dirValue,
label: 'New Version',
},
{
value: 'v2020_01_01',
label: 'v2020_01_01',
},
]);
await this.ui.sendInput(newVersion.dirValue);
await createPromise;
}
// should only have 2 options, none of which are "new version" since that was created in secondAnswers
static async assertAnswersSecondTime(action, newVersion) {
const createPromise = action.execute({
nameReadable: 'Third schema',
namePascal: 'ThirdSchema',
nameCamel: 'thirdSchema',
});
await this.wait(1000);
const term = this.ui;
const last = term.invocations[term.invocations.length - 1];
test_utils_1.assert.isEqualDeep(last?.options?.options?.choices, [
{
value: newVersion.dirValue,
label: newVersion.dirValue,
},
{
value: 'v2020_01_01',
label: 'v2020_01_01',
},
]);
void this.ui.sendInput(newVersion.dirValue);
const createResults = await createPromise;
const schemaMatch = test_utility_1.default.assertFileByNameInGeneratedFiles('secondSchema.schema.ts', createResults.files);
test_utils_1.assert.doesInclude(schemaMatch, newVersion.dirValue);
}
static validateSchemaFiles(response, expectedFileName, expectedVersion, expectedSchemaInterfaceName) {
test_utils_1.assert.isUndefined(response.errors);
let schemaFile = test_utility_1.default.assertFileByNameInGeneratedFiles(expectedFileName, response.files);
const errors = response.errors ?? [];
if (errors.length > 0) {
test_utils_1.assert.fail(errors[0].friendlyMessage());
}
test_utils_1.assert.doesInclude(schemaFile, expectedVersion);
const schemaContents = spruce_skill_utils_2.diskUtil.readFile(schemaFile);
test_utils_1.assert.doesInclude(schemaContents, `version: '${expectedVersion}'`);
test_utils_1.assert.doesInclude(schemaContents, new RegExp('SpruceSchemas.TestingSchemas.' +
expectedVersion +
'.*?' +
expectedSchemaInterfaceName, 'gis'));
}
static async buildTestSchema() {
const action = await this.syncSchemasAndGetCreateAction('schemas');
const response = await action.execute({
nameReadable: 'Test schema!',
namePascal: 'AnotherTest',
nameCamel: 'anotherTest',
description: 'this is so great!',
});
test_utils_1.assert.isUndefined(response.errors);
return response;
}
}
exports.default = CreatingANewSchemaBuilderTest;
__decorate([
(0, test_utils_1.test)()
], CreatingANewSchemaBuilderTest, "failsWhenASkillIsNotInstalled", null);
__decorate([
(0, test_utils_1.test)()
], CreatingANewSchemaBuilderTest, "canBuildFileWithoutCrashing", null);
__decorate([
(0, test_utils_1.test)()
], CreatingANewSchemaBuilderTest, "builderAndSchemaFilesValidate", null);
__decorate([
(0, test_utils_1.test)()
], CreatingANewSchemaBuilderTest, "errorsWithBadVersion", null);
__decorate([
(0, test_utils_1.test)()
], CreatingANewSchemaBuilderTest, "canBuild2SpecificVersions", null);
__decorate([
(0, test_utils_1.test)()
], CreatingANewSchemaBuilderTest, "asksForVersionOnSecondSchema", null);
//# sourceMappingURL=CreatingANewSchemaBuilder.test.js.map