11
2- import { Observable } from '@reactivex/rxjs' ;
2+ import { Observable , Subject } from '@reactivex/rxjs' ;
33import * as assert from 'assert' ;
44import { EventEmitter } from 'events' ;
5- import { OpPatch } from 'json-patch' ;
5+ import jsonpatch from 'fast- json-patch' ;
66import { Span } from 'opentracing' ;
77import * as sinon from 'sinon' ;
88import { PassThrough } from 'stream' ;
@@ -93,7 +93,7 @@ describe('connection', () => {
9393 } ) ;
9494 it ( 'should call a handler on request and send the result of the returned Observable' , async ( ) => {
9595 const handler : TypeScriptService = Object . create ( TypeScriptService . prototype ) ;
96- const hoverStub = sinon . stub ( handler , 'textDocumentHover' ) . returns ( Observable . of < OpPatch > (
96+ const hoverStub = sinon . stub ( handler , 'textDocumentHover' ) . returns ( Observable . of < jsonpatch . Operation > (
9797 { op : 'add' , path : '' , value : [ ] } ,
9898 { op : 'add' , path : '/-' , value : 123 }
9999 ) ) ;
@@ -260,20 +260,19 @@ describe('connection', () => {
260260 it ( 'should call a handler on request and send partial results of the returned Observable' , async ( ) => {
261261 const handler : { [ K in keyof TypeScriptService ] : TypeScriptService [ K ] & sinon . SinonStub } = sinon . createStubInstance ( TypeScriptService ) ;
262262 handler . initialize . returns ( Observable . of ( { op : 'add' , path : '' , value : { capabilities : { streaming : true } } } ) ) ;
263- handler . textDocumentHover . returns ( Observable . of < OpPatch > (
264- { op : 'add' , path : '' , value : [ ] } ,
265- { op : 'add' , path : '/-' , value : 123 }
266- ) ) ;
263+
264+ const hoverSubject = new Subject ( ) ;
265+ handler . textDocumentHover . returns ( hoverSubject ) ;
266+
267267 const emitter = new EventEmitter ( ) ;
268268 const writer = {
269269 write : sinon . spy ( )
270270 } ;
271+
271272 registerLanguageHandler ( emitter as MessageEmitter , writer as any , handler as any ) ;
272- emitter . emit ( 'message' , { jsonrpc : '2.0' , id : 1 , method : 'initialize' , params : { capabilities : { streaming : true } } } ) ;
273- emitter . emit ( 'message' , { jsonrpc : '2.0' , id : 2 , method : 'textDocument/hover' , params : [ 1 , 2 ] } ) ;
274- sinon . assert . calledOnce ( handler . textDocumentHover ) ;
275273
276- sinon . assert . callCount ( writer . write , 5 ) ;
274+ // Send initialize
275+ emitter . emit ( 'message' , { jsonrpc : '2.0' , id : 1 , method : 'initialize' , params : { capabilities : { streaming : true } } } ) ;
277276 assert . deepEqual ( writer . write . args [ 0 ] , [ {
278277 jsonrpc : '2.0' ,
279278 method : '$/partialResult' ,
@@ -287,16 +286,29 @@ describe('connection', () => {
287286 id : 1 ,
288287 result : { capabilities : { streaming : true } }
289288 } ] , 'Expected to send final result for initialize' ) ;
289+
290+ // Send hover
291+ emitter . emit ( 'message' , { jsonrpc : '2.0' , id : 2 , method : 'textDocument/hover' , params : [ 1 , 2 ] } ) ;
292+ sinon . assert . calledOnce ( handler . textDocumentHover ) ;
293+
294+ // Simulate initializing JSON Patch Operation
295+ hoverSubject . next ( { op : 'add' , path : '' , value : [ ] } ) ;
290296 assert . deepEqual ( writer . write . args [ 2 ] , [ {
291297 jsonrpc : '2.0' ,
292298 method : '$/partialResult' ,
293299 params : { id : 2 , patch : [ { op : 'add' , path : '' , value : [ ] } ] }
294300 } ] , 'Expected to send partial result that initializes array' ) ;
301+
302+ // Simulate streamed value
303+ hoverSubject . next ( { op : 'add' , path : '/-' , value : 123 } ) ;
295304 assert . deepEqual ( writer . write . args [ 3 ] , [ {
296305 jsonrpc : '2.0' ,
297306 method : '$/partialResult' ,
298307 params : { id : 2 , patch : [ { op : 'add' , path : '/-' , value : 123 } ] }
299308 } ] , 'Expected to send partial result that adds 123 to array' ) ;
309+
310+ // Complete Subject to trigger final response
311+ hoverSubject . complete ( ) ;
300312 assert . deepEqual ( writer . write . args [ 4 ] , [ {
301313 jsonrpc : '2.0' ,
302314 id : 2 ,
0 commit comments