|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +const REMINDERS = ['first', 'second', 'third']; |
| 6 | +const EXPECTED_CREATE_DELETE_RESULT = REMINDERS.reduce( |
| 7 | + (expected: any, reminder) => { |
| 8 | + expected[reminder] = 1; |
| 9 | + return expected; |
| 10 | + }, |
| 11 | + {} as Record<string, number> |
| 12 | +); |
| 13 | + |
| 14 | +const config = require('../config').default.getProperties(); |
| 15 | +const mocks = require('../test/mocks'); |
| 16 | + |
| 17 | +describe('#integration - lib/cad-reminders', () => { |
| 18 | + let log: any, mockConfig: any, redis: any, cadReminders: any; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + jest.resetModules(); |
| 22 | + log = mocks.mockLog(); |
| 23 | + mockConfig = { |
| 24 | + redis: config.redis, |
| 25 | + cadReminders: { |
| 26 | + rolloutRate: 1, |
| 27 | + firstInterval: 1, |
| 28 | + secondInterval: 2, |
| 29 | + thirdInterval: 60000, |
| 30 | + redis: { |
| 31 | + maxConnections: 1, |
| 32 | + minConnections: 1, |
| 33 | + prefix: 'test-cad-reminders:', |
| 34 | + }, |
| 35 | + }, |
| 36 | + }; |
| 37 | + redis = require('./redis')( |
| 38 | + { |
| 39 | + ...config.redis, |
| 40 | + ...mockConfig.cadReminders.redis, |
| 41 | + enabled: true, |
| 42 | + }, |
| 43 | + mocks.mockLog() |
| 44 | + ); |
| 45 | + cadReminders = require('./cad-reminders')(mockConfig, log); |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(async () => { |
| 49 | + await redis.close(); |
| 50 | + await cadReminders.close(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returned the expected interface', () => { |
| 54 | + expect(cadReminders).toBeDefined(); |
| 55 | + expect(Object.keys(cadReminders)).toHaveLength(5); |
| 56 | + expect(cadReminders.keys).toEqual(['first', 'second', 'third']); |
| 57 | + expect(typeof cadReminders.create).toBe('function'); |
| 58 | + expect(cadReminders.create).toHaveLength(1); |
| 59 | + expect(typeof cadReminders.delete).toBe('function'); |
| 60 | + expect(cadReminders.delete).toHaveLength(1); |
| 61 | + expect(typeof cadReminders.process).toBe('function'); |
| 62 | + expect(cadReminders.process).toHaveLength(0); |
| 63 | + expect(typeof cadReminders.get).toBe('function'); |
| 64 | + expect(cadReminders.get).toHaveLength(1); |
| 65 | + expect(typeof cadReminders.close).toBe('function'); |
| 66 | + expect(cadReminders.close).toHaveLength(0); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('create', () => { |
| 70 | + let before: number, createResult: any; |
| 71 | + |
| 72 | + beforeEach(async () => { |
| 73 | + before = Date.now(); |
| 74 | + createResult = await cadReminders.create('wibble', before - 1); |
| 75 | + }); |
| 76 | + |
| 77 | + afterEach(async () => { |
| 78 | + await cadReminders.delete('wibble'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('returned the correct result', () => { |
| 82 | + expect(createResult).toEqual(EXPECTED_CREATE_DELETE_RESULT); |
| 83 | + }); |
| 84 | + |
| 85 | + it.each(REMINDERS)('wrote %s reminder to redis', async (reminder) => { |
| 86 | + const reminders = await redis.zrange(reminder, 0, -1); |
| 87 | + expect(reminders).toEqual(['wibble']); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('delete', () => { |
| 91 | + let deleteResult: any; |
| 92 | + |
| 93 | + beforeEach(async () => { |
| 94 | + deleteResult = await cadReminders.delete('wibble'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('returned the correct result', () => { |
| 98 | + expect(deleteResult).toEqual(EXPECTED_CREATE_DELETE_RESULT); |
| 99 | + }); |
| 100 | + |
| 101 | + it.each(REMINDERS)( |
| 102 | + 'removed %s reminder from redis', |
| 103 | + async (reminder) => { |
| 104 | + const reminders = await redis.zrange(reminder, 0, -1); |
| 105 | + expect(reminders).toHaveLength(0); |
| 106 | + } |
| 107 | + ); |
| 108 | + |
| 109 | + it('did not call log.error', () => { |
| 110 | + expect(log.error.callCount).toBe(0); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('get', () => { |
| 115 | + let result: any; |
| 116 | + |
| 117 | + beforeEach(async () => { |
| 118 | + result = await cadReminders.get('wibble'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('returned the correct result', () => { |
| 122 | + expect(result).toEqual({ first: 0, second: 0, third: 0 }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('did not call log.error', () => { |
| 126 | + expect(log.error.callCount).toBe(0); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + describe('process', () => { |
| 131 | + let processResult: any; |
| 132 | + |
| 133 | + beforeEach(async () => { |
| 134 | + await cadReminders.create('blee', before); |
| 135 | + processResult = await cadReminders.process(before + 2); |
| 136 | + }); |
| 137 | + |
| 138 | + afterEach(async () => { |
| 139 | + await cadReminders.delete('blee'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('returned the correct result', () => { |
| 143 | + expect(processResult).toBeDefined(); |
| 144 | + |
| 145 | + expect(processResult.first).toHaveLength(2); |
| 146 | + expect(processResult.first[0].uid).toBe('wibble'); |
| 147 | + expect(parseInt(processResult.first[0].timestamp)).toBeGreaterThan( |
| 148 | + before - 1000 |
| 149 | + ); |
| 150 | + expect(parseInt(processResult.first[0].timestamp)).toBeLessThan( |
| 151 | + before |
| 152 | + ); |
| 153 | + expect(processResult.first[1].uid).toBe('blee'); |
| 154 | + expect(parseInt(processResult.first[1].timestamp)).toBeGreaterThanOrEqual( |
| 155 | + before |
| 156 | + ); |
| 157 | + expect(parseInt(processResult.first[1].timestamp)).toBeLessThan( |
| 158 | + before + 1000 |
| 159 | + ); |
| 160 | + |
| 161 | + expect(processResult.second).toHaveLength(2); |
| 162 | + expect(processResult.second[0].uid).toBe('wibble'); |
| 163 | + expect(processResult.second[0].timestamp).toBe( |
| 164 | + processResult.first[0].timestamp |
| 165 | + ); |
| 166 | + expect(processResult.second[1].uid).toBe('blee'); |
| 167 | + expect(processResult.second[1].timestamp).toBe( |
| 168 | + processResult.first[1].timestamp |
| 169 | + ); |
| 170 | + |
| 171 | + expect(processResult.third).toEqual([]); |
| 172 | + }); |
| 173 | + |
| 174 | + it.each(['first', 'second'])( |
| 175 | + 'removed %s reminder from redis correctly', |
| 176 | + async (reminder) => { |
| 177 | + const reminders = await redis.zrange(reminder, 0, -1); |
| 178 | + expect(reminders).toHaveLength(0); |
| 179 | + } |
| 180 | + ); |
| 181 | + |
| 182 | + it('left the third reminders in redis', async () => { |
| 183 | + const reminders = await redis.zrange('third', 0, -1); |
| 184 | + expect(new Set(reminders)).toEqual(new Set(['wibble', 'blee'])); |
| 185 | + }); |
| 186 | + |
| 187 | + it('did not call log.error', () => { |
| 188 | + expect(log.error.callCount).toBe(0); |
| 189 | + }); |
| 190 | + }); |
| 191 | + }); |
| 192 | +}); |
0 commit comments