1
1
import assert from 'assert'
2
2
import sinon from 'sinon'
3
- import crypto from 'crypto'
3
+ import crypto , { randomUUID } from 'crypto'
4
4
import { describe , beforeEach , afterEach , test , vi } from 'vitest'
5
5
6
6
import RealtimeClient from '../src/RealtimeClient'
@@ -169,7 +169,7 @@ describe('constructor', () => {
169
169
170
170
describe ( 'subscribe' , ( ) => {
171
171
beforeEach ( ( ) => {
172
- channel = socket . channel ( 'topic' , { one : 'two' } )
172
+ channel = socket . channel ( 'topic' )
173
173
} )
174
174
175
175
afterEach ( ( ) => {
@@ -208,6 +208,7 @@ describe('subscribe', () => {
208
208
209
209
assert . equal ( channel . state , CHANNEL_STATES . joining )
210
210
} )
211
+
211
212
test ( 'updates join push payload access token' , ( ) => {
212
213
socket . accessTokenValue = 'token123'
213
214
@@ -221,10 +222,30 @@ describe('subscribe', () => {
221
222
postgres_changes : [ ] ,
222
223
private : false ,
223
224
} ,
224
- one : 'two' ,
225
225
} )
226
226
} )
227
227
228
+ test ( 'triggers setAuth when socket is not connected' , async ( ) => {
229
+ clock . restore ( ) // Use real timers for this test
230
+ let callCount = 0
231
+ const tokens = [ randomUUID ( ) , randomUUID ( ) ]
232
+ const accessToken = async ( ) => tokens [ callCount ++ ]
233
+ const testSocket = new RealtimeClient ( url , {
234
+ accessToken : accessToken ,
235
+ transport : WebSocket ,
236
+ } )
237
+ const channel = testSocket . channel ( 'topic' )
238
+
239
+ channel . subscribe ( )
240
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
241
+ assert . equal ( channel . socket . accessTokenValue , tokens [ 0 ] )
242
+
243
+ testSocket . disconnect ( )
244
+ channel . subscribe ( )
245
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
246
+ assert . equal ( channel . socket . accessTokenValue , tokens [ 1 ] )
247
+ } )
248
+
228
249
test ( 'triggers socket push with default channel params' , ( ) => {
229
250
sinon . stub ( socket , '_makeRef' ) . callsFake ( ( ) => defaultRef )
230
251
const spy = sinon . spy ( socket , 'push' )
@@ -247,7 +268,6 @@ describe('subscribe', () => {
247
268
postgres_changes : [ ] ,
248
269
private : false ,
249
270
} ,
250
- one : 'two' ,
251
271
} ,
252
272
ref : defaultRef ,
253
273
join_ref : defaultRef ,
@@ -325,7 +345,6 @@ describe('subscribe', () => {
325
345
] ,
326
346
private : false ,
327
347
} ,
328
- one : 'two' ,
329
348
} ,
330
349
ref : defaultRef ,
331
350
join_ref : defaultRef ,
0 commit comments