2021-05-28 19:11:19 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
2022-11-30 00:53:39 +00:00
|
|
|
import { pick } from 'lodash';
|
2021-05-28 19:11:19 +00:00
|
|
|
|
|
|
|
import { isOverHourIntoPast, cleanupSessionResets } from '../background';
|
|
|
|
|
|
|
|
describe('#isOverHourIntoPast', () => {
|
|
|
|
it('returns false for now', () => {
|
|
|
|
assert.isFalse(isOverHourIntoPast(Date.now()));
|
|
|
|
});
|
|
|
|
it('returns false for 5 minutes ago', () => {
|
|
|
|
const fiveMinutesAgo = Date.now() - 5 * 60 * 1000;
|
|
|
|
assert.isFalse(isOverHourIntoPast(fiveMinutesAgo));
|
|
|
|
});
|
|
|
|
it('returns true for 65 minutes ago', () => {
|
|
|
|
const sixtyFiveMinutesAgo = Date.now() - 65 * 60 * 1000;
|
|
|
|
assert.isTrue(isOverHourIntoPast(sixtyFiveMinutesAgo));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#cleanupSessionResets', () => {
|
2022-12-21 18:41:48 +00:00
|
|
|
it('leaves empty object alone', async () => {
|
|
|
|
await window.storage.put('sessionResets', {});
|
|
|
|
await cleanupSessionResets();
|
2021-05-28 19:11:19 +00:00
|
|
|
const actual = window.storage.get('sessionResets');
|
|
|
|
|
|
|
|
const expected = {};
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
});
|
2022-12-21 18:41:48 +00:00
|
|
|
it('filters out any timestamp older than one hour', async () => {
|
2021-05-28 19:11:19 +00:00
|
|
|
const startValue = {
|
|
|
|
one: Date.now() - 1,
|
|
|
|
two: Date.now(),
|
|
|
|
three: Date.now() - 65 * 60 * 1000,
|
|
|
|
};
|
2022-12-21 18:41:48 +00:00
|
|
|
await window.storage.put('sessionResets', startValue);
|
|
|
|
await cleanupSessionResets();
|
2021-05-28 19:11:19 +00:00
|
|
|
const actual = window.storage.get('sessionResets');
|
|
|
|
|
2022-11-30 00:53:39 +00:00
|
|
|
const expected = pick(startValue, ['one', 'two']);
|
2021-05-28 19:11:19 +00:00
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
});
|
2022-12-21 18:41:48 +00:00
|
|
|
it('filters out falsey items', async () => {
|
2021-05-28 19:11:19 +00:00
|
|
|
const startValue = {
|
|
|
|
one: 0,
|
2021-06-15 00:09:37 +00:00
|
|
|
two: Date.now(),
|
2021-05-28 19:11:19 +00:00
|
|
|
};
|
2022-12-21 18:41:48 +00:00
|
|
|
await window.storage.put('sessionResets', startValue);
|
|
|
|
await cleanupSessionResets();
|
2021-05-28 19:11:19 +00:00
|
|
|
const actual = window.storage.get('sessionResets');
|
|
|
|
|
2022-11-30 00:53:39 +00:00
|
|
|
const expected = pick(startValue, ['two']);
|
2021-05-28 19:11:19 +00:00
|
|
|
assert.deepEqual(actual, expected);
|
2021-06-15 00:09:37 +00:00
|
|
|
|
|
|
|
assert.deepEqual(Object.keys(startValue), ['two']);
|
2021-05-28 19:11:19 +00:00
|
|
|
});
|
|
|
|
});
|