Skip to content

Commit

Permalink
feat: add v59 metadata definition (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon authored Sep 26, 2023
1 parent 443034b commit 91966d5
Show file tree
Hide file tree
Showing 3 changed files with 2,139 additions and 17 deletions.
30 changes: 24 additions & 6 deletions __tests__/unit/lib/metadata/metadataManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import { Metadata, MetadataRepository } from '../../../../src/types/metadata'

describe(`test if metadata`, () => {
it('provide latest when apiVersion does not exist', async () => {
let metadata = await getDefinition(0)
const metadata = await getDefinition(0)
const latestVersionSupported = await getLatestSupportedVersion()
const latestMetadataDef = await getDefinition(latestVersionSupported)

expect(metadata).toBeDefined()
expect(metadata.get('classes')).toBeDefined()
expect(metadata.get('do not exist')).toBeFalsy()
expect(metadata).toEqual(latestMetadataDef)
expect(latestMetadataDef.get('classes')).toBeDefined()
expect(latestMetadataDef.get('do not exist')).toBeUndefined()
})

it('has classes', async () => {
Expand All @@ -30,11 +34,25 @@ describe(`test if metadata`, () => {
})

it('getLatestSupportedVersion', async () => {
let latestVersion = await getLatestSupportedVersion()
const latestVersion = await getLatestSupportedVersion()
expect(latestVersion).toBeDefined()
expect(latestVersion).toEqual(expect.any(Number))
})

it('latest supported version is the second last version', async () => {
// Arrange
let i = 45

// Act(s)
while (await isVersionSupported(++i));
// Here latest version should not be supported because it is equal to last version + 1

// Assert
const defaultLatestSupportedVersion = await getLatestSupportedVersion()
// defaultLatestSupportedVersion should be equal to i + 1 (latest) + 1 (iteration)
expect(i).toBe(defaultLatestSupportedVersion + 2)
})

it('isVersionSupported', async () => {
// Arrange
const dataSet = [
Expand Down Expand Up @@ -91,7 +109,7 @@ describe(`test if metadata`, () => {
])

// Act
let inFileAttributes = getInFileAttributes(metadata)
const inFileAttributes = getInFileAttributes(metadata)

// Assert
expect(inFileAttributes.has('waveTemplates')).toBe(false)
Expand All @@ -109,7 +127,7 @@ describe(`test if metadata`, () => {
})

// Act
let otherInFileAttributes = getInFileAttributes(metadata)
const otherInFileAttributes = getInFileAttributes(metadata)

// Assert
expect(otherInFileAttributes).toBe(inFileAttributes)
Expand Down
25 changes: 14 additions & 11 deletions src/metadata/metadataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ const buildAPIMap = async () => {
.forEach((file: string) => {
const version: number = parseInt(file.match(/\d+/)?.[0] as string)
_apiMap.set(version, file)
_latestVersion = Math.max(_latestVersion, version)
})
setLatestSupportedVersion()
}
}

const setLatestSupportedVersion = () => {
const versions: number[] = Array.from(_apiMap.keys())
versions.sort((a, b) => a - b)
_latestVersion = versions[versions.length - 2]
}

export const getLatestSupportedVersion = async () => {
await buildAPIMap()
return _latestVersion
Expand All @@ -41,23 +47,20 @@ export const isVersionSupported = async (version: number) => {
export const getDefinition = async (
apiVersion: number
): Promise<MetadataRepository> => {
if (!describeMetadata.has(apiVersion)) {
await buildAPIMap()
const apiFile = (
_apiMap.has(apiVersion)
? _apiMap.get(apiVersion)
: _apiMap.get(_latestVersion)
) as string
await buildAPIMap()
const version: number = _apiMap.has(apiVersion) ? apiVersion : _latestVersion
if (!describeMetadata.has(version)) {
const apiFile = _apiMap.get(version)
const fileContent: string = await readFile(
resolve(__dirname, apiFile),
resolve(__dirname, apiFile!),
'utf-8'
)
describeMetadata.set(apiVersion, JSON.parse(fileContent))
describeMetadata.set(version, JSON.parse(fileContent))
}

const metadataRepository: MetadataRepository = new Map<string, Metadata>()
describeMetadata
.get(apiVersion)
.get(version)
?.reduce((metadata: MetadataRepository, describe: Metadata) => {
metadata.set(describe.directoryName, describe)
return metadata
Expand Down
Loading

0 comments on commit 91966d5

Please sign in to comment.