summaryrefslogtreecommitdiffstats
path: root/test/backend-test/monitor-conditions/test-operators.js
blob: e663c9a50a9f91f5be45af5863a970d30c87862d (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
const test = require("node:test");
const assert = require("node:assert");
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");

test("Test StringEqualsOperator", async (t) => {
    const op = operatorMap.get(OP_STR_EQUALS);
    assert.strictEqual(true, op.test("mx1.example.com", "mx1.example.com"));
    assert.strictEqual(false, op.test("mx1.example.com", "mx1.example.org"));
    assert.strictEqual(false, op.test("1", 1)); // strict equality
});

test("Test StringNotEqualsOperator", async (t) => {
    const op = operatorMap.get(OP_STR_NOT_EQUALS);
    assert.strictEqual(true, op.test("mx1.example.com", "mx1.example.org"));
    assert.strictEqual(false, op.test("mx1.example.com", "mx1.example.com"));
    assert.strictEqual(true, op.test(1, "1")); // variable is not typecasted (strict equality)
});

test("Test ContainsOperator with scalar", async (t) => {
    const op = operatorMap.get(OP_CONTAINS);
    assert.strictEqual(true, op.test("mx1.example.org", "example.org"));
    assert.strictEqual(false, op.test("mx1.example.org", "example.com"));
});

test("Test ContainsOperator with array", async (t) => {
    const op = operatorMap.get(OP_CONTAINS);
    assert.strictEqual(true, op.test([ "example.org" ], "example.org"));
    assert.strictEqual(false, op.test([ "example.org" ], "example.com"));
});

test("Test NotContainsOperator with scalar", async (t) => {
    const op = operatorMap.get(OP_NOT_CONTAINS);
    assert.strictEqual(true, op.test("example.org", ".com"));
    assert.strictEqual(false, op.test("example.org", ".org"));
});

test("Test NotContainsOperator with array", async (t) => {
    const op = operatorMap.get(OP_NOT_CONTAINS);
    assert.strictEqual(true, op.test([ "example.org" ], "example.com"));
    assert.strictEqual(false, op.test([ "example.org" ], "example.org"));
});

test("Test StartsWithOperator", async (t) => {
    const op = operatorMap.get(OP_STARTS_WITH);
    assert.strictEqual(true, op.test("mx1.example.com", "mx1"));
    assert.strictEqual(false, op.test("mx1.example.com", "mx2"));
});

test("Test NotStartsWithOperator", async (t) => {
    const op = operatorMap.get(OP_NOT_STARTS_WITH);
    assert.strictEqual(true, op.test("mx1.example.com", "mx2"));
    assert.strictEqual(false, op.test("mx1.example.com", "mx1"));
});

test("Test EndsWithOperator", async (t) => {
    const op = operatorMap.get(OP_ENDS_WITH);
    assert.strictEqual(true, op.test("mx1.example.com", "example.com"));
    assert.strictEqual(false, op.test("mx1.example.com", "example.net"));
});

test("Test NotEndsWithOperator", async (t) => {
    const op = operatorMap.get(OP_NOT_ENDS_WITH);
    assert.strictEqual(true, op.test("mx1.example.com", "example.net"));
    assert.strictEqual(false, op.test("mx1.example.com", "example.com"));
});

test("Test NumberEqualsOperator", async (t) => {
    const op = operatorMap.get(OP_NUM_EQUALS);
    assert.strictEqual(true, op.test(1, 1));
    assert.strictEqual(true, op.test(1, "1"));
    assert.strictEqual(false, op.test(1, "2"));
});

test("Test NumberNotEqualsOperator", async (t) => {
    const op = operatorMap.get(OP_NUM_NOT_EQUALS);
    assert.strictEqual(true, op.test(1, "2"));
    assert.strictEqual(false, op.test(1, "1"));
});

test("Test LessThanOperator", async (t) => {
    const op = operatorMap.get(OP_LT);
    assert.strictEqual(true, op.test(1, 2));
    assert.strictEqual(true, op.test(1, "2"));
    assert.strictEqual(false, op.test(1, 1));
});

test("Test GreaterThanOperator", async (t) => {
    const op = operatorMap.get(OP_GT);
    assert.strictEqual(true, op.test(2, 1));
    assert.strictEqual(true, op.test(2, "1"));
    assert.strictEqual(false, op.test(1, 1));
});

test("Test LessThanOrEqualToOperator", async (t) => {
    const op = operatorMap.get(OP_LTE);
    assert.strictEqual(true, op.test(1, 1));
    assert.strictEqual(true, op.test(1, 2));
    assert.strictEqual(true, op.test(1, "2"));
    assert.strictEqual(false, op.test(1, 0));
});

test("Test GreaterThanOrEqualToOperator", async (t) => {
    const op = operatorMap.get(OP_GTE);
    assert.strictEqual(true, op.test(1, 1));
    assert.strictEqual(true, op.test(2, 1));
    assert.strictEqual(true, op.test(2, "2"));
    assert.strictEqual(false, op.test(2, 3));
});