|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import { expect } from 'chai'; |
| 8 | +import { MetadataRegistry } from '../../src'; |
| 9 | +import { mockRegistryData as defaultRegistry } from '../mock/registry/mockRegistry'; |
| 10 | +import { MetadataType } from '../../src/registry/types'; |
| 11 | + |
| 12 | +describe('Mock Registry Validation', () => { |
| 13 | + const registry = defaultRegistry as MetadataRegistry; |
| 14 | + const typesWithChildren = Object.values(registry.types).filter((type) => type.children); |
| 15 | + |
| 16 | + describe('every child type has an entry in children', () => { |
| 17 | + const childMapping = new Map<string, string>(); |
| 18 | + |
| 19 | + typesWithChildren.map((parentType) => |
| 20 | + Object.values(parentType.children.types).map((childType) => { |
| 21 | + childMapping.set(childType.id, parentType.id); |
| 22 | + }) |
| 23 | + ); |
| 24 | + |
| 25 | + childMapping.forEach((parentId, childId) => { |
| 26 | + it(`has a childType for ${childId} : ${parentId}`, () => { |
| 27 | + expect(parentId).to.be.a('string'); |
| 28 | + expect(childId).to.be.a('string'); |
| 29 | + expect(registry.childTypes[childId]).to.equal(parentId); |
| 30 | + }); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('suffix object is complete', () => { |
| 35 | + const knownExceptions: string[] = []; |
| 36 | + |
| 37 | + const suffixMap = new Map<string, string>(); |
| 38 | + Object.values(registry.types) |
| 39 | + .filter((type) => type.suffix && !type.strictDirectoryName && !knownExceptions.includes(type.name)) |
| 40 | + .map((type) => { |
| 41 | + // mapping for the type's suffix |
| 42 | + suffixMap.set(type.suffix, type.id); |
| 43 | + if (type.children) { |
| 44 | + Object.values(type.children.types).map((childType) => { |
| 45 | + // mapping for the child's suffix |
| 46 | + suffixMap.set(childType.suffix, childType.id); |
| 47 | + }); |
| 48 | + } |
| 49 | + }); |
| 50 | + suffixMap.forEach((typeid, suffix) => { |
| 51 | + it(`has a suffix entry for "${suffix}" : "${typeid}"`, () => { |
| 52 | + expect(typeid).to.be.a('string'); |
| 53 | + expect(suffix).to.be.a('string'); |
| 54 | + expect(registry.suffixes[suffix]).to.equal(typeid); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('suffixes must be unique on non-strict types', () => { |
| 60 | + const suffixMap = new Map<string, MetadataType[]>(); |
| 61 | + Object.values(registry.types).map((type) => { |
| 62 | + if (type.suffix) { |
| 63 | + // some bundle types have no suffix |
| 64 | + suffixMap.set(type.suffix, suffixMap.has(type.suffix) ? [...suffixMap.get(type.suffix), type] : [type]); |
| 65 | + } |
| 66 | + }); |
| 67 | + suffixMap.forEach((types, suffix) => { |
| 68 | + if (types.length > 1) { |
| 69 | + // identify when a single suffix is used in multiple metadata types. |
| 70 | + // when the happens, there can only be one who is not marked as strictDirectoryName |
| 71 | + it(`Suffix "${suffix}" used by types (${types |
| 72 | + .map((type) => type.name) |
| 73 | + .join(',')}) should have only 1 non-strict directory`, () => { |
| 74 | + const nonStrictTypes = types.filter((type) => !type.strictDirectoryName); |
| 75 | + expect(nonStrictTypes.length, nonStrictTypes.map((type) => type.name).join(',')).lessThan(2); |
| 76 | + }); |
| 77 | + } |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('strict types are all in strictDirectoryNames', () => { |
| 82 | + const strictTypeMap = new Map<string, string>(); |
| 83 | + Object.values(registry.types) |
| 84 | + .filter((type) => type.strictDirectoryName) |
| 85 | + .map((type) => { |
| 86 | + // the type's suffix |
| 87 | + strictTypeMap.set(type.directoryName, type.id); |
| 88 | + }); |
| 89 | + strictTypeMap.forEach((typeid, strictDirectoryName) => { |
| 90 | + it(`has a strictDirectoryNames entry for "${strictDirectoryName}" : "${typeid}"`, () => { |
| 91 | + expect(typeid).to.be.a('string'); |
| 92 | + expect(strictDirectoryName).to.be.a('string'); |
| 93 | + expect(registry.strictDirectoryNames[strictDirectoryName]).to.equal(typeid); |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments