signal-desktop/ts/test-electron/background_test.ts

60 lines
1.8 KiB
TypeScript
Raw Normal View History

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', () => {
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);
});
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,
};
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);
});
it('filters out falsey items', async () => {
2021-05-28 19:11:19 +00:00
const startValue = {
one: 0,
two: Date.now(),
2021-05-28 19:11:19 +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);
assert.deepEqual(Object.keys(startValue), ['two']);
2021-05-28 19:11:19 +00:00
});
});