Migration and data access functions for stories
This commit is contained in:
parent
9f4a01c535
commit
fdc9885baa
18 changed files with 3428 additions and 202 deletions
126
ts/test-electron/sql/storyDistribution_test.ts
Normal file
126
ts/test-electron/sql/storyDistribution_test.ts
Normal file
|
@ -0,0 +1,126 @@
|
|||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { assert } from 'chai';
|
||||
|
||||
import dataInterface from '../../sql/Client';
|
||||
import { getRandomBytes } from '../../Crypto';
|
||||
import { UUID } from '../../types/UUID';
|
||||
import type { UUIDStringType } from '../../types/UUID';
|
||||
|
||||
import type { StoryDistributionWithMembersType } from '../../sql/Interface';
|
||||
|
||||
const {
|
||||
_deleteAllStoryDistributions,
|
||||
_getAllStoryDistributionMembers,
|
||||
_getAllStoryDistributions,
|
||||
createNewStoryDistribution,
|
||||
deleteStoryDistribution,
|
||||
getAllStoryDistributionsWithMembers,
|
||||
modifyStoryDistributionMembers,
|
||||
} = dataInterface;
|
||||
|
||||
function getUuid(): UUIDStringType {
|
||||
return UUID.generate().toString();
|
||||
}
|
||||
|
||||
describe('sql/storyDistribution', () => {
|
||||
beforeEach(async () => {
|
||||
await _deleteAllStoryDistributions();
|
||||
});
|
||||
|
||||
it('roundtrips with create/fetch/delete', async () => {
|
||||
const list: StoryDistributionWithMembersType = {
|
||||
id: getUuid(),
|
||||
name: 'My Story',
|
||||
avatarUrlPath: getUuid(),
|
||||
avatarKey: getRandomBytes(128),
|
||||
members: [getUuid(), getUuid()],
|
||||
senderKeyInfo: {
|
||||
createdAtDate: Date.now(),
|
||||
distributionId: getUuid(),
|
||||
memberDevices: [],
|
||||
},
|
||||
};
|
||||
|
||||
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('adds and removes with modifyStoryDistributionMembers', async () => {
|
||||
const UUID_1 = getUuid();
|
||||
const UUID_2 = getUuid();
|
||||
const UUID_3 = getUuid();
|
||||
const UUID_4 = getUuid();
|
||||
const list: StoryDistributionWithMembersType = {
|
||||
id: getUuid(),
|
||||
name: 'My Story',
|
||||
avatarUrlPath: getUuid(),
|
||||
avatarKey: getRandomBytes(128),
|
||||
members: [UUID_1, UUID_2],
|
||||
senderKeyInfo: {
|
||||
createdAtDate: Date.now(),
|
||||
distributionId: getUuid(),
|
||||
memberDevices: [],
|
||||
},
|
||||
};
|
||||
|
||||
await createNewStoryDistribution(list);
|
||||
|
||||
assert.lengthOf(await _getAllStoryDistributions(), 1);
|
||||
assert.lengthOf(await _getAllStoryDistributionMembers(), 2);
|
||||
|
||||
await modifyStoryDistributionMembers(list.id, {
|
||||
toAdd: [UUID_3, UUID_4],
|
||||
toRemove: [UUID_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: [UUID_2, UUID_3, UUID_4],
|
||||
});
|
||||
});
|
||||
|
||||
it('eliminates duplicates without complaint in createNewStoryDistribution', async () => {
|
||||
const UUID_1 = getUuid();
|
||||
const UUID_2 = getUuid();
|
||||
const list: StoryDistributionWithMembersType = {
|
||||
id: getUuid(),
|
||||
name: 'My Story',
|
||||
avatarUrlPath: getUuid(),
|
||||
avatarKey: getRandomBytes(128),
|
||||
members: [UUID_1, UUID_1, UUID_2],
|
||||
senderKeyInfo: {
|
||||
createdAtDate: Date.now(),
|
||||
distributionId: getUuid(),
|
||||
memberDevices: [],
|
||||
},
|
||||
};
|
||||
|
||||
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, [UUID_1, UUID_2]);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue