UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

260 lines • 11.6 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 schema_1 = require("@sprucelabs/schema"); 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 fieldClassMap_1 = __importDefault(require("./../../.spruce/schemas/fields/fieldClassMap")); const SchemaStore_1 = require("../../features/schema/stores/SchemaStore"); const AbstractSchemaTest_1 = __importDefault(require("../../tests/AbstractSchemaTest")); const LOCAL_NAMESPACE = 'TestSkill'; class SchemaStoreTest extends AbstractSchemaTest_1.default { static async canInstantiateSchemaStore() { test_utils_1.assert.isTruthy(this.Store('schema')); } static async hasFetchSchemaTemplateItemsMethod() { test_utils_1.assert.isFunction(this.Store('schema').fetchSchemas); test_utils_1.assert.isFunction(this.Store('schema').fetchFields); } static async fetchesCoreSchemas() { const results = await this.Store('schema').fetchSchemas({ localNamespace: LOCAL_NAMESPACE, }); const { schemasByNamespace, errors } = results; test_utils_1.assert.isEqual(errors.length, 0); test_utils_1.assert.isTruthy(schemasByNamespace[spruce_skill_utils_1.CORE_NAMESPACE]); const coreSchemas = schemasByNamespace[spruce_skill_utils_1.CORE_NAMESPACE]; test_utils_1.assert.isAbove(coreSchemas.length, 0); this.validateSchemas(coreSchemas); test_utils_1.assert.doesInclude(coreSchemas, { id: 'person' }); test_utils_1.assert.doesInclude(coreSchemas, { id: 'skill' }); } static async fetchesLocalSchemas() { const results = await this.copySchemasAndFetchSchemas({ localSchemaDir: this.resolvePath('src', 'schemas'), }); const { schemasByNamespace, errors } = results; test_utils_1.assert.isEqual(errors.length, 0); test_utils_1.assert.isTruthy(schemasByNamespace[LOCAL_NAMESPACE]); test_utils_1.assert.isLength(Object.keys(schemasByNamespace), 2); const localSchemas = schemasByNamespace[LOCAL_NAMESPACE]; this.validateSchemas(localSchemas); test_utils_1.assert.isLength(localSchemas, 3); } static async canHandleABadSchema() { const results = await this.copySchemasAndFetchSchemas({ localSchemaDir: this.resolvePath('src', 'schemas'), }, 'test_builders_one_bad'); test_utils_1.assert.isEqual(results.errors.length, 1); test_utils_1.assert.doesInclude(results.errors[0].message, 'badSchema'); const { schemasByNamespace } = results; const localSchemas = schemasByNamespace[LOCAL_NAMESPACE]; this.validateSchemas(localSchemas); test_utils_1.assert.isLength(localSchemas, 3); } static async canFetchCoreFields() { const results = await SchemaStoreTest.copySchemasAndFieldsThenFetchFields(); const fieldTypes = Object.keys(fieldClassMap_1.default); for (const type of fieldTypes) { test_utils_1.assert.doesInclude(results, { 'fields[].registration.type': spruce_skill_utils_1.namesUtil.toPascal(type), }); } } static async canFetchLocalFields() { const results = await this.copySchemasAndFieldsThenFetchFields({ localAddonsDir: this.resolvePath('src', 'addons'), }); test_utils_1.assert.isLength(results.errors, 0); test_utils_1.assert.isLength(results.fields, Object.keys(fieldClassMap_1.default).length + 1); test_utils_1.assert.doesInclude(results, { 'fields[].registration.type': 'Test' }); test_utils_1.assert.doesInclude(results, { 'fields[].registration.description': 'A test for us all', }); } static async canHandleBadFields() { const results = await this.copySchemasAndFieldsThenFetchFields({ localAddonsDir: this.resolvePath('src', 'addons'), }, 'field_registrations_one_bad'); test_utils_1.assert.isLength(results.errors, 1); test_utils_1.assert.doesInclude(results.errors[0].message, 'badField'); } static async wontLetYouSpecifyANamespaceNorVersion() { const results = await this.copySchemasAndFetchSchemas({}, 'test_builders_with_namespace_and_version'); test_utils_1.assert.isTruthy(results.errors); test_utils_1.assert.isLength(results.errors, 2); test_utils_2.errorAssert.assertError(results.errors[0], 'SCHEMA_FAILED_TO_IMPORT'); test_utils_2.errorAssert.assertError(results.errors[1], 'SCHEMA_FAILED_TO_IMPORT'); results.errors = results.errors.sort((a, b) => //@ts-ignore a.originalError.options.schemaId > b.originalError.options.schemaId ? 1 : -1); test_utils_2.errorAssert.assertError( // @ts-ignore results.errors[0].originalError, // @ts-ignore 'INVALID_SCHEMA', { schemaId: 'schemaWithNamespace', errors: ['namespace_should_not_be_set'], }); test_utils_2.errorAssert.assertError( // @ts-ignore results.errors[1].originalError, // @ts-ignore 'INVALID_SCHEMA', { schemaId: 'schemaWithVersion', errors: ['version_should_not_be_set'], }); } static async emitsDidFetchEvent() { const cli = await this.installSchemaFeature('schemas'); let wasFired = false; await cli.on('schema.did-fetch-schemas', () => { wasFired = true; return {}; }); await this.Store('schema').fetchSchemas({ localNamespace: LOCAL_NAMESPACE, }); test_utils_1.assert.isTrue(wasFired); } static async fetchEventPayloadIncludesAllOtherSchemas() { const cli = await this.installSchemaFeature('schemas'); let emitPayload; await cli.on('schema.did-fetch-schemas', (payload) => { emitPayload = payload; return {}; }); await this.Store('schema').fetchSchemas({ localNamespace: LOCAL_NAMESPACE, }); for (const name in SchemaStore_1.coreSchemas) { test_utils_1.assert.doesInclude(emitPayload.schemas, //@ts-ignore (0, schema_1.normalizeSchemaToIdWithVersion)(SchemaStore_1.coreSchemas[name])); } } static async canListenToFetchEventToDropInAdditionalSchemas() { const cli = await this.installSchemaFeature('schemas'); await cli.on('schema.did-fetch-schemas', () => { return { schemas: [ (0, schema_1.buildSchema)({ id: 'test', namespace: 'MyCoolNamespace', fields: {}, }), ], }; }); const results = await this.Store('schema').fetchSchemas({ localNamespace: LOCAL_NAMESPACE, }); test_utils_1.assert.isLength(results.errors, 0); const { schemasByNamespace } = results; test_utils_1.assert.isTruthy(schemasByNamespace.MyCoolNamespace); } static async multipleSchemasWithSameIdAndVersionReturnOnce() { const cli = await this.installSchemaFeature('schemas'); await this.copySchemas(); await cli.on('schema.did-fetch-schemas', () => { return { schemas: [ { id: 'schemaOne', name: 'First schema', namespace: 'TestSkill', version: 'v2020_06_23', description: 'It is going to be great!', fields: { name: { type: 'text', isRequired: true, }, }, }, ], }; }); const results = await this.Store('schema').fetchSchemas({ localNamespace: LOCAL_NAMESPACE, }); test_utils_1.assert.isLength(results.schemasByNamespace.TestSkill, 3); } static validateSchemas(schemas) { for (const schema of schemas) { (0, schema_1.validateSchema)(schema); } } static async copySchemasAndFetchSchemas(options, testBuilderDir = 'test_builders') { await this.syncSchemas('schemas'); await this.copySchemas(testBuilderDir); const results = await this.Store('schema').fetchSchemas({ localNamespace: LOCAL_NAMESPACE, ...(options || {}), }); return results; } static async copySchemas(testBuilderDir = 'test_builders') { const schemasDir = this.resolvePath('src', 'schemas'); await spruce_skill_utils_1.diskUtil.copyDir(this.resolveTestPath(testBuilderDir), schemasDir); } static async copySchemasAndFieldsThenFetchFields(options, registrationsDir = 'field_registrations') { await this.syncSchemas('schemas'); const addonsDir = this.resolvePath('src', 'addons'); await spruce_skill_utils_1.diskUtil.copyDir(this.resolveTestPath(registrationsDir), addonsDir); const results = await this.Store('schema').fetchFields(options); return results; } } exports.default = SchemaStoreTest; __decorate([ (0, test_utils_1.test)() ], SchemaStoreTest, "canInstantiateSchemaStore", null); __decorate([ (0, test_utils_1.test)() ], SchemaStoreTest, "hasFetchSchemaTemplateItemsMethod", null); __decorate([ (0, test_utils_1.test)() ], SchemaStoreTest, "fetchesCoreSchemas", null); __decorate([ (0, test_utils_1.test)() ], SchemaStoreTest, "fetchesLocalSchemas", null); __decorate([ test_utils_1.test.skip("Skipped because schemas are bulk imported and it's all or nothing.") ], SchemaStoreTest, "canHandleABadSchema", null); __decorate([ (0, test_utils_1.test)() ], SchemaStoreTest, "canFetchCoreFields", null); __decorate([ (0, test_utils_1.test)() ], SchemaStoreTest, "canFetchLocalFields", null); __decorate([ (0, test_utils_1.test)() ], SchemaStoreTest, "canHandleBadFields", null); __decorate([ (0, test_utils_1.test)() ], SchemaStoreTest, "wontLetYouSpecifyANamespaceNorVersion", null); __decorate([ (0, test_utils_1.test)() ], SchemaStoreTest, "emitsDidFetchEvent", null); __decorate([ (0, test_utils_1.test)() ], SchemaStoreTest, "fetchEventPayloadIncludesAllOtherSchemas", null); __decorate([ (0, test_utils_1.test)() ], SchemaStoreTest, "canListenToFetchEventToDropInAdditionalSchemas", null); __decorate([ (0, test_utils_1.test)() ], SchemaStoreTest, "multipleSchemasWithSameIdAndVersionReturnOnce", null); //# sourceMappingURL=SchemaStore.test.js.map