Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: abfbef8a313eed1cd1280cd385af0ce80c34a57b (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
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
145
/**
 * 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 { EventMediator as Sut } from '../../src/system/EventMediator';
import { context } from '../../src/error/ContextError';
import { EventEmitter } from "events";
import { expect } from 'chai';
import { PsrLogger } from '../../src/system/PsrLogger';


describe( 'system.EventLogger captures and logs events', () =>
{
    it( 'document-processed triggers log#notice', () =>
    {
        let method_called = false;

        const event_id = 'document-processed';
        const emitter  = new EventEmitter();
        const log      = createMockLogger();

        log.notice = ( _str: string ) => { method_called = true; };

        new Sut( log, emitter );

        emitter.emit( event_id );

        expect( method_called ).to.be.true;
    } );

    it( 'delta-publish triggers log#notice', () =>
    {
        let method_called = false;

        const event_id = 'delta-publish';
        const emitter  = new EventEmitter();
        const log      = createMockLogger();

        log.notice = ( _str: string ) => { method_called = true; };

        new Sut( log, emitter );

        emitter.emit( event_id );

        expect( method_called ).to.be.true;
    } );

    it( 'amqp-conn-warn triggers log#warning', () =>
    {
        let method_called = false;

        const event_id = 'amqp-conn-warn';
        const emitter  = new EventEmitter();
        const log      = createMockLogger();

        log.warning = ( _str: string ) => { method_called = true; };

        new Sut( log, emitter );

        emitter.emit( event_id );

        expect( method_called ).to.be.true;
    } );

    it( 'amqp-reconnect triggers log#warning', () =>
    {
        let method_called = false;

        const event_id = 'amqp-reconnect';
        const emitter  = new EventEmitter();
        const log      = createMockLogger();

        log.warning = ( _str: string ) => { method_called = true; };

        new Sut( log, emitter );

        emitter.emit( event_id );

        expect( method_called ).to.be.true;
    } );

    it( 'context and stack are retrieved from error', () =>
    {
        let method_called = false;

        const event_id    = 'error';
        const err_msg     = 'Foo';
        const stub_err    = new Error( err_msg );
        const emitter     = new EventEmitter();
        const log         = createMockLogger();
        const err_context = { bar: 'baz' };

        const expected_context = {
            bar:   err_context.bar,
            stack: stub_err.stack,
        };

        log.error = ( str: string, context: any ) =>
        {
            expect( str ).to.equal( err_msg );
            expect( context ).to.deep.equal( expected_context );

            method_called = true;
        };

        new Sut( log, emitter );

        emitter.emit( event_id, context( stub_err, err_context ) );

        expect( method_called ).to.be.true;
    } );
} );


function createMockLogger(): PsrLogger
{
    return <PsrLogger>{
        debug( _msg: string | object, _context: object ){},
        info( _msg: string | object, _context: object ){},
        notice( _msg: string | object, _context: object ){ console.log( 'asdasd msg: ', _msg ); },
        warning( _msg: string | object, _context: object ){},
        error( _msg: string | object, _context: object ){},
        critical( _msg: string | object, _context: object ){},
        alert( _msg: string | object, _context: object ){},
        emergency( _msg: string | object, _context: object ){},
        log( _level: any, _msg: string | object, _context: object ){},
    };
}