Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: b3d5f0f0c63fb8e819810dc3800135117e2c9297 (plain)
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
/**
 * Event logger test
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import { EventLogger as Sut } from '../../src/system/EventLogger';
import { EventEmitter } from "events";
import { expect } from 'chai';

const sinon = require( 'sinon' );

declare interface MockConsole extends Console {
    getLevel(): string,
}

describe( 'system.EventLogger captures and logs events', () =>
{
    [
        {
            event_id:      'document-processed',
            console_level: 'log',
        },
        {
            event_id:      'delta-publish',
            console_level: 'log',
        },
        {
            event_id:      'amqp-conn-error',
            console_level: 'warn',
        },
        {
            event_id:      'amqp-reconnect',
            console_level: 'warn',
        },
        {
            event_id:      'amqp-reconnect-fail',
            console_level: 'error',
        },
        {
            event_id:      'avro-err',
            console_level: 'error',
        },
        {
            event_id:      'dao-err',
            console_level: 'error',
        },
        {
            event_id:      'publish-err',
            console_level: 'error',
        },
    ].forEach( ( { event_id, console_level } ) =>
    {
        it( event_id + ' triggers console output level: ' + console_level, () =>
        {
            const emitter = new EventEmitter();
            const con     = createMockConsole();
            const env     = 'test';

            new Sut( con, env, emitter, ts_ctr );

            emitter.emit( event_id );

            expect( con.getLevel() ).to.equal( console_level );
        } );
    } );
} );


function ts_ctr(): UnixTimestamp
{
    return <UnixTimestamp>Math.floor( new Date().getTime() / 1000 );
}


function createMockConsole(): MockConsole
{
    const mock = sinon.mock( console );

    mock.level    = '';
    mock.info     = ( _str: string ) => { mock.level = 'info'; };
    mock.log      = ( _str: string ) => { mock.level = 'log'; };
    mock.warn     = ( _str: string ) => { mock.level = 'warn'; };
    mock.error    = ( _str: string ) => { mock.level = 'error'; };
    mock.getLevel = () => mock.level;

    return mock;
}