@@ -4,7 +4,7 @@ import {beforeEach, describe, expect, it, vi} from 'vitest'
4
4
import { createSanityInstance , type SanityInstance } from '../../../store/createSanityInstance'
5
5
import { createStoreState } from '../../../store/createStoreState'
6
6
import { type FrameMessage , type WindowMessage } from '../../types'
7
- import { type ComlinkNodeState , type NodeEntry } from '../comlinkNodeStore'
7
+ import { type ComlinkNodeState } from '../comlinkNodeStore'
8
8
import { releaseNode } from './releaseNode'
9
9
10
10
const nodeConfig = {
@@ -18,15 +18,16 @@ describe('releaseNode', () => {
18
18
let mockNode : Partial < Node < WindowMessage , FrameMessage > > & {
19
19
start : ReturnType < typeof vi . fn >
20
20
stop : ReturnType < typeof vi . fn >
21
+ onStatus : ReturnType < typeof vi . fn >
21
22
}
22
23
23
24
beforeEach ( ( ) => {
24
25
instance = createSanityInstance ( {
25
26
projectId : 'test-project-id' ,
26
27
dataset : 'test-dataset' ,
27
28
} )
28
- mockNode = { start : vi . fn ( ) , stop : vi . fn ( ) }
29
- state = createStoreState < ComlinkNodeState > ( { nodes : new Map ( ) } )
29
+ mockNode = { start : vi . fn ( ) , stop : vi . fn ( ) , onStatus : vi . fn ( ) }
30
+ state = createStoreState < ComlinkNodeState > ( { nodes : new Map ( ) , subscriptions : new Map ( ) } )
30
31
vi . clearAllMocks ( )
31
32
} )
32
33
@@ -40,7 +41,6 @@ describe('releaseNode', () => {
40
41
nodes . set ( 'test-node' , {
41
42
node : mockNode as Node < WindowMessage , FrameMessage > ,
42
43
options : nodeConfig ,
43
- refCount : 1 ,
44
44
} )
45
45
state . set ( 'setup' , { nodes} )
46
46
@@ -54,83 +54,18 @@ describe('releaseNode', () => {
54
54
expect ( state . get ( ) . nodes . has ( 'test-node' ) ) . toBe ( false )
55
55
} )
56
56
57
- it ( 'should not stop the node if refCount is still above 0 ' , ( ) => {
58
- // Create a node twice to increment refCount
57
+ it ( 'should call statusUnsub if present when releasing node ' , ( ) => {
58
+ const statusUnsub = vi . fn ( )
59
59
const nodes = new Map ( )
60
60
nodes . set ( 'test-node' , {
61
61
node : mockNode as Node < WindowMessage , FrameMessage > ,
62
62
options : nodeConfig ,
63
- refCount : 2 ,
63
+ statusUnsub ,
64
64
} )
65
65
state . set ( 'setup' , { nodes} )
66
66
67
- // Release once
68
67
releaseNode ( { state, instance} , 'test-node' )
69
68
70
- // Node should not be stopped
71
- expect ( mockNode . stop ) . not . toHaveBeenCalled ( )
72
-
73
- // Verify refCount is 1
74
- const nodeEntry = state . get ( ) . nodes . get ( 'test-node' ) as NodeEntry
75
- expect ( nodeEntry ?. refCount ) . toBe ( 1 )
76
- } )
77
-
78
- it ( 'should handle multiple releases gracefully' , ( ) => {
79
- // Set up a node in the state
80
- const nodes = new Map ( )
81
- nodes . set ( 'test-node' , {
82
- node : mockNode as Node < WindowMessage , FrameMessage > ,
83
- options : nodeConfig ,
84
- refCount : 1 ,
85
- } )
86
- state . set ( 'setup' , { nodes} )
87
-
88
- // Release multiple times
89
- releaseNode ( { state, instance} , 'test-node' )
90
- releaseNode ( { state, instance} , 'test-node' )
91
- releaseNode ( { state, instance} , 'test-node' )
92
-
93
- // Verify node is removed after first release
94
- expect ( state . get ( ) . nodes . has ( 'test-node' ) ) . toBe ( false )
95
- // Stop should be called exactly once
96
- expect ( mockNode . stop ) . toHaveBeenCalledTimes ( 1 )
97
- } )
98
-
99
- it ( 'should handle releasing non-existent nodes' , ( ) => {
100
- // Should not throw when releasing non-existent node
101
- expect ( ( ) => releaseNode ( { state, instance} , 'non-existent' ) ) . not . toThrow ( )
102
- } )
103
-
104
- it ( 'should maintain correct state after complex operations' , ( ) => {
105
- // Set up a node with refCount = 3
106
- const nodes = new Map ( )
107
- nodes . set ( 'test-node' , {
108
- node : mockNode as Node < WindowMessage , FrameMessage > ,
109
- options : nodeConfig ,
110
- refCount : 3 ,
111
- } )
112
- state . set ( 'setup' , { nodes} )
113
-
114
- // Initial refCount should be 3
115
- let nodeEntry = state . get ( ) . nodes . get ( 'test-node' ) as NodeEntry
116
- expect ( nodeEntry ?. refCount ) . toBe ( 3 )
117
-
118
- // Release twice
119
- releaseNode ( { state, instance} , 'test-node' )
120
- releaseNode ( { state, instance} , 'test-node' )
121
-
122
- nodeEntry = state . get ( ) . nodes . get ( 'test-node' ) as NodeEntry
123
- expect ( nodeEntry ?. refCount ) . toBe ( 1 )
124
-
125
- // Verify node hasn't been stopped yet
126
- expect ( mockNode . stop ) . not . toHaveBeenCalled ( )
127
-
128
- // Release final reference
129
- releaseNode ( { state, instance} , 'test-node' )
130
-
131
- // Verify node was stopped
132
- expect ( mockNode . stop ) . toHaveBeenCalled ( )
133
-
134
- expect ( state . get ( ) . nodes . has ( 'test-node' ) ) . toBe ( false )
69
+ expect ( statusUnsub ) . toHaveBeenCalled ( )
135
70
} )
136
71
} )
0 commit comments