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