|
| 1 | +// |
| 2 | +// NotificationTests.swift |
| 3 | +// swift-webpush |
| 4 | +// |
| 5 | +// Created by Dimitri Bouniol on 2025-03-01. |
| 6 | +// Copyright © 2024 Mochi Development, Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#if canImport(FoundationEssentials) |
| 10 | +import FoundationEssentials |
| 11 | +#else |
| 12 | +import Foundation |
| 13 | +#endif |
| 14 | +import Testing |
| 15 | +@testable import WebPush |
| 16 | + |
| 17 | +@Suite("Push Message Notification") |
| 18 | +struct NotificationTests { |
| 19 | + @Test func simpleNotificationEncodesProperly() async throws { |
| 20 | + let notification = PushMessage.Notification( |
| 21 | + destination: URL(string: "https://jiiiii.moe")!, |
| 22 | + title: "New Anime", |
| 23 | + timestamp: Date(timeIntervalSince1970: 1_000_000_000) |
| 24 | + ) |
| 25 | + |
| 26 | + let encoder = JSONEncoder() |
| 27 | + encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] |
| 28 | + |
| 29 | + let encodedString = String(decoding: try encoder.encode(notification), as: UTF8.self) |
| 30 | + #expect(encodedString == """ |
| 31 | + { |
| 32 | + "notification" : { |
| 33 | + "navigate" : "https://jiiiii.moe", |
| 34 | + "timestamp" : 1000000000000, |
| 35 | + "title" : "New Anime" |
| 36 | + }, |
| 37 | + "web_push" : 8030 |
| 38 | + } |
| 39 | + """) |
| 40 | + |
| 41 | + let decodedNotification = try JSONDecoder().decode(PushMessage.SimpleNotification.self, from: Data(encodedString.utf8)) |
| 42 | + #expect(decodedNotification == notification) |
| 43 | + } |
| 44 | + |
| 45 | + @Test func legacyNotificationEncodesProperly() async throws { |
| 46 | + let notification = PushMessage.Notification( |
| 47 | + kind: .legacy, |
| 48 | + destination: URL(string: "https://jiiiii.moe")!, |
| 49 | + title: "New Anime", |
| 50 | + timestamp: Date(timeIntervalSince1970: 1_000_000_000) |
| 51 | + ) |
| 52 | + |
| 53 | + let encoder = JSONEncoder() |
| 54 | + encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] |
| 55 | + |
| 56 | + let encodedString = String(decoding: try encoder.encode(notification), as: UTF8.self) |
| 57 | + #expect(encodedString == """ |
| 58 | + { |
| 59 | + "notification" : { |
| 60 | + "navigate" : "https://jiiiii.moe", |
| 61 | + "timestamp" : 1000000000000, |
| 62 | + "title" : "New Anime" |
| 63 | + } |
| 64 | + } |
| 65 | + """) |
| 66 | + |
| 67 | + let decodedNotification = try JSONDecoder().decode(PushMessage.SimpleNotification.self, from: Data(encodedString.utf8)) |
| 68 | + #expect(decodedNotification == notification) |
| 69 | + } |
| 70 | + |
| 71 | + @Test func completeNotificationEncodesProperly() async throws { |
| 72 | + let notification = PushMessage.Notification( |
| 73 | + kind: .declarative, |
| 74 | + destination: URL(string: "https://jiiiii.moe")!, |
| 75 | + title: "New Anime", |
| 76 | + body: "New anime is available!", |
| 77 | + image: URL(string: "https://jiiiii.moe/animeImage")!, |
| 78 | + actions: [ |
| 79 | + PushMessage.NotificationAction( |
| 80 | + id: "ok", |
| 81 | + label: "OK", |
| 82 | + destination: URL(string: "https://jiiiii.moe/ok")!, |
| 83 | + icon: URL(string: "https://jiiiii.moe/okIcon") |
| 84 | + ), |
| 85 | + PushMessage.NotificationAction( |
| 86 | + id: "cancel", |
| 87 | + label: "Cancel", |
| 88 | + destination: URL(string: "https://jiiiii.moe/cancel")!, |
| 89 | + icon: URL(string: "https://jiiiii.moe/cancelIcon") |
| 90 | + ), |
| 91 | + ], |
| 92 | + timestamp: Date(timeIntervalSince1970: 1_000_000_000), |
| 93 | + appBadgeCount: 0, |
| 94 | + isMutable: true, |
| 95 | + options: PushMessage.NotificationOptions( |
| 96 | + direction: .rightToLeft, |
| 97 | + language: "jp", |
| 98 | + tag: "new-anime", |
| 99 | + icon: URL(string: "https://jiiiii.moe/icon")!, |
| 100 | + badgeIcon: URL(string: "https://jiiiii.moe/badgeIcon")!, |
| 101 | + vibrate: [200, 100, 200], |
| 102 | + shouldRenotify: true, |
| 103 | + isSilent: true, |
| 104 | + requiresInteraction: true |
| 105 | + ) |
| 106 | + ) |
| 107 | + |
| 108 | + let encoder = JSONEncoder() |
| 109 | + encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] |
| 110 | + |
| 111 | + let encodedString = String(decoding: try encoder.encode(notification), as: UTF8.self) |
| 112 | + #expect(encodedString == """ |
| 113 | + { |
| 114 | + "app_badge" : 0, |
| 115 | + "mutable" : true, |
| 116 | + "notification" : { |
| 117 | + "actions" : [ |
| 118 | + { |
| 119 | + "action" : "ok", |
| 120 | + "icon" : "https://jiiiii.moe/okIcon", |
| 121 | + "navigate" : "https://jiiiii.moe/ok", |
| 122 | + "title" : "OK" |
| 123 | + }, |
| 124 | + { |
| 125 | + "action" : "cancel", |
| 126 | + "icon" : "https://jiiiii.moe/cancelIcon", |
| 127 | + "navigate" : "https://jiiiii.moe/cancel", |
| 128 | + "title" : "Cancel" |
| 129 | + } |
| 130 | + ], |
| 131 | + "badge" : "https://jiiiii.moe/badgeIcon", |
| 132 | + "body" : "New anime is available!", |
| 133 | + "dir" : "rtf", |
| 134 | + "icon" : "https://jiiiii.moe/icon", |
| 135 | + "image" : "https://jiiiii.moe/animeImage", |
| 136 | + "lang" : "jp", |
| 137 | + "navigate" : "https://jiiiii.moe", |
| 138 | + "renotify" : true, |
| 139 | + "require_interaction" : true, |
| 140 | + "silent" : true, |
| 141 | + "tag" : "new-anime", |
| 142 | + "timestamp" : 1000000000000, |
| 143 | + "title" : "New Anime", |
| 144 | + "vibrate" : [ |
| 145 | + 200, |
| 146 | + 100, |
| 147 | + 200 |
| 148 | + ] |
| 149 | + }, |
| 150 | + "web_push" : 8030 |
| 151 | + } |
| 152 | + """) |
| 153 | + |
| 154 | + let decodedNotification = try JSONDecoder().decode(PushMessage.SimpleNotification.self, from: Data(encodedString.utf8)) |
| 155 | + #expect(decodedNotification == notification) |
| 156 | + } |
| 157 | + |
| 158 | + @Test func customNotificationEncodesProperly() async throws { |
| 159 | + let notification = PushMessage.Notification( |
| 160 | + destination: URL(string: "https://jiiiii.moe")!, |
| 161 | + title: "New Anime", |
| 162 | + timestamp: Date(timeIntervalSince1970: 1_000_000_000), |
| 163 | + data: ["episodeID": "123"] |
| 164 | + ) |
| 165 | + |
| 166 | + let encoder = JSONEncoder() |
| 167 | + encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] |
| 168 | + |
| 169 | + let encodedString = String(decoding: try encoder.encode(notification), as: UTF8.self) |
| 170 | + #expect(encodedString == """ |
| 171 | + { |
| 172 | + "notification" : { |
| 173 | + "data" : { |
| 174 | + "episodeID" : "123" |
| 175 | + }, |
| 176 | + "navigate" : "https://jiiiii.moe", |
| 177 | + "timestamp" : 1000000000000, |
| 178 | + "title" : "New Anime" |
| 179 | + }, |
| 180 | + "web_push" : 8030 |
| 181 | + } |
| 182 | + """) |
| 183 | + |
| 184 | + let decodedNotification = try JSONDecoder().decode(type(of: notification), from: Data(encodedString.utf8)) |
| 185 | + #expect(decodedNotification == notification) |
| 186 | + } |
| 187 | +} |
0 commit comments