2022-11-11 04:10:30 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
|
|
|
|
import type { RecipientsByConversation } from '../../state/ducks/stories';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { ServiceIdString } from '../../types/ServiceId';
|
2022-11-11 04:10:30 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
import { generateAci } from '../../types/ServiceId';
|
|
|
|
import { generateStoryDistributionId } from '../../types/StoryDistributionId';
|
2022-11-11 04:10:30 +00:00
|
|
|
import {
|
2023-08-10 16:43:33 +00:00
|
|
|
getAllServiceIds,
|
|
|
|
filterServiceIds,
|
2022-11-11 04:10:30 +00:00
|
|
|
} from '../../util/blockSendUntilConversationsAreVerified';
|
|
|
|
|
|
|
|
describe('both/util/blockSendUntilConversationsAreVerified', () => {
|
2023-08-10 16:43:33 +00:00
|
|
|
const SERVICE_ID_1 = generateAci();
|
|
|
|
const SERVICE_ID_2 = generateAci();
|
|
|
|
const SERVICE_ID_3 = generateAci();
|
|
|
|
const SERVICE_ID_4 = generateAci();
|
2022-11-11 04:10:30 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const LIST_ID_1 = generateStoryDistributionId();
|
|
|
|
const LIST_ID_2 = generateStoryDistributionId();
|
|
|
|
const LIST_ID_3 = generateStoryDistributionId();
|
|
|
|
|
|
|
|
describe('#getAllServiceIds', () => {
|
2022-11-11 04:10:30 +00:00
|
|
|
it('should return empty set for empty object', () => {
|
|
|
|
const starting: RecipientsByConversation = {};
|
2023-08-10 16:43:33 +00:00
|
|
|
const expected: Array<ServiceIdString> = [];
|
|
|
|
const actual = getAllServiceIds(starting);
|
2022-11-11 04:10:30 +00:00
|
|
|
|
|
|
|
assert.sameMembers(Array.from(actual), expected);
|
|
|
|
});
|
2023-08-10 16:43:33 +00:00
|
|
|
it('should return serviceIds multiple conversations', () => {
|
2022-11-11 04:10:30 +00:00
|
|
|
const starting: RecipientsByConversation = {
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_1]: {
|
|
|
|
serviceIds: [SERVICE_ID_1, SERVICE_ID_2],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_2]: {
|
|
|
|
serviceIds: [],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_3]: {
|
|
|
|
serviceIds: [SERVICE_ID_2, SERVICE_ID_3],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
|
|
|
};
|
2023-08-10 16:43:33 +00:00
|
|
|
const expected: Array<ServiceIdString> = [
|
|
|
|
SERVICE_ID_1,
|
|
|
|
SERVICE_ID_2,
|
|
|
|
SERVICE_ID_3,
|
|
|
|
];
|
|
|
|
const actual = getAllServiceIds(starting);
|
2022-11-11 04:10:30 +00:00
|
|
|
|
|
|
|
assert.sameMembers(Array.from(actual), expected);
|
|
|
|
});
|
2023-08-10 16:43:33 +00:00
|
|
|
it('should return serviceIds from byDistributionId and its parent', () => {
|
2022-11-11 04:10:30 +00:00
|
|
|
const starting: RecipientsByConversation = {
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_1]: {
|
|
|
|
serviceIds: [SERVICE_ID_1, SERVICE_ID_2],
|
2022-11-11 04:10:30 +00:00
|
|
|
byDistributionId: {
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_1]: {
|
|
|
|
serviceIds: [SERVICE_ID_3],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_2]: {
|
|
|
|
serviceIds: [],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_3]: {
|
|
|
|
serviceIds: [SERVICE_ID_4],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2023-08-10 16:43:33 +00:00
|
|
|
const expected: Array<ServiceIdString> = [
|
|
|
|
SERVICE_ID_1,
|
|
|
|
SERVICE_ID_2,
|
|
|
|
SERVICE_ID_3,
|
|
|
|
SERVICE_ID_4,
|
|
|
|
];
|
|
|
|
const actual = getAllServiceIds(starting);
|
2022-11-11 04:10:30 +00:00
|
|
|
|
|
|
|
assert.sameMembers(Array.from(actual), expected);
|
|
|
|
});
|
2023-08-10 16:43:33 +00:00
|
|
|
it('should return serviceIds from byDistributionId with empty parent', () => {
|
2022-11-11 04:10:30 +00:00
|
|
|
const starting: RecipientsByConversation = {
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_1]: {
|
|
|
|
serviceIds: [],
|
2022-11-11 04:10:30 +00:00
|
|
|
byDistributionId: {
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_1]: {
|
|
|
|
serviceIds: [SERVICE_ID_3],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2023-08-10 16:43:33 +00:00
|
|
|
const expected: Array<ServiceIdString> = [SERVICE_ID_3];
|
|
|
|
const actual = getAllServiceIds(starting);
|
2022-11-11 04:10:30 +00:00
|
|
|
|
|
|
|
assert.sameMembers(Array.from(actual), expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
describe('#filterServiceIds', () => {
|
2022-11-11 04:10:30 +00:00
|
|
|
const starting: RecipientsByConversation = {
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_1]: {
|
|
|
|
serviceIds: [SERVICE_ID_1],
|
2022-11-11 04:10:30 +00:00
|
|
|
byDistributionId: {
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_1]: {
|
|
|
|
serviceIds: [SERVICE_ID_2, SERVICE_ID_3],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_2]: {
|
|
|
|
serviceIds: [SERVICE_ID_1],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_2]: {
|
|
|
|
serviceIds: [SERVICE_ID_1, SERVICE_ID_4],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_3]: {
|
|
|
|
serviceIds: [SERVICE_ID_3],
|
2022-11-11 04:10:30 +00:00
|
|
|
byDistributionId: {
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_1]: {
|
|
|
|
serviceIds: [SERVICE_ID_4],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
it('should return empty object if predicate always returns false', () => {
|
|
|
|
const expected: RecipientsByConversation = {};
|
2023-08-10 16:43:33 +00:00
|
|
|
const actual = filterServiceIds(starting, () => false);
|
2022-11-11 04:10:30 +00:00
|
|
|
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
});
|
|
|
|
it('should return exact copy of object if predicate always returns true', () => {
|
|
|
|
const expected = starting;
|
2023-08-10 16:43:33 +00:00
|
|
|
const actual = filterServiceIds(starting, () => true);
|
2022-11-11 04:10:30 +00:00
|
|
|
|
|
|
|
assert.notStrictEqual(actual, expected);
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
});
|
2023-08-10 16:43:33 +00:00
|
|
|
it('should return just a few serviceIds for selective predicate', () => {
|
2022-11-11 04:10:30 +00:00
|
|
|
const expected: RecipientsByConversation = {
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_1]: {
|
|
|
|
serviceIds: [],
|
2022-11-11 04:10:30 +00:00
|
|
|
byDistributionId: {
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_1]: {
|
|
|
|
serviceIds: [SERVICE_ID_2, SERVICE_ID_3],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
[LIST_ID_3]: {
|
|
|
|
serviceIds: [SERVICE_ID_3],
|
2022-11-11 04:10:30 +00:00
|
|
|
},
|
|
|
|
};
|
2023-08-10 16:43:33 +00:00
|
|
|
const actual = filterServiceIds(
|
2022-11-11 04:10:30 +00:00
|
|
|
starting,
|
2023-08-10 16:43:33 +00:00
|
|
|
(uuid: ServiceIdString) =>
|
|
|
|
uuid === SERVICE_ID_2 || uuid === SERVICE_ID_3
|
2022-11-11 04:10:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|