|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Red Hat, Inc. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + * ------------------------------------------------------------------------------------------ */ |
| 5 | +import * as sinon from 'sinon'; |
| 6 | +import * as sinonChai from 'sinon-chai'; |
| 7 | +import * as chai from 'chai'; |
| 8 | +import * as vscode from 'vscode'; |
| 9 | +import { TelemetryErrorHandler, TelemetryOutputChannel } from '../src/telemetry'; |
| 10 | +import { TelemetryEvent, TelemetryService } from '@redhat-developer/vscode-redhat-telemetry/lib/interfaces/telemetry'; |
| 11 | + |
| 12 | +const expect = chai.expect; |
| 13 | +chai.use(sinonChai); |
| 14 | +class TelemetryStub implements TelemetryService { |
| 15 | + sendStartupEvent(): Promise<void> { |
| 16 | + throw new Error('Method not implemented.'); |
| 17 | + } |
| 18 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 19 | + send(event: TelemetryEvent): Promise<void> { |
| 20 | + throw new Error('Method not implemented.'); |
| 21 | + } |
| 22 | + sendShutdownEvent(): Promise<void> { |
| 23 | + throw new Error('Method not implemented.'); |
| 24 | + } |
| 25 | + flushQueue(): Promise<void> { |
| 26 | + throw new Error('Method not implemented.'); |
| 27 | + } |
| 28 | + dispose(): Promise<void> { |
| 29 | + throw new Error('Method not implemented.'); |
| 30 | + } |
| 31 | +} |
| 32 | +describe('Telemetry Test', () => { |
| 33 | + const sandbox = sinon.createSandbox(); |
| 34 | + const testOutputChannel = vscode.window.createOutputChannel('YAML_TEST'); |
| 35 | + afterEach(() => { |
| 36 | + sandbox.restore(); |
| 37 | + }); |
| 38 | + describe('TelemetryOutputChannel', () => { |
| 39 | + let telemetryChannel: TelemetryOutputChannel; |
| 40 | + let outputChannel: sinon.SinonStubbedInstance<vscode.OutputChannel>; |
| 41 | + let telemetry: sinon.SinonStubbedInstance<TelemetryService>; |
| 42 | + beforeEach(() => { |
| 43 | + outputChannel = sandbox.stub(testOutputChannel); |
| 44 | + telemetry = sandbox.stub(new TelemetryStub()); |
| 45 | + telemetryChannel = new TelemetryOutputChannel( |
| 46 | + (outputChannel as unknown) as vscode.OutputChannel, |
| 47 | + (telemetry as unknown) as TelemetryService |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should delegate "append" method', () => { |
| 52 | + telemetryChannel.append('Some'); |
| 53 | + expect(outputChannel.append).calledOnceWith('Some'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should delegate "appendLine" method', () => { |
| 57 | + telemetryChannel.appendLine('Some'); |
| 58 | + expect(outputChannel.appendLine).calledOnceWith('Some'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should delegate "clear" method', () => { |
| 62 | + telemetryChannel.clear(); |
| 63 | + expect(outputChannel.clear).calledOnce; |
| 64 | + }); |
| 65 | + |
| 66 | + it('should delegate "dispose" method', () => { |
| 67 | + telemetryChannel.dispose(); |
| 68 | + expect(outputChannel.dispose).calledOnce; |
| 69 | + }); |
| 70 | + |
| 71 | + it('should delegate "hide" method', () => { |
| 72 | + telemetryChannel.hide(); |
| 73 | + expect(outputChannel.hide).calledOnce; |
| 74 | + }); |
| 75 | + |
| 76 | + it('should delegate "show" method', () => { |
| 77 | + telemetryChannel.show(); |
| 78 | + expect(outputChannel.show).calledOnce; |
| 79 | + }); |
| 80 | + |
| 81 | + it('should send telemetry if log error in "append"', () => { |
| 82 | + telemetryChannel.append('[Error Some'); |
| 83 | + expect(telemetry.send).calledOnceWith({ name: 'yaml.server.error', properties: { error: '[Error Some' } }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should send telemetry if log error on "appendLine"', () => { |
| 87 | + telemetryChannel.appendLine('[Error Some'); |
| 88 | + expect(telemetry.send).calledOnceWith({ name: 'yaml.server.error', properties: { error: '[Error Some' } }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('TelemetryErrorHandler', () => { |
| 93 | + let telemetry: sinon.SinonStubbedInstance<TelemetryService>; |
| 94 | + let errorHandler: TelemetryErrorHandler; |
| 95 | + |
| 96 | + beforeEach(() => { |
| 97 | + telemetry = sandbox.stub(new TelemetryStub()); |
| 98 | + errorHandler = new TelemetryErrorHandler(telemetry, 'YAML LS', 3); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should log telemetry on error', () => { |
| 102 | + errorHandler.error(new Error('Some'), { jsonrpc: 'Error message' }, 3); |
| 103 | + expect(telemetry.send).calledOnceWith({ |
| 104 | + name: 'yaml.lsp.error', |
| 105 | + properties: { jsonrpc: 'Error message', error: 'Some' }, |
| 106 | + }); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments