UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

160 lines (158 loc) • 9.03 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 ImportService_1 = __importDefault(require("../../../services/ImportService")); const AbstractErrorTest_1 = __importDefault(require("../../../tests/AbstractErrorTest")); const test_utility_1 = __importDefault(require("../../../tests/utilities/test.utility")); class KeepingErrorsInSyncTest extends AbstractErrorTest_1.default { static async hasSyncErrorAction() { await this.Cli(); test_utils_1.assert.isFunction(this.Action('error', 'sync').execute); } static async returnsErrorWhenSyncingSchemasFails() { await this.installErrorFeature('errors'); const createAction = this.Action('error', 'create'); await createAction.execute({ nameReadable: 'Test error', nameCamel: 'testError', }); this.commandFaker.fakeCommand(/yarn/, 1); this.commandFaker.fakeCommand(/node/, 1); ImportService_1.default.disableCache(); ImportService_1.default.clearCache(); const results = await this.Action('error', 'sync').execute({}); test_utils_1.assert.isTruthy(results.errors); test_utils_1.assert.isLength(results.errors, 1); } static async errorsStayInSyncWhenSchemasAreDeleted() { await this.installErrorFeature('errors'); const createAction = this.Action('error', 'create'); const results = await createAction.execute({ nameReadable: 'Test error', nameCamel: 'testError', }); const optionsMatch = test_utility_1.default.assertFileByNameInGeneratedFiles(/^options\.types/, results.files); await this.Service('typeChecker').check(optionsMatch); const typesMatch = test_utility_1.default.assertFileByNameInGeneratedFiles(/^errors\.types/, results.files); // should contain our test error let optionsContent = spruce_skill_utils_1.diskUtil.readFile(optionsMatch); test_utils_1.assert.doesInclude(optionsContent, /SpruceErrors\.TestingErrors\.TestError/); let typesContent = spruce_skill_utils_1.diskUtil.readFile(typesMatch); test_utils_1.assert.doesInclude(typesContent, /SpruceErrors\.TestingErrors.*?TestError/gis); // delete our testError const builderMatch = test_utility_1.default.assertFileByNameInGeneratedFiles(/testError\.builder/, results.files); spruce_skill_utils_1.diskUtil.deleteFile(builderMatch); // resync await this.Action('error', 'sync').execute({}); // #spruce/errors should not exist test_utils_1.assert.isFalse(spruce_skill_utils_1.diskUtil.doesFileExist(this.resolveHashSprucePath('errors'))); // build 2 errors const testError1 = await createAction.execute({ nameCamel: 'testError1', nameReadable: 'Test error 1', }); const testError1SchemaMatch = test_utility_1.default.assertFileByNameInGeneratedFiles('testError1.schema', testError1.files); const testError1BuilderMatch = test_utility_1.default.assertFileByNameInGeneratedFiles('testError1.builder', testError1.files); await createAction.execute({ nameCamel: 'testError2', nameReadable: 'Test error 2', }); // #spruce/errors should exist test_utils_1.assert.isTrue(spruce_skill_utils_1.diskUtil.doesFileExist(this.resolveHashSprucePath('errors'))); typesContent = spruce_skill_utils_1.diskUtil.readFile(this.errorTypesFile); // types should include both test_utils_1.assert.doesInclude(typesContent, /SpruceErrors\.TestingErrors.*?TestError1/gis); test_utils_1.assert.doesInclude(typesContent, /SpruceErrors\.TestingErrors.*?TestError2/gis); // definition file for testError1 test_utils_1.assert.isTrue(spruce_skill_utils_1.diskUtil.doesFileExist(testError1SchemaMatch)); // delete builder 1 spruce_skill_utils_1.diskUtil.deleteFile(testError1BuilderMatch); test_utils_1.assert.isFalse(spruce_skill_utils_1.diskUtil.doesFileExist(testError1BuilderMatch)); // sync await this.Action('error', 'sync').execute({}); // types should no longer include test error 1 typesContent = spruce_skill_utils_1.diskUtil.readFile(this.errorTypesFile); test_utils_1.assert.doesNotInclude(typesContent, /SpruceErrors\.TestingErrors.*?TestError1/); // the definition file should be gone now test_utils_1.assert.isFalse(spruce_skill_utils_1.diskUtil.doesFileExist(testError1SchemaMatch)); } static async canHandleNestedSchemasWithoutAddingThemToOptions() { await this.installErrorFeature('errors'); const source = this.resolveTestPath('nested_error_schemas'); const destination = this.resolvePath('src/errors'); await spruce_skill_utils_1.diskUtil.copyDir(source, destination); const results = await this.Action('error', 'sync').execute({}); const errorTypesFile = test_utility_1.default.assertFileByNameInGeneratedFiles(/errors\.types/, results.files); const typeChecker = this.Service('typeChecker'); await typeChecker.check(errorTypesFile); const errorOptionsFile = test_utility_1.default.assertFileByNameInGeneratedFiles(/options\.types/, results.files); const contents = spruce_skill_utils_1.diskUtil.readFile(errorOptionsFile); test_utils_1.assert.doesNotInclude(contents, 'INestedSchema'); const errorClassFile = test_utility_1.default.assertFileByNameInGeneratedFiles('SpruceError.ts', results.files); let classContents = spruce_skill_utils_1.diskUtil.readFile(errorClassFile); test_utils_1.assert.doesNotInclude(classContents, /NESTED_SCHEMA/); const createAction = this.Action('error', 'create'); await createAction.execute({ nameCamel: 'testError2', nameReadable: 'Test error 2', }); const syncResults = await this.Action('error', 'sync').execute({}); classContents = spruce_skill_utils_1.diskUtil.readFile(errorClassFile); test_utils_1.assert.doesNotInclude(classContents, /NESTED_SCHEMA/); await this.assertValidActionResponseFiles(syncResults); const parentSchema = test_utility_1.default.assertFileByNameInGeneratedFiles('good.schema.ts', syncResults.files); const parentSchemaContents = spruce_skill_utils_1.diskUtil.readFile(parentSchema); test_utils_1.assert.doesNotInclude(parentSchemaContents, this.cwd); } static async canCreateAndSyncErrorsWithNoCoreSchemasAndNoFields() { await this.installErrorFeature('errors'); const createAction = this.Action('error', 'create'); const results = await createAction.execute({ nameReadable: 'Test error', nameCamel: 'testError', shouldFetchCoreSchemas: false, }); test_utils_1.assert.isFalsy(results.errors); test_utils_1.assert.isTruthy(results.files); spruce_skill_utils_1.diskUtil.writeFile(results.files[0].path, `import { buildErrorSchema } from '@sprucelabs/schema' export default buildErrorSchema({ id: 'testError', name: 'Test error', description: '', fields: { } })`); const syncAction = this.Action('error', 'sync'); const syncResults = await syncAction.execute({ shouldFetchCoreSchemas: false, }); test_utils_1.assert.isFalsy(syncResults.errors); } } exports.default = KeepingErrorsInSyncTest; __decorate([ (0, test_utils_1.test)() ], KeepingErrorsInSyncTest, "hasSyncErrorAction", null); __decorate([ (0, test_utils_1.test)() ], KeepingErrorsInSyncTest, "returnsErrorWhenSyncingSchemasFails", null); __decorate([ (0, test_utils_1.test)() ], KeepingErrorsInSyncTest, "errorsStayInSyncWhenSchemasAreDeleted", null); __decorate([ (0, test_utils_1.test)() ], KeepingErrorsInSyncTest, "canHandleNestedSchemasWithoutAddingThemToOptions", null); __decorate([ (0, test_utils_1.test)() ], KeepingErrorsInSyncTest, "canCreateAndSyncErrorsWithNoCoreSchemasAndNoFields", null); //# sourceMappingURL=KeepingErrorsInSync.test.js.map