11import assert from 'assert'
22import sinon from 'sinon'
3- import crypto from 'crypto'
3+ import crypto , { randomUUID } from 'crypto'
44import { describe , beforeEach , afterEach , test , vi } from 'vitest'
55
66import RealtimeClient from '../src/RealtimeClient'
@@ -169,7 +169,7 @@ describe('constructor', () => {
169169
170170describe ( 'subscribe' , ( ) => {
171171 beforeEach ( ( ) => {
172- channel = socket . channel ( 'topic' , { one : 'two' } )
172+ channel = socket . channel ( 'topic' )
173173 } )
174174
175175 afterEach ( ( ) => {
@@ -208,6 +208,7 @@ describe('subscribe', () => {
208208
209209 assert . equal ( channel . state , CHANNEL_STATES . joining )
210210 } )
211+
211212 test ( 'updates join push payload access token' , ( ) => {
212213 socket . accessTokenValue = 'token123'
213214
@@ -221,10 +222,30 @@ describe('subscribe', () => {
221222 postgres_changes : [ ] ,
222223 private : false ,
223224 } ,
224- one : 'two' ,
225225 } )
226226 } )
227227
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+
228249 test ( 'triggers socket push with default channel params' , ( ) => {
229250 sinon . stub ( socket , '_makeRef' ) . callsFake ( ( ) => defaultRef )
230251 const spy = sinon . spy ( socket , 'push' )
@@ -247,7 +268,6 @@ describe('subscribe', () => {
247268 postgres_changes : [ ] ,
248269 private : false ,
249270 } ,
250- one : 'two' ,
251271 } ,
252272 ref : defaultRef ,
253273 join_ref : defaultRef ,
@@ -325,7 +345,6 @@ describe('subscribe', () => {
325345 ] ,
326346 private : false ,
327347 } ,
328- one : 'two' ,
329348 } ,
330349 ref : defaultRef ,
331350 join_ref : defaultRef ,
0 commit comments