Skip to content

Commit 9b2293e

Browse files
committed
Wire up custom collection reconciliation (TODO: add final test, fix tests
1 parent 4403b23 commit 9b2293e

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

Sources/App/Commands/Reconcile.swift

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,35 @@ func reconcile(client: Client, database: Database) async throws {
5050
let start = DispatchTime.now().uptimeNanoseconds
5151
defer { AppMetrics.reconcileDurationSeconds?.time(since: start) }
5252

53-
do { // reconcile main package list
54-
async let sourcePackageList = try Current.fetchPackageList(client)
55-
async let sourcePackageDenyList = try Current.fetchPackageDenyList(client)
56-
async let currentList = try fetchCurrentPackageList(database)
57-
58-
let packageList = processPackageDenyList(packageList: try await sourcePackageList,
59-
denyList: try await sourcePackageDenyList)
60-
61-
try await reconcileLists(db: database,
62-
source: packageList,
63-
target: currentList)
64-
}
53+
// reconcile main package list
54+
let fullPackageList = try await reconcileMainPackageList(client: client, database: database)
6555

6656
do { // reconcile custom package collections
67-
// - fetch custom-package-collections.json
68-
// - for each entry: reconcileCustomCollection
57+
@Dependency(\.packageListRepository) var packageListRepository
58+
let collections = try await packageListRepository.fetchCustomCollections(client: client)
59+
for collection in collections {
60+
try await reconcileCustomCollection(client: client, database: database, fullPackageList: fullPackageList, collection)
61+
}
6962
}
7063
}
7164

7265

66+
func reconcileMainPackageList(client: Client, database: Database) async throws -> [URL] {
67+
async let sourcePackageList = try Current.fetchPackageList(client)
68+
async let sourcePackageDenyList = try Current.fetchPackageDenyList(client)
69+
async let currentList = try fetchCurrentPackageList(database)
70+
71+
let packageList = processPackageDenyList(packageList: try await sourcePackageList,
72+
denyList: try await sourcePackageDenyList)
73+
74+
try await reconcileLists(db: database,
75+
source: packageList,
76+
target: currentList)
77+
78+
return packageList
79+
}
80+
81+
7382
func liveFetchPackageList(_ client: Client) async throws -> [URL] {
7483
try await client
7584
.get(Constants.packageListUri)

Sources/App/Core/Dependencies/PackageListRepositoryClient.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Vapor
2020
@DependencyClient
2121
struct PackageListRepositoryClient {
2222
var fetchCustomCollection: @Sendable (_ client: Client, _ url: URL) async throws -> [URL]
23+
var fetchCustomCollections: @Sendable (_ client: Client) async throws -> [CustomCollection.DTO]
2324
// TODO: move other package list dependencies here
2425
}
2526

Tests/AppTests/ReconcilerTests.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ class ReconcilerTests: AppTestCase {
3535
XCTAssertEqual(urls.map(\.absoluteString).sorted(), ["1", "2", "3"])
3636
}
3737

38-
func test_basic_reconciliation() async throws {
38+
func test_reconcileMainPackageList() async throws {
3939
// setup
4040
let urls = ["1", "2", "3"]
4141
Current.fetchPackageList = { _ in urls.asURLs }
4242

4343
// MUT
44-
try await reconcile(client: app.client, database: app.db)
44+
_ = try await reconcileMainPackageList(client: app.client, database: app.db)
4545

4646
// validate
4747
let packages = try await Package.query(on: app.db).all()
@@ -55,7 +55,7 @@ class ReconcilerTests: AppTestCase {
5555
}
5656
}
5757

58-
func test_adds_and_deletes() async throws {
58+
func test_reconcileMainPackageList_adds_and_deletes() async throws {
5959
// save intial set of packages 1, 2, 3
6060
for url in ["1", "2", "3"].asURLs {
6161
try await Package(url: url).save(on: app.db)
@@ -66,14 +66,14 @@ class ReconcilerTests: AppTestCase {
6666
Current.fetchPackageList = { _ in urls.asURLs }
6767

6868
// MUT
69-
try await reconcile(client: app.client, database: app.db)
69+
_ = try await reconcileMainPackageList(client: app.client, database: app.db)
7070

7171
// validate
7272
let packages = try await Package.query(on: app.db).all()
7373
XCTAssertEqual(packages.map(\.url).sorted(), urls.sorted())
7474
}
7575

76-
func test_packageDenyList() async throws {
76+
func test_reconcileMainPackageList_packageDenyList() async throws {
7777
// Save the intial set of packages
7878
for url in ["1", "2", "3"].asURLs {
7979
try await Package(url: url).save(on: app.db)
@@ -88,14 +88,14 @@ class ReconcilerTests: AppTestCase {
8888
Current.fetchPackageDenyList = { _ in packageDenyList.asURLs }
8989

9090
// MUT
91-
try await reconcile(client: app.client, database: app.db)
91+
_ = try await reconcileMainPackageList(client: app.client, database: app.db)
9292

9393
// validate
9494
let packages = try await Package.query(on: app.db).all()
9595
XCTAssertEqual(packages.map(\.url).sorted(), ["1", "3", "5"])
9696
}
9797

98-
func test_packageDenyList_caseSensitivity() async throws {
98+
func test_reconcileMainPackageList_packageDenyList_caseSensitivity() async throws {
9999
// Save the intial set of packages
100100
for url in ["https://example.com/one/one", "https://example.com/two/two"].asURLs {
101101
try await Package(url: url).save(on: app.db)
@@ -110,7 +110,7 @@ class ReconcilerTests: AppTestCase {
110110
Current.fetchPackageDenyList = { _ in packageDenyList.asURLs }
111111

112112
// MUT
113-
try await reconcile(client: app.client, database: app.db)
113+
_ = try await reconcileMainPackageList(client: app.client, database: app.db)
114114

115115
// validate
116116
let packages = try await Package.query(on: app.db).all()
@@ -206,4 +206,8 @@ class ReconcilerTests: AppTestCase {
206206
}
207207
}
208208

209+
func test_reconcile() async throws {
210+
XCTFail("Implement integration test for both parts, main list + custom collection")
211+
}
212+
209213
}

0 commit comments

Comments
 (0)