aboutsummaryrefslogtreecommitdiff
path: root/test/formatters/token.js
blob: e9baefdad5e795b3b207344ae7b626ad55d0028c (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
'use strict';

const Tap = require('tap');

const TokenFormatter = require('../../lib/formatters/token');

// Prepare the test

Tap.plan(15);

const logObject = {
  _timestamp: BigInt(Date.now()) * 1000000n,
  _cologneLog: '1.0.0',
  _from: 'Dummy Logger',
  _level: 6,
  _levelString: 'error',
  message: 'testing stuff!'
};
const isoDate = (new Date(Number(logObject._timestamp) / 1000000)).toISOString();

const defaultFormatter = new TokenFormatter();
const customFormatter = new TokenFormatter({
  formatString: '{{_level}} {{_cologneLog}} {{_timestamp}} {{_magic}}'
});
const ansiFormatter = new TokenFormatter({
  formatString: 'string {{_ansi:red}}with color:{{_ansi:reset}} {{message}}'
});
const ansiLevelFormatter = new TokenFormatter({
  formatString: 'string {{_ansi:_level}}with color:{{_ansi:reset}} {{message}}'
});
const plainDateFormatter = new TokenFormatter({
  isoDate: false,
  formatString: '{{_timestamp}}'
});
const customSearchFormatter = new TokenFormatter({
  formatString: '[[message]]',
  replaceRule: /\[\[(.*?)\]\]/g
});

/**
 * TEST: #format() - default
 */

const defaultFormattedString = defaultFormatter.format(logObject);

Tap.equal(typeof defaultFormattedString, 'string',
  '#format() should output a string in default mode');

Tap.equal(defaultFormattedString, logObject.message,
  '#format() should include the message in default mode');

/**
 * TEST: #format() - custom
 */

const customFormattedString = customFormatter.format(logObject);

Tap.equal(typeof customFormattedString, 'string',
  '#format() should output a string in custom mode');

Tap.ok(customFormattedString.match(logObject._level),
  '#format() with custom string should include the specified tokens (check 1)');

Tap.ok(customFormattedString.match(logObject._cologneLog),
  '#format() with custom string should include the specified tokens (check 2)');

Tap.ok(customFormattedString.match(isoDate),
  '#format() with iso date should include the timestamp as an iso date');

Tap.ok(customFormattedString.match('{{_magic}}'),
  '#format() should not replace tokens that don\'t match');

/**
 * TEST: #format() - ansi
 */

const ansiFormattedString = ansiFormatter.format(logObject);

Tap.equal(typeof ansiFormattedString, 'string',
  '#format() should output a string in ansi mode');

Tap.equal(ansiFormattedString.split(String.fromCharCode(27) + '[31m').length, 2,
  '#format() with ansi tokens should colorize the string');

Tap.equal(ansiFormattedString.split(String.fromCharCode(27) + '[0m').length, 2,
  '#format() with ansi reset should reset the string');

/**
 * TEST: #format() - ansi based on level
 */

const ansiLevelFormattedString = ansiLevelFormatter.format(logObject);

Tap.equal(ansiLevelFormattedString.split(String.fromCharCode(27) + '[34m').length, 2,
  '#format() with ansi tokens should colorize the string based on level');



/**
 * TEST: #format() - plain date
 */

const plainDateFormattedString = plainDateFormatter.format(logObject);

Tap.equal(typeof plainDateFormattedString, 'string',
  '#format() should output a string in plain date mode');

Tap.equal(plainDateFormattedString, logObject._timestamp.toString(),
  '#format() with plain date should include the timestamp as-is');

/**
 * TEST: #format() - custom search
 */

const customSearchFormattedString = customSearchFormatter.format(logObject);

Tap.equal(typeof customSearchFormattedString, 'string',
  '#format() should output a string in custom search mode');

Tap.equal(customSearchFormattedString, logObject.message,
  '#format() with a custom search, should properly match the new tokens');