-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathPackageIndexAndCollectionsTests.swift
762 lines (631 loc) · 42.1 KB
/
PackageIndexAndCollectionsTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
import Foundation
@testable import PackageCollections
import PackageModel
import _InternalTestSupport
import XCTest
import struct TSCUtility.Version
class PackageIndexAndCollectionsTests: XCTestCase {
func testCollectionAddRemoveGetList() async throws {
try PackageCollectionsTests_skipIfUnsupportedPlatform()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }
let mockCollections = makeMockCollections()
let packageCollections = makePackageCollections(mockCollections: mockCollections, storage: storage)
let packageIndex = MockPackageIndex()
let indexAndCollections = PackageIndexAndCollections(index: packageIndex, collections: packageCollections, observabilityScope: ObservabilitySystem.NOOP)
defer { XCTAssertNoThrow(try indexAndCollections.close()) }
do {
let list = try await indexAndCollections.listCollections()
XCTAssertEqual(list.count, 0, "list should be empty")
}
do {
for collection in mockCollections {
_ = try await indexAndCollections.addCollection(collection.source, order: nil)
}
let list = try await indexAndCollections.listCollections()
XCTAssertEqual(list, mockCollections, "list count should match")
}
do {
let collection = try await indexAndCollections.getCollection(mockCollections.first!.source)
XCTAssertEqual(collection, mockCollections.first, "collection should match")
}
do {
try await indexAndCollections.removeCollection(mockCollections.first!.source)
let list = try await indexAndCollections.listCollections()
XCTAssertEqual(list.count, mockCollections.count - 1, "list count should match")
}
}
func testRefreshCollections() async throws {
try PackageCollectionsTests_skipIfUnsupportedPlatform()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }
let mockCollections = makeMockCollections()
let packageCollections = makePackageCollections(mockCollections: mockCollections, storage: storage)
let packageIndex = MockPackageIndex()
let indexAndCollections = PackageIndexAndCollections(index: packageIndex, collections: packageCollections, observabilityScope: ObservabilitySystem.NOOP)
defer { XCTAssertNoThrow(try indexAndCollections.close()) }
for collection in mockCollections {
// save directly to storage to circumvent refresh on add
try await storage.sources.add(source: collection.source, order: nil)
}
_ = try await indexAndCollections.refreshCollections()
let list = try await indexAndCollections.listCollections()
XCTAssertEqual(list.count, mockCollections.count, "list count should match")
}
func testRefreshCollection() async throws {
try PackageCollectionsTests_skipIfUnsupportedPlatform()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }
let mockCollections = makeMockCollections()
let packageCollections = makePackageCollections(mockCollections: mockCollections, storage: storage)
let packageIndex = MockPackageIndex()
let indexAndCollections = PackageIndexAndCollections(index: packageIndex, collections: packageCollections, observabilityScope: ObservabilitySystem.NOOP)
defer { XCTAssertNoThrow(try indexAndCollections.close()) }
for collection in mockCollections {
// save directly to storage to circumvent refresh on add
try await storage.sources.add(source: collection.source, order: nil)
}
_ = try await indexAndCollections.refreshCollection(mockCollections.first!.source)
let collection = try await indexAndCollections.getCollection(mockCollections.first!.source)
XCTAssertEqual(collection, mockCollections.first, "collection should match")
}
func testListPackages() async throws {
try PackageCollectionsTests_skipIfUnsupportedPlatform()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }
var mockCollections = makeMockCollections(count: 5)
let mockTargets = [UUID().uuidString, UUID().uuidString].map {
PackageCollectionsModel.Target(name: $0, moduleName: $0)
}
let mockProducts = [PackageCollectionsModel.Product(name: UUID().uuidString, type: .executable, targets: [mockTargets.first!]),
PackageCollectionsModel.Product(name: UUID().uuidString, type: .executable, targets: mockTargets)]
let toolsVersion = ToolsVersion(string: "5.2")!
let mockManifest = PackageCollectionsModel.Package.Version.Manifest(
toolsVersion: toolsVersion,
packageName: UUID().uuidString,
targets: mockTargets,
products: mockProducts,
minimumPlatformVersions: nil
)
let mockVersion = PackageCollectionsModel.Package.Version(version: TSCUtility.Version(1, 0, 0),
title: nil,
summary: nil,
manifests: [toolsVersion: mockManifest],
defaultToolsVersion: toolsVersion,
verifiedCompatibility: nil,
license: nil,
author: nil,
signer: nil,
createdAt: nil)
let mockPackageURL = "https://packages.mock/\(UUID().uuidString)"
let mockPackage = PackageCollectionsModel.Package(identity: .init(urlString: mockPackageURL),
location: mockPackageURL,
summary: UUID().uuidString,
keywords: [UUID().uuidString, UUID().uuidString],
versions: [mockVersion],
watchersCount: nil,
readmeURL: nil,
license: nil,
authors: nil,
languages: nil)
let mockCollection = PackageCollectionsModel.Collection(source: .init(type: .json, url: "https://feed.mock/\(UUID().uuidString)"),
name: UUID().uuidString,
overview: UUID().uuidString,
keywords: [UUID().uuidString, UUID().uuidString],
packages: [mockPackage],
createdAt: Date(),
createdBy: nil,
signature: nil)
let mockCollection2 = PackageCollectionsModel.Collection(source: .init(type: .json, url: "https://feed.mock/\(UUID().uuidString)"),
name: UUID().uuidString,
overview: UUID().uuidString,
keywords: [UUID().uuidString, UUID().uuidString],
packages: [mockPackage],
createdAt: Date(),
createdBy: nil,
signature: nil)
mockCollections.append(mockCollection)
mockCollections.append(mockCollection2)
let packageCollections = makePackageCollections(mockCollections: mockCollections, storage: storage)
let packageIndex = MockPackageIndex()
let indexAndCollections = PackageIndexAndCollections(index: packageIndex, collections: packageCollections, observabilityScope: ObservabilitySystem.NOOP)
defer { XCTAssertNoThrow(try indexAndCollections.close()) }
for collection in mockCollections {
_ = try await indexAndCollections.addCollection(collection.source, trustConfirmationProvider: { _, cb in cb(true) })
}
do {
let fetchCollections = Set(mockCollections.map { $0.identifier } + [mockCollection.identifier, mockCollection2.identifier])
let expectedPackages = Set(mockCollections.flatMap { $0.packages.map { $0.identity } } + [mockPackage.identity])
let expectedCollections = Set([mockCollection.identifier, mockCollection2.identifier])
let searchResult = try await indexAndCollections.listPackages(collections: fetchCollections)
XCTAssertEqual(searchResult.items.count, expectedPackages.count, "list count should match")
XCTAssertEqual(Set(searchResult.items.map { $0.package.identity }), expectedPackages, "items should match")
XCTAssertEqual(Set(searchResult.items.first(where: { $0.package.identity == mockPackage.identity })?.collections ?? []), expectedCollections, "collections should match")
}
// Call API for specific collections
do {
let fetchCollections = Set([mockCollections[0].identifier, mockCollection.identifier, mockCollection2.identifier])
let expectedPackages = Set(mockCollections[0].packages.map { $0.identity } + [mockPackage.identity])
let expectedCollections = Set([mockCollection.identifier, mockCollection2.identifier])
let searchResult = try await indexAndCollections.listPackages(collections: fetchCollections)
XCTAssertEqual(searchResult.items.count, expectedPackages.count, "list count should match")
XCTAssertEqual(Set(searchResult.items.map { $0.package.identity }), expectedPackages, "items should match")
XCTAssertEqual(Set(searchResult.items.first(where: { $0.package.identity == mockPackage.identity })?.collections ?? []), expectedCollections, "collections should match")
}
}
func testListTargets() async throws {
try PackageCollectionsTests_skipIfUnsupportedPlatform()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }
let mockCollections = makeMockCollections()
let packageCollections = makePackageCollections(mockCollections: mockCollections, storage: storage)
let packageIndex = MockPackageIndex()
let indexAndCollections = PackageIndexAndCollections(index: packageIndex, collections: packageCollections, observabilityScope: ObservabilitySystem.NOOP)
defer { XCTAssertNoThrow(try indexAndCollections.close()) }
do {
let list = try await indexAndCollections.listCollections()
XCTAssertEqual(list.count, 0, "list should be empty")
}
do {
for collection in mockCollections {
_ = try await indexAndCollections.addCollection(collection.source, order: nil)
}
let list = try await indexAndCollections.listCollections()
XCTAssertEqual(list.count, mockCollections.count, "list count should match")
}
let targetsList = try await indexAndCollections.listTargets()
let expectedTargets = Set(mockCollections.flatMap { $0.packages.flatMap { $0.versions.flatMap { $0.defaultManifest!.targets.map { $0.name } } } })
XCTAssertEqual(Set(targetsList.map { $0.target.name }), expectedTargets, "targets should match")
let targetsPackagesList = Set(targetsList.flatMap { $0.packages })
let expectedPackages = Set(mockCollections.flatMap { $0.packages.filter { !$0.versions.filter { !expectedTargets.isDisjoint(with: $0.defaultManifest!.targets.map { $0.name }) }.isEmpty } }.map { $0.identity })
XCTAssertEqual(targetsPackagesList.count, expectedPackages.count, "packages should match")
let targetsCollectionsList = Set(targetsList.flatMap { $0.packages.flatMap { $0.collections } })
let expectedCollections = Set(mockCollections.filter { !$0.packages.filter { expectedPackages.contains($0.identity) }.isEmpty }.map { $0.identifier })
XCTAssertEqual(targetsCollectionsList, expectedCollections, "collections should match")
}
func testFindTargets() async throws {
try PackageCollectionsTests_skipIfUnsupportedPlatform()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }
var mockCollections = makeMockCollections()
let mockTargets = [UUID().uuidString, UUID().uuidString].map {
PackageCollectionsModel.Target(name: $0, moduleName: $0)
}
let mockProducts = [PackageCollectionsModel.Product(name: UUID().uuidString, type: .executable, targets: [mockTargets.first!]),
PackageCollectionsModel.Product(name: UUID().uuidString, type: .executable, targets: mockTargets)]
let toolsVersion = ToolsVersion(string: "5.2")!
let mockManifest = PackageCollectionsModel.Package.Version.Manifest(
toolsVersion: toolsVersion,
packageName: UUID().uuidString,
targets: mockTargets,
products: mockProducts,
minimumPlatformVersions: nil
)
let mockVersion = PackageCollectionsModel.Package.Version(version: TSCUtility.Version(1, 0, 0),
title: nil,
summary: nil,
manifests: [toolsVersion: mockManifest],
defaultToolsVersion: toolsVersion,
verifiedCompatibility: nil,
license: nil,
author: nil,
signer: nil,
createdAt: nil)
let mockPackageURL = "https://packages.mock/\(UUID().uuidString)"
let mockPackage = PackageCollectionsModel.Package(identity: .init(urlString: mockPackageURL),
location: mockPackageURL,
summary: UUID().uuidString,
keywords: [UUID().uuidString, UUID().uuidString],
versions: [mockVersion],
watchersCount: nil,
readmeURL: nil,
license: nil,
authors: nil,
languages: nil)
let mockCollection = PackageCollectionsModel.Collection(source: .init(type: .json, url: "https://feed.mock/\(UUID().uuidString)"),
name: UUID().uuidString,
overview: UUID().uuidString,
keywords: [UUID().uuidString, UUID().uuidString],
packages: [mockPackage],
createdAt: Date(),
createdBy: nil,
signature: nil)
let mockCollection2 = PackageCollectionsModel.Collection(source: .init(type: .json, url: "https://feed.mock/\(UUID().uuidString)"),
name: UUID().uuidString,
overview: UUID().uuidString,
keywords: [UUID().uuidString, UUID().uuidString],
packages: [mockPackage],
createdAt: Date(),
createdBy: nil,
signature: nil)
let expectedCollections = [mockCollection, mockCollection2]
let expectedCollectionsIdentifiers = expectedCollections.map { $0.identifier }.sorted()
mockCollections.append(contentsOf: expectedCollections)
let packageCollections = makePackageCollections(mockCollections: mockCollections, storage: storage)
let packageIndex = MockPackageIndex()
let indexAndCollections = PackageIndexAndCollections(index: packageIndex, collections: packageCollections, observabilityScope: ObservabilitySystem.NOOP)
defer { XCTAssertNoThrow(try indexAndCollections.close()) }
for collection in mockCollections {
_ = try await indexAndCollections.addCollection(collection.source, trustConfirmationProvider: { _, cb in cb(true) })
}
do {
// search by exact target name
let searchResult = try await indexAndCollections.findTargets(mockTargets.first!.name, searchType: .exactMatch)
XCTAssertEqual(searchResult.items.count, 1, "list count should match")
XCTAssertEqual(searchResult.items.first?.packages.map { $0.identity }, [mockPackage.identity], "packages should match")
XCTAssertEqual(searchResult.items.first?.packages.flatMap { $0.collections }.sorted(), expectedCollectionsIdentifiers, "collections should match")
}
do {
// search by prefix target name
let searchResult = try await indexAndCollections.findTargets(String(mockTargets.first!.name.prefix(mockTargets.first!.name.count - 1)), searchType: .prefix)
XCTAssertEqual(searchResult.items.count, 1, "list count should match")
XCTAssertEqual(searchResult.items.first?.packages.map { $0.identity }, [mockPackage.identity], "packages should match")
XCTAssertEqual(searchResult.items.first?.packages.flatMap { $0.collections }.sorted(), expectedCollectionsIdentifiers, "collections should match")
}
do {
// empty search
let searchResult = try await indexAndCollections.findTargets(UUID().uuidString, searchType: .exactMatch)
XCTAssertEqual(searchResult.items.count, 0, "list count should match")
}
}
func testListPackagesInIndex() async throws {
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }
let packageCollections = makePackageCollections(mockCollections: [], storage: storage)
let mockPackages = (0..<10).map { packageIndex -> PackageCollectionsModel.Package in
makeMockPackage(id: "package-\(packageIndex)")
}
let packageIndex = MockPackageIndex(packages: mockPackages)
let indexAndCollections = PackageIndexAndCollections(index: packageIndex, collections: packageCollections, observabilityScope: ObservabilitySystem.NOOP)
defer { XCTAssertNoThrow(try indexAndCollections.close()) }
let result = try await indexAndCollections.listPackagesInIndex(offset: 1, limit: 5)
XCTAssertFalse(result.items.isEmpty)
}
func testGetPackageMetadata() async throws {
try PackageCollectionsTests_skipIfUnsupportedPlatform()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }
let mockCollections = makeMockCollections(count: 3)
let mockPackage = mockCollections.last!.packages.last!
let mockMetadata = makeMockPackageBasicMetadata()
let metadataProvider = MockMetadataProvider([mockPackage.identity: mockMetadata])
let packageCollections = makePackageCollections(mockCollections: mockCollections, metadataProvider: metadataProvider, storage: storage)
let packageIndex = MockPackageIndex(packages: mockCollections.last!.packages)
let indexAndCollections = PackageIndexAndCollections(index: packageIndex, collections: packageCollections, observabilityScope: ObservabilitySystem.NOOP)
defer { XCTAssertNoThrow(try indexAndCollections.close()) }
do {
let list = try await indexAndCollections.listCollections()
XCTAssertEqual(list.count, 0, "list should be empty")
}
do {
for collection in mockCollections {
_ = try await indexAndCollections.addCollection(collection.source, order: nil)
}
let list = try await indexAndCollections.listCollections()
XCTAssertEqual(list.count, mockCollections.count, "list count should match")
}
let metadata = try await indexAndCollections.getPackageMetadata(identity: mockPackage.identity, location: mockPackage.location)
let expectedCollections = Set(mockCollections.filter { $0.packages.map { $0.identity }.contains(mockPackage.identity) }.map { $0.identifier })
XCTAssertEqual(Set(metadata.collections), expectedCollections, "collections should match")
// Metadata comes from package index - package returned as-is, no merging
XCTAssertEqual(metadata.package, mockPackage)
XCTAssertNotNil(metadata.provider)
XCTAssertEqual(metadata.provider?.name, "package index")
}
func testGetPackageMetadata_brokenIndex() async throws {
try PackageCollectionsTests_skipIfUnsupportedPlatform()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }
let mockCollections = makeMockCollections(count: 3)
let mockPackage = mockCollections.last!.packages.last!
let mockMetadata = makeMockPackageBasicMetadata()
let metadataProvider = MockMetadataProvider([mockPackage.identity: mockMetadata])
let packageCollections = makePackageCollections(mockCollections: mockCollections, metadataProvider: metadataProvider, storage: storage)
let packageIndex = BrokenPackageIndex()
let indexAndCollections = PackageIndexAndCollections(index: packageIndex, collections: packageCollections, observabilityScope: ObservabilitySystem.NOOP)
defer { XCTAssertNoThrow(try indexAndCollections.close()) }
do {
let list = try await indexAndCollections.listCollections()
XCTAssertEqual(list.count, 0, "list should be empty")
}
do {
for collection in mockCollections {
_ = try await indexAndCollections.addCollection(collection.source, order: nil)
}
let list = try await indexAndCollections.listCollections()
XCTAssertEqual(list.count, mockCollections.count, "list count should match")
}
let metadata = try await indexAndCollections.getPackageMetadata(identity: mockPackage.identity, location: mockPackage.location)
let expectedCollections = Set(mockCollections.filter { $0.packages.map { $0.identity }.contains(mockPackage.identity) }.map { $0.identifier })
XCTAssertEqual(Set(metadata.collections), expectedCollections, "collections should match")
// Metadata comes from collections - merged with basic metadata
let expectedMetadata = PackageCollections.mergedPackageMetadata(package: mockPackage, basicMetadata: mockMetadata)
XCTAssertEqual(metadata.package, expectedMetadata, "package should match")
XCTAssertNil(metadata.provider)
}
func testGetPackageMetadata_indexAndCollectionError() async throws {
try PackageCollectionsTests_skipIfUnsupportedPlatform()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }
let packageCollections = makePackageCollections(mockCollections: [], storage: storage)
let packageIndex = BrokenPackageIndex()
let indexAndCollections = PackageIndexAndCollections(index: packageIndex, collections: packageCollections, observabilityScope: ObservabilitySystem.NOOP)
defer { XCTAssertNoThrow(try indexAndCollections.close()) }
let mockPackage = makeMockPackage(id: "test-package")
// Package not found in collections; index is broken
await XCTAssertAsyncThrowsError(try await indexAndCollections.getPackageMetadata(identity: mockPackage.identity, location: mockPackage.location)) { error in
// Index error is returned
guard let _ = error as? BrokenPackageIndex.TerribleThing else {
return XCTFail("Expected BrokenPackageIndex.TerribleThing")
}
}
}
func testFindPackages() async throws {
try PackageCollectionsTests_skipIfUnsupportedPlatform()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }
var mockCollections = makeMockCollections()
let mockTargets = [UUID().uuidString, UUID().uuidString].map {
PackageCollectionsModel.Target(name: $0, moduleName: $0)
}
let mockProducts = [PackageCollectionsModel.Product(name: UUID().uuidString, type: .executable, targets: [mockTargets.first!]),
PackageCollectionsModel.Product(name: UUID().uuidString, type: .executable, targets: mockTargets)]
let toolsVersion = ToolsVersion(string: "5.2")!
let mockManifest = PackageCollectionsModel.Package.Version.Manifest(
toolsVersion: toolsVersion,
packageName: UUID().uuidString,
targets: mockTargets,
products: mockProducts,
minimumPlatformVersions: nil
)
let mockVersion = PackageCollectionsModel.Package.Version(version: TSCUtility.Version(1, 0, 0),
title: nil,
summary: nil,
manifests: [toolsVersion: mockManifest],
defaultToolsVersion: toolsVersion,
verifiedCompatibility: nil,
license: nil,
author: nil,
signer: nil,
createdAt: nil)
let url = "https://packages.mock/\(UUID().uuidString)"
let mockPackage = PackageCollectionsModel.Package(identity: .init(urlString: url),
location: url,
summary: UUID().uuidString,
keywords: [UUID().uuidString, UUID().uuidString],
versions: [mockVersion],
watchersCount: nil,
readmeURL: nil,
license: nil,
authors: nil,
languages: nil)
let mockCollection = PackageCollectionsModel.Collection(source: .init(type: .json, url: "https://feed.mock/\(UUID().uuidString)"),
name: UUID().uuidString,
overview: UUID().uuidString,
keywords: [UUID().uuidString, UUID().uuidString],
packages: [mockPackage],
createdAt: Date(),
createdBy: nil,
signature: nil)
let mockCollection2 = PackageCollectionsModel.Collection(source: .init(type: .json, url: "https://feed.mock/\(UUID().uuidString)"),
name: UUID().uuidString,
overview: UUID().uuidString,
keywords: [UUID().uuidString, UUID().uuidString],
packages: [mockPackage],
createdAt: Date(),
createdBy: nil,
signature: nil)
let expectedCollections = [mockCollection, mockCollection2]
let expectedCollectionsIdentifiers = expectedCollections.map { $0.identifier }.sorted()
mockCollections.append(contentsOf: expectedCollections)
let packageCollections = makePackageCollections(mockCollections: mockCollections, storage: storage)
let packageIndex = MockPackageIndex(packages: [mockPackage])
let indexAndCollections = PackageIndexAndCollections(index: packageIndex, collections: packageCollections, observabilityScope: ObservabilitySystem.NOOP)
defer { XCTAssertNoThrow(try indexAndCollections.close()) }
for collection in mockCollections {
_ = try await indexAndCollections.addCollection(collection.source, trustConfirmationProvider: { _, cb in cb(true) })
}
// both index and collections
do {
let searchResult = try await indexAndCollections.findPackages(mockPackage.identity.description, in: .both(collections: nil))
XCTAssertEqual(searchResult.items.count, 1, "list count should match")
XCTAssertEqual(searchResult.items.first?.collections.sorted(), expectedCollectionsIdentifiers, "collections should match")
XCTAssertEqual(searchResult.items.first?.indexes, [packageIndex.url], "indexes should match")
}
// index only
do {
let searchResult = try await indexAndCollections.findPackages(mockPackage.identity.description, in: .index)
XCTAssertEqual(searchResult.items.count, 1, "list count should match")
XCTAssertTrue(searchResult.items.first?.collections.isEmpty ?? true, "collections should match")
XCTAssertEqual(searchResult.items.first?.indexes, [packageIndex.url], "indexes should match")
}
// collections only
do {
let searchResult = try await indexAndCollections.findPackages(mockPackage.identity.description, in: .collections(nil))
XCTAssertEqual(searchResult.items.count, 1, "list count should match")
XCTAssertEqual(searchResult.items.first?.collections.sorted(), expectedCollectionsIdentifiers, "collections should match")
XCTAssertTrue(searchResult.items.first?.indexes.isEmpty ?? true, "indexes should match")
}
}
func testFindPackages_brokenIndex() async throws {
try PackageCollectionsTests_skipIfUnsupportedPlatform()
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }
var mockCollections = makeMockCollections()
let mockTargets = [UUID().uuidString, UUID().uuidString].map {
PackageCollectionsModel.Target(name: $0, moduleName: $0)
}
let mockProducts = [PackageCollectionsModel.Product(name: UUID().uuidString, type: .executable, targets: [mockTargets.first!]),
PackageCollectionsModel.Product(name: UUID().uuidString, type: .executable, targets: mockTargets)]
let toolsVersion = ToolsVersion(string: "5.2")!
let mockManifest = PackageCollectionsModel.Package.Version.Manifest(
toolsVersion: toolsVersion,
packageName: UUID().uuidString,
targets: mockTargets,
products: mockProducts,
minimumPlatformVersions: nil
)
let mockVersion = PackageCollectionsModel.Package.Version(version: TSCUtility.Version(1, 0, 0),
title: nil,
summary: nil,
manifests: [toolsVersion: mockManifest],
defaultToolsVersion: toolsVersion,
verifiedCompatibility: nil,
license: nil,
author: nil,
signer: nil,
createdAt: nil)
let url = "https://packages.mock/\(UUID().uuidString)"
let mockPackage = PackageCollectionsModel.Package(identity: .init(urlString: url),
location: url,
summary: UUID().uuidString,
keywords: [UUID().uuidString, UUID().uuidString],
versions: [mockVersion],
watchersCount: nil,
readmeURL: nil,
license: nil,
authors: nil,
languages: nil)
let mockCollection = PackageCollectionsModel.Collection(source: .init(type: .json, url: "https://feed.mock/\(UUID().uuidString)"),
name: UUID().uuidString,
overview: UUID().uuidString,
keywords: [UUID().uuidString, UUID().uuidString],
packages: [mockPackage],
createdAt: Date(),
createdBy: nil,
signature: nil)
let mockCollection2 = PackageCollectionsModel.Collection(source: .init(type: .json, url: "https://feed.mock/\(UUID().uuidString)"),
name: UUID().uuidString,
overview: UUID().uuidString,
keywords: [UUID().uuidString, UUID().uuidString],
packages: [mockPackage],
createdAt: Date(),
createdBy: nil,
signature: nil)
let expectedCollections = [mockCollection, mockCollection2]
let expectedCollectionsIdentifiers = expectedCollections.map { $0.identifier }.sorted()
mockCollections.append(contentsOf: expectedCollections)
let packageCollections = makePackageCollections(mockCollections: mockCollections, storage: storage)
let packageIndex = BrokenPackageIndex()
let indexAndCollections = PackageIndexAndCollections(index: packageIndex, collections: packageCollections, observabilityScope: ObservabilitySystem.NOOP)
defer { XCTAssertNoThrow(try indexAndCollections.close()) }
for collection in mockCollections {
_ = try await indexAndCollections.addCollection(collection.source, trustConfirmationProvider: { _, cb in cb(true) })
}
// both index and collections
do {
let searchResult = try await indexAndCollections.findPackages(mockPackage.identity.description, in: .both(collections: nil))
XCTAssertEqual(searchResult.items.count, 1, "list count should match")
XCTAssertEqual(searchResult.items.first?.collections.sorted(), expectedCollectionsIdentifiers, "collections should match")
// Results come from collections since index is broken
XCTAssertEqual(searchResult.items.first?.indexes, [], "indexes should match")
}
// index only
do {
await XCTAssertAsyncThrowsError(try await indexAndCollections.findPackages(mockPackage.identity.description, in: .index)) { error in
guard error is BrokenPackageIndex.TerribleThing else {
return XCTFail("invalid error \(error)")
}
}
}
// collections only
do {
let searchResult = try await indexAndCollections.findPackages(mockPackage.identity.description, in: .collections(nil))
XCTAssertEqual(searchResult.items.count, 1, "list count should match")
XCTAssertEqual(searchResult.items.first?.collections.sorted(), expectedCollectionsIdentifiers, "collections should match")
// Not searching in index so should not be impacted by its error
XCTAssertTrue(searchResult.items.first?.indexes.isEmpty ?? true, "indexes should match")
}
}
}
private func makePackageCollections(
mockCollections: [PackageCollectionsModel.Collection],
metadataProvider: PackageMetadataProvider = MockMetadataProvider([:]),
storage: PackageCollections.Storage
) -> PackageCollections {
let configuration = PackageCollections.Configuration()
let collectionProviders = [PackageCollectionsModel.CollectionSourceType.json: MockCollectionsProvider(mockCollections)]
let metadataProvider = metadataProvider
return PackageCollections(
configuration: configuration,
fileSystem: localFileSystem,
observabilityScope: ObservabilitySystem.NOOP,
storage: storage,
collectionProviders: collectionProviders,
metadataProvider: metadataProvider
)
}
private struct MockPackageIndex: PackageIndexProtocol {
let isEnabled = true
let url: URL
private let packages: [PackageCollectionsModel.Package]
init(
url: URL = "https://mock-package-index",
packages: [PackageCollectionsModel.Package] = []
) {
self.url = url
self.packages = packages
}
func getPackageMetadata(
identity: PackageIdentity,
location: String?
) async throws -> PackageCollectionsModel.PackageMetadata {
guard let package = self.packages.first(where: { $0.identity == identity }) else {
throw NotFoundError("Package \(identity) not found")
}
return (package: package, collections: [], provider: .init(name: "package index", authTokenType: nil, isAuthTokenConfigured: true))
}
func findPackages(
_ query: String
) async throws -> PackageCollectionsModel.PackageSearchResult{
let items = self.packages.filter { $0.identity.description.contains(query) }
return PackageCollectionsModel.PackageSearchResult(items: items.map { .init(package: $0, collections: [], indexes: [self.url]) })
}
func listPackages(
offset: Int,
limit: Int
) async throws -> PackageCollectionsModel.PaginatedPackageList {
guard !self.packages.isEmpty, offset < self.packages.count, limit > 0 else {
return PackageCollectionsModel.PaginatedPackageList(items: [], offset: offset, limit: limit, total: self.packages.count)
}
return PackageCollectionsModel.PaginatedPackageList(
items: Array(self.packages[offset..<min(self.packages.count, offset + limit)]),
offset: offset,
limit: limit,
total: self.packages.count
)
}
}
private struct BrokenPackageIndex: PackageIndexProtocol {
let isEnabled = true
func getPackageMetadata(
identity: PackageIdentity,
location: String?
) async throws -> PackageCollectionsModel.PackageMetadata {
throw TerribleThing()
}
func findPackages(
_ query: String
) async throws -> PackageCollectionsModel.PackageSearchResult {
throw TerribleThing()
}
func listPackages(
offset: Int,
limit: Int
) async throws -> PackageCollectionsModel.PaginatedPackageList {
throw TerribleThing()
}
struct TerribleThing: Error {}
}