Skip to content

Commit 357c3ff

Browse files
committed
fix: documents renaming test
1 parent f9e7ff5 commit 357c3ff

File tree

5 files changed

+32
-39
lines changed

5 files changed

+32
-39
lines changed

src/strategies/document-group.strategy.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ import {
2121
BuildTypeContexts,
2222
FileFormat,
2323
JSON_EXPORT_GROUP_FORMAT,
24-
ResolvedDocument, ResolvedReferenceMap,
24+
ResolvedDocument,
25+
ResolvedReferenceMap,
2526
VersionDocument,
2627
} from '../types'
2728
import { REST_API_TYPE } from '../apitypes'
2829
import {
2930
EXPORT_FORMAT_TO_FILE_FORMAT,
3031
fromBase64,
31-
getDocumentTitle,
32-
getFileExtension,
3332
removeFirstSlash,
3433
slugify,
3534
takeIfDefined,
@@ -41,7 +40,6 @@ import { VersionRestDocument } from '../apitypes/rest/rest.types'
4140
import { INLINE_REFS_FLAG, NORMALIZE_OPTIONS } from '../consts'
4241
import { normalize } from '@netcracker/qubership-apihub-api-unifier'
4342
import { calculateSpecRefs, extractCommonPathItemProperties } from '../apitypes/rest/rest.operation'
44-
import { groupBy } from 'graphql/jsutils/groupBy'
4543

4644
async function getTransformedDocument(document: ResolvedDocument, format: FileFormat, packages: ResolvedReferenceMap): Promise<VersionRestDocument> {
4745
const versionDocument = toVersionDocument(document, format)
@@ -104,36 +102,15 @@ export class DocumentGroupStrategy implements BuilderStrategy {
104102
}
105103

106104
const transformedDocuments = await Promise.all(transformTasks)
107-
const transformedDocumentsWithoutCollisions = (['fileId', 'filename'] as const).reduce(resolveCollisionsByField, transformedDocuments)
108105

109-
for (const document of transformedDocumentsWithoutCollisions) {
106+
for (const document of transformedDocuments) {
110107
buildResult.documents.set(document.fileId, document)
111108
}
112109

113110
return buildResult
114111
}
115112
}
116113

117-
// there is a chance that the renamed document will be the same as another document (this case has not been fixed yet)
118-
function resolveCollisionsByField(docs: VersionRestDocument[], field: 'fileId' | 'filename'): VersionRestDocument[] {
119-
const fileIdMap = groupBy(docs, (document) => document[field])
120-
return ([...fileIdMap.values()] as VersionRestDocument[][]).reduce((acc, docs) => {
121-
const [_, ...duplicates] = docs
122-
duplicates.forEach((document, index) => {document[field] = renameDuplicate(document[field], index)})
123-
return [...acc, ...docs]
124-
}, [] as VersionRestDocument[])
125-
}
126-
127-
function renameDuplicate(fileName: string, index: number): string {
128-
const extension = getFileExtension(fileName)
129-
const nameWithPostfix = `${getDocumentTitle(fileName)}-${index + 1}`
130-
131-
if (extension) {
132-
return `${nameWithPostfix}.${extension}`
133-
}
134-
return nameWithPostfix
135-
}
136-
137114
function parseBase64String(value: string): object {
138115
return JSON.parse(fromBase64(value))
139116
}

src/types/external/references.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export interface ReferencedPackage {
4141
name: string
4242
version: string
4343
status: VersionStatus
44-
parentPackages: string[]
44+
parentPackages?: string[]
4545
deletedAt?: string
4646
deletedBy?: string
4747
notLatestRevision?: string

test/document-group.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ describe('Document Group test', () => {
7979

8080
expect(Array.from(result.documents.values())).toEqual(
8181
expect.toIncludeSameMembers([
82-
expect.objectContaining({ fileId: '1.yaml', filename: '1.json' }),
83-
expect.objectContaining({ fileId: '2.yaml', filename: '2.json' }),
84-
expect.objectContaining({ fileId: '1-1.yaml', filename: '1-1.json' }),
85-
expect.objectContaining({ fileId: '1-2.yaml', filename: '1-2.json' }),
82+
expect.objectContaining({ fileId: 'documents-collision/package1_1.yaml', filename: 'documents-collision/package1_1.json' }),
83+
expect.objectContaining({ fileId: 'documents-collision/package1_2.yaml', filename: 'documents-collision/package1_2.json' }),
84+
expect.objectContaining({ fileId: 'documents-collision/package2_1.yaml', filename: 'documents-collision/package2_1.json' }),
85+
expect.objectContaining({ fileId: 'documents-collision/package3_1.yaml', filename: 'documents-collision/package3_1.json' }),
8686
]),
8787
)
8888

test/helpers/registry/apihub.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class ApihubRegistry implements IRegistry {
239239
): Promise<ResolvedDocuments | null> {
240240
const encodedPackageKey = encodeURIComponent(packageId)
241241
const encodedVersionKey = encodeURIComponent(version)
242-
const response: ResolvedDocuments = { documents: [] }
242+
const response: ResolvedDocuments = { documents: [], packages: {} }
243243

244244
const LIMIT = 100
245245
let page = 0

test/helpers/registry/local.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import {
2626
EMPTY_CHANGE_SUMMARY,
2727
FILE_FORMAT,
2828
GRAPHQL_API_TYPE,
29-
graphqlApiBuilder, MESSAGE_SEVERITY,
29+
graphqlApiBuilder,
30+
KIND_PACKAGE,
31+
MESSAGE_SEVERITY,
3032
NotificationMessage,
3133
OperationId,
3234
OperationsApiType,
@@ -67,7 +69,7 @@ import {
6769
saveNotifications,
6870
saveOperationsArray,
6971
} from './utils'
70-
import { getCompositeKey, getSplittedVersionKey, isNotEmpty, toBase64 } from '../../../src/utils'
72+
import { getCompositeKey, getSplittedVersionKey, isNotEmpty, takeIfDefined, toBase64 } from '../../../src/utils'
7173
import { groupBy } from 'graphql/jsutils/groupBy'
7274
import { IRegistry } from './types'
7375
import { calculateTotalChangeSummary } from '../../../src/components/compare'
@@ -199,26 +201,37 @@ export class LocalRegistry implements IRegistry {
199201
const documentsFromVersion = Array.from(documents?.values() ?? [])
200202

201203
if (isNotEmpty(documentsFromVersion)) {
202-
return { documents: this.resolveDocuments(documentsFromVersion, this.filterOperationIdsByGroup(filterByOperationGroup)) }
204+
return { documents: this.resolveDocuments(documentsFromVersion, this.filterOperationIdsByGroup(filterByOperationGroup)), packages: {} }
203205
}
204206

205207
const documentsFromRefs = (
206208
await Promise.all(refs.map(async ({ refId, version }) => {
207209
const versionCache = await this.getVersion(refId, version)
208210
if (!versionCache) return []
209211
const { documents } = versionCache
210-
return Array.from(documents.values())
212+
return this.resolveDocuments(Array.from(documents.values()), this.filterOperationIdsByGroup(filterByOperationGroup), refId)
211213
}))
212214
).flat()
213215

214-
return { documents: this.resolveDocuments(documentsFromRefs, this.filterOperationIdsByGroup(filterByOperationGroup)) }
216+
const packages = refs.reduce((acc, ref) => {
217+
acc[ref.refId] = {
218+
refId: ref.refId,
219+
version: ref.version,
220+
kind: KIND_PACKAGE,
221+
name: ref.refId,
222+
status: VERSION_STATUS.DRAFT,
223+
}
224+
return acc
225+
}, {} as ResolvedReferenceMap)
226+
227+
return { documents: documentsFromRefs, packages: packages }
215228
}
216229

217230
private filterOperationIdsByGroup(filterByOperationGroup: string): (id: string) => boolean {
218231
return (id: string): boolean => this.groupToOperationIdsMap[filterByOperationGroup]?.includes(id)
219232
}
220233

221-
private resolveDocuments(documents: VersionDocument[], filterOperationIdsByGroup: (id: string) => boolean): ResolvedDocument[] {
234+
private resolveDocuments(documents: VersionDocument[], filterOperationIdsByGroup: (id: string) => boolean, refId?: string): ResolvedDocument[] {
222235
return documents
223236
.filter(versionDocument => versionDocument.operationIds.some(filterOperationIdsByGroup))
224237
.map(document => ({
@@ -232,6 +245,7 @@ export class LocalRegistry implements IRegistry {
232245
title: document.title,
233246
includedOperationIds: document.operationIds.filter(filterOperationIdsByGroup),
234247
data: toBase64(JSON.stringify(document.data)),
248+
...takeIfDefined({ packageRef: refId }),
235249
}))
236250
}
237251

@@ -295,7 +309,9 @@ export class LocalRegistry implements IRegistry {
295309
packages[packageRef] = {
296310
refId: ref.refId,
297311
version: ref.version,
298-
versionRevision: 0,
312+
kind: KIND_PACKAGE,
313+
name: ref.refId,
314+
status: VERSION_STATUS.DRAFT,
299315
}
300316
references.push({
301317
packageRef: packageRef,

0 commit comments

Comments
 (0)