2021-12-08 19:52:46 +00:00
|
|
|
// Copyright 2021 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';
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
import dataInterface from '../../sql/Client';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { generateAci } from '../../types/ServiceId';
|
|
|
|
import { generateStoryDistributionId } from '../../types/StoryDistributionId';
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
import type { StoryDistributionWithMembersType } from '../../sql/Interface';
|
|
|
|
|
|
|
|
const {
|
|
|
|
_deleteAllStoryDistributions,
|
|
|
|
_getAllStoryDistributionMembers,
|
|
|
|
_getAllStoryDistributions,
|
|
|
|
createNewStoryDistribution,
|
|
|
|
deleteStoryDistribution,
|
|
|
|
getAllStoryDistributionsWithMembers,
|
2021-12-10 02:15:59 +00:00
|
|
|
modifyStoryDistribution,
|
2021-12-08 19:52:46 +00:00
|
|
|
modifyStoryDistributionMembers,
|
2022-07-01 00:52:03 +00:00
|
|
|
modifyStoryDistributionWithMembers,
|
2021-12-08 19:52:46 +00:00
|
|
|
} = dataInterface;
|
|
|
|
|
|
|
|
describe('sql/storyDistribution', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await _deleteAllStoryDistributions();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('roundtrips with create/fetch/delete', async () => {
|
|
|
|
const list: StoryDistributionWithMembersType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateStoryDistributionId(),
|
2021-12-08 19:52:46 +00:00
|
|
|
name: 'My Story',
|
2022-07-01 00:52:03 +00:00
|
|
|
allowsReplies: true,
|
|
|
|
isBlockList: false,
|
2023-08-10 16:43:33 +00:00
|
|
|
members: [generateAci(), generateAci()],
|
2021-12-08 19:52:46 +00:00
|
|
|
senderKeyInfo: {
|
|
|
|
createdAtDate: Date.now(),
|
2023-08-10 16:43:33 +00:00
|
|
|
distributionId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
memberDevices: [],
|
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
storageID: generateUuid(),
|
2022-07-01 00:52:03 +00:00
|
|
|
storageVersion: 1,
|
|
|
|
storageNeedsSync: false,
|
|
|
|
storageUnknownFields: undefined,
|
|
|
|
deletedAtTimestamp: undefined,
|
2021-12-08 19:52:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
await createNewStoryDistribution(list);
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllStoryDistributions(), 1);
|
|
|
|
assert.lengthOf(await _getAllStoryDistributionMembers(), 2);
|
|
|
|
|
|
|
|
const allHydratedLists = await getAllStoryDistributionsWithMembers();
|
|
|
|
assert.lengthOf(allHydratedLists, 1);
|
|
|
|
assert.deepEqual(allHydratedLists[0], list);
|
|
|
|
|
|
|
|
await deleteStoryDistribution(list.id);
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllStoryDistributions(), 0);
|
|
|
|
assert.lengthOf(await _getAllStoryDistributionMembers(), 0);
|
|
|
|
assert.lengthOf(await getAllStoryDistributionsWithMembers(), 0);
|
2021-12-10 02:15:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('updates core fields with modifyStoryDistribution', async () => {
|
2023-08-10 16:43:33 +00:00
|
|
|
const SERVICE_ID_1 = generateAci();
|
|
|
|
const SERVICE_ID_2 = generateAci();
|
2021-12-10 02:15:59 +00:00
|
|
|
const list: StoryDistributionWithMembersType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateStoryDistributionId(),
|
2021-12-10 02:15:59 +00:00
|
|
|
name: 'My Story',
|
2022-07-01 00:52:03 +00:00
|
|
|
allowsReplies: true,
|
|
|
|
isBlockList: false,
|
2023-08-10 16:43:33 +00:00
|
|
|
members: [SERVICE_ID_1, SERVICE_ID_2],
|
2021-12-10 02:15:59 +00:00
|
|
|
senderKeyInfo: {
|
|
|
|
createdAtDate: Date.now(),
|
2023-08-10 16:43:33 +00:00
|
|
|
distributionId: generateUuid(),
|
2021-12-10 02:15:59 +00:00
|
|
|
memberDevices: [],
|
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
storageID: generateUuid(),
|
2022-07-01 00:52:03 +00:00
|
|
|
storageVersion: 1,
|
|
|
|
storageNeedsSync: false,
|
|
|
|
storageUnknownFields: undefined,
|
|
|
|
deletedAtTimestamp: undefined,
|
2021-12-10 02:15:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
await createNewStoryDistribution(list);
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllStoryDistributions(), 1);
|
|
|
|
assert.lengthOf(await _getAllStoryDistributionMembers(), 2);
|
|
|
|
|
|
|
|
const updated = {
|
|
|
|
...list,
|
|
|
|
name: 'Updated story',
|
|
|
|
senderKeyInfo: {
|
|
|
|
createdAtDate: Date.now() + 10,
|
2023-08-10 16:43:33 +00:00
|
|
|
distributionId: generateUuid(),
|
2021-12-10 02:15:59 +00:00
|
|
|
memberDevices: [
|
|
|
|
{
|
|
|
|
id: 1,
|
2023-08-10 16:43:33 +00:00
|
|
|
identifier: SERVICE_ID_1,
|
2021-12-10 02:15:59 +00:00
|
|
|
registrationId: 232,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
await modifyStoryDistribution(updated);
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllStoryDistributions(), 1);
|
|
|
|
assert.lengthOf(await _getAllStoryDistributionMembers(), 2);
|
|
|
|
|
|
|
|
const allHydratedLists = await getAllStoryDistributionsWithMembers();
|
|
|
|
assert.lengthOf(allHydratedLists, 1);
|
|
|
|
assert.deepEqual(allHydratedLists[0], updated);
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('adds and removes with modifyStoryDistributionMembers', async () => {
|
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();
|
2021-12-08 19:52:46 +00:00
|
|
|
const list: StoryDistributionWithMembersType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateStoryDistributionId(),
|
2021-12-08 19:52:46 +00:00
|
|
|
name: 'My Story',
|
2022-07-01 00:52:03 +00:00
|
|
|
allowsReplies: true,
|
|
|
|
isBlockList: false,
|
2023-08-10 16:43:33 +00:00
|
|
|
members: [SERVICE_ID_1, SERVICE_ID_2],
|
2021-12-08 19:52:46 +00:00
|
|
|
senderKeyInfo: {
|
|
|
|
createdAtDate: Date.now(),
|
2023-08-10 16:43:33 +00:00
|
|
|
distributionId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
memberDevices: [],
|
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
storageID: generateUuid(),
|
2022-07-01 00:52:03 +00:00
|
|
|
storageVersion: 1,
|
|
|
|
storageNeedsSync: false,
|
|
|
|
storageUnknownFields: undefined,
|
|
|
|
deletedAtTimestamp: undefined,
|
2021-12-08 19:52:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
await createNewStoryDistribution(list);
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllStoryDistributions(), 1);
|
|
|
|
assert.lengthOf(await _getAllStoryDistributionMembers(), 2);
|
|
|
|
|
|
|
|
await modifyStoryDistributionMembers(list.id, {
|
2023-08-10 16:43:33 +00:00
|
|
|
toAdd: [SERVICE_ID_3, SERVICE_ID_4],
|
|
|
|
toRemove: [SERVICE_ID_1],
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllStoryDistributions(), 1);
|
|
|
|
assert.lengthOf(await _getAllStoryDistributionMembers(), 3);
|
|
|
|
|
|
|
|
const allHydratedLists = await getAllStoryDistributionsWithMembers();
|
|
|
|
assert.lengthOf(allHydratedLists, 1);
|
|
|
|
assert.deepEqual(allHydratedLists[0], {
|
|
|
|
...list,
|
2023-08-10 16:43:33 +00:00
|
|
|
members: [SERVICE_ID_2, SERVICE_ID_3, SERVICE_ID_4],
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
it('adds and removes with modifyStoryDistributionWithMembers', async () => {
|
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-07-01 00:52:03 +00:00
|
|
|
const list: StoryDistributionWithMembersType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateStoryDistributionId(),
|
2022-07-01 00:52:03 +00:00
|
|
|
name: 'My Story',
|
|
|
|
allowsReplies: true,
|
|
|
|
isBlockList: false,
|
2023-08-10 16:43:33 +00:00
|
|
|
members: [SERVICE_ID_1, SERVICE_ID_2],
|
2022-07-01 00:52:03 +00:00
|
|
|
senderKeyInfo: {
|
|
|
|
createdAtDate: Date.now(),
|
2023-08-10 16:43:33 +00:00
|
|
|
distributionId: generateUuid(),
|
2022-07-01 00:52:03 +00:00
|
|
|
memberDevices: [],
|
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
storageID: generateUuid(),
|
2022-07-01 00:52:03 +00:00
|
|
|
storageVersion: 1,
|
|
|
|
storageNeedsSync: false,
|
|
|
|
storageUnknownFields: undefined,
|
|
|
|
deletedAtTimestamp: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
await createNewStoryDistribution(list);
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllStoryDistributions(), 1);
|
|
|
|
assert.lengthOf(await _getAllStoryDistributionMembers(), 2);
|
|
|
|
|
|
|
|
await modifyStoryDistributionWithMembers(list, {
|
2023-08-10 16:43:33 +00:00
|
|
|
toAdd: [SERVICE_ID_3, SERVICE_ID_4],
|
|
|
|
toRemove: [SERVICE_ID_1],
|
2022-07-01 00:52:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllStoryDistributions(), 1);
|
|
|
|
assert.lengthOf(await _getAllStoryDistributionMembers(), 3);
|
|
|
|
|
|
|
|
const allHydratedLists = await getAllStoryDistributionsWithMembers();
|
|
|
|
assert.lengthOf(allHydratedLists, 1);
|
|
|
|
assert.deepEqual(allHydratedLists[0], {
|
|
|
|
...list,
|
2023-08-10 16:43:33 +00:00
|
|
|
members: [SERVICE_ID_2, SERVICE_ID_3, SERVICE_ID_4],
|
2022-07-01 00:52:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
it('eliminates duplicates without complaint in createNewStoryDistribution', async () => {
|
2023-08-10 16:43:33 +00:00
|
|
|
const SERVICE_ID_1 = generateAci();
|
|
|
|
const SERVICE_ID_2 = generateAci();
|
2021-12-08 19:52:46 +00:00
|
|
|
const list: StoryDistributionWithMembersType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateStoryDistributionId(),
|
2021-12-08 19:52:46 +00:00
|
|
|
name: 'My Story',
|
2022-07-01 00:52:03 +00:00
|
|
|
allowsReplies: true,
|
|
|
|
isBlockList: false,
|
2023-08-10 16:43:33 +00:00
|
|
|
members: [SERVICE_ID_1, SERVICE_ID_1, SERVICE_ID_2],
|
2021-12-08 19:52:46 +00:00
|
|
|
senderKeyInfo: {
|
|
|
|
createdAtDate: Date.now(),
|
2023-08-10 16:43:33 +00:00
|
|
|
distributionId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
memberDevices: [],
|
|
|
|
},
|
2023-08-10 16:43:33 +00:00
|
|
|
storageID: generateUuid(),
|
2022-07-01 00:52:03 +00:00
|
|
|
storageVersion: 1,
|
|
|
|
storageNeedsSync: false,
|
|
|
|
storageUnknownFields: undefined,
|
|
|
|
deletedAtTimestamp: undefined,
|
2021-12-08 19:52:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
await createNewStoryDistribution(list);
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllStoryDistributions(), 1);
|
|
|
|
assert.lengthOf(await _getAllStoryDistributionMembers(), 2);
|
|
|
|
|
|
|
|
const allHydratedLists = await getAllStoryDistributionsWithMembers();
|
|
|
|
assert.lengthOf(allHydratedLists, 1);
|
2023-08-10 16:43:33 +00:00
|
|
|
assert.deepEqual(allHydratedLists[0].members, [SERVICE_ID_1, SERVICE_ID_2]);
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
});
|