@@ -25,6 +25,7 @@ const mockInstance = (overrides: Partial<VST3ActiveInstance> = {}): VST3ActiveIn
2525 parameters : [ ] ,
2626 presets : [ ] ,
2727 activePreset : null ,
28+ latencySamples : 0 ,
2829 ...overrides ,
2930} ) ;
3031
@@ -249,29 +250,21 @@ describe('vst3Store', () => {
249250 const { _getBridgeClient } = await import ( '../../hooks/useVST3Connection' ) ;
250251 const client = _getBridgeClient ( ) ;
251252
252- // Intercept on/off to capture event listeners registered by loadPlugin
253- const listeners : Record < string , ( ( ...args : unknown [ ] ) => void ) [ ] > = { } ;
254- client . on = vi . fn ( ) . mockImplementation ( ( event : string , handler : ( ...args : unknown [ ] ) => void ) => {
255- if ( ! listeners [ event ] ) listeners [ event ] = [ ] ;
256- listeners [ event ] . push ( handler ) ;
257- } ) ;
258- client . off = vi . fn ( ) ;
259-
260- // Mock createInstance to simulate the companion responding with instanceCreated
261- const mockCreateInstance = vi . fn ( ) . mockImplementation ( ( _pluginUid : string , instanceId : string ) => {
262- queueMicrotask ( ( ) => {
263- for ( const fn of listeners [ 'instanceCreated' ] ?? [ ] ) {
264- fn ( { type : 'instanceCreated' , instanceId, parameters : [ ] } ) ;
265- }
266- } ) ;
267- return Promise . resolve ( ) ;
268- } ) ;
269- client . createInstance = mockCreateInstance ;
253+ const mockInstantiate = vi . fn ( ) . mockImplementation ( ( _pluginUid : string , instanceId : string ) => Promise . resolve ( {
254+ type : 'instantiated' ,
255+ reqId : 'req-1' ,
256+ instanceId,
257+ parameters : [ ] ,
258+ latencySamples : 384 ,
259+ tailSamples : 0 ,
260+ presets : [ ] ,
261+ } ) ) ;
262+ client . instantiate = mockInstantiate ;
270263
271264 await useVST3Store . getState ( ) . loadPlugin ( 'com.xfer.serum' , 'track-1' ) ;
272265
273- // Should have called createInstance with the pluginId and a generated instanceId
274- expect ( mockCreateInstance ) . toHaveBeenCalledWith ( 'com.xfer.serum' , expect . any ( String ) ) ;
266+ // Should have called instantiate with the pluginId and a generated instanceId
267+ expect ( mockInstantiate ) . toHaveBeenCalledWith ( 'com.xfer.serum' , expect . any ( String ) ) ;
275268
276269 // Should have created an instance in the store
277270 const { instances } = useVST3Store . getState ( ) ;
@@ -285,28 +278,26 @@ describe('vst3Store', () => {
285278 expect ( instance . trackId ) . toBe ( 'track-1' ) ;
286279 expect ( instance . enabled ) . toBe ( true ) ;
287280 expect ( instance . online ) . toBe ( true ) ;
281+ expect ( instance . latencySamples ) . toBe ( 384 ) ;
282+ } ) ;
283+
284+ it ( 'updates store and PluginEngine when the companion reports latency changes' , ( ) => {
285+ const setPluginLatencySpy = vi . spyOn ( pluginEngine , 'setPluginLatency' ) . mockImplementation ( ( ) => undefined ) ;
286+ useVST3Store . getState ( ) . _upsertInstance ( mockInstance ( { instanceId : 'inst-1' , trackId : 'track-1' , latencySamples : 128 } ) ) ;
287+
288+ useVST3Store . getState ( ) . _updateLatency ( 'inst-1' , 512.8 ) ;
289+
290+ expect ( setPluginLatencySpy ) . toHaveBeenCalledWith ( 'track-1' , 'inst-1' , 512 ) ;
291+ expect ( useVST3Store . getState ( ) . instances [ 'inst-1' ] . latencySamples ) . toBe ( 512 ) ;
292+
293+ setPluginLatencySpy . mockRestore ( ) ;
288294 } ) ;
289295
290296 it ( 'does not create an instance when bridge call fails' , async ( ) => {
291297 const { _getBridgeClient } = await import ( '../../hooks/useVST3Connection' ) ;
292298 const client = _getBridgeClient ( ) ;
293299
294- const listeners : Record < string , ( ( ...args : unknown [ ] ) => void ) [ ] > = { } ;
295- client . on = vi . fn ( ) . mockImplementation ( ( event : string , handler : ( ...args : unknown [ ] ) => void ) => {
296- if ( ! listeners [ event ] ) listeners [ event ] = [ ] ;
297- listeners [ event ] . push ( handler ) ;
298- } ) ;
299- client . off = vi . fn ( ) ;
300-
301- client . createInstance = vi . fn ( ) . mockImplementation ( ( ) => {
302- // Trigger the error listener
303- queueMicrotask ( ( ) => {
304- for ( const fn of listeners [ 'error' ] ?? [ ] ) {
305- fn ( 'Connection lost' ) ;
306- }
307- } ) ;
308- return Promise . resolve ( ) ;
309- } ) ;
300+ client . instantiate = vi . fn ( ) . mockRejectedValue ( new Error ( 'Connection lost' ) ) ;
310301
311302 await useVST3Store . getState ( ) . loadPlugin ( 'com.xfer.serum' , 'track-1' ) ;
312303
0 commit comments