@@ -486,28 +486,85 @@ describe("bundle LSP runtime", () => {
486486 expect ( killProcessTreeMock ) . not . toHaveBeenCalled ( ) ;
487487 } ) ;
488488
489- it ( "rejects retained tools after the LSP runtime is disposed" , async ( ) => {
489+ it . each ( [
490+ [ "lsp_hover_typescript" , "textDocument/hover" ] ,
491+ [ "lsp_definition_typescript" , "textDocument/definition" ] ,
492+ [ "lsp_references_typescript" , "textDocument/references" ] ,
493+ ] ) ( "rejects retained %s requests throughout disposal" , async ( toolName , method ) => {
490494 configureSingleLspServer ( ) ;
491495 const child = new MockChildProcess ( ) ;
492496 spawnMock . mockReturnValue ( child ) ;
493-
494497 const runtime = await createBundleLspToolRuntime ( { workspaceDir : "/tmp/workspace" } ) ;
495- const hoverTool = runtime . tools . find ( ( tool ) => tool . name === "lsp_hover_typescript" ) ;
496- if ( ! hoverTool ) {
497- throw new Error ( " expected hover tool" ) ;
498+ const tool = runtime . tools . find ( ( candidate ) => candidate . name === toolName ) ;
499+ if ( ! tool ) {
500+ throw new Error ( ` expected ${ toolName } tool` ) ;
498501 }
502+ const input = { uri : "file:///tmp/workspace/index.ts" , line : 0 , character : 0 } ;
503+ await tool . execute ( "before-dispose" , input ) ;
499504
500- await runtime . dispose ( ) ;
505+ const disposal = runtime . dispose ( ) ;
506+ const during = await tool . execute ( "during-dispose" , input ) . then (
507+ ( ) => "accepted" ,
508+ ( error : unknown ) => ( error instanceof Error ? error . message : String ( error ) ) ,
509+ ) ;
510+ await disposal ;
511+ const after = await tool . execute ( "after-dispose" , input ) . then (
512+ ( ) => "accepted" ,
513+ ( error : unknown ) => ( error instanceof Error ? error . message : String ( error ) ) ,
514+ ) ;
501515
502- await expect (
503- hoverTool . execute ( "retained-call" , {
504- uri : "file:///tmp/workspace/index.ts" ,
505- line : 0 ,
506- character : 0 ,
507- } ) ,
508- ) . rejects . toThrow ( "LSP session disposed" ) ;
509- expect ( child . receivedMessages ) . not . toContainEqual (
510- expect . objectContaining ( { method : "textDocument/hover" } ) ,
516+ expect ( { during, after } ) . toEqual ( {
517+ during : "LSP session disposed" ,
518+ after : "LSP session disposed" ,
519+ } ) ;
520+ expect ( child . receivedMessages . filter ( ( message ) => message . method === method ) ) . toHaveLength ( 1 ) ;
521+ expect ( child . receivedMessages . filter ( ( message ) => message . method === "shutdown" ) ) . toHaveLength (
522+ 1 ,
511523 ) ;
524+ expect ( child . receivedMessages . filter ( ( message ) => message . method === "exit" ) ) . toHaveLength ( 1 ) ;
525+ expect ( killProcessTreeMock ) . toHaveBeenCalledTimes ( 1 ) ;
526+ } ) ;
527+
528+ it ( "rejects outstanding requests before shutdown and detaches their abort listeners" , async ( ) => {
529+ configureSingleLspServer ( ) ;
530+ const child = new MockChildProcess ( "" , new Set ( [ "initialize" ] ) ) ;
531+ spawnMock . mockReturnValue ( child ) ;
532+ const runtime = await createBundleLspToolRuntime ( { workspaceDir : "/tmp/workspace" } ) ;
533+ const hover = runtime . tools . find ( ( tool ) => tool . name === "lsp_hover_typescript" ) ;
534+ if ( ! hover ) {
535+ throw new Error ( "expected hover tool" ) ;
536+ }
537+ const controller = new AbortController ( ) ;
538+ const pending = hover
539+ . execute (
540+ "pending" ,
541+ {
542+ uri : "file:///tmp/workspace/index.ts" ,
543+ line : 0 ,
544+ character : 0 ,
545+ } ,
546+ controller . signal ,
547+ )
548+ . then (
549+ ( ) => "accepted" ,
550+ ( error : unknown ) => ( error instanceof Error ? error . message : String ( error ) ) ,
551+ ) ;
552+
553+ const disposal = runtime . dispose ( ) ;
554+ const shutdown = child . receivedMessages . find ( ( message ) => message . method === "shutdown" ) ;
555+ controller . abort ( new Error ( "caller aborted during shutdown" ) ) ;
556+ const outcome = await pending ;
557+ child . stdout . write ( encodeLspMessage ( { jsonrpc : "2.0" , id : shutdown ?. id , result : null } ) ) ;
558+ await disposal ;
559+
560+ expect ( outcome ) . toBe ( "LSP session disposed" ) ;
561+ expect ( child . receivedMessages . map ( ( message ) => message . method ) ) . toEqual ( [
562+ "initialize" ,
563+ "initialized" ,
564+ "textDocument/hover" ,
565+ "shutdown" ,
566+ "exit" ,
567+ ] ) ;
568+ expect ( killProcessTreeMock ) . toHaveBeenCalledTimes ( 1 ) ;
512569 } ) ;
513570} ) ;
0 commit comments