|
1 | | -const test = require("node:test"); |
| 1 | +const { describe, test } = require("node:test"); |
2 | 2 | const assert = require("node:assert"); |
3 | 3 | const { operatorMap, OP_CONTAINS, OP_NOT_CONTAINS, OP_LT, OP_GT, OP_LTE, OP_GTE, OP_STR_EQUALS, OP_STR_NOT_EQUALS, OP_NUM_EQUALS, OP_NUM_NOT_EQUALS, OP_STARTS_WITH, OP_ENDS_WITH, OP_NOT_STARTS_WITH, OP_NOT_ENDS_WITH } = require("../../../server/monitor-conditions/operators.js"); |
4 | 4 |
|
5 | | -test("Test StringEqualsOperator", async (t) => { |
6 | | - const op = operatorMap.get(OP_STR_EQUALS); |
7 | | - assert.strictEqual(true, op.test("mx1.example.com", "mx1.example.com")); |
8 | | - assert.strictEqual(false, op.test("mx1.example.com", "mx1.example.org")); |
9 | | - assert.strictEqual(false, op.test("1", 1)); // strict equality |
10 | | -}); |
11 | | - |
12 | | -test("Test StringNotEqualsOperator", async (t) => { |
13 | | - const op = operatorMap.get(OP_STR_NOT_EQUALS); |
14 | | - assert.strictEqual(true, op.test("mx1.example.com", "mx1.example.org")); |
15 | | - assert.strictEqual(false, op.test("mx1.example.com", "mx1.example.com")); |
16 | | - assert.strictEqual(true, op.test(1, "1")); // variable is not typecasted (strict equality) |
17 | | -}); |
18 | | - |
19 | | -test("Test ContainsOperator with scalar", async (t) => { |
20 | | - const op = operatorMap.get(OP_CONTAINS); |
21 | | - assert.strictEqual(true, op.test("mx1.example.org", "example.org")); |
22 | | - assert.strictEqual(false, op.test("mx1.example.org", "example.com")); |
23 | | -}); |
24 | | - |
25 | | -test("Test ContainsOperator with array", async (t) => { |
26 | | - const op = operatorMap.get(OP_CONTAINS); |
27 | | - assert.strictEqual(true, op.test([ "example.org" ], "example.org")); |
28 | | - assert.strictEqual(false, op.test([ "example.org" ], "example.com")); |
29 | | -}); |
30 | | - |
31 | | -test("Test NotContainsOperator with scalar", async (t) => { |
32 | | - const op = operatorMap.get(OP_NOT_CONTAINS); |
33 | | - assert.strictEqual(true, op.test("example.org", ".com")); |
34 | | - assert.strictEqual(false, op.test("example.org", ".org")); |
35 | | -}); |
36 | | - |
37 | | -test("Test NotContainsOperator with array", async (t) => { |
38 | | - const op = operatorMap.get(OP_NOT_CONTAINS); |
39 | | - assert.strictEqual(true, op.test([ "example.org" ], "example.com")); |
40 | | - assert.strictEqual(false, op.test([ "example.org" ], "example.org")); |
41 | | -}); |
42 | | - |
43 | | -test("Test StartsWithOperator", async (t) => { |
44 | | - const op = operatorMap.get(OP_STARTS_WITH); |
45 | | - assert.strictEqual(true, op.test("mx1.example.com", "mx1")); |
46 | | - assert.strictEqual(false, op.test("mx1.example.com", "mx2")); |
47 | | -}); |
48 | | - |
49 | | -test("Test NotStartsWithOperator", async (t) => { |
50 | | - const op = operatorMap.get(OP_NOT_STARTS_WITH); |
51 | | - assert.strictEqual(true, op.test("mx1.example.com", "mx2")); |
52 | | - assert.strictEqual(false, op.test("mx1.example.com", "mx1")); |
53 | | -}); |
54 | | - |
55 | | -test("Test EndsWithOperator", async (t) => { |
56 | | - const op = operatorMap.get(OP_ENDS_WITH); |
57 | | - assert.strictEqual(true, op.test("mx1.example.com", "example.com")); |
58 | | - assert.strictEqual(false, op.test("mx1.example.com", "example.net")); |
59 | | -}); |
60 | | - |
61 | | -test("Test NotEndsWithOperator", async (t) => { |
62 | | - const op = operatorMap.get(OP_NOT_ENDS_WITH); |
63 | | - assert.strictEqual(true, op.test("mx1.example.com", "example.net")); |
64 | | - assert.strictEqual(false, op.test("mx1.example.com", "example.com")); |
65 | | -}); |
66 | | - |
67 | | -test("Test NumberEqualsOperator", async (t) => { |
68 | | - const op = operatorMap.get(OP_NUM_EQUALS); |
69 | | - assert.strictEqual(true, op.test(1, 1)); |
70 | | - assert.strictEqual(true, op.test(1, "1")); |
71 | | - assert.strictEqual(false, op.test(1, "2")); |
72 | | -}); |
73 | | - |
74 | | -test("Test NumberNotEqualsOperator", async (t) => { |
75 | | - const op = operatorMap.get(OP_NUM_NOT_EQUALS); |
76 | | - assert.strictEqual(true, op.test(1, "2")); |
77 | | - assert.strictEqual(false, op.test(1, "1")); |
78 | | -}); |
79 | | - |
80 | | -test("Test LessThanOperator", async (t) => { |
81 | | - const op = operatorMap.get(OP_LT); |
82 | | - assert.strictEqual(true, op.test(1, 2)); |
83 | | - assert.strictEqual(true, op.test(1, "2")); |
84 | | - assert.strictEqual(false, op.test(1, 1)); |
85 | | -}); |
86 | | - |
87 | | -test("Test GreaterThanOperator", async (t) => { |
88 | | - const op = operatorMap.get(OP_GT); |
89 | | - assert.strictEqual(true, op.test(2, 1)); |
90 | | - assert.strictEqual(true, op.test(2, "1")); |
91 | | - assert.strictEqual(false, op.test(1, 1)); |
92 | | -}); |
93 | | - |
94 | | -test("Test LessThanOrEqualToOperator", async (t) => { |
95 | | - const op = operatorMap.get(OP_LTE); |
96 | | - assert.strictEqual(true, op.test(1, 1)); |
97 | | - assert.strictEqual(true, op.test(1, 2)); |
98 | | - assert.strictEqual(true, op.test(1, "2")); |
99 | | - assert.strictEqual(false, op.test(1, 0)); |
100 | | -}); |
101 | | - |
102 | | -test("Test GreaterThanOrEqualToOperator", async (t) => { |
103 | | - const op = operatorMap.get(OP_GTE); |
104 | | - assert.strictEqual(true, op.test(1, 1)); |
105 | | - assert.strictEqual(true, op.test(2, 1)); |
106 | | - assert.strictEqual(true, op.test(2, "2")); |
107 | | - assert.strictEqual(false, op.test(2, 3)); |
| 5 | +describe("Expression Operators", () => { |
| 6 | + test("StringEqualsOperator returns true for identical strings and false otherwise", () => { |
| 7 | + const op = operatorMap.get(OP_STR_EQUALS); |
| 8 | + assert.strictEqual(true, op.test("mx1.example.com", "mx1.example.com")); |
| 9 | + assert.strictEqual(false, op.test("mx1.example.com", "mx1.example.org")); |
| 10 | + assert.strictEqual(false, op.test("1", 1)); // strict equality |
| 11 | + }); |
| 12 | + |
| 13 | + test("StringNotEqualsOperator returns true for different strings and false for identical strings", () => { |
| 14 | + const op = operatorMap.get(OP_STR_NOT_EQUALS); |
| 15 | + assert.strictEqual(true, op.test("mx1.example.com", "mx1.example.org")); |
| 16 | + assert.strictEqual(false, op.test("mx1.example.com", "mx1.example.com")); |
| 17 | + assert.strictEqual(true, op.test(1, "1")); // variable is not typecasted (strict equality) |
| 18 | + }); |
| 19 | + |
| 20 | + test("ContainsOperator returns true when scalar contains substring", () => { |
| 21 | + const op = operatorMap.get(OP_CONTAINS); |
| 22 | + assert.strictEqual(true, op.test("mx1.example.org", "example.org")); |
| 23 | + assert.strictEqual(false, op.test("mx1.example.org", "example.com")); |
| 24 | + }); |
| 25 | + |
| 26 | + test("ContainsOperator returns true when array contains element", () => { |
| 27 | + const op = operatorMap.get(OP_CONTAINS); |
| 28 | + assert.strictEqual(true, op.test([ "example.org" ], "example.org")); |
| 29 | + assert.strictEqual(false, op.test([ "example.org" ], "example.com")); |
| 30 | + }); |
| 31 | + |
| 32 | + test("NotContainsOperator returns true when scalar does not contain substring", () => { |
| 33 | + const op = operatorMap.get(OP_NOT_CONTAINS); |
| 34 | + assert.strictEqual(true, op.test("example.org", ".com")); |
| 35 | + assert.strictEqual(false, op.test("example.org", ".org")); |
| 36 | + }); |
| 37 | + |
| 38 | + test("NotContainsOperator returns true when array does not contain element", () => { |
| 39 | + const op = operatorMap.get(OP_NOT_CONTAINS); |
| 40 | + assert.strictEqual(true, op.test([ "example.org" ], "example.com")); |
| 41 | + assert.strictEqual(false, op.test([ "example.org" ], "example.org")); |
| 42 | + }); |
| 43 | + |
| 44 | + test("StartsWithOperator returns true when string starts with prefix", () => { |
| 45 | + const op = operatorMap.get(OP_STARTS_WITH); |
| 46 | + assert.strictEqual(true, op.test("mx1.example.com", "mx1")); |
| 47 | + assert.strictEqual(false, op.test("mx1.example.com", "mx2")); |
| 48 | + }); |
| 49 | + |
| 50 | + test("NotStartsWithOperator returns true when string does not start with prefix", () => { |
| 51 | + const op = operatorMap.get(OP_NOT_STARTS_WITH); |
| 52 | + assert.strictEqual(true, op.test("mx1.example.com", "mx2")); |
| 53 | + assert.strictEqual(false, op.test("mx1.example.com", "mx1")); |
| 54 | + }); |
| 55 | + |
| 56 | + test("EndsWithOperator returns true when string ends with suffix", () => { |
| 57 | + const op = operatorMap.get(OP_ENDS_WITH); |
| 58 | + assert.strictEqual(true, op.test("mx1.example.com", "example.com")); |
| 59 | + assert.strictEqual(false, op.test("mx1.example.com", "example.net")); |
| 60 | + }); |
| 61 | + |
| 62 | + test("NotEndsWithOperator returns true when string does not end with suffix", () => { |
| 63 | + const op = operatorMap.get(OP_NOT_ENDS_WITH); |
| 64 | + assert.strictEqual(true, op.test("mx1.example.com", "example.net")); |
| 65 | + assert.strictEqual(false, op.test("mx1.example.com", "example.com")); |
| 66 | + }); |
| 67 | + |
| 68 | + test("NumberEqualsOperator returns true for equal numbers with type coercion", () => { |
| 69 | + const op = operatorMap.get(OP_NUM_EQUALS); |
| 70 | + assert.strictEqual(true, op.test(1, 1)); |
| 71 | + assert.strictEqual(true, op.test(1, "1")); |
| 72 | + assert.strictEqual(false, op.test(1, "2")); |
| 73 | + }); |
| 74 | + |
| 75 | + test("NumberNotEqualsOperator returns true for different numbers", () => { |
| 76 | + const op = operatorMap.get(OP_NUM_NOT_EQUALS); |
| 77 | + assert.strictEqual(true, op.test(1, "2")); |
| 78 | + assert.strictEqual(false, op.test(1, "1")); |
| 79 | + }); |
| 80 | + |
| 81 | + test("LessThanOperator returns true when first number is less than second", () => { |
| 82 | + const op = operatorMap.get(OP_LT); |
| 83 | + assert.strictEqual(true, op.test(1, 2)); |
| 84 | + assert.strictEqual(true, op.test(1, "2")); |
| 85 | + assert.strictEqual(false, op.test(1, 1)); |
| 86 | + }); |
| 87 | + |
| 88 | + test("GreaterThanOperator returns true when first number is greater than second", () => { |
| 89 | + const op = operatorMap.get(OP_GT); |
| 90 | + assert.strictEqual(true, op.test(2, 1)); |
| 91 | + assert.strictEqual(true, op.test(2, "1")); |
| 92 | + assert.strictEqual(false, op.test(1, 1)); |
| 93 | + }); |
| 94 | + |
| 95 | + test("LessThanOrEqualToOperator returns true when first number is less than or equal to second", () => { |
| 96 | + const op = operatorMap.get(OP_LTE); |
| 97 | + assert.strictEqual(true, op.test(1, 1)); |
| 98 | + assert.strictEqual(true, op.test(1, 2)); |
| 99 | + assert.strictEqual(true, op.test(1, "2")); |
| 100 | + assert.strictEqual(false, op.test(1, 0)); |
| 101 | + }); |
| 102 | + |
| 103 | + test("GreaterThanOrEqualToOperator returns true when first number is greater than or equal to second", () => { |
| 104 | + const op = operatorMap.get(OP_GTE); |
| 105 | + assert.strictEqual(true, op.test(1, 1)); |
| 106 | + assert.strictEqual(true, op.test(2, 1)); |
| 107 | + assert.strictEqual(true, op.test(2, "2")); |
| 108 | + assert.strictEqual(false, op.test(2, 3)); |
| 109 | + }); |
108 | 110 | }); |
0 commit comments