@@ -91,130 +91,122 @@ import XCTest
9191 await client. connect ( )
9292 }
9393
94- // func testBehavior() async throws {
95- // let channel = sut.channel("public:messages")
96- // var subscriptions: Set<ObservationToken> = []
97- //
98- // channel.onPostgresChange(InsertAction.self, table: "messages") { _ in
99- // }
100- // .store(in: &subscriptions)
101- //
102- // channel.onPostgresChange(UpdateAction.self, table: "messages") { _ in
103- // }
104- // .store(in: &subscriptions)
105- //
106- // channel.onPostgresChange(DeleteAction.self, table: "messages") { _ in
107- // }
108- // .store(in: &subscriptions)
109- //
110- // let socketStatuses = LockIsolated([RealtimeClientStatus]())
111- //
112- // sut.onStatusChange { status in
113- // socketStatuses.withValue { $0.append(status) }
114- // }
115- // .store(in: &subscriptions)
116- //
117- // // Set up server to respond to heartbeats
118- // server.onEvent = { @Sendable [server] event in
119- // guard let msg = event.realtimeMessage else { return }
120- //
121- // if msg.event == "heartbeat" {
122- // server?.send(
123- // RealtimeMessageV2(
124- // joinRef: msg.joinRef,
125- // ref: msg.ref,
126- // topic: "phoenix",
127- // event: "phx_reply",
128- // payload: ["response": [:]]
129- // )
130- // )
131- // }
132- // }
133- //
134- // await waitUntil {
135- // socketStatuses.value.count >= 3
136- // }
137- //
138- // XCTAssertEqual(
139- // Array(socketStatuses.value.prefix(3)),
140- // [.disconnected, .connecting, .connected]
141- // )
142- //
143- // let messageTask = sut.mutableState.messageTask
144- // XCTAssertNotNil(messageTask)
145- //
146- // let heartbeatTask = sut.mutableState.heartbeatTask
147- // XCTAssertNotNil(heartbeatTask)
148- //
149- // let channelStatuses = LockIsolated([RealtimeChannelStatus]())
150- // channel.onStatusChange { status in
151- // channelStatuses.withValue {
152- // $0.append(status)
153- // }
154- // }
155- // .store(in: &subscriptions)
156- //
157- // let subscribeTask = Task {
158- // try await channel.subscribeWithError()
159- // }
160- // await Task.yield()
161- // server.send(.messagesSubscribed)
162- //
163- // // Wait until it subscribes to assert WS events
164- // do {
165- // try await subscribeTask.value
166- // } catch {
167- // XCTFail("Expected .subscribed but got error: \(error)")
168- // }
169- // XCTAssertEqual(channelStatuses.value, [.unsubscribed, .subscribing, .subscribed])
170- //
171- // assertInlineSnapshot(of: client.sentEvents.map(\.json), as: .json) {
172- // #"""
173- // [
174- // {
175- // "text" : {
176- // "event" : "phx_join",
177- // "join_ref" : "1",
178- // "payload" : {
179- // "access_token" : "custom.access.token",
180- // "config" : {
181- // "broadcast" : {
182- // "ack" : false,
183- // "self" : false
184- // },
185- // "postgres_changes" : [
186- // {
187- // "event" : "INSERT",
188- // "schema" : "public",
189- // "table" : "messages"
190- // },
191- // {
192- // "event" : "UPDATE",
193- // "schema" : "public",
194- // "table" : "messages"
195- // },
196- // {
197- // "event" : "DELETE",
198- // "schema" : "public",
199- // "table" : "messages"
200- // }
201- // ],
202- // "presence" : {
203- // "enabled" : false,
204- // "key" : ""
205- // },
206- // "private" : false
207- // },
208- // "version" : "realtime-swift\/0.0.0"
209- // },
210- // "ref" : "1",
211- // "topic" : "realtime:public:messages"
212- // }
213- // }
214- // ]
215- // """#
216- // }
217- // }
94+ func testBehavior( ) async throws {
95+ let channel = sut. channel ( " public:messages " )
96+ var subscriptions : Set < ObservationToken > = [ ]
97+
98+ channel. onPostgresChange ( InsertAction . self, table: " messages " ) { _ in
99+ }
100+ . store ( in: & subscriptions)
101+
102+ channel. onPostgresChange ( UpdateAction . self, table: " messages " ) { _ in
103+ }
104+ . store ( in: & subscriptions)
105+
106+ channel. onPostgresChange ( DeleteAction . self, table: " messages " ) { _ in
107+ }
108+ . store ( in: & subscriptions)
109+
110+ let socketStatuses = LockIsolated ( [ RealtimeClientStatus] ( ) )
111+
112+ sut. onStatusChange { status in
113+ socketStatuses. withValue { $0. append ( status) }
114+ }
115+ . store ( in: & subscriptions)
116+
117+ // Set up server to respond to heartbeats
118+ server. onEvent = { @Sendable [ server] event in
119+ guard let msg = event. realtimeMessage else { return }
120+
121+ if msg. event == " heartbeat " {
122+ server? . send (
123+ RealtimeMessageV2 (
124+ joinRef: msg. joinRef,
125+ ref: msg. ref,
126+ topic: " phoenix " ,
127+ event: " phx_reply " ,
128+ payload: [ " response " : [ : ] ]
129+ )
130+ )
131+ } else if msg. event == " phx_join " {
132+ server? . send ( . messagesSubscribed)
133+ }
134+ }
135+
136+ let channelStatuses = LockIsolated ( [ RealtimeChannelStatus] ( ) )
137+ channel. onStatusChange { status in
138+ channelStatuses. withValue {
139+ $0. append ( status)
140+ }
141+ }
142+ . store ( in: & subscriptions)
143+
144+ // Wait until it subscribes to assert WS events
145+ do {
146+ try await channel. subscribeWithError ( )
147+ } catch {
148+ XCTFail ( " Expected .subscribed but got error: \( error) " )
149+ }
150+ XCTAssertEqual ( channelStatuses. value, [ . unsubscribed, . subscribing, . subscribed] )
151+
152+ XCTAssertEqual (
153+ Array ( socketStatuses. value. prefix ( 3 ) ) ,
154+ [ . disconnected, . connecting, . connected]
155+ )
156+
157+ let messageTask = sut. mutableState. messageTask
158+ XCTAssertNotNil ( messageTask)
159+
160+ let heartbeatTask = sut. mutableState. heartbeatTask
161+ XCTAssertNotNil ( heartbeatTask)
162+
163+ assertInlineSnapshot ( of: client. sentEvents. map ( \. json) , as: . json) {
164+ #"""
165+ [
166+ {
167+ "text" : {
168+ "event" : "phx_join",
169+ "join_ref" : "1",
170+ "payload" : {
171+ "access_token" : "custom.access.token",
172+ "config" : {
173+ "broadcast" : {
174+ "ack" : false,
175+ "self" : false
176+ },
177+ "postgres_changes" : [
178+ {
179+ "event" : "INSERT",
180+ "schema" : "public",
181+ "table" : "messages"
182+ },
183+ {
184+ "event" : "UPDATE",
185+ "schema" : "public",
186+ "table" : "messages"
187+ },
188+ {
189+ "event" : "DELETE",
190+ "schema" : "public",
191+ "table" : "messages"
192+ }
193+ ],
194+ "presence" : {
195+ "enabled" : false,
196+ "key" : ""
197+ },
198+ "private" : false
199+ },
200+ "version" : "realtime-swift\/0.0.0"
201+ },
202+ "ref" : "1",
203+ "topic" : "realtime:public:messages"
204+ }
205+ }
206+ ]
207+ """#
208+ }
209+ }
218210
219211 func testSubscribeTimeout( ) async throws {
220212 let channel = sut. channel ( " public:messages " )
0 commit comments