|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +import Foundation |
| 16 | + |
15 | 17 | @testable import App |
16 | 18 |
|
17 | 19 | import Dependencies |
18 | 20 | import Prometheus |
19 | | -import XCTest |
| 21 | +import Testing |
20 | 22 |
|
21 | 23 |
|
22 | | -class MetricsTests: AppTestCase { |
| 24 | +@Suite struct MetricsTests { |
23 | 25 |
|
24 | | - func test_basic() async throws { |
| 26 | + @Test func basic() async throws { |
25 | 27 | try await withDependencies { |
26 | 28 | $0.buildSystem.triggerBuild = { @Sendable _, _, _, _, _, _, _ in |
27 | 29 | .init(status: .ok, webUrl: "") |
28 | 30 | } |
29 | 31 | $0.environment.builderToken = { "builder token" } |
30 | 32 | $0.environment.gitlabPipelineToken = { "pipeline token" } |
31 | 33 | } operation: { |
32 | | - // setup - trigger build to increment counter |
33 | | - let versionId = UUID() |
34 | | - do { // save minimal package + version |
35 | | - let p = Package(id: UUID(), url: "1") |
36 | | - try await p.save(on: app.db) |
37 | | - try await Version(id: versionId, package: p, reference: .branch("main")).save(on: app.db) |
| 34 | + try await withApp { app in |
| 35 | + // setup - trigger build to increment counter |
| 36 | + let versionId = UUID() |
| 37 | + do { // save minimal package + version |
| 38 | + let p = Package(id: UUID(), url: "1") |
| 39 | + try await p.save(on: app.db) |
| 40 | + try await Version(id: versionId, package: p, reference: .branch("main")).save(on: app.db) |
| 41 | + } |
| 42 | + try await triggerBuildsUnchecked(on: app.db, |
| 43 | + triggers: [ |
| 44 | + .init(versionId: versionId, |
| 45 | + buildPairs: [.init(.macosSpm, .v3)])! |
| 46 | + ]) |
| 47 | + |
| 48 | + // MUT |
| 49 | + try await app.test(.GET, "metrics", afterResponse: { res async in |
| 50 | + // validation |
| 51 | + #expect(res.status == .ok) |
| 52 | + let content = res.body.asString() |
| 53 | + #expect(content.contains( |
| 54 | + #"spi_build_trigger_count{swiftVersion="\#(SwiftVersion.v3)", platform="macos-spm"}"# |
| 55 | + ), "was:\n\(content)") |
| 56 | + }) |
38 | 57 | } |
39 | | - try await triggerBuildsUnchecked(on: app.db, |
40 | | - triggers: [ |
41 | | - .init(versionId: versionId, |
42 | | - buildPairs: [.init(.macosSpm, .v3)])! |
43 | | - ]) |
44 | | - |
45 | | - // MUT |
46 | | - try await app.test(.GET, "metrics", afterResponse: { res async in |
47 | | - // validation |
48 | | - XCTAssertEqual(res.status, .ok) |
49 | | - let content = res.body.asString() |
50 | | - XCTAssertTrue(content.contains( |
51 | | - #"spi_build_trigger_count{swiftVersion="\#(SwiftVersion.v3)", platform="macos-spm"}"# |
52 | | - ), "was:\n\(content)") |
53 | | - }) |
54 | 58 | } |
55 | 59 | } |
56 | 60 |
|
57 | | - func test_versions_added() async throws { |
58 | | - // setup |
59 | | - let initialAddedBranch = try XCTUnwrap( |
60 | | - AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .branch)) |
61 | | - ) |
62 | | - let initialAddedTag = try XCTUnwrap( |
63 | | - AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .tag)) |
64 | | - ) |
65 | | - let initialDeletedBranch = try XCTUnwrap( |
66 | | - AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .branch)) |
67 | | - ) |
68 | | - let initialDeletedTag = try XCTUnwrap( |
69 | | - AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .tag)) |
70 | | - ) |
71 | | - let pkg = try await savePackage(on: app.db, "1") |
72 | | - let new = [ |
73 | | - try Version(package: pkg, reference: .branch("main")), |
74 | | - try Version(package: pkg, reference: .tag(1, 2, 3)), |
75 | | - try Version(package: pkg, reference: .tag(2, 0, 0)), |
76 | | - ] |
77 | | - let del = [ |
78 | | - try Version(package: pkg, reference: .branch("main")), |
79 | | - try Version(package: pkg, reference: .tag(1, 0, 0)), |
80 | | - ] |
81 | | - try await del.save(on: app.db) |
82 | | - |
83 | | - // MUT |
84 | | - try await Analyze.applyVersionDelta(on: app.db, |
85 | | - delta: .init(toAdd: new, toDelete: del)) |
86 | | - |
87 | | - // validation |
88 | | - XCTAssertEqual( |
89 | | - AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .branch)), |
90 | | - initialAddedBranch + 1 |
91 | | - ) |
92 | | - XCTAssertEqual( |
93 | | - AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .tag)), |
94 | | - initialAddedTag + 2 |
95 | | - ) |
96 | | - XCTAssertEqual( |
97 | | - AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .branch)), |
98 | | - initialDeletedBranch + 1 |
99 | | - ) |
100 | | - XCTAssertEqual( |
101 | | - AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .tag)), |
102 | | - initialDeletedTag + 1 |
103 | | - ) |
| 61 | + @Test func versions_added() async throws { |
| 62 | + try await withApp { app in |
| 63 | + // setup |
| 64 | + let initialAddedBranch = try #require( |
| 65 | + AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .branch)) |
| 66 | + ) |
| 67 | + let initialAddedTag = try #require( |
| 68 | + AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .tag)) |
| 69 | + ) |
| 70 | + let initialDeletedBranch = try #require( |
| 71 | + AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .branch)) |
| 72 | + ) |
| 73 | + let initialDeletedTag = try #require( |
| 74 | + AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .tag)) |
| 75 | + ) |
| 76 | + let pkg = try await savePackage(on: app.db, "1") |
| 77 | + let new = [ |
| 78 | + try Version(package: pkg, reference: .branch("main")), |
| 79 | + try Version(package: pkg, reference: .tag(1, 2, 3)), |
| 80 | + try Version(package: pkg, reference: .tag(2, 0, 0)), |
| 81 | + ] |
| 82 | + let del = [ |
| 83 | + try Version(package: pkg, reference: .branch("main")), |
| 84 | + try Version(package: pkg, reference: .tag(1, 0, 0)), |
| 85 | + ] |
| 86 | + try await del.save(on: app.db) |
| 87 | + |
| 88 | + // MUT |
| 89 | + try await Analyze.applyVersionDelta(on: app.db, |
| 90 | + delta: .init(toAdd: new, toDelete: del)) |
| 91 | + |
| 92 | + // validation |
| 93 | + #expect( |
| 94 | + AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .branch)) == initialAddedBranch + 1 |
| 95 | + ) |
| 96 | + #expect( |
| 97 | + AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .tag)) == initialAddedTag + 2 |
| 98 | + ) |
| 99 | + #expect( |
| 100 | + AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .branch)) == initialDeletedBranch + 1 |
| 101 | + ) |
| 102 | + #expect( |
| 103 | + AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .tag)) == initialDeletedTag + 1 |
| 104 | + ) |
| 105 | + } |
104 | 106 | } |
105 | 107 |
|
106 | | - func test_reconcileDurationSeconds() async throws { |
| 108 | + @Test func reconcileDurationSeconds() async throws { |
107 | 109 | try await withDependencies { |
108 | 110 | $0.packageListRepository.fetchPackageList = { @Sendable _ in ["1", "2", "3"].asURLs } |
109 | 111 | $0.packageListRepository.fetchPackageDenyList = { @Sendable _ in [] } |
110 | 112 | $0.packageListRepository.fetchCustomCollections = { @Sendable _ in [] } |
111 | 113 | } operation: { |
112 | | - // MUT |
113 | | - try await reconcile(client: app.client, database: app.db) |
| 114 | + try await withApp { app in |
| 115 | + // MUT |
| 116 | + try await reconcile(client: app.client, database: app.db) |
114 | 117 |
|
115 | | - // validation |
116 | | - XCTAssert((AppMetrics.reconcileDurationSeconds?.get()) ?? 0 > 0) |
| 118 | + // validation |
| 119 | + #expect((AppMetrics.reconcileDurationSeconds?.get()) ?? 0 > 0) |
| 120 | + } |
117 | 121 | } |
118 | 122 | } |
119 | 123 |
|
120 | | - func test_ingestDurationSeconds() async throws { |
121 | | - // setup |
122 | | - let pkg = try await savePackage(on: app.db, "1") |
| 124 | + @Test func ingestDurationSeconds() async throws { |
| 125 | + try await withApp { app in |
| 126 | + // setup |
| 127 | + let pkg = try await savePackage(on: app.db, "1") |
123 | 128 |
|
124 | | - // MUT |
125 | | - try await Ingestion.ingest(client: app.client, database: app.db, mode: .id(pkg.id!)) |
| 129 | + // MUT |
| 130 | + try await Ingestion.ingest(client: app.client, database: app.db, mode: .id(pkg.id!)) |
126 | 131 |
|
127 | | - // validation |
128 | | - XCTAssert((AppMetrics.ingestDurationSeconds?.get()) ?? 0 > 0) |
| 132 | + // validation |
| 133 | + #expect((AppMetrics.ingestDurationSeconds?.get()) ?? 0 > 0) |
| 134 | + } |
129 | 135 | } |
130 | 136 |
|
131 | | - func test_analyzeDurationSeconds() async throws { |
| 137 | + @Test func analyzeDurationSeconds() async throws { |
132 | 138 | try await withDependencies { |
133 | 139 | $0.fileManager.fileExists = { @Sendable _ in true } |
134 | 140 | } operation: { |
135 | | - // setup |
136 | | - let pkg = try await savePackage(on: app.db, "1") |
| 141 | + try await withApp { app in |
| 142 | + // setup |
| 143 | + let pkg = try await savePackage(on: app.db, "1") |
137 | 144 |
|
138 | | - // MUT |
139 | | - try await Analyze.analyze(client: app.client, database: app.db, mode: .id(pkg.id!)) |
| 145 | + // MUT |
| 146 | + try await Analyze.analyze(client: app.client, database: app.db, mode: .id(pkg.id!)) |
140 | 147 |
|
141 | | - // validation |
142 | | - XCTAssert((AppMetrics.analyzeDurationSeconds?.get()) ?? 0 > 0) |
| 148 | + // validation |
| 149 | + #expect((AppMetrics.analyzeDurationSeconds?.get()) ?? 0 > 0) |
| 150 | + } |
143 | 151 | } |
144 | 152 | } |
145 | 153 |
|
146 | | - func test_triggerBuildsDurationSeconds() async throws { |
| 154 | + @Test func triggerBuildsDurationSeconds() async throws { |
147 | 155 | try await withDependencies { |
148 | 156 | $0.environment.allowBuildTriggers = { true } |
149 | 157 | } operation: { |
150 | | - // setup |
151 | | - let pkg = try await savePackage(on: app.db, "1") |
152 | | - |
153 | | - // MUT |
154 | | - try await triggerBuilds(on: app.db, mode: .packageId(pkg.id!, force: true)) |
155 | | - |
156 | | - // validation |
157 | | - XCTAssert((AppMetrics.buildTriggerDurationSeconds?.get()) ?? 0 > 0) |
158 | | - print(AppMetrics.buildTriggerDurationSeconds!.get()) |
| 158 | + try await withApp { app in |
| 159 | + // setup |
| 160 | + let pkg = try await savePackage(on: app.db, "1") |
| 161 | + |
| 162 | + // MUT |
| 163 | + try await triggerBuilds(on: app.db, mode: .packageId(pkg.id!, force: true)) |
| 164 | + |
| 165 | + // validation |
| 166 | + #expect((AppMetrics.buildTriggerDurationSeconds?.get()) ?? 0 > 0) |
| 167 | + print(AppMetrics.buildTriggerDurationSeconds!.get()) |
| 168 | + } |
159 | 169 | } |
160 | 170 | } |
161 | 171 |
|
|
0 commit comments