@@ -210,6 +210,8 @@ namespace ts.projectSystem {
210210
211211 class TestSession extends server . Session {
212212 private seq = 0 ;
213+ public events : protocol . Event [ ] = [ ] ;
214+ public host : TestServerHost ;
213215
214216 getProjectService ( ) {
215217 return this . projectService ;
@@ -229,6 +231,16 @@ namespace ts.projectSystem {
229231 request . type = "request" ;
230232 return this . executeCommand ( < T > request ) ;
231233 }
234+
235+ public event < T > ( body : T , eventName : string ) {
236+ this . events . push ( server . toEvent ( eventName , body ) ) ;
237+ super . event ( body , eventName ) ;
238+ }
239+
240+ public clearMessages ( ) {
241+ clear ( this . events ) ;
242+ this . host . clearOutput ( ) ;
243+ }
232244 }
233245
234246 export function createSession ( host : server . ServerHost , opts : Partial < server . SessionOptions > = { } ) {
@@ -436,48 +448,29 @@ namespace ts.projectSystem {
436448 verifyDiagnostics ( actual , [ ] ) ;
437449 }
438450
439- function assertEvent ( actualOutput : string , expectedEvent : protocol . Event , host : TestServerHost ) {
440- assert . equal ( actualOutput , server . formatMessage ( expectedEvent , nullLogger , Utils . byteLength , host . newLine ) ) ;
451+ function checkErrorMessage ( session : TestSession , eventName : "syntaxDiag" | "semanticDiag" , diagnostics : protocol . DiagnosticEventBody ) {
452+ checkNthEvent ( session , ts . server . toEvent ( eventName , diagnostics ) , 0 , /*isMostRecent*/ false ) ;
441453 }
442454
443- function checkErrorMessage ( host : TestServerHost , eventName : "syntaxDiag" | "semanticDiag" , diagnostics : protocol . DiagnosticEventBody ) {
444- const outputs = host . getOutput ( ) ;
445- assert . isTrue ( outputs . length >= 1 , outputs . toString ( ) ) ;
446- const event : protocol . Event = {
447- seq : 0 ,
448- type : "event" ,
449- event : eventName ,
450- body : diagnostics
451- } ;
452- assertEvent ( outputs [ 0 ] , event , host ) ;
455+ function checkCompleteEvent ( session : TestSession , numberOfCurrentEvents : number , expectedSequenceId : number ) {
456+ checkNthEvent ( session , ts . server . toEvent ( "requestCompleted" , { request_seq : expectedSequenceId } ) , numberOfCurrentEvents - 1 , /*isMostRecent*/ true ) ;
453457 }
454458
455- function checkCompleteEvent ( host : TestServerHost , numberOfCurrentEvents : number , expectedSequenceId : number ) {
456- const outputs = host . getOutput ( ) ;
457- assert . equal ( outputs . length , numberOfCurrentEvents , outputs . toString ( ) ) ;
458- const event : protocol . RequestCompletedEvent = {
459- seq : 0 ,
460- type : "event" ,
461- event : "requestCompleted" ,
462- body : {
463- request_seq : expectedSequenceId
464- }
465- } ;
466- assertEvent ( outputs [ numberOfCurrentEvents - 1 ] , event , host ) ;
459+ function checkProjectUpdatedInBackgroundEvent ( session : TestSession , openFiles : string [ ] ) {
460+ checkNthEvent ( session , ts . server . toEvent ( "projectsUpdatedInBackground" , { openFiles } ) , 0 , /*isMostRecent*/ true ) ;
467461 }
468462
469- function checkProjectUpdatedInBackgroundEvent ( host : TestServerHost , openFiles : string [ ] ) {
470- const outputs = host . getOutput ( ) ;
471- assert . equal ( outputs . length , 1 , outputs . toString ( ) ) ;
472- const event : protocol . ProjectsUpdatedInBackgroundEvent = {
473- seq : 0 ,
474- type : "event" ,
475- event : "projectsUpdatedInBackground" ,
476- body : {
477- openFiles
478- }
479- } ;
480- assertEvent ( outputs [ 0 ] , event , host ) ;
463+ function checkNthEvent ( session : TestSession , expectedEvent : protocol . Event , index : number , isMostRecent : boolean ) {
464+ const events = session . events ;
465+ assert . deepEqual ( events [ index ] , expectedEvent ) ;
466+
467+ const outputs = session . host . getOutput ( ) ;
468+ assert . equal ( outputs [ index ] , server . formatMessage ( expectedEvent , nullLogger , Utils . byteLength , session . host . newLine ) ) ;
469+
470+ if ( isMostRecent ) {
471+ assert . strictEqual ( events . length , index + 1 , JSON . stringify ( events ) ) ;
472+ assert . strictEqual ( outputs . length , index + 1 , JSON . stringify ( outputs ) ) ;
473+ }
481474 }
482475
483476 describe ( "tsserverProjectSystem" , ( ) => {
@@ -2891,14 +2884,14 @@ namespace ts.projectSystem {
28912884
28922885 assert . isFalse ( hasError ) ;
28932886 host . checkTimeoutQueueLength ( 2 ) ;
2894- checkErrorMessage ( host , "syntaxDiag" , { file : untitledFile , diagnostics : [ ] } ) ;
2895- host . clearOutput ( ) ;
2887+ checkErrorMessage ( session , "syntaxDiag" , { file : untitledFile , diagnostics : [ ] } ) ;
2888+ session . clearMessages ( ) ;
28962889
28972890 host . runQueuedImmediateCallbacks ( ) ;
28982891 assert . isFalse ( hasError ) ;
2899- checkErrorMessage ( host , "semanticDiag" , { file : untitledFile , diagnostics : [ ] } ) ;
2892+ checkErrorMessage ( session , "semanticDiag" , { file : untitledFile , diagnostics : [ ] } ) ;
29002893
2901- checkCompleteEvent ( host , 2 , expectedSequenceId ) ;
2894+ checkCompleteEvent ( session , 2 , expectedSequenceId ) ;
29022895 }
29032896
29042897 it ( "has projectRoot" , ( ) => {
@@ -2942,7 +2935,7 @@ namespace ts.projectSystem {
29422935 verifyErrorsInApp ( ) ;
29432936
29442937 function verifyErrorsInApp ( ) {
2945- host . clearOutput ( ) ;
2938+ session . clearMessages ( ) ;
29462939 const expectedSequenceId = session . getNextSeq ( ) ;
29472940 session . executeCommandSeq < protocol . GeterrRequest > ( {
29482941 command : server . CommandNames . Geterr ,
@@ -2952,13 +2945,13 @@ namespace ts.projectSystem {
29522945 }
29532946 } ) ;
29542947 host . checkTimeoutQueueLengthAndRun ( 1 ) ;
2955- checkErrorMessage ( host , "syntaxDiag" , { file : app . path , diagnostics : [ ] } ) ;
2956- host . clearOutput ( ) ;
2948+ checkErrorMessage ( session , "syntaxDiag" , { file : app . path , diagnostics : [ ] } ) ;
2949+ session . clearMessages ( ) ;
29572950
29582951 host . runQueuedImmediateCallbacks ( ) ;
2959- checkErrorMessage ( host , "semanticDiag" , { file : app . path , diagnostics : [ ] } ) ;
2960- checkCompleteEvent ( host , 2 , expectedSequenceId ) ;
2961- host . clearOutput ( ) ;
2952+ checkErrorMessage ( session , "semanticDiag" , { file : app . path , diagnostics : [ ] } ) ;
2953+ checkCompleteEvent ( session , 2 , expectedSequenceId ) ;
2954+ session . clearMessages ( ) ;
29622955 }
29632956 } ) ;
29642957 } ) ;
@@ -3683,7 +3676,7 @@ namespace ts.projectSystem {
36833676 }
36843677 } ) ;
36853678 checkNumberOfProjects ( service , { inferredProjects : 1 } ) ;
3686- host . clearOutput ( ) ;
3679+ session . clearMessages ( ) ;
36873680 const expectedSequenceId = session . getNextSeq ( ) ;
36883681 session . executeCommandSeq < protocol . GeterrRequest > ( {
36893682 command : server . CommandNames . Geterr ,
@@ -3694,23 +3687,24 @@ namespace ts.projectSystem {
36943687 } ) ;
36953688
36963689 host . checkTimeoutQueueLengthAndRun ( 1 ) ;
3697- checkErrorMessage ( host , "syntaxDiag" , { file : file1 . path , diagnostics : [ ] } ) ;
3698- host . clearOutput ( ) ;
3690+ checkErrorMessage ( session , "syntaxDiag" , { file : file1 . path , diagnostics : [ ] } ) ;
3691+ session . clearMessages ( ) ;
36993692
37003693 host . runQueuedImmediateCallbacks ( ) ;
37013694 const moduleNotFound = Diagnostics . Cannot_find_module_0 ;
37023695 const startOffset = file1 . content . indexOf ( '"' ) + 1 ;
3703- checkErrorMessage ( host , "semanticDiag" , {
3696+ checkErrorMessage ( session , "semanticDiag" , {
37043697 file : file1 . path , diagnostics : [ {
37053698 start : { line : 1 , offset : startOffset } ,
37063699 end : { line : 1 , offset : startOffset + '"pad"' . length } ,
37073700 text : formatStringFromArgs ( moduleNotFound . message , [ "pad" ] ) ,
37083701 code : moduleNotFound . code ,
3709- category : DiagnosticCategory [ moduleNotFound . category ] . toLowerCase ( )
3702+ category : DiagnosticCategory [ moduleNotFound . category ] . toLowerCase ( ) ,
3703+ source : undefined
37103704 } ]
37113705 } ) ;
3712- checkCompleteEvent ( host , 2 , expectedSequenceId ) ;
3713- host . clearOutput ( ) ;
3706+ checkCompleteEvent ( session , 2 , expectedSequenceId ) ;
3707+ session . clearMessages ( ) ;
37143708
37153709 const padIndex : FileOrFolder = {
37163710 path : `${ folderPath } /node_modules/@types/pad/index.d.ts` ,
@@ -3719,15 +3713,15 @@ namespace ts.projectSystem {
37193713 files . push ( padIndex ) ;
37203714 host . reloadFS ( files , { ignoreWatchInvokedWithTriggerAsFileCreate : true } ) ;
37213715 host . runQueuedTimeoutCallbacks ( ) ;
3722- checkProjectUpdatedInBackgroundEvent ( host , [ file1 . path ] ) ;
3723- host . clearOutput ( ) ;
3716+ checkProjectUpdatedInBackgroundEvent ( session , [ file1 . path ] ) ;
3717+ session . clearMessages ( ) ;
37243718
37253719 host . runQueuedTimeoutCallbacks ( ) ;
3726- checkErrorMessage ( host , "syntaxDiag" , { file : file1 . path , diagnostics : [ ] } ) ;
3727- host . clearOutput ( ) ;
3720+ checkErrorMessage ( session , "syntaxDiag" , { file : file1 . path , diagnostics : [ ] } ) ;
3721+ session . clearMessages ( ) ;
37283722
37293723 host . runQueuedImmediateCallbacks ( ) ;
3730- checkErrorMessage ( host , "semanticDiag" , { file : file1 . path , diagnostics : [ ] } ) ;
3724+ checkErrorMessage ( session , "semanticDiag" , { file : file1 . path , diagnostics : [ ] } ) ;
37313725 } ) ;
37323726 } ) ;
37333727
@@ -4841,7 +4835,7 @@ namespace ts.projectSystem {
48414835 command : "projectInfo" ,
48424836 arguments : { file : f1 . path }
48434837 } ) ;
4844- host . clearOutput ( ) ;
4838+ session . clearMessages ( ) ;
48454839
48464840 // cancel previously issued Geterr
48474841 cancellationToken . setRequestToCancel ( getErrId ) ;
@@ -4865,7 +4859,7 @@ namespace ts.projectSystem {
48654859 assert . equal ( host . getOutput ( ) . length , 1 , "expect 1 message" ) ;
48664860 const e1 = < protocol . Event > getMessage ( 0 ) ;
48674861 assert . equal ( e1 . event , "syntaxDiag" ) ;
4868- host . clearOutput ( ) ;
4862+ session . clearMessages ( ) ;
48694863
48704864 cancellationToken . setRequestToCancel ( getErrId ) ;
48714865 host . runQueuedImmediateCallbacks ( ) ;
@@ -4887,7 +4881,7 @@ namespace ts.projectSystem {
48874881 assert . equal ( host . getOutput ( ) . length , 1 , "expect 1 message" ) ;
48884882 const e1 = < protocol . Event > getMessage ( 0 ) ;
48894883 assert . equal ( e1 . event , "syntaxDiag" ) ;
4890- host . clearOutput ( ) ;
4884+ session . clearMessages ( ) ;
48914885
48924886 // the semanticDiag message
48934887 host . runQueuedImmediateCallbacks ( ) ;
@@ -4910,7 +4904,7 @@ namespace ts.projectSystem {
49104904 assert . equal ( host . getOutput ( ) . length , 1 , "expect 1 message" ) ;
49114905 const e1 = < protocol . Event > getMessage ( 0 ) ;
49124906 assert . equal ( e1 . event , "syntaxDiag" ) ;
4913- host . clearOutput ( ) ;
4907+ session . clearMessages ( ) ;
49144908
49154909 session . executeCommandSeq ( < protocol . GeterrRequest > {
49164910 command : "geterr" ,
@@ -4924,7 +4918,7 @@ namespace ts.projectSystem {
49244918 const event = < protocol . RequestCompletedEvent > getMessage ( n ) ;
49254919 assert . equal ( event . event , "requestCompleted" ) ;
49264920 assert . equal ( event . body . request_seq , expectedSeq , "expectedSeq" ) ;
4927- host . clearOutput ( ) ;
4921+ session . clearMessages ( ) ;
49284922 }
49294923
49304924 function getMessage ( n : number ) {
@@ -6427,7 +6421,7 @@ namespace ts.projectSystem {
64276421 } ) ;
64286422
64296423 // Verified the events, reset them
6430- host . clearOutput ( ) ;
6424+ session . clearMessages ( ) ;
64316425 }
64326426 }
64336427 } ) ;
0 commit comments