signal-desktop/ts/test-both/groups/message_merge_test.ts

219 lines
5.3 KiB
TypeScript
Raw Normal View History

// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { v4 as generateUuid } from 'uuid';
import { generateAci } from '../../types/ServiceId';
import {
_isGroupChangeMessageBounceable,
_mergeGroupChangeMessages,
} from '../../groups';
describe('group message merging', () => {
const defaultMessage = {
id: generateUuid(),
conversationId: generateUuid(),
timestamp: Date.now(),
sent_at: Date.now(),
received_at: Date.now(),
};
2023-08-16 20:54:39 +00:00
const aci = generateAci();
describe('_isGroupChangeMessageBounceable', () => {
it('should return true for admin approval add', () => {
assert.isTrue(
_isGroupChangeMessageBounceable({
...defaultMessage,
type: 'group-v2-change',
groupV2Change: {
details: [
{
type: 'admin-approval-add-one',
2023-08-16 20:54:39 +00:00
aci,
},
],
},
})
);
});
it('should return true for bounce message', () => {
assert.isTrue(
_isGroupChangeMessageBounceable({
...defaultMessage,
type: 'group-v2-change',
groupV2Change: {
details: [
{
type: 'admin-approval-bounce',
times: 1,
isApprovalPending: true,
2023-08-16 20:54:39 +00:00
aci,
},
],
},
})
);
});
it('should return false otherwise', () => {
assert.isFalse(
_isGroupChangeMessageBounceable({
...defaultMessage,
type: 'group-v2-change',
groupV2Change: {
details: [
{
type: 'admin-approval-remove-one',
2023-08-16 20:54:39 +00:00
aci,
},
],
},
})
);
});
});
describe('_mergeGroupChangeMessages', () => {
const add = {
...defaultMessage,
type: 'group-v2-change' as const,
groupV2Change: {
details: [
{
type: 'admin-approval-add-one' as const,
2023-08-16 20:54:39 +00:00
aci,
},
],
},
};
const remove = {
...defaultMessage,
type: 'group-v2-change' as const,
groupV2Change: {
details: [
{
type: 'admin-approval-remove-one' as const,
2023-08-16 20:54:39 +00:00
aci,
},
],
},
};
const addOther = {
...defaultMessage,
type: 'group-v2-change' as const,
groupV2Change: {
details: [
{
type: 'admin-approval-add-one' as const,
2023-08-16 20:54:39 +00:00
aci: generateAci(),
},
],
},
};
const removeOther = {
...defaultMessage,
type: 'group-v2-change' as const,
groupV2Change: {
details: [
{
type: 'admin-approval-remove-one' as const,
2023-08-16 20:54:39 +00:00
aci: generateAci(),
},
],
},
};
const bounce = {
...defaultMessage,
type: 'group-v2-change' as const,
groupV2Change: {
details: [
{
type: 'admin-approval-bounce' as const,
times: 1,
isApprovalPending: false,
2023-08-16 20:54:39 +00:00
aci,
},
],
},
};
const bounceAndAdd = {
...defaultMessage,
type: 'group-v2-change' as const,
groupV2Change: {
details: [
{
type: 'admin-approval-bounce' as const,
times: 1,
isApprovalPending: true,
2023-08-16 20:54:39 +00:00
aci,
},
],
},
};
2023-08-16 20:54:39 +00:00
it('should merge add with remove if aci matches', () => {
assert.deepStrictEqual(
_mergeGroupChangeMessages(add, remove)?.groupV2Change?.details,
[
{
isApprovalPending: false,
times: 1,
type: 'admin-approval-bounce',
2023-08-16 20:54:39 +00:00
aci,
},
]
);
});
2023-08-16 20:54:39 +00:00
it('should not merge add with remove if aci does not match', () => {
assert.isUndefined(_mergeGroupChangeMessages(add, removeOther));
});
2023-08-16 20:54:39 +00:00
it('should merge bounce with add if aci matches', () => {
assert.deepStrictEqual(
_mergeGroupChangeMessages(bounce, add)?.groupV2Change?.details,
[
{
isApprovalPending: true,
times: 1,
type: 'admin-approval-bounce',
2023-08-16 20:54:39 +00:00
aci,
},
]
);
});
2023-08-16 20:54:39 +00:00
it('should merge bounce and add with remove if aci matches', () => {
assert.deepStrictEqual(
_mergeGroupChangeMessages(bounceAndAdd, remove)?.groupV2Change?.details,
[
{
isApprovalPending: false,
times: 2,
type: 'admin-approval-bounce',
2023-08-16 20:54:39 +00:00
aci,
},
]
);
});
2023-08-16 20:54:39 +00:00
it('should not merge bounce with add if aci does not match', () => {
assert.isUndefined(_mergeGroupChangeMessages(bounce, addOther));
});
it('should not merge bounce and add with add', () => {
assert.isUndefined(_mergeGroupChangeMessages(bounceAndAdd, add));
});
2023-08-16 20:54:39 +00:00
it('should not merge bounce and add with remove if aci does not match', () => {
assert.isUndefined(_mergeGroupChangeMessages(bounceAndAdd, removeOther));
});
it('should not merge bounce with remove', () => {
assert.isUndefined(_mergeGroupChangeMessages(bounce, remove));
});
});
});