]> git.r.bdr.sh - rbdr/cologne/blob - test/formatters/token.js
Move the cache to the global scope
[rbdr/cologne] / test / formatters / token.js
1 'use strict';
2
3 const Tap = require('tap');
4
5 const TokenFormatter = require('../../lib/formatters/token');
6
7 // Prepare the test
8
9 Tap.plan(15);
10
11 const logObject = {
12 _timestamp: BigInt(Date.now()) * 1000000n,
13 _cologneLog: '1.0.0',
14 _from: 'Dummy Logger',
15 _level: 6,
16 _levelString: 'error',
17 message: 'testing stuff!'
18 };
19 const isoDate = (new Date(Number(logObject._timestamp) / 1000000)).toISOString();
20
21 const defaultFormatter = new TokenFormatter();
22 const customFormatter = new TokenFormatter({
23 formatString: '{{_level}} {{_cologneLog}} {{_timestamp}} {{_magic}}'
24 });
25 const ansiFormatter = new TokenFormatter({
26 formatString: 'string {{_ansi:red}}with color:{{_ansi:reset}} {{message}}'
27 });
28 const ansiLevelFormatter = new TokenFormatter({
29 formatString: 'string {{_ansi:_level}}with color:{{_ansi:reset}} {{message}}'
30 });
31 const plainDateFormatter = new TokenFormatter({
32 isoDate: false,
33 formatString: '{{_timestamp}}'
34 });
35 const customSearchFormatter = new TokenFormatter({
36 formatString: '[[message]]',
37 replaceRule: /\[\[(.*?)\]\]/g
38 });
39
40 /**
41 * TEST: #format() - default
42 */
43
44 const defaultFormattedString = defaultFormatter.format(logObject);
45
46 Tap.equal(typeof defaultFormattedString, 'string',
47 '#format() should output a string in default mode');
48
49 Tap.equal(defaultFormattedString, logObject.message,
50 '#format() should include the message in default mode');
51
52 /**
53 * TEST: #format() - custom
54 */
55
56 const customFormattedString = customFormatter.format(logObject);
57
58 Tap.equal(typeof customFormattedString, 'string',
59 '#format() should output a string in custom mode');
60
61 Tap.ok(customFormattedString.match(logObject._level),
62 '#format() with custom string should include the specified tokens (check 1)');
63
64 Tap.ok(customFormattedString.match(logObject._cologneLog),
65 '#format() with custom string should include the specified tokens (check 2)');
66
67 Tap.ok(customFormattedString.match(isoDate),
68 '#format() with iso date should include the timestamp as an iso date');
69
70 Tap.ok(customFormattedString.match('{{_magic}}'),
71 '#format() should not replace tokens that don\'t match');
72
73 /**
74 * TEST: #format() - ansi
75 */
76
77 const ansiFormattedString = ansiFormatter.format(logObject);
78
79 Tap.equal(typeof ansiFormattedString, 'string',
80 '#format() should output a string in ansi mode');
81
82 Tap.equal(ansiFormattedString.split(String.fromCharCode(27) + '[31m').length, 2,
83 '#format() with ansi tokens should colorize the string');
84
85 Tap.equal(ansiFormattedString.split(String.fromCharCode(27) + '[0m').length, 2,
86 '#format() with ansi reset should reset the string');
87
88 /**
89 * TEST: #format() - ansi based on level
90 */
91
92 const ansiLevelFormattedString = ansiLevelFormatter.format(logObject);
93
94 Tap.equal(ansiLevelFormattedString.split(String.fromCharCode(27) + '[34m').length, 2,
95 '#format() with ansi tokens should colorize the string based on level');
96
97
98
99 /**
100 * TEST: #format() - plain date
101 */
102
103 const plainDateFormattedString = plainDateFormatter.format(logObject);
104
105 Tap.equal(typeof plainDateFormattedString, 'string',
106 '#format() should output a string in plain date mode');
107
108 Tap.equal(plainDateFormattedString, logObject._timestamp.toString(),
109 '#format() with plain date should include the timestamp as-is');
110
111 /**
112 * TEST: #format() - custom search
113 */
114
115 const customSearchFormattedString = customSearchFormatter.format(logObject);
116
117 Tap.equal(typeof customSearchFormattedString, 'string',
118 '#format() should output a string in custom search mode');
119
120 Tap.equal(customSearchFormattedString, logObject.message,
121 '#format() with a custom search, should properly match the new tokens');