signal-desktop/ts/test-electron/sql/storyDistribution_test.ts

239 lines
7.4 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { v4 as generateUuid } from 'uuid';
2024-07-22 18:16:33 +00:00
import { DataReader, DataWriter } from '../../sql/Client';
import { generateAci } from '../../types/ServiceId';
import { generateStoryDistributionId } from '../../types/StoryDistributionId';
import type { StoryDistributionWithMembersType } from '../../sql/Interface';
const {
_getAllStoryDistributionMembers,
_getAllStoryDistributions,
2024-07-22 18:16:33 +00:00
getAllStoryDistributionsWithMembers,
} = DataReader;
const {
_deleteAllStoryDistributions,
createNewStoryDistribution,
deleteStoryDistribution,
modifyStoryDistribution,
modifyStoryDistributionMembers,
2022-07-01 00:52:03 +00:00
modifyStoryDistributionWithMembers,
2024-07-22 18:16:33 +00:00
} = DataWriter;
describe('sql/storyDistribution', () => {
beforeEach(async () => {
await _deleteAllStoryDistributions();
});
it('roundtrips with create/fetch/delete', async () => {
const list: StoryDistributionWithMembersType = {
id: generateStoryDistributionId(),
name: 'My Story',
2022-07-01 00:52:03 +00:00
allowsReplies: true,
isBlockList: false,
members: [generateAci(), generateAci()],
senderKeyInfo: {
createdAtDate: Date.now(),
distributionId: generateUuid(),
memberDevices: [],
},
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);
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);
});
it('updates core fields with modifyStoryDistribution', async () => {
const SERVICE_ID_1 = generateAci();
const SERVICE_ID_2 = generateAci();
const list: StoryDistributionWithMembersType = {
id: generateStoryDistributionId(),
name: 'My Story',
2022-07-01 00:52:03 +00:00
allowsReplies: true,
isBlockList: false,
members: [SERVICE_ID_1, SERVICE_ID_2],
senderKeyInfo: {
createdAtDate: Date.now(),
distributionId: generateUuid(),
memberDevices: [],
},
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);
const updated = {
...list,
name: 'Updated story',
senderKeyInfo: {
createdAtDate: Date.now() + 10,
distributionId: generateUuid(),
memberDevices: [
{
id: 1,
2023-08-16 20:54:39 +00:00
serviceId: SERVICE_ID_1,
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);
});
it('adds and removes with modifyStoryDistributionMembers', async () => {
const SERVICE_ID_1 = generateAci();
const SERVICE_ID_2 = generateAci();
const SERVICE_ID_3 = generateAci();
const SERVICE_ID_4 = generateAci();
const list: StoryDistributionWithMembersType = {
id: generateStoryDistributionId(),
name: 'My Story',
2022-07-01 00:52:03 +00:00
allowsReplies: true,
isBlockList: false,
members: [SERVICE_ID_1, SERVICE_ID_2],
senderKeyInfo: {
createdAtDate: Date.now(),
distributionId: generateUuid(),
memberDevices: [],
},
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 modifyStoryDistributionMembers(list.id, {
toAdd: [SERVICE_ID_3, SERVICE_ID_4],
toRemove: [SERVICE_ID_1],
});
assert.lengthOf(await _getAllStoryDistributions(), 1);
assert.lengthOf(await _getAllStoryDistributionMembers(), 3);
const allHydratedLists = await getAllStoryDistributionsWithMembers();
assert.lengthOf(allHydratedLists, 1);
assert.deepEqual(allHydratedLists[0], {
...list,
members: [SERVICE_ID_2, SERVICE_ID_3, SERVICE_ID_4],
});
});
2022-07-01 00:52:03 +00:00
it('adds and removes with modifyStoryDistributionWithMembers', async () => {
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 = {
id: generateStoryDistributionId(),
2022-07-01 00:52:03 +00:00
name: 'My Story',
allowsReplies: true,
isBlockList: false,
members: [SERVICE_ID_1, SERVICE_ID_2],
2022-07-01 00:52:03 +00:00
senderKeyInfo: {
createdAtDate: Date.now(),
distributionId: generateUuid(),
2022-07-01 00:52:03 +00:00
memberDevices: [],
},
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, {
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,
members: [SERVICE_ID_2, SERVICE_ID_3, SERVICE_ID_4],
2022-07-01 00:52:03 +00:00
});
});
it('eliminates duplicates without complaint in createNewStoryDistribution', async () => {
const SERVICE_ID_1 = generateAci();
const SERVICE_ID_2 = generateAci();
const list: StoryDistributionWithMembersType = {
id: generateStoryDistributionId(),
name: 'My Story',
2022-07-01 00:52:03 +00:00
allowsReplies: true,
isBlockList: false,
members: [SERVICE_ID_1, SERVICE_ID_1, SERVICE_ID_2],
senderKeyInfo: {
createdAtDate: Date.now(),
distributionId: generateUuid(),
memberDevices: [],
},
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);
const allHydratedLists = await getAllStoryDistributionsWithMembers();
assert.lengthOf(allHydratedLists, 1);
assert.deepEqual(allHydratedLists[0].members, [SERVICE_ID_1, SERVICE_ID_2]);
});
});