signal-desktop/ts/test-node/util/groupSendEndorsements_test.ts
2024-10-18 14:51:45 -04:00

46 lines
1.7 KiB
TypeScript

// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import assert from 'node:assert/strict';
import { isValidGroupSendEndorsementsExpiration } from '../../util/groupSendEndorsements';
import { DAY, HOUR, SECOND } from '../../util/durations';
describe('groupSendEndorsements', () => {
describe('isValidGroupSendEndorsementsExpiration', () => {
function validateDistance(distance: number) {
const expiration = Date.now() + distance;
return isValidGroupSendEndorsementsExpiration(expiration);
}
function checkValid(label: string, distance: number) {
it(label, () => {
const actual = validateDistance(distance);
assert.equal(actual.valid, true);
assert.equal(actual.reason, undefined);
});
}
function checkInvalid(label: string, distance: number, reason: RegExp) {
it(label, () => {
const actual = validateDistance(distance);
assert.equal(actual.valid, false);
assert.match(actual.reason, reason);
});
}
const TWO_HOURS = HOUR * 2;
const TWO_DAYS = DAY * 2;
checkInvalid('2d ago', -TWO_DAYS, /already expired/);
checkInvalid('2h ago', -TWO_HOURS, /already expired/);
checkInvalid('1s ago', -SECOND, /already expired/);
checkInvalid('now', 0, /already expired/);
checkInvalid('in 1s', SECOND, /expires soon/);
checkInvalid('in <2h', TWO_HOURS - SECOND, /expires soon/);
checkInvalid('in 2h', TWO_HOURS, /expires soon/);
checkValid('in >2h', TWO_HOURS + SECOND);
checkValid('in <2d', TWO_DAYS - SECOND);
checkInvalid('in 2d', TWO_DAYS, /expires too far in future/);
checkInvalid('in >2d', TWO_DAYS + SECOND, /expires too far in future/);
});
});