forked from redhat-developer/vscode-yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.test.ts
More file actions
144 lines (127 loc) · 5.22 KB
/
telemetry.test.ts
File metadata and controls
144 lines (127 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* --------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';
import * as chai from 'chai';
import * as vscode from 'vscode';
import { TelemetryErrorHandler, TelemetryOutputChannel } from '../src/telemetry';
import { TelemetryEvent, TelemetryService } from '@redhat-developer/vscode-redhat-telemetry/lib/interfaces/telemetry';
const expect = chai.expect;
chai.use(sinonChai);
class TelemetryStub implements TelemetryService {
sendStartupEvent(): Promise<void> {
throw new Error('Method not implemented.');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
send(event: TelemetryEvent): Promise<void> {
throw new Error('Method not implemented.');
}
sendShutdownEvent(): Promise<void> {
throw new Error('Method not implemented.');
}
flushQueue(): Promise<void> {
throw new Error('Method not implemented.');
}
dispose(): Promise<void> {
throw new Error('Method not implemented.');
}
}
describe('Telemetry Test', () => {
const sandbox = sinon.createSandbox();
const testOutputChannel = vscode.window.createOutputChannel('YAML_TEST');
afterEach(() => {
sandbox.restore();
});
describe('TelemetryOutputChannel', () => {
let telemetryChannel: TelemetryOutputChannel;
let outputChannel: sinon.SinonStubbedInstance<vscode.OutputChannel>;
let telemetry: sinon.SinonStubbedInstance<TelemetryService>;
let clock: sinon.SinonFakeTimers;
beforeEach(() => {
outputChannel = sandbox.stub(testOutputChannel);
telemetry = sandbox.stub(new TelemetryStub());
telemetryChannel = new TelemetryOutputChannel(
(outputChannel as unknown) as vscode.OutputChannel,
(telemetry as unknown) as TelemetryService
);
clock = sinon.useFakeTimers();
});
afterEach(() => {
clock.restore();
});
it('should delegate "append" method', () => {
telemetryChannel.append('Some');
expect(outputChannel.append).calledOnceWith('Some');
});
it('should delegate "appendLine" method', () => {
telemetryChannel.appendLine('Some');
expect(outputChannel.appendLine).calledOnceWith('Some');
});
it('should delegate "clear" method', () => {
telemetryChannel.clear();
expect(outputChannel.clear).calledOnce;
});
it('should delegate "dispose" method', () => {
telemetryChannel.dispose();
expect(outputChannel.dispose).calledOnce;
});
it('should delegate "hide" method', () => {
telemetryChannel.hide();
expect(outputChannel.hide).calledOnce;
});
it('should delegate "show" method', () => {
telemetryChannel.show();
expect(outputChannel.show).calledOnce;
});
it('should send telemetry if log error in "append"', () => {
telemetryChannel.append('[Error] Some');
clock.tick(510);
expect(telemetry.send).calledOnceWith({ name: 'yaml.server.error', properties: { error: 'Some' } });
});
it('should send telemetry if log error on "appendLine"', () => {
telemetryChannel.appendLine('[Error] Some error');
clock.tick(510);
expect(telemetry.send).calledOnceWith({ name: 'yaml.server.error', properties: { error: 'Some error' } });
});
it("shouldn't send telemetry if error should be skipped", () => {
telemetryChannel.append(
"[Error - 15:10:33] (node:25052) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification."
);
clock.tick(510);
expect(telemetry.send).not.called;
});
it('should throttle send telemetry if "append" called multiple times', () => {
telemetryChannel.append('[Error] Some');
telemetryChannel.append('[Error] Second Error');
clock.tick(510);
expect(telemetry.send).calledOnceWith({ name: 'yaml.server.error', properties: { error: 'Some\nSecond Error' } });
});
it('should throttle send telemetry if "appendLine" called multiple times', () => {
telemetryChannel.appendLine('[Error] Some');
telemetryChannel.appendLine('[Error] Second Error');
telemetryChannel.appendLine('[Error] Third Error');
clock.tick(510);
expect(telemetry.send).calledOnceWith({
name: 'yaml.server.error',
properties: { error: 'Some\nSecond Error\nThird Error' },
});
});
});
describe('TelemetryErrorHandler', () => {
let telemetry: sinon.SinonStubbedInstance<TelemetryService>;
let errorHandler: TelemetryErrorHandler;
beforeEach(() => {
telemetry = sandbox.stub(new TelemetryStub());
errorHandler = new TelemetryErrorHandler(telemetry, 'YAML LS', 3);
});
it('should log telemetry on error', () => {
errorHandler.error(new Error('Some'), { jsonrpc: 'Error message' }, 3);
expect(telemetry.send).calledOnceWith({
name: 'yaml.lsp.error',
properties: { jsonrpc: 'Error message', error: 'Some' },
});
});
});
});