diff --git a/src/modules/cards/tests/application/GetCollectionsForUrlUseCase.test.ts b/src/modules/cards/tests/application/GetCollectionsForUrlUseCase.test.ts index db84db89..7dab4981 100644 --- a/src/modules/cards/tests/application/GetCollectionsForUrlUseCase.test.ts +++ b/src/modules/cards/tests/application/GetCollectionsForUrlUseCase.test.ts @@ -554,7 +554,7 @@ describe('GetCollectionsForUrlUseCase', () => { }); describe('Sorting', () => { - it('should use default sorting parameters (NAME, ASC)', async () => { + it('should use default sorting parameters (ADDED_AT, DESC)', async () => { const testUrl = 'https://example.com/test-article'; const query = { @@ -565,8 +565,8 @@ describe('GetCollectionsForUrlUseCase', () => { expect(result.isOk()).toBe(true); const response = result.unwrap(); - expect(response.sorting.sortBy).toBe(CollectionSortField.NAME); - expect(response.sorting.sortOrder).toBe(SortOrder.ASC); + expect(response.sorting.sortBy).toBe(CollectionSortField.ADDED_AT); + expect(response.sorting.sortOrder).toBe(SortOrder.DESC); }); it('should use provided sorting parameters', async () => { @@ -586,7 +586,7 @@ describe('GetCollectionsForUrlUseCase', () => { expect(response.sorting.sortOrder).toBe(SortOrder.DESC); }); - it('should sort collections by name in ascending order by default', async () => { + it('should sort collections by name in ascending order when specified', async () => { const testUrl = 'https://example.com/article'; const url = URL.create(testUrl).unwrap(); @@ -659,6 +659,8 @@ describe('GetCollectionsForUrlUseCase', () => { const query = { url: testUrl, + sortBy: CollectionSortField.NAME, + sortOrder: SortOrder.ASC, }; const result = await useCase.execute(query); diff --git a/src/modules/cards/tests/utils/InMemoryCollectionQueryRepository.ts b/src/modules/cards/tests/utils/InMemoryCollectionQueryRepository.ts index 7d42fbf1..ba454a19 100644 --- a/src/modules/cards/tests/utils/InMemoryCollectionQueryRepository.ts +++ b/src/modules/cards/tests/utils/InMemoryCollectionQueryRepository.ts @@ -183,12 +183,51 @@ export class InMemoryCollectionQueryRepository ), ); - // Sort collections - const sortedCollections = this.sortCollections( - collectionsWithUrl, - options.sortBy, - options.sortOrder, - ); + // For ADDED_AT sorting, we need to track the max addedAt for each collection + let sortedCollections: Collection[]; + if (options.sortBy === CollectionSortField.ADDED_AT) { + // Create a map of collection -> max addedAt from the matching cards + const collectionAddedAtMap = new Map(); + + for (const collection of collectionsWithUrl) { + const matchingCardLinks = collection.cardLinks.filter((link) => + cardIds.has(link.cardId.getStringValue()), + ); + + if (matchingCardLinks.length > 0) { + const maxAddedAt = matchingCardLinks.reduce((max, link) => { + return link.addedAt > max ? link.addedAt : max; + }, matchingCardLinks[0]!.addedAt); + + collectionAddedAtMap.set( + collection.collectionId.getStringValue(), + maxAddedAt, + ); + } + } + + // Sort by the max addedAt + sortedCollections = [...collectionsWithUrl].sort((a, b) => { + const aAddedAt = collectionAddedAtMap.get( + a.collectionId.getStringValue(), + )!; + const bAddedAt = collectionAddedAtMap.get( + b.collectionId.getStringValue(), + )!; + + const comparison = aAddedAt.getTime() - bAddedAt.getTime(); + return options.sortOrder === SortOrder.DESC + ? -comparison + : comparison; + }); + } else { + // Use the standard sorting method for other fields + sortedCollections = this.sortCollections( + collectionsWithUrl, + options.sortBy, + options.sortOrder, + ); + } // Apply pagination const { page, limit } = options;