@@ -4,6 +4,7 @@ import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'
4
4
5
5
import { getTokenState } from '../auth/authStore'
6
6
import { getClient } from '../client/clientStore'
7
+ import { sourceFor } from '../config/sanityConfig'
7
8
import { createSanityInstance , type SanityInstance } from '../store/createSanityInstance'
8
9
import { type SanityUser } from '../users/types'
9
10
import { getUserState } from '../users/usersStore'
@@ -17,6 +18,7 @@ vi.mock('../users/usersStore')
17
18
vi . mock ( './bifurTransport' )
18
19
19
20
describe ( 'presenceStore' , ( ) => {
21
+ const source = sourceFor ( { projectId : 'test-project' , dataset : 'test-dataset' } )
20
22
let instance : SanityInstance
21
23
let mockClient : SanityClient
22
24
let mockTokenState : Subject < string | null >
@@ -78,7 +80,7 @@ describe('presenceStore', () => {
78
80
79
81
describe ( 'getPresence' , ( ) => {
80
82
it ( 'creates bifur transport with correct parameters' , ( ) => {
81
- getPresence ( instance , { } )
83
+ getPresence ( instance , { source } )
82
84
83
85
expect ( createBifurTransport ) . toHaveBeenCalledWith ( {
84
86
client : mockClient ,
@@ -88,21 +90,21 @@ describe('presenceStore', () => {
88
90
} )
89
91
90
92
it ( 'sends rollCall message on initialization' , ( ) => {
91
- getPresence ( instance , { } )
93
+ getPresence ( instance , { source } )
92
94
93
95
expect ( mockDispatchMessage ) . toHaveBeenCalledWith ( { type : 'rollCall' } )
94
96
} )
95
97
96
98
it ( 'returns empty array when no users present' , ( ) => {
97
- const source = getPresence ( instance , { } )
98
- expect ( source . getCurrent ( ) ) . toEqual ( [ ] )
99
+ const stateSource = getPresence ( instance , { source } )
100
+ expect ( stateSource . getCurrent ( ) ) . toEqual ( [ ] )
99
101
} )
100
102
101
103
it ( 'handles state events from other users' , async ( ) => {
102
- const source = getPresence ( instance , { } )
104
+ const stateSource = getPresence ( instance , { source } )
103
105
104
106
// Subscribe to initialize the store
105
- const unsubscribe = source . subscribe ( ( ) => { } )
107
+ const unsubscribe = stateSource . subscribe ( ( ) => { } )
106
108
107
109
// Wait a bit for initialization
108
110
await firstValueFrom ( of ( null ) . pipe ( delay ( 10 ) ) )
@@ -127,7 +129,7 @@ describe('presenceStore', () => {
127
129
// Wait for processing
128
130
await firstValueFrom ( of ( null ) . pipe ( delay ( 20 ) ) )
129
131
130
- const presence = source . getCurrent ( )
132
+ const presence = stateSource . getCurrent ( )
131
133
expect ( presence ) . toHaveLength ( 1 )
132
134
expect ( presence [ 0 ] . sessionId ) . toBe ( 'other-session' )
133
135
expect ( presence [ 0 ] . locations ) . toEqual ( locations )
@@ -136,8 +138,8 @@ describe('presenceStore', () => {
136
138
} )
137
139
138
140
it ( 'ignores events from own session' , async ( ) => {
139
- const source = getPresence ( instance , { } )
140
- const unsubscribe = source . subscribe ( ( ) => { } )
141
+ const stateSource = getPresence ( instance , { source } )
142
+ const unsubscribe = stateSource . subscribe ( ( ) => { } )
141
143
142
144
await firstValueFrom ( of ( null ) . pipe ( delay ( 10 ) ) )
143
145
@@ -151,15 +153,15 @@ describe('presenceStore', () => {
151
153
152
154
await firstValueFrom ( of ( null ) . pipe ( delay ( 20 ) ) )
153
155
154
- const presence = source . getCurrent ( )
156
+ const presence = stateSource . getCurrent ( )
155
157
expect ( presence ) . toHaveLength ( 0 )
156
158
157
159
unsubscribe ( )
158
160
} )
159
161
160
162
it ( 'handles disconnect events' , async ( ) => {
161
- const source = getPresence ( instance , { } )
162
- const unsubscribe = source . subscribe ( ( ) => { } )
163
+ const stateSource = getPresence ( instance , { source } )
164
+ const unsubscribe = stateSource . subscribe ( ( ) => { } )
163
165
164
166
await firstValueFrom ( of ( null ) . pipe ( delay ( 10 ) ) )
165
167
@@ -173,7 +175,7 @@ describe('presenceStore', () => {
173
175
} )
174
176
175
177
await firstValueFrom ( of ( null ) . pipe ( delay ( 20 ) ) )
176
- expect ( source . getCurrent ( ) ) . toHaveLength ( 1 )
178
+ expect ( stateSource . getCurrent ( ) ) . toHaveLength ( 1 )
177
179
178
180
// Then disconnect them
179
181
mockIncomingEvents . next ( {
@@ -184,14 +186,14 @@ describe('presenceStore', () => {
184
186
} )
185
187
186
188
await firstValueFrom ( of ( null ) . pipe ( delay ( 20 ) ) )
187
- expect ( source . getCurrent ( ) ) . toHaveLength ( 0 )
189
+ expect ( stateSource . getCurrent ( ) ) . toHaveLength ( 0 )
188
190
189
191
unsubscribe ( )
190
192
} )
191
193
192
194
it ( 'fetches user data for present users' , async ( ) => {
193
- const source = getPresence ( instance , { } )
194
- const unsubscribe = source . subscribe ( ( ) => { } )
195
+ const stateSource = getPresence ( instance , { source } )
196
+ const unsubscribe = stateSource . subscribe ( ( ) => { } )
195
197
196
198
await firstValueFrom ( of ( null ) . pipe ( delay ( 10 ) ) )
197
199
@@ -222,8 +224,8 @@ describe('presenceStore', () => {
222
224
} )
223
225
224
226
it ( 'handles presence events correctly' , async ( ) => {
225
- const source = getPresence ( instance , { } )
226
- const unsubscribe = source . subscribe ( ( ) => { } )
227
+ const stateSource = getPresence ( instance , { source } )
228
+ const unsubscribe = stateSource . subscribe ( ( ) => { } )
227
229
228
230
await firstValueFrom ( of ( null ) . pipe ( delay ( 10 ) ) )
229
231
@@ -237,7 +239,7 @@ describe('presenceStore', () => {
237
239
238
240
await firstValueFrom ( of ( null ) . pipe ( delay ( 50 ) ) )
239
241
240
- const presence = source . getCurrent ( )
242
+ const presence = stateSource . getCurrent ( )
241
243
expect ( presence ) . toHaveLength ( 1 )
242
244
expect ( presence [ 0 ] . sessionId ) . toBe ( 'other-session' )
243
245
0 commit comments