@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
407 lines • 18.7 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 test_utils_1 = require("@sprucelabs/test-utils");
const test_utils_2 = require("@sprucelabs/test-utils");
const SchemaTemplateItemBuilder_1 = __importDefault(require("../../../templateItemBuilders/SchemaTemplateItemBuilder"));
const AbstractSkillTest_1 = __importDefault(require("../../../tests/AbstractSkillTest"));
const test_utility_1 = __importDefault(require("../../../tests/utilities/test.utility"));
const LOCAL_NAMESPACE = 'TestingSchemas';
const REMOTE_NAMESPACE = 'RemoteSchemas';
const REMOTE_MODULE = '@sprucelabs/calendar-utils';
const REMOTE_MODULE2 = '@sprucelabs/spruce-templates';
class SettingUpSchemasForModuleDistributionTest extends AbstractSkillTest_1.default {
static skillCacheKey = 'schemas';
static builder1;
static schema1 = {
id: 'calendarEvent',
version: 'v2021_09_20',
namespace: 'TestingSchemas',
name: 'Schema imported as module',
moduleToImportFromWhenRemote: '@sprucelabs/calendar-utils',
fields: {
fieldName1: {
label: 'First Field',
type: 'text',
isRequired: true,
},
fieldName2: {
label: 'Second Field',
type: 'number',
isRequired: true,
hint: 'A hint',
},
},
};
//@ts-ignore
static schema1b;
static schema2 = {
id: 'calendarEvent2',
version: 'v2021_09_20',
namespace: 'TestingSchemas',
name: 'Schema imported as module',
moduleToImportFromWhenRemote: '@sprucelabs/spruce-templates',
fields: {
fieldName1: {
label: 'First Field',
type: 'text',
isRequired: true,
},
fieldName2: {
label: 'Second Field',
type: 'number',
isRequired: true,
hint: 'A hint',
},
},
};
static async canSpecifyModuleToImportFromWhenRemote(namePascal, remoteModule, count) {
const nameCamel = spruce_skill_utils_1.namesUtil.toCamel(namePascal);
const promise = this.Action('schema', 'create').execute({
nameReadable: 'Schema imported as module',
namePascal,
nameCamel,
moduleToImportFromWhenRemote: remoteModule,
});
await this.wait(10);
if (this.ui.isWaitingForInput()) {
await this.ui.sendInput(spruce_skill_utils_1.versionUtil.generateVersion().constValue);
}
const results = await promise;
test_utils_1.assert.isFalsy(results.errors);
const builderFile = test_utility_1.default.assertFileByNameInGeneratedFiles(`${nameCamel}.builder.ts`, results.files);
const builder = await this.Service('import').importDefault(builderFile);
test_utils_1.assert.isEqual(builder.moduleToImportFromWhenRemote, remoteModule);
//@ts-ignore
this[`builder${count}`] = builder;
const schemaFile = test_utility_1.default.assertFileByNameInGeneratedFiles(`${nameCamel}.schema.ts`, results.files);
const schema = await this.Service('import').importDefault(schemaFile);
test_utils_1.assert.isEqual(schema.moduleToImportFromWhenRemote, remoteModule);
//@ts-ignore
this[`schema${count}`] = schema;
//@ts-ignore
this[`schema${count}b`] = { ...schema, id: `schema${count}B` };
}
static async templateItemGeneratorHonorsModuleToImportWhenRemoteWhenInRemoteNamespace() {
const templateItems = this.generateTemplateItems(REMOTE_NAMESPACE);
for (const templateItem of templateItems) {
test_utils_1.assert.isEqual(templateItem.schema.moduleToImportFromWhenRemote, REMOTE_MODULE);
test_utils_1.assert.isEqual(templateItem.importFrom, REMOTE_MODULE);
}
}
static async moduleToImportWhenRemoteIgnoredWhenInSameNamespace() {
const templateItems = this.generateTemplateItems(LOCAL_NAMESPACE);
for (const templateItem of templateItems) {
test_utils_1.assert.isEqual(templateItem.schema.moduleToImportFromWhenRemote, REMOTE_MODULE);
test_utils_1.assert.isFalse('importFrom' in templateItem);
}
}
static async asksIfModuleShouldBeInstalled(schemaNames, expectedModules, shouldConfirmInstall) {
await this.reInstallSkill();
await this.Store('skill').setCurrentSkillsNamespace('TestingTwo');
const schemas = schemaNames.map((name) => this[name]);
await this.emitter.on('schema.did-fetch-schemas', async () => {
return {
schemas,
};
});
const promise = this.SyncAction().execute({});
await this.waitForInput();
const secondToLast = this.ui.invocations[this.ui.invocations.length - 2];
test_utils_1.assert.isEqual(secondToLast.options.headline, `Missing ${expectedModules.length} module${expectedModules.length === 1 ? '' : 's'}`);
for (const expected of expectedModules) {
test_utils_1.assert.doesInclude(secondToLast.options.lines, expected);
}
await this.ui.sendInput(shouldConfirmInstall ? 'y' : 'n');
const results = await promise;
if (shouldConfirmInstall) {
test_utils_1.assert.isFalsy(results.errors);
const pkg = this.Service('pkg');
for (const expected of expectedModules) {
test_utils_1.assert.isTrue(pkg.isInstalled(expected));
}
}
else {
test_utils_1.assert.isTruthy(results.errors);
test_utils_2.errorAssert.assertError(results.errors[0], 'ACTION_CANCELLED');
}
}
static async schemasAreImportedFromModuleWhenSyncedFromDifferentNamespace(schemaNames) {
await this.reInstallSkill();
await this.Store('skill').setCurrentSkillsNamespace('TestingTwo');
const schemas = [];
for (const name of schemaNames) {
schemas.push(this[name]);
}
await this.emitter.on('schema.did-fetch-schemas', async () => {
return {
schemas,
};
});
const results = await this.SyncAction().execute({
shouldInstallMissingDependencies: true,
});
await this.assertCalendarEventSchemaIsImported(results);
test_utils_1.assert.doesThrow(() => test_utility_1.default.assertFileByNameInGeneratedFiles('schemas.types.ts', results.files));
}
static async canReferenceSchemaImportedFromAnotherModule(importsWhenRemote) {
await this.reInstallSkill();
await this.Store('skill').setCurrentSkillsNamespace('TestingTwo');
const mySchema = {
id: 'localSchema',
importsWhenRemote,
fields: {
calendarEvent: {
type: 'schema',
options: {
schema: {
...this.schema1,
namespace: 'CalendarUtils',
version: 'v2021_05_19',
},
},
},
},
};
await this.emitter.on('schema.did-fetch-schemas', async () => {
return {
schemas: [mySchema],
};
});
const results = await this.SyncAction().execute({
shouldInstallMissingDependencies: true,
});
const localSchemaFile = test_utility_1.default.assertFileByNameInGeneratedFiles('localSchema.schema.ts', results.files);
const localSchema = await this.Service('import').importDefault(localSchemaFile);
test_utils_1.assert.isEqualDeep(localSchema.importsWhenRemote, importsWhenRemote);
await this.assertCalendarEventSchemaIsImported(results);
await this.assertValidTypesFileWhenAddingRemoteImport(results);
}
static async canSetImportFromModuleWithNestedSchemasWhenRemoteWhenSyncing() {
await this.reInstallSkill();
await this.Action('schema', 'create').execute({
nameReadable: 'Schema imported as module',
namePascal: 'CalandarEvent',
nameCamel: 'calendarEvent',
});
const results = await this.SyncAction().execute({
moduleToImportFromWhenRemote: REMOTE_MODULE,
});
this.schema1 = (await this.importGeneratedFile(results, `calendarEvent.schema.ts`));
test_utils_1.assert.isEqual(this.schema1.moduleToImportFromWhenRemote, REMOTE_MODULE);
}
static async canHandleFormBuilderImportExportObject() {
const version = spruce_skill_utils_1.versionUtil.generateVersion().constValue;
const completedFormBuilder = {
id: 'fullCompletedForm',
version,
moduleToImportFromWhenRemote: '@sprucelabs/heartwood-view-controllers',
fields: {
id: {
type: 'id',
isRequired: true,
},
personId: {
type: 'id',
isRequired: true,
},
values: {
type: 'raw',
isArray: true,
options: {
valueType: 'Record<string, any>',
},
},
},
};
const builderForm = {
id: 'builtForm',
version,
moduleToImportFromWhenRemote: '@sprucelabs/heartwood-view-controllers',
fields: {
id: {
type: 'id',
isRequired: true,
},
dateDeleted: {
type: 'number',
},
completedFormBuilder: {
type: 'schema',
options: {
schemaId: {
version,
id: 'fullCompletedForm',
},
},
},
},
};
const itemBuilder = new SchemaTemplateItemBuilder_1.default('TestSkill');
const results = itemBuilder.buildTemplateItems({
//@ts-ignore
TestSkill: [completedFormBuilder, builderForm],
});
test_utils_1.assert.isFalsy(results.find((r) => r.id === 'builtForm')?.schema.fields
?.completedFormBuilder?.options?.schemaIds?.[0]?.moduleToImportFromWhenRemote);
}
static async importedSchemasDoNotShowUpInSchemaTypes() {
// await this.reInstallSkill()
await this.Store('skill').setCurrentSkillsNamespace('TestingTwo');
const schema1 = {
id: 'fullCompletedForm',
namespace: 'Heartwood',
version: spruce_skill_utils_1.versionUtil.generateVersion().dirValue,
moduleToImportFromWhenRemote: '@sprucelabs/heartwood-view-controllers',
importsWhenRemote: ['import * from bananarama'],
fields: {
id: {
type: 'id',
isRequired: true,
},
personId: {
type: 'id',
isRequired: true,
},
values: {
type: 'raw',
isArray: true,
options: {
valueType: 'Record<string, any>',
},
},
},
};
const schema2 = {
id: 'localCompleted',
version: spruce_skill_utils_1.versionUtil.generateVersion().dirValue,
fields: {
id: {
type: 'id',
isRequired: true,
},
personId: {
type: 'id',
isRequired: true,
},
values: {
type: 'raw',
isArray: true,
options: {
valueType: 'Record<string, any>',
},
},
},
};
await this.emitter.on('schema.did-fetch-schemas', async () => {
return {
schemas: [schema1, schema2],
};
});
const results = await this.SyncAction().execute({
shouldInstallMissingDependencies: true,
});
test_utils_1.assert.isFalsy(results.errors);
const schemaTypesFile = test_utility_1.default.assertFileByNameInGeneratedFiles('schemas.types.ts', results.files);
await this.Service('typeChecker').check(schemaTypesFile);
}
static SyncAction() {
return this.Action('schema', 'sync', {
optionOverrides: {
'sync.schemas': {},
},
});
}
static async importGeneratedFile(results, fileName) {
const schemaFile = test_utility_1.default.assertFileByNameInGeneratedFiles(fileName, results.files);
return await this.Service('import').importDefault(schemaFile);
}
static async assertCalendarEventSchemaIsImported(results) {
test_utils_1.assert.isFalsy(results.errors);
const imported = test_utility_1.default.assertFileByNameInGeneratedFiles('calendarEvent.schema.ts', results.files);
const calendarEventContents = spruce_skill_utils_1.diskUtil.readFile(imported);
test_utils_1.assert.isEqual(calendarEventContents.trim(), `export { calendarEventSchema as default } from '@sprucelabs/calendar-utils'`);
}
static generateTemplateItems(namespace) {
const itemBuilder = new SchemaTemplateItemBuilder_1.default(namespace);
const templateItems = itemBuilder.buildTemplateItems({
[LOCAL_NAMESPACE]: [
{
...this.builder1,
fields: {
...this.builder1.fields,
nestedSchema: {
type: 'schema',
options: {
schema: {
id: 'nested',
fields: {
firstName: {
type: 'text',
},
},
},
},
},
},
},
],
}, this.resolveHashSprucePath('schemas'));
return templateItems;
}
static async assertValidTypesFileWhenAddingRemoteImport(results) {
const schemaTypesFile = test_utility_1.default.assertFileByNameInGeneratedFiles('schemas.types.ts', results.files);
const typesContent = `import '${REMOTE_MODULE}'\n\n${spruce_skill_utils_1.diskUtil.readFile(schemaTypesFile)}`;
spruce_skill_utils_1.diskUtil.writeFile(schemaTypesFile, typesContent);
await this.Service('typeChecker').check(schemaTypesFile);
}
}
exports.default = SettingUpSchemasForModuleDistributionTest;
__decorate([
(0, test_utils_1.test)('can specify importFromModuleWhenRemote 1', 'CalendarEvent', REMOTE_MODULE, '1'),
(0, test_utils_1.test)('can specify importFromModuleWhenRemote 2', 'CalendarEvent2', REMOTE_MODULE2, '2')
], SettingUpSchemasForModuleDistributionTest, "canSpecifyModuleToImportFromWhenRemote", null);
__decorate([
(0, test_utils_1.test)()
], SettingUpSchemasForModuleDistributionTest, "templateItemGeneratorHonorsModuleToImportWhenRemoteWhenInRemoteNamespace", null);
__decorate([
(0, test_utils_1.test)()
], SettingUpSchemasForModuleDistributionTest, "moduleToImportWhenRemoteIgnoredWhenInSameNamespace", null);
__decorate([
(0, test_utils_1.test)('should ask about missing module and cancel install', ['schema1'], [REMOTE_MODULE], false),
(0, test_utils_1.test)('should ask about missing module and do install', ['schema1'], [REMOTE_MODULE], true),
(0, test_utils_1.test)('should ask about missing module and do install', ['schema1', 'schema2'], [REMOTE_MODULE, REMOTE_MODULE2], true),
(0, test_utils_1.test)('does not return the same module twice', ['schema1', 'schema1b', 'schema2'], [REMOTE_MODULE, REMOTE_MODULE2], true)
], SettingUpSchemasForModuleDistributionTest, "asksIfModuleShouldBeInstalled", null);
__decorate([
(0, test_utils_1.test)('schemas are imported from one module', ['schema1']),
(0, test_utils_1.test)('schemas are imported from two modules', ['schema1', 'schema2'])
], SettingUpSchemasForModuleDistributionTest, "schemasAreImportedFromModuleWhenSyncedFromDifferentNamespace", null);
__decorate([
(0, test_utils_1.test)('can reference impored from another module', [
`import * as HeartwoodTypesLocal from '@sprucelabs/heartwood-view-controllers'`,
]),
(0, test_utils_1.test)('can reference impored from another module with two importsWhenLocal', [
`import * as HeartwoodTypesLocal from '@sprucelabs/heartwood-view-controllers'`,
`import * as HeartwoodTypesLocal2 from '@sprucelabs/heartwood-view-controllers'`,
])
], SettingUpSchemasForModuleDistributionTest, "canReferenceSchemaImportedFromAnotherModule", null);
__decorate([
(0, test_utils_1.test)()
], SettingUpSchemasForModuleDistributionTest, "canSetImportFromModuleWithNestedSchemasWhenRemoteWhenSyncing", null);
__decorate([
(0, test_utils_1.test)()
], SettingUpSchemasForModuleDistributionTest, "canHandleFormBuilderImportExportObject", null);
__decorate([
(0, test_utils_1.test)()
], SettingUpSchemasForModuleDistributionTest, "importedSchemasDoNotShowUpInSchemaTypes", null);
//# sourceMappingURL=SettingUpSchemasForModuleDistribution.test.js.map