|
6 | 6 | // Copyright © 2024 Mochi Development, Inc. All rights reserved.
|
7 | 7 | //
|
8 | 8 |
|
| 9 | +import Foundation |
9 | 10 | import Logging
|
10 | 11 | import ServiceLifecycle
|
11 | 12 | import Testing
|
12 | 13 | @testable import WebPush
|
13 | 14 |
|
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() |
19 | 24 | }
|
20 |
| - group.cancelAll() |
21 | 25 | }
|
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)) |
33 | 91 | }
|
34 |
| - group.cancelAll() |
35 | 92 | }
|
36 | 93 | }
|
0 commit comments