Skip to content

Commit 534125b

Browse files
Added tests for durations and expirations
1 parent 5b447ff commit 534125b

File tree

2 files changed

+123
-18
lines changed

2 files changed

+123
-18
lines changed

Tests/WebPushTests/VAPIDConfigurationTests.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,54 @@ struct VAPIDConfigurationTests {
261261
)
262262
}
263263
}
264+
265+
@Suite
266+
struct Duration {
267+
@Test func makingDurations() {
268+
#expect(VAPID.Configuration.Duration.zero.seconds == 0)
269+
270+
#expect(VAPID.Configuration.Duration(seconds: 15).seconds == 15)
271+
#expect(VAPID.Configuration.Duration(seconds: -15).seconds == -15)
272+
273+
#expect((15 as VAPID.Configuration.Duration).seconds == 15)
274+
#expect((-15 as VAPID.Configuration.Duration).seconds == -15)
275+
276+
#expect(VAPID.Configuration.Duration.seconds(15).seconds == 15)
277+
#expect(VAPID.Configuration.Duration.seconds(-15).seconds == -15)
278+
279+
#expect(VAPID.Configuration.Duration.minutes(15).seconds == 900)
280+
#expect(VAPID.Configuration.Duration.minutes(-15).seconds == -900)
281+
282+
#expect(VAPID.Configuration.Duration.hours(15).seconds == 54_000)
283+
#expect(VAPID.Configuration.Duration.hours(-15).seconds == -54_000)
284+
285+
#expect(VAPID.Configuration.Duration.days(15).seconds == 1_296_000)
286+
#expect(VAPID.Configuration.Duration.days(-15).seconds == -1_296_000)
287+
}
288+
289+
@Test func arithmatic() {
290+
let base: VAPID.Configuration.Duration = 15
291+
#expect((base + 15).seconds == 30)
292+
#expect((base - 15).seconds == 0)
293+
294+
#expect((base - .seconds(30)) == -15)
295+
#expect((base + .minutes(2)) == 135)
296+
#expect((base + .minutes(2) + .hours(1)) == 3_735)
297+
#expect((base + .minutes(2) + .hours(1) + .days(2)) == 176_535)
298+
#expect((base + .seconds(45) + .minutes(59)) == .hours(1))
299+
}
300+
301+
@Test func comparison() {
302+
#expect(VAPID.Configuration.Duration.seconds(75) < VAPID.Configuration.Duration.minutes(2))
303+
#expect(VAPID.Configuration.Duration.seconds(175) > VAPID.Configuration.Duration.minutes(2))
304+
}
305+
306+
@Test func coding() throws {
307+
#expect(String(decoding: try JSONEncoder().encode(VAPID.Configuration.Duration(60)), as: UTF8.self) == "60")
308+
309+
#expect(try JSONDecoder().decode(VAPID.Configuration.Duration.self, from: Data("60".utf8)) == .minutes(1))
310+
}
311+
}
264312
}
265313

266314
@Suite("Contact Information Coding")

Tests/WebPushTests/WebPushTests.swift

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,88 @@
66
// Copyright © 2024 Mochi Development, Inc. All rights reserved.
77
//
88

9+
import Foundation
910
import Logging
1011
import ServiceLifecycle
1112
import Testing
1213
@testable import WebPush
1314

14-
@Test func webPushManagerInitializesOnItsOwn() async throws {
15-
let manager = WebPushManager(vapidConfiguration: .makeTesting())
16-
await withThrowingTaskGroup(of: Void.self) { group in
17-
group.addTask {
18-
try await manager.run()
15+
@Suite("WebPush Manager")
16+
struct WebPushManagerTests {
17+
@Test func webPushManagerInitializesOnItsOwn() async throws {
18+
let manager = WebPushManager(vapidConfiguration: .makeTesting())
19+
await withThrowingTaskGroup(of: Void.self) { group in
20+
group.addTask {
21+
try await manager.run()
22+
}
23+
group.cancelAll()
1924
}
20-
group.cancelAll()
2125
}
22-
}
23-
24-
@Test func webPushManagerInitializesAsService() async throws {
25-
let logger = Logger(label: "ServiceLogger", factory: { PrintLogHandler(label: $0, metadataProvider: $1) })
26-
let manager = WebPushManager(
27-
vapidConfiguration: .makeTesting(),
28-
logger: logger
29-
)
30-
await withThrowingTaskGroup(of: Void.self) { group in
31-
group.addTask {
32-
try await ServiceGroup(services: [manager], logger: logger).run()
26+
27+
@Test func webPushManagerInitializesAsService() async throws {
28+
let logger = Logger(label: "ServiceLogger", factory: { PrintLogHandler(label: $0, metadataProvider: $1) })
29+
let manager = WebPushManager(
30+
vapidConfiguration: .makeTesting(),
31+
logger: logger
32+
)
33+
await withThrowingTaskGroup(of: Void.self) { group in
34+
group.addTask {
35+
try await ServiceGroup(services: [manager], logger: logger).run()
36+
}
37+
group.cancelAll()
38+
}
39+
}
40+
41+
@Suite
42+
struct Expiration {
43+
@Test func stableConstants() {
44+
#expect(WebPushManager.Expiration.dropIfUndeliverable == 0)
45+
#expect(WebPushManager.Expiration.recommendedMaximum == 2_592_000)
46+
}
47+
48+
@Test func makingExpirations() {
49+
#expect(WebPushManager.Expiration.zero.seconds == 0)
50+
51+
#expect(WebPushManager.Expiration(seconds: 15).seconds == 15)
52+
#expect(WebPushManager.Expiration(seconds: -15).seconds == -15)
53+
54+
#expect((15 as WebPushManager.Expiration).seconds == 15)
55+
#expect((-15 as WebPushManager.Expiration).seconds == -15)
56+
57+
#expect(WebPushManager.Expiration.seconds(15).seconds == 15)
58+
#expect(WebPushManager.Expiration.seconds(-15).seconds == -15)
59+
60+
#expect(WebPushManager.Expiration.minutes(15).seconds == 900)
61+
#expect(WebPushManager.Expiration.minutes(-15).seconds == -900)
62+
63+
#expect(WebPushManager.Expiration.hours(15).seconds == 54_000)
64+
#expect(WebPushManager.Expiration.hours(-15).seconds == -54_000)
65+
66+
#expect(WebPushManager.Expiration.days(15).seconds == 1_296_000)
67+
#expect(WebPushManager.Expiration.days(-15).seconds == -1_296_000)
68+
}
69+
70+
@Test func arithmatic() {
71+
let base: WebPushManager.Expiration = 15
72+
#expect((base + 15).seconds == 30)
73+
#expect((base - 15).seconds == 0)
74+
75+
#expect((base - .seconds(30)) == -15)
76+
#expect((base + .minutes(2)) == 135)
77+
#expect((base + .minutes(2) + .hours(1)) == 3_735)
78+
#expect((base + .minutes(2) + .hours(1) + .days(2)) == 176_535)
79+
#expect((base + .seconds(45) + .minutes(59)) == .hours(1))
80+
}
81+
82+
@Test func comparison() {
83+
#expect(WebPushManager.Expiration.seconds(75) < WebPushManager.Expiration.minutes(2))
84+
#expect(WebPushManager.Expiration.seconds(175) > WebPushManager.Expiration.minutes(2))
85+
}
86+
87+
@Test func coding() throws {
88+
#expect(String(decoding: try JSONEncoder().encode(WebPushManager.Expiration(60)), as: UTF8.self) == "60")
89+
90+
#expect(try JSONDecoder().decode(WebPushManager.Expiration.self, from: Data("60".utf8)) == .minutes(1))
3391
}
34-
group.cancelAll()
3592
}
3693
}

0 commit comments

Comments
 (0)