-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathEventExclusionPlugin.test.ts
205 lines (174 loc) · 11.8 KB
/
EventExclusionPlugin.test.ts
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { describe, test } from "@jest/globals";
import { expect } from "expect";
import { ExceptionlessClient } from "../../../src/ExceptionlessClient.js";
import { Event, EventType, KnownEventDataKeys } from "../../../src/models/Event.js";
import { InnerErrorInfo } from "../../../src/models/data/ErrorInfo.js";
import { EventExclusionPlugin } from "../../../src/plugins/default/EventExclusionPlugin.js";
import { EventPluginContext } from "../../../src/plugins/EventPluginContext.js";
import { EventContext } from "../../../src/models/EventContext.js";
describe("EventExclusionPlugin", () => {
describe("should exclude log levels", () => {
const run = async (source: string | undefined, level: string | null | undefined, settingKey: string | null | undefined, settingValue: string | null | undefined): Promise<boolean> => {
const client = new ExceptionlessClient();
if (typeof settingKey == "string") {
client.config.settings[settingKey] = settingValue as string;
}
const ev: Event = <Event>{ type: "log", source, data: {} };
if (ev.data && level) {
ev.data[KnownEventDataKeys.Level] = level;
}
const context = new EventPluginContext(client, ev, new EventContext());
const plugin = new EventExclusionPlugin();
await plugin.run(context);
return context.cancelled;
}
test("<null>", async () => expect(await run(undefined, null, null, null)).toBe(false));
test("Test", async () => expect(await run("Test", null, null, null)).toBe(false));
test("[Trace] Test", async () => expect(await run("Test", "Trace", null, null)).toBe(false));
test("[Off] Test", async () => expect(await run("Test", "Off", null, null)).toBe(true));
test("[Abc] Test", async () => expect(await run("Test", "Abc", null, null)).toBe(false));
test("[Trace] <null> (source min level: Off", async () => expect(await run(undefined, "Trace", "@@log:", "Off")).toBe(true));
test("[Trace] <null> (global min level: Off", async () => expect(await run(undefined, "Trace", "@@log:*", "Off")).toBe(true));
test("[Trace] <undefined> (source min level: Off", async () => expect(await run(undefined, "Trace", "@@log:", "Off")).toBe(true));
test("[Trace] <undefined> (global min level: Off", async () => expect(await run(undefined, "Trace", "@@log:*", "Off")).toBe(true));
test("[Trace] <empty> (source min level: Off", async () => expect(await run("", "Trace", "@@log:", "Off")).toBe(true)); // Becomes Global Log Level
test("[Trace] <empty> (global min level: Off", async () => expect(await run("", "Trace", "@@log:*", "Off")).toBe(true));
test("[Trace] Test (source min level: false)", async () => expect(await run("Test", "Trace", "@@log:Test", "false")).toBe(true));
test("[Trace] Test (source min level: no)", async () => expect(await run("Test", "Trace", "@@log:Test", "no")).toBe(true));
test("[Trace] Test (source min level: 0)", async () => expect(await run("Test", "Trace", "@@log:Test", "0")).toBe(true));
test("[Trace] Test (source min level: true)", async () => expect(await run("Test", "Trace", "@@log:Test", "true")).toBe(false));
test("[Trace] Test (source min level: yes)", async () => expect(await run("Test", "Trace", "@@log:Test", "yes")).toBe(false));
test("[Trace] Test (source min level: 1)", async () => expect(await run("Test", "Trace", "@@log:Test", "1")).toBe(false));
test("[Trace] Test (source min level: Debug)", async () => expect(await run("Test", "Trace", "@@log:Test", "Debug")).toBe(true));
test("[Info] Test (source min level: Debug)", async () => expect(await run("Test", "Info", "@@log:Test", "Debug")).toBe(false));
test("[Trace] Test (global min level: Debug)", async () => expect(await run("Test", "Trace", "@@log:*", "Debug")).toBe(true));
test("[Warn] Test (global min level: Debug)", async () => expect(await run("Test", "Warn", "@@log:*", "Debug")).toBe(false));
});
describe("should exclude log levels with info default", () => {
const run = async (source: string | undefined, level: string | null | undefined, settingKey: string | null | undefined, settingValue: string | null | undefined): Promise<boolean> => {
const client = new ExceptionlessClient();
client.config.settings["@@log:*"] = "Info";
if (typeof settingKey === "string") {
client.config.settings[settingKey] = settingValue as string;
}
const ev: Event = <Event>{ type: "log", source, data: {} };
if (ev.data && level) {
ev.data[KnownEventDataKeys.Level] = level;
}
const context = new EventPluginContext(client, ev, new EventContext());
const plugin = new EventExclusionPlugin();
await plugin.run(context);
return context.cancelled;
}
test("<null>", async () => expect(await run(undefined, null, null, null)).toBe(false));
test("Test", async () => expect(await run("Test", null, null, null)).toBe(false));
test("[Trace] Test", async () => expect(await run("Test", "Trace", null, null)).toBe(true));
test("[Warn] Test", async () => expect(await run("Test", "Warn", null, null)).toBe(false));
test("[Error] Test (source min level: Debug)", async () => expect(await run("Test", "Error", "@@log:Test", "Debug")).toBe(false));
test("[Debug] Test (source min level: Debug)", async () => expect(await run("Test", "Debug", "@@log:Test", "Debug")).toBe(false));
});
describe("should resolve null and undefined log source levels in reverse settings order", () => {
const plugin = new EventExclusionPlugin();
const settings: Record<string, string> = { "@@log:": "Info", "@@log:*": "Debug" };
test("<undefined> (global min level: info)", () => expect(plugin.getMinLogLevel(settings, undefined)).toBe(2));
test("<empty> (source min level: info)", () => expect(plugin.getMinLogLevel(settings, "")).toBe(2));
test("* (global min level: debug)", () => expect(plugin.getMinLogLevel(settings, "*")).toBe(1));
});
describe("should resolve log source levels and respect settings order", () => {
const plugin = new EventExclusionPlugin();
const settings = { "@@log:*": "Debug", "@@log:": "Info" };
test("<empty> (source min level: info)", () => expect(plugin.getMinLogLevel(settings, "")).toBe(2));
test("* (global min level: debug)", () => expect(plugin.getMinLogLevel(settings, "*")).toBe(1));
});
describe("should fallback to global log level setting", () => {
const plugin = new EventExclusionPlugin();
const settings = {
"@@log:*": "Fatal",
};
test("<undefined> (source min level: off)", () => expect(plugin.getMinLogLevel(settings, undefined)).toBe(5));
test("<empty> (source min level: off)", () => expect(plugin.getMinLogLevel(settings, "")).toBe(5));
test("* (source min level: off)", () => expect(plugin.getMinLogLevel(settings, "*")).toBe(5));
test("abc (source min level: off)", () => expect(plugin.getMinLogLevel(settings, "abc")).toBe(5));
});
describe("should respect min log levels settings order with global settings", () => {
const plugin = new EventExclusionPlugin();
const settings = {
"@@log:*": "Fatal",
"@@log:": "Debug",
"@@log:abc*": "Off",
"@@log:abc.de*": "Debug",
"@@log:abc.def*": "Info",
"@@log:abc.def.ghi": "Trace"
};
test("<undefined> (source min level: debug)", () => expect(plugin.getMinLogLevel(settings, undefined)).toBe(1));
test("<empty> (source min level: debug)", () => expect(plugin.getMinLogLevel(settings, "")).toBe(1));
test("fallback (global min level: debug)", () => expect(plugin.getMinLogLevel(settings, "fallback")).toBe(5));
test("abc (source min level: off)", () => expect(plugin.getMinLogLevel(settings, "abc")).toBe(6));
test("abc.def (source min level: info)", () => expect(plugin.getMinLogLevel(settings, "abc.def")).toBe(2));
test("abc.def.random (source min level: info)", () => expect(plugin.getMinLogLevel(settings, "abc.def.random")).toBe(2));
test("abc.def.ghi (source min level: trace)", () => expect(plugin.getMinLogLevel(settings, "abc.def.ghi")).toBe(0));
});
describe("should respect min log levels settings order", () => {
const plugin = new EventExclusionPlugin();
const settings = {
"@@log:abc.def.ghi": "Trace",
"@@log:abc.def*": "Info",
"@@log:abc*": "Off"
};
test("abc (source min level: off)", () => expect(plugin.getMinLogLevel(settings, "abc")).toBe(6));
test("abc.def (source min level: info)", () => expect(plugin.getMinLogLevel(settings, "abc.def")).toBe(2));
test("abc.def.ghi (source min level: trace)", () => expect(plugin.getMinLogLevel(settings, "abc.def.ghi")).toBe(0));
});
describe("should exclude source type", () => {
const run = async (type: EventType | null | undefined, source: string | undefined, settingKey: string | null | undefined, settingValue: string | null | undefined): Promise<boolean> => {
const client = new ExceptionlessClient();
if (typeof settingKey === "string") {
client.config.settings[settingKey] = settingValue as string;
}
const context = new EventPluginContext(client, <Event>{ type: <string>type, source, data: {} }, new EventContext());
const plugin = new EventExclusionPlugin();
await plugin.run(context);
return context.cancelled;
}
test("<null>", async () => expect(await run(null, undefined, null, null)).toBe(false));
test("usage=<null>", async () => expect(await run("usage", undefined, null, null)).toBe(false));
test("usage=test", async () => expect(await run("usage", "test", null, null)).toBe(false));
test("usage=test on", async () => expect(await run("usage", "test", "@@usage:Test", "true")).toBe(false));
test("usage=test off", async () => expect(await run("usage", "test", "@@usage:Test", "false")).toBe(true));
test("usage=test (global off)", async () => expect(await run("usage", "test", "@@usage:*", "false")).toBe(true));
test("404=/unknown (global off)", async () => expect(await run("404", "/unknown", "@@404:*", "false")).toBe(true));
test("404=/unknown on", async () => expect(await run("404", "/unknown", "@@404:/unknown", "true")).toBe(false));
test("404=/unknown off", async () => expect(await run("404", "/unknown", "@@404:/unknown", "false")).toBe(true));
test("404=<null> off", async () => expect(await run("404", undefined, "@@404:*", "false")).toBe(true));
test("404=<undefined> empty off", async () => expect(await run("404", undefined, "@@404:", "false")).toBe(true));
test("404=<undefined> global off", async () => expect(await run("404", undefined, "@@404:*", "false")).toBe(true));
test("404=<null> empty off", async () => expect(await run("404", undefined, "@@404:", "false")).toBe(true));
test("404=<empty> off", async () => expect(await run("404", "", "@@404:", "false")).toBe(true));
});
describe("should exclude exception type", () => {
const run = async (settingKey: string | null | undefined): Promise<boolean> => {
const client = new ExceptionlessClient();
if (typeof settingKey === "string") {
client.config.settings[settingKey] = "false";
}
const context = new EventPluginContext(client, {
type: "error",
data: {
"@error": <InnerErrorInfo>{
type: "ReferenceError",
message: "This is a test",
stack_trace: []
}
}
}, new EventContext());
const plugin = new EventExclusionPlugin();
await plugin.run(context);
return context.cancelled;
}
test("<null>", async () => expect(await run(null)).toBe(false));
test("@@error:Error", async () => expect(await run("@@error:Error")).toBe(false));
test("@@error:ReferenceError", async () => expect(await run("@@error:ReferenceError")).toBe(true));
test("@@error:*Error", async () => expect(await run("@@error:*Error")).toBe(true));
test("@@error:*", async () => expect(await run("@@error:*")).toBe(true));
});
});