3 var assert = require('assert');
5 describe('room-membership-flags', function() {
8 underTest = require('../lib/room-membership-flags');
11 describe('getModeFromFlags', function() {
12 describe('strict', function() {
15 '1111101': 'all', // Ignore other values
17 '0010110': 'mute', // Ignore other values
18 '0001101': 'announcement',
19 '0011101': 'announcement', // Ignore other values
23 Object.keys(FIXTURES).forEach(function(flags) {
24 var mode = FIXTURES[flags];
26 it('should handle ' + flags + ' to ' + mode, function() {
27 var result = underTest.getModeFromFlags(parseInt(flags, 2), true);
28 assert.strictEqual(result, mode);
33 describe('not-strict', function() {
37 '1111101': 'all', // Ignore other values
40 '0010110': 'mute', // Ignore other values
41 '0001101': 'announcement',
42 '0011101': 'announcement' // Ignore other values
45 Object.keys(FIXTURES).forEach(function(flags) {
46 var mode = FIXTURES[flags];
48 it('should handle ' + flags + ' to ' + mode, function() {
49 var result = underTest.getModeFromFlags(parseInt(flags, 2), false);
50 assert.strictEqual(result, mode);
56 describe('getUpdateForMode', function() {
57 var UNTOUCHED_BITS = '111111111111111111111';
62 and: UNTOUCHED_BITS + '1101101',
67 'announcement-no-default': {
69 and: UNTOUCHED_BITS + '0001101',
74 'mention-no-default': {
76 and: UNTOUCHED_BITS + '0001101',
83 and: UNTOUCHED_BITS + '0000110',
89 // -------------------------------------------
93 and: UNTOUCHED_BITS + '1111101',
98 'announcement-is-default': {
100 and: UNTOUCHED_BITS + '0011101',
105 'mention-is-default': {
107 and: UNTOUCHED_BITS + '0011101',
114 and: UNTOUCHED_BITS + '0010110',
120 // -------------------------------------------
124 and: UNTOUCHED_BITS + '1101101',
129 'announcement-not-default': {
130 mode: 'announcement',
131 and: UNTOUCHED_BITS + '0001101',
136 'mention-not-default': {
138 and: UNTOUCHED_BITS + '0001101',
143 'mute-not-default': {
145 and: UNTOUCHED_BITS + '0000110',
153 '0000000000000000000000000000',
154 '1111111111111111111111111111',
155 '1010101010101010101010101010',
156 '1001001001001001001001001001'
159 Object.keys(FIXTURES).forEach(function(testName) {
160 var values = FIXTURES[testName];
161 var mode = values.mode;
163 it('should handle ' + testName, function() {
164 var result = underTest.getUpdateForMode(mode, values.isDefault);
165 assert.deepEqual(result, {
168 and: parseInt(values.and, 2),
169 or: parseInt(values.or, 2)
174 FLAG_VALUES.forEach(function(flags) {
175 var flagValue = parseInt(flags, 2);
176 // Test for bit idempotency
177 var result1 = (flagValue & parseInt(values.and, 2)) | parseInt(values.or, 2);
178 var result2 = (flagValue | parseInt(values.or, 2)) & parseInt(values.and, 2);
180 assert.strictEqual(result1.toString(2), result2.toString(2));
181 var newMode = underTest.getModeFromFlags(result1);
185 mode === 'mention' ? 'announcement' : mode,
186 'For flags ' + flags + ', expected mode ' + mode + ' got ' + newMode
193 describe('getLurkForFlags', function() {
200 '11110': true // Ignore other values
203 Object.keys(FIXTURES).forEach(function(flags) {
204 var isLurking = FIXTURES[flags];
206 it('should handle ' + flags, function() {
207 var result = underTest.getLurkForFlags(parseInt(flags, 2));
208 assert.strictEqual(result, isLurking);
213 describe('getLurkForMode', function() {
221 Object.keys(FIXTURES).forEach(function(mode) {
222 var isLurking = FIXTURES[mode];
224 it('should handle ' + mode, function() {
225 var result = underTest.getLurkForMode(mode);
226 assert.strictEqual(result, isLurking);
231 describe('getFlagsForMode', function() {
238 'announcement-default': {
239 mode: 'announcement',
254 /* ----------------------- */
261 'announcement-not-default': {
262 mode: 'announcement',
266 'mention-not-default': {
271 'mute-not-default': {
278 Object.keys(FIXTURES).forEach(function(testName) {
279 var values = FIXTURES[testName];
281 it('should handle ' + testName, function() {
282 var result = underTest.getFlagsForMode(values.mode, values.default);
283 assert.strictEqual(result.toString(2), values.value);
288 describe('toggleLegacyLurkMode', function() {
332 FIXTURES.forEach(function(values, index) {
333 it('should handle case ' + index, function() {
334 var result = underTest.toggleLegacyLurkMode(parseInt(values.flags, 2), values.lurk);
335 assert.strictEqual(result.toString(2), values.expected);