gitbayTests/PushPayloadTests.swift

v1.7.0
gitbay-ios/gitbayTests/PushPayloadTests.swift history · blame · raw

34 lines · 1430 bytes

 1import Foundation
 2import Testing
 3@testable import gitbay
 4
 5@Test func payloadDecodesTheAccountAndPath() throws {
 6    let userInfo: [AnyHashable: Any] = [
 7        "aps": ["alert": ["title": "krz/gitbay", "body": "cmc opened issue #12"], "badge": 3],
 8        "instance": "https://gitbay.org",
 9        "user": "cmc",
10        "path": "krz/gitbay/issues/12",
11    ]
12    let payload = try #require(PushPayload(userInfo: userInfo))
13    #expect(payload.instance == URL(string: "https://gitbay.org"))
14    #expect(payload.user == "cmc")
15    #expect(payload.path == "krz/gitbay/issues/12")
16    #expect(payload.badge == 3)
17}
18
19@Test func payloadTreatsAMissingBadgeAsZero() throws {
20    let userInfo: [AnyHashable: Any] = [
21        "aps": ["alert": ["title": "t", "body": "b"]],
22        "instance": "https://gitbay.org", "user": "cmc", "path": "a/b/issues/1",
23    ]
24    let payload = try #require(PushPayload(userInfo: userInfo))
25    #expect(payload.badge == 0)
26}
27
28// A payload from a server that predates the account fields, or one
29// mangled in transit, is dropped rather than guessed at.
30@Test func payloadRejectsAnIncompleteNotification() {
31    #expect(PushPayload(userInfo: ["aps": ["alert": "x"], "path": "a/b/issues/1"]) == nil)
32    #expect(PushPayload(userInfo: ["instance": "https://gitbay.org", "user": "cmc"]) == nil)
33    #expect(PushPayload(userInfo: ["instance": "not a url at all", "user": "cmc", "path": "a/b/issues/1"]) == nil)
34}