Skip to content

Commit d2cc4f1

Browse files
committed
test overriding Session.event
1 parent 890820b commit d2cc4f1

File tree

3 files changed

+70
-72
lines changed

3 files changed

+70
-72
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 58 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -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", () => {
@@ -2887,14 +2880,14 @@ namespace ts.projectSystem {
28872880

28882881
assert.isFalse(hasError);
28892882
host.checkTimeoutQueueLength(2);
2890-
checkErrorMessage(host, "syntaxDiag", { file: untitledFile, diagnostics: [] });
2891-
host.clearOutput();
2883+
checkErrorMessage(session, "syntaxDiag", { file: untitledFile, diagnostics: [] });
2884+
session.clearMessages();
28922885

28932886
host.runQueuedImmediateCallbacks();
28942887
assert.isFalse(hasError);
2895-
checkErrorMessage(host, "semanticDiag", { file: untitledFile, diagnostics: [] });
2888+
checkErrorMessage(session, "semanticDiag", { file: untitledFile, diagnostics: [] });
28962889

2897-
checkCompleteEvent(host, 2, expectedSequenceId);
2890+
checkCompleteEvent(session, 2, expectedSequenceId);
28982891
}
28992892

29002893
it("has projectRoot", () => {
@@ -2938,7 +2931,7 @@ namespace ts.projectSystem {
29382931
verifyErrorsInApp();
29392932

29402933
function verifyErrorsInApp() {
2941-
host.clearOutput();
2934+
session.clearMessages();
29422935
const expectedSequenceId = session.getNextSeq();
29432936
session.executeCommandSeq<protocol.GeterrRequest>({
29442937
command: server.CommandNames.Geterr,
@@ -2948,13 +2941,13 @@ namespace ts.projectSystem {
29482941
}
29492942
});
29502943
host.checkTimeoutQueueLengthAndRun(1);
2951-
checkErrorMessage(host, "syntaxDiag", { file: app.path, diagnostics: [] });
2952-
host.clearOutput();
2944+
checkErrorMessage(session, "syntaxDiag", { file: app.path, diagnostics: [] });
2945+
session.clearMessages();
29532946

29542947
host.runQueuedImmediateCallbacks();
2955-
checkErrorMessage(host, "semanticDiag", { file: app.path, diagnostics: [] });
2956-
checkCompleteEvent(host, 2, expectedSequenceId);
2957-
host.clearOutput();
2948+
checkErrorMessage(session, "semanticDiag", { file: app.path, diagnostics: [] });
2949+
checkCompleteEvent(session, 2, expectedSequenceId);
2950+
session.clearMessages();
29582951
}
29592952
});
29602953
});
@@ -3679,7 +3672,7 @@ namespace ts.projectSystem {
36793672
}
36803673
});
36813674
checkNumberOfProjects(service, { inferredProjects: 1 });
3682-
host.clearOutput();
3675+
session.clearMessages();
36833676
const expectedSequenceId = session.getNextSeq();
36843677
session.executeCommandSeq<protocol.GeterrRequest>({
36853678
command: server.CommandNames.Geterr,
@@ -3690,23 +3683,24 @@ namespace ts.projectSystem {
36903683
});
36913684

36923685
host.checkTimeoutQueueLengthAndRun(1);
3693-
checkErrorMessage(host, "syntaxDiag", { file: file1.path, diagnostics: [] });
3694-
host.clearOutput();
3686+
checkErrorMessage(session, "syntaxDiag", { file: file1.path, diagnostics: [] });
3687+
session.clearMessages();
36953688

36963689
host.runQueuedImmediateCallbacks();
36973690
const moduleNotFound = Diagnostics.Cannot_find_module_0;
36983691
const startOffset = file1.content.indexOf('"') + 1;
3699-
checkErrorMessage(host, "semanticDiag", {
3692+
checkErrorMessage(session, "semanticDiag", {
37003693
file: file1.path, diagnostics: [{
37013694
start: { line: 1, offset: startOffset },
37023695
end: { line: 1, offset: startOffset + '"pad"'.length },
37033696
text: formatStringFromArgs(moduleNotFound.message, ["pad"]),
37043697
code: moduleNotFound.code,
3705-
category: DiagnosticCategory[moduleNotFound.category].toLowerCase()
3698+
category: DiagnosticCategory[moduleNotFound.category].toLowerCase(),
3699+
source: undefined
37063700
}]
37073701
});
3708-
checkCompleteEvent(host, 2, expectedSequenceId);
3709-
host.clearOutput();
3702+
checkCompleteEvent(session, 2, expectedSequenceId);
3703+
session.clearMessages();
37103704

37113705
const padIndex: FileOrFolder = {
37123706
path: `${folderPath}/node_modules/@types/pad/index.d.ts`,
@@ -3715,15 +3709,15 @@ namespace ts.projectSystem {
37153709
files.push(padIndex);
37163710
host.reloadFS(files, { ignoreWatchInvokedWithTriggerAsFileCreate: true });
37173711
host.runQueuedTimeoutCallbacks();
3718-
checkProjectUpdatedInBackgroundEvent(host, [file1.path]);
3719-
host.clearOutput();
3712+
checkProjectUpdatedInBackgroundEvent(session, [file1.path]);
3713+
session.clearMessages();
37203714

37213715
host.runQueuedTimeoutCallbacks();
3722-
checkErrorMessage(host, "syntaxDiag", { file: file1.path, diagnostics: [] });
3723-
host.clearOutput();
3716+
checkErrorMessage(session, "syntaxDiag", { file: file1.path, diagnostics: [] });
3717+
session.clearMessages();
37243718

37253719
host.runQueuedImmediateCallbacks();
3726-
checkErrorMessage(host, "semanticDiag", { file: file1.path, diagnostics: [] });
3720+
checkErrorMessage(session, "semanticDiag", { file: file1.path, diagnostics: [] });
37273721
});
37283722
});
37293723

@@ -4837,7 +4831,7 @@ namespace ts.projectSystem {
48374831
command: "projectInfo",
48384832
arguments: { file: f1.path }
48394833
});
4840-
host.clearOutput();
4834+
session.clearMessages();
48414835

48424836
// cancel previously issued Geterr
48434837
cancellationToken.setRequestToCancel(getErrId);
@@ -4861,7 +4855,7 @@ namespace ts.projectSystem {
48614855
assert.equal(host.getOutput().length, 1, "expect 1 message");
48624856
const e1 = <protocol.Event>getMessage(0);
48634857
assert.equal(e1.event, "syntaxDiag");
4864-
host.clearOutput();
4858+
session.clearMessages();
48654859

48664860
cancellationToken.setRequestToCancel(getErrId);
48674861
host.runQueuedImmediateCallbacks();
@@ -4883,7 +4877,7 @@ namespace ts.projectSystem {
48834877
assert.equal(host.getOutput().length, 1, "expect 1 message");
48844878
const e1 = <protocol.Event>getMessage(0);
48854879
assert.equal(e1.event, "syntaxDiag");
4886-
host.clearOutput();
4880+
session.clearMessages();
48874881

48884882
// the semanticDiag message
48894883
host.runQueuedImmediateCallbacks();
@@ -4906,7 +4900,7 @@ namespace ts.projectSystem {
49064900
assert.equal(host.getOutput().length, 1, "expect 1 message");
49074901
const e1 = <protocol.Event>getMessage(0);
49084902
assert.equal(e1.event, "syntaxDiag");
4909-
host.clearOutput();
4903+
session.clearMessages();
49104904

49114905
session.executeCommandSeq(<protocol.GeterrRequest>{
49124906
command: "geterr",
@@ -4920,7 +4914,7 @@ namespace ts.projectSystem {
49204914
const event = <protocol.RequestCompletedEvent>getMessage(n);
49214915
assert.equal(event.event, "requestCompleted");
49224916
assert.equal(event.body.request_seq, expectedSeq, "expectedSeq");
4923-
host.clearOutput();
4917+
session.clearMessages();
49244918
}
49254919

49264920
function getMessage(n: number) {
@@ -6423,7 +6417,7 @@ namespace ts.projectSystem {
64236417
});
64246418

64256419
// Verified the events, reset them
6426-
host.clearOutput();
6420+
session.clearMessages();
64276421
}
64286422
}
64296423
});

src/server/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ namespace ts.server {
573573
}
574574

575575
private writeToEventSocket(body: any, eventName: string): void {
576-
this.eventSocket.write(formatMessage({ seq: 0, type: "event", event: eventName, body }, this.logger, this.byteLength, this.host.newLine), "utf8");
576+
this.eventSocket.write(formatMessage(toEvent(body, eventName), this.logger, this.byteLength, this.host.newLine), "utf8");
577577
}
578578

579579
exit() {

src/server/session.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,16 @@ namespace ts.server {
245245
event: Event;
246246
}
247247

248+
/** @internal */
249+
export function toEvent(eventName: string, body: {}): protocol.Event {
250+
return {
251+
seq: 0,
252+
type: "event",
253+
event: eventName,
254+
body
255+
};
256+
}
257+
248258
export interface SessionOptions {
249259
host: ServerHost;
250260
cancellationToken: ServerCancellationToken;
@@ -400,13 +410,7 @@ namespace ts.server {
400410
}
401411

402412
public event<T>(body: T, eventName: string): void {
403-
const ev: protocol.Event = {
404-
seq: 0,
405-
type: "event",
406-
event: eventName,
407-
body
408-
};
409-
this.send(ev);
413+
this.send(toEvent(eventName, body));
410414
}
411415

412416
// For backwards-compatibility only.

0 commit comments

Comments
 (0)