Skip to content

Commit 1d2dc6e

Browse files
committed
Replace Current.mastodonPost(...) with environment.mastodonPost(...)
1 parent 8dfcdc6 commit 1d2dc6e

File tree

9 files changed

+40
-32
lines changed

9 files changed

+40
-32
lines changed

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ struct AppEnvironment: Sendable {
5656
var httpClient: @Sendable () -> Client
5757
var loadSPIManifest: @Sendable (String) -> SPIManifest.Manifest?
5858
var logger: @Sendable () -> Logger
59-
// TODO: remove
60-
var mastodonPost: @Sendable (_ client: Client, _ post: String) async throws -> Void
6159
var metricsPushGatewayUrl: @Sendable () -> String?
6260
var plausibleBackendReportingSiteID: @Sendable () -> String?
6361
var postPlausibleEvent: @Sendable (Client, Plausible.Event.Kind, Plausible.Path, User?) async throws -> Void
@@ -180,7 +178,6 @@ extension AppEnvironment {
180178
httpClient: { httpClient },
181179
loadSPIManifest: { path in SPIManifest.Manifest.load(in: path) },
182180
logger: { logger },
183-
mastodonPost: { client, message in try await Mastodon.post(client: client, message: message) },
184181
metricsPushGatewayUrl: { Environment.get("METRICS_PUSHGATEWAY_URL") },
185182
plausibleBackendReportingSiteID: { Environment.get("PLAUSIBLE_BACKEND_REPORTING_SITE_ID") },
186183
postPlausibleEvent: { client, kind, path, user in try await Plausible.postEvent(client: client, kind: kind, path: path, user: user) },

Sources/App/Core/Dependencies/EnvironmentClient.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct EnvironmentClient {
3030
// .development and does not raise an error when not injected.
3131
var current: @Sendable () -> Environment = { .development }
3232
var mastodonCredentials: @Sendable () -> Mastodon.Credentials?
33+
var mastodonPost: @Sendable (_ client: Client, _ post: String) async throws -> Void
3334
}
3435

3536

@@ -48,7 +49,8 @@ extension EnvironmentClient: DependencyKey {
4849
mastodonCredentials: {
4950
Environment.get("MASTODON_ACCESS_TOKEN")
5051
.map(Mastodon.Credentials.init(accessToken:))
51-
}
52+
},
53+
mastodonPost: { client, message in try await Mastodon.post(client: client, message: message) }
5254
)
5355
}
5456
}

Sources/App/Core/Social.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ enum Social {
105105
throw Error.invalidMessage
106106
}
107107
// Ignore errors from here for now to keep concurrency simpler
108-
async let _ = try? await Current.mastodonPost(client, message)
108+
async let _ = try? await environment.mastodonPost(client, message)
109109
}
110110

111111
static func postToFirehose(client: Client,

Tests/AppTests/AnalyzeErrorTests.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,15 @@ final class AnalyzeErrorTests: AppTestCase {
9898
}
9999

100100
Current.shell.run = Self.defaultShellRun
101-
102-
Current.mastodonPost = { [socialPosts = self.socialPosts] _, message in
103-
socialPosts.withValue { $0.append(message) }
104-
}
105101
}
106102

107103
func test_analyze_refreshCheckout_failed() async throws {
108104
try await withDependencies {
109105
$0.date.now = .t0
110106
$0.environment.allowSocialPosts = { true }
107+
$0.environment.mastodonPost = { @Sendable [socialPosts = self.socialPosts] _, message in
108+
socialPosts.withValue { $0.append(message) }
109+
}
111110
} operation: {
112111
Current.shell.run = { @Sendable cmd, path in
113112
switch cmd {
@@ -141,6 +140,9 @@ final class AnalyzeErrorTests: AppTestCase {
141140
try await withDependencies {
142141
$0.date.now = .t0
143142
$0.environment.allowSocialPosts = { true }
143+
$0.environment.mastodonPost = { @Sendable [socialPosts = self.socialPosts] _, message in
144+
socialPosts.withValue { $0.append(message) }
145+
}
144146
} operation: {
145147
// setup
146148
let pkg = try await Package.find(badPackageID, on: app.db).unwrap()
@@ -169,6 +171,9 @@ final class AnalyzeErrorTests: AppTestCase {
169171
try await withDependencies {
170172
$0.date.now = .t0
171173
$0.environment.allowSocialPosts = { true }
174+
$0.environment.mastodonPost = { @Sendable [socialPosts = self.socialPosts] _, message in
175+
socialPosts.withValue { $0.append(message) }
176+
}
172177
} operation: {
173178
// setup
174179
Current.shell.run = { @Sendable cmd, path in
@@ -200,6 +205,9 @@ final class AnalyzeErrorTests: AppTestCase {
200205
try await withDependencies {
201206
$0.date.now = .t0
202207
$0.environment.allowSocialPosts = { true }
208+
$0.environment.mastodonPost = { @Sendable [socialPosts = self.socialPosts] _, message in
209+
socialPosts.withValue { $0.append(message) }
210+
}
203211
} operation: {
204212
// setup
205213
Current.fileManager.fileExists = { @Sendable path in

Tests/AppTests/AnalyzerTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class AnalyzerTests: AppTestCase {
3636
try await withDependencies {
3737
$0.date.now = .now
3838
$0.environment.allowSocialPosts = { true }
39+
$0.environment.mastodonPost = { @Sendable _, _ in }
3940
} operation: {
4041
// setup
4142
let urls = ["https://github.com/foo/1", "https://github.com/foo/2"]
@@ -218,6 +219,7 @@ class AnalyzerTests: AppTestCase {
218219
try await withDependencies {
219220
$0.date.now = .now
220221
$0.environment.allowSocialPosts = { true }
222+
$0.environment.mastodonPost = { @Sendable _, _ in }
221223
} operation: {
222224
// setup
223225
let pkgId = UUID()

Tests/AppTests/MastodonTests.swift

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,21 @@ import XCTVapor
2222
final class MastodonTests: AppTestCase {
2323

2424
func test_endToEnd() async throws {
25+
let message = QueueIsolated<String?>(nil)
2526
try await withDependencies {
2627
$0.environment.allowSocialPosts = { true }
27-
} operation: {
28-
// setup
29-
let message = QueueIsolated<String?>(nil)
30-
Current.mastodonPost = { _, msg in
28+
$0.environment.mastodonPost = { @Sendable _, msg in
3129
if message.value == nil {
3230
message.setValue(msg)
3331
} else {
3432
XCTFail("message must only be set once")
3533
}
3634
}
37-
35+
} operation: {
36+
// setup
3837
let url = "https://github.com/foo/bar"
3938
Current.fetchMetadata = { _, owner, repository in .mock(owner: owner, repository: repository) }
40-
39+
4140
Current.git.commitCount = { @Sendable _ in 12 }
4241
Current.git.firstCommitDate = { @Sendable _ in .t0 }
4342
Current.git.lastCommitDate = { @Sendable _ in .t2 }
@@ -50,14 +49,14 @@ final class MastodonTests: AppTestCase {
5049
2\tPerson 2
5150
"""
5251
}
53-
52+
5453
Current.shell.run = { @Sendable cmd, path in
5554
if cmd.description.hasSuffix("swift package dump-package") {
5655
return #"{ "name": "Mock", "products": [], "targets": [] }"#
5756
}
5857
return ""
5958
}
60-
59+
6160
try await withDependencies {
6261
$0.date.now = .now
6362
$0.packageListRepository.fetchPackageList = { @Sendable _ in [url.url] }
@@ -68,39 +67,39 @@ final class MastodonTests: AppTestCase {
6867
// run first two processing steps
6968
try await reconcile(client: app.client, database: app.db)
7069
try await ingest(client: app.client, database: app.db, mode: .limit(10))
71-
70+
7271
// MUT - analyze, triggering the post
7372
try await Analyze.analyze(client: app.client,
7473
database: app.db,
7574
mode: .limit(10))
76-
75+
7776
do {
7877
let msg = try XCTUnwrap(message.value)
7978
XCTAssertTrue(msg.hasPrefix("📦 foo just added a new package, Mock"), "was \(msg)")
8079
}
81-
80+
8281
// run stages again to simulate the cycle...
8382
message.setValue(nil)
8483
try await reconcile(client: app.client, database: app.db)
8584
}
86-
85+
8786
try await withDependencies {
8887
$0.date.now = .now.addingTimeInterval(Constants.reIngestionDeadtime)
8988
} operation: {
9089
try await ingest(client: app.client, database: app.db, mode: .limit(10))
91-
90+
9291
// MUT - analyze, triggering posts if any
9392
try await Analyze.analyze(client: app.client,
9493
database: app.db,
9594
mode: .limit(10))
9695
}
97-
96+
9897
// validate - there are no new posts to send
9998
XCTAssertNil(message.value)
100-
99+
101100
// Now simulate receiving a package update: version 2.0.0
102101
Current.git.getTags = { @Sendable _ in [.tag(2, 0, 0)] }
103-
102+
104103
try await withDependencies {
105104
// fast forward our clock by the deadtime interval again (*2) and re-ingest
106105
$0.date.now = .now.addingTimeInterval(Constants.reIngestionDeadtime * 2)
@@ -111,7 +110,7 @@ final class MastodonTests: AppTestCase {
111110
database: app.db,
112111
mode: .limit(10))
113112
}
114-
113+
115114
// validate
116115
let msg = try XCTUnwrap(message.value)
117116
XCTAssertTrue(msg.hasPrefix("⬆️ foo just released Mock v2.0.0"), "was: \(msg)")

Tests/AppTests/Mocks/AppEnvironment+mock.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ extension AppEnvironment {
5555
httpClient: { httpClient },
5656
loadSPIManifest: { _ in nil },
5757
logger: { logger },
58-
mastodonPost: { _, _ in },
5958
metricsPushGatewayUrl: { "http://pushgateway:9091" },
6059
plausibleBackendReportingSiteID: { nil },
6160
postPlausibleEvent: { _, _, _, _ in },

Tests/AppTests/ReAnalyzeVersionsTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ReAnalyzeVersionsTests: AppTestCase {
2929
try await withDependencies {
3030
$0.date.now = .t0
3131
$0.environment.allowSocialPosts = { true }
32+
$0.environment.mastodonPost = { @Sendable _, _ in }
3233
} operation: {
3334
// setup
3435
// - package dump does not include toolsVersion, targets to simulate an "old version"

Tests/AppTests/SocialTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ class SocialTests: AppTestCase {
193193
let versions = try await Analyze.updateLatestVersions(on: app.db, package: jpr)
194194

195195
let posted: NIOLockedValueBox<Int> = .init(0)
196-
Current.mastodonPost = { _, _ in posted.withLockedValue { $0 += 1 } }
197196

198197
try await withDependencies {
199198
$0.environment.allowSocialPosts = { true }
199+
$0.environment.mastodonPost = { @Sendable _, _ in posted.withLockedValue { $0 += 1 } }
200200
} operation: {
201201
// MUT
202202
try await Social.postToFirehose(client: app.client,
@@ -225,13 +225,13 @@ class SocialTests: AppTestCase {
225225
let versions = try await Analyze.updateLatestVersions(on: app.db, package: jpr)
226226

227227
let posted: NIOLockedValueBox<Int> = .init(0)
228-
Current.mastodonPost = { _, msg in
229-
XCTAssertTrue(msg.contains("v2.0.0"))
230-
posted.withLockedValue { $0 += 1 }
231-
}
232228

233229
try await withDependencies {
234230
$0.environment.allowSocialPosts = { true }
231+
$0.environment.mastodonPost = { @Sendable _, msg in
232+
XCTAssertTrue(msg.contains("v2.0.0"))
233+
posted.withLockedValue { $0 += 1 }
234+
}
235235
} operation: {
236236
// MUT
237237
try await Social.postToFirehose(client: app.client,

0 commit comments

Comments
 (0)