Tests/Unit/NotificationNavigationBrokerTests.swift (3511B)
1import Foundation
2import Testing
3
4@testable import Crossmate
5
6@Suite("Notification navigation broker", .serialized)
7@MainActor
8struct NotificationNavigationBrokerTests {
9 @Test("Game opens are delivered to the game handler")
10 func gameOpenDelivered() {
11 let broker = NotificationNavigationBroker.shared
12 broker.onOpenGame = nil
13 broker.onOpenGameList = nil
14 broker.onOpenInviteInGameList = nil
15 let gameID = UUID()
16 var openedGameID: UUID?
17 var openedList = false
18
19 broker.onOpenGame = { openedGameID = $0 }
20 broker.onOpenGameList = { openedList = true }
21 broker.openGame(gameID)
22
23 #expect(openedGameID == gameID)
24 #expect(!openedList)
25 }
26
27 @Test("Game opens are buffered until the game handler is installed")
28 func gameOpenBuffered() {
29 let broker = NotificationNavigationBroker.shared
30 broker.onOpenGame = nil
31 broker.onOpenGameList = nil
32 broker.onOpenInviteInGameList = nil
33 let gameID = UUID()
34 var openedGameIDs: [UUID] = []
35
36 broker.openGame(gameID)
37 #expect(openedGameIDs.isEmpty)
38
39 broker.onOpenGame = { openedGameIDs.append($0) }
40
41 #expect(openedGameIDs == [gameID])
42 }
43
44 @Test("Game List opens are delivered to the list handler")
45 func gameListOpenDelivered() {
46 let broker = NotificationNavigationBroker.shared
47 broker.onOpenGame = nil
48 broker.onOpenGameList = nil
49 broker.onOpenInviteInGameList = nil
50 var openedGameID: UUID?
51 var openedList = false
52
53 broker.onOpenGame = { openedGameID = $0 }
54 broker.onOpenGameList = { openedList = true }
55 broker.openGameList()
56
57 #expect(openedGameID == nil)
58 #expect(openedList)
59 }
60
61 @Test("Game List opens are buffered until the list handler is installed")
62 func gameListOpenBuffered() {
63 let broker = NotificationNavigationBroker.shared
64 broker.onOpenGame = nil
65 broker.onOpenGameList = nil
66 broker.onOpenInviteInGameList = nil
67 var openedListCount = 0
68
69 broker.openGameList()
70 #expect(openedListCount == 0)
71
72 broker.onOpenGameList = { openedListCount += 1 }
73
74 #expect(openedListCount == 1)
75 }
76
77 @Test("Invite Game List opens are delivered to the invite handler")
78 func inviteGameListOpenDelivered() {
79 let broker = NotificationNavigationBroker.shared
80 broker.onOpenGame = nil
81 broker.onOpenGameList = nil
82 broker.onOpenInviteInGameList = nil
83 let gameID = UUID()
84 var openedInviteGameID: UUID?
85 var openedList = false
86
87 broker.onOpenGameList = { openedList = true }
88 broker.onOpenInviteInGameList = { openedInviteGameID = $0 }
89 broker.openGameList(inviteGameID: gameID)
90
91 #expect(openedInviteGameID == gameID)
92 #expect(!openedList)
93 }
94
95 @Test("Invite Game List opens are buffered until the invite handler is installed")
96 func inviteGameListOpenBuffered() {
97 let broker = NotificationNavigationBroker.shared
98 broker.onOpenGame = nil
99 broker.onOpenGameList = nil
100 broker.onOpenInviteInGameList = nil
101 let gameID = UUID()
102 var openedInviteGameIDs: [UUID] = []
103
104 broker.openGameList(inviteGameID: gameID)
105 #expect(openedInviteGameIDs.isEmpty)
106
107 broker.onOpenInviteInGameList = { openedInviteGameIDs.append($0) }
108
109 #expect(openedInviteGameIDs == [gameID])
110 }
111}