|
| 1 | +/* |
| 2 | +Copyright 2016 OpenMarket Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +var notifications = require('../../../src/notifications'); |
| 18 | + |
| 19 | +var ContentRules = notifications.ContentRules; |
| 20 | +var PushRuleVectorState = notifications.PushRuleVectorState; |
| 21 | + |
| 22 | +var expect = require('expect'); |
| 23 | +var test_utils = require('../../test-utils'); |
| 24 | + |
| 25 | +var NORMAL_RULE = { |
| 26 | + actions: [ |
| 27 | + "notify", |
| 28 | + { set_tweak: "highlight", value: false }, |
| 29 | + ], |
| 30 | + enabled: true, |
| 31 | + pattern: "vdh2", |
| 32 | + rule_id: "vdh2", |
| 33 | +}; |
| 34 | + |
| 35 | +var LOUD_RULE = { |
| 36 | + actions: [ |
| 37 | + "notify", |
| 38 | + { set_tweak: "highlight" }, |
| 39 | + { set_tweak: "sound", value: "default" }, |
| 40 | + ], |
| 41 | + enabled: true, |
| 42 | + pattern: "vdh2", |
| 43 | + rule_id: "vdh2", |
| 44 | +}; |
| 45 | + |
| 46 | +var USERNAME_RULE = { |
| 47 | + actions: [ |
| 48 | + "notify", |
| 49 | + { set_tweak: "sound", value: "default" }, |
| 50 | + { set_tweak: "highlight" }, |
| 51 | + ], |
| 52 | + default: true, |
| 53 | + enabled: true, |
| 54 | + pattern: "richvdh", |
| 55 | + rule_id: ".m.rule.contains_user_name", |
| 56 | +}; |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +describe("ContentRules", function() { |
| 61 | + beforeEach(function() { |
| 62 | + test_utils.beforeEach(this); |
| 63 | + }); |
| 64 | + |
| 65 | + describe("parseContentRules", function() { |
| 66 | + it("should handle there being no keyword rules", function() { |
| 67 | + var rules = { 'global': { 'content': [ |
| 68 | + USERNAME_RULE, |
| 69 | + ]}}; |
| 70 | + var parsed = ContentRules.parseContentRules(rules); |
| 71 | + expect(parsed.rules).toEqual([]); |
| 72 | + expect(parsed.vectorState).toEqual(PushRuleVectorState.ON); |
| 73 | + expect(parsed.externalRules).toEqual([]); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should parse regular keyword notifications", function() { |
| 77 | + var rules = { 'global': { 'content': [ |
| 78 | + NORMAL_RULE, |
| 79 | + USERNAME_RULE, |
| 80 | + ]}}; |
| 81 | + |
| 82 | + var parsed = ContentRules.parseContentRules(rules); |
| 83 | + expect(parsed.rules.length).toEqual(1); |
| 84 | + expect(parsed.rules[0]).toEqual(NORMAL_RULE); |
| 85 | + expect(parsed.vectorState).toEqual(PushRuleVectorState.ON); |
| 86 | + expect(parsed.externalRules).toEqual([]); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should parse loud keyword notifications", function() { |
| 90 | + var rules = { 'global': { 'content': [ |
| 91 | + LOUD_RULE, |
| 92 | + USERNAME_RULE, |
| 93 | + ]}}; |
| 94 | + |
| 95 | + var parsed = ContentRules.parseContentRules(rules); |
| 96 | + expect(parsed.rules.length).toEqual(1); |
| 97 | + expect(parsed.rules[0]).toEqual(LOUD_RULE); |
| 98 | + expect(parsed.vectorState).toEqual(PushRuleVectorState.LOUD); |
| 99 | + expect(parsed.externalRules).toEqual([]); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should parse mixed keyword notifications", function() { |
| 103 | + var rules = { 'global': { 'content': [ |
| 104 | + LOUD_RULE, |
| 105 | + NORMAL_RULE, |
| 106 | + USERNAME_RULE, |
| 107 | + ]}}; |
| 108 | + |
| 109 | + var parsed = ContentRules.parseContentRules(rules); |
| 110 | + expect(parsed.rules.length).toEqual(1); |
| 111 | + expect(parsed.rules[0]).toEqual(LOUD_RULE); |
| 112 | + expect(parsed.vectorState).toEqual(PushRuleVectorState.LOUD); |
| 113 | + expect(parsed.externalRules.length).toEqual(1); |
| 114 | + expect(parsed.externalRules[0]).toEqual(NORMAL_RULE); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments