syncMessage done

pull/1199/head
vincent 5 years ago
parent f72423c2ba
commit 152adbf4d5

@ -65,8 +65,5 @@ export async function getSyncContacts(): Promise<Array<any> | undefined> {
.filter(c => c.id !== primaryDevice.key); .filter(c => c.id !== primaryDevice.key);
// Return unique contacts // Return unique contacts
return _.uniqBy( return _.uniqBy([...primaryContacts, ...secondaryContacts], 'id');
[...primaryContacts, ...secondaryContacts],
'id'
);
} }

@ -1,13 +1,10 @@
import chai from 'chai'; import chai from 'chai';
import * as sinon from 'sinon'; import * as sinon from 'sinon';
import { PubKey } from '../../../session/types/';
import { SyncMessageUtils } from '../../../session/utils/'; import { SyncMessageUtils } from '../../../session/utils/';
import { SyncMessage } from '../../../session/messages/outgoing';
import { TestUtils } from '../../test-utils'; import { TestUtils } from '../../test-utils';
import { UserUtil } from '../../../util'; import { UserUtil } from '../../../util';
import { MultiDeviceProtocol } from '../../../session/protocols'; import { MultiDeviceProtocol } from '../../../session/protocols';
import { Integer } from '../../../types/Util';
// tslint:disable-next-line: no-require-imports no-var-requires // tslint:disable-next-line: no-require-imports no-var-requires
const chaiAsPromised = require('chai-as-promised'); const chaiAsPromised = require('chai-as-promised');
@ -24,8 +21,6 @@ describe('Sync Message Utils', () => {
// Stubbed // Stubbed
expect(syncMessage).to.not.exist; expect(syncMessage).to.not.exist;
// expect(syncMessage instanceof SyncMessage).to.equal(true, 'message was not converted to SyncMessage'); // expect(syncMessage instanceof SyncMessage).to.equal(true, 'message was not converted to SyncMessage');
// Further tests required
}); });
}); });
@ -52,36 +47,14 @@ describe('Sync Message Utils', () => {
let getOrCreateAndWaitStub: sinon.SinonStub; let getOrCreateAndWaitStub: sinon.SinonStub;
let getOrCreatAndWaitItem: any; let getOrCreatAndWaitItem: any;
// tslint:disable-next-line: insecure-random
const randomBoolean = () => !!Math.round(Math.random());
const randomMockConv = (primary: boolean) => (
// new (function(primary) {
// return {
// id: generateFakePubKey().key,
// isPrivate: () => true,
// isOurLocalDevice: () => false,
// isBlocked: () => false,
// getPrimaryDevicePubKey: () => this.isPrivate ?
// attributes: {
// secondaryStatus: !primary,
// },
// };
// })();
{}
);
// Fill half with secondaries, half with primaries // Fill half with secondaries, half with primaries
const numConversations = 20; const numConversations = 20;
const primaryConversations = new Array(numConversations / 2) const primaryConversations = new Array(numConversations / 2)
.fill({}) .fill({})
.map(() => randomMockConv(true)); .map(() => new TestUtils.MockPrivateConversation({ isPrimary: true }));
const secondaryConversations = new Array(numConversations / 2) const secondaryConversations = new Array(numConversations / 2)
.fill({}) .fill({})
.map(() => randomMockConv(false)); .map(() => new TestUtils.MockPrivateConversation({ isPrimary: false }));
const conversations = [...primaryConversations, ...secondaryConversations]; const conversations = [...primaryConversations, ...secondaryConversations];
const sandbox = sinon.createSandbox(); const sandbox = sinon.createSandbox();
@ -102,7 +75,8 @@ describe('Sync Message Utils', () => {
// Scale result in sync with secondaryConversations on callCount // Scale result in sync with secondaryConversations on callCount
getOrCreateAndWaitStub = sandbox.stub().callsFake(() => { getOrCreateAndWaitStub = sandbox.stub().callsFake(() => {
const item = secondaryConversations[getOrCreateAndWaitStub.callCount - 1]; const item =
secondaryConversations[getOrCreateAndWaitStub.callCount - 1];
// Make the item a primary device to match the call in SyncMessage under secondaryContactsPromise // Make the item a primary device to match the call in SyncMessage under secondaryContactsPromise
getOrCreatAndWaitItem = { getOrCreatAndWaitItem = {
@ -153,11 +127,9 @@ describe('Sync Message Utils', () => {
// All contacts should be primary; half of which some from secondaries in secondaryContactsPromise // All contacts should be primary; half of which some from secondaries in secondaryContactsPromise
expect(contacts?.find(c => c.attributes.secondaryStatus)).to.not.exist; expect(contacts?.find(c => c.attributes.secondaryStatus)).to.not.exist;
expect(contacts) expect(contacts?.filter(c => c.isPrimary)).to.have.length(
numConversations / 2
);
}); });
}); });
// MAKE MORE SPECIFIC, CHECK PARAMETERS
}); });

@ -10,8 +10,10 @@ import {
ClosedGroupChatMessage, ClosedGroupChatMessage,
OpenGroupMessage, OpenGroupMessage,
} from '../../session/messages/outgoing'; } from '../../session/messages/outgoing';
import { Integer } from '../../types/Util'; import {
import { ConversationModel, ConversationAttributes } from '../../../js/models/conversation'; ConversationAttributes,
} from '../../../js/models/conversation';
import { TestUtils } from '.';
const globalAny: any = global; const globalAny: any = global;
const sandbox = sinon.createSandbox(); const sandbox = sinon.createSandbox();
@ -79,11 +81,11 @@ export function generateFakePubKey(): PubKey {
return new PubKey(pubkeyString); return new PubKey(pubkeyString);
} }
export function generateFakePubKeys(amount: Integer): Array<PubKey> { export function generateFakePubKeys(amount: number): Array<PubKey> {
const numPubKeys = amount > 0 ? Math.floor(amount) : 0; const numPubKeys = amount > 0 ? Math.floor(amount) : 0;
// tslint:disable-next-line: no-unnecessary-callback-wrapper // tslint:disable-next-line: no-unnecessary-callback-wrapper
return new Array(amount).fill(0).map(() => generateFakePubKey()); return new Array(numPubKeys).fill(0).map(() => generateFakePubKey());
} }
export function generateChatMessage(identifier?: string): ChatMessage { export function generateChatMessage(identifier?: string): ChatMessage {
@ -126,19 +128,34 @@ export function generateClosedGroupMessage(
}); });
} }
// Mock ConversationModel interface MockPrivateConversationParams {
export class MockPrivateConversation implements ConversationModel { id?: string;
isPrimary: boolean;
}
export class MockPrivateConversation {
public id: string; public id: string;
public isPrimary: boolean; public isPrimary: boolean;
public attributes: ConversationAttributes; public attributes: ConversationAttributes;
constructor(isPrimary: boolean) { constructor(params: MockPrivateConversationParams) {
this.isPrimary = isPrimary; const dayInSeconds = 86400;
this.isPrimary = params.isPrimary;
this.id = params.id ?? TestUtils.generateFakePubKey().key;
this.id = TestUtils.generateFakePubKey().key;
this.attributes = { this.attributes = {
members members: [],
} left: false,
expireTimer: dayInSeconds,
profileSharing: true,
mentionedUs: false,
unreadCount: 99,
isArchived: false,
active_at: Date.now(),
timestamp: Date.now(),
secondaryStatus: !this.isPrimary,
};
} }
public isPrivate() { public isPrivate() {
@ -154,10 +171,6 @@ export class MockPrivateConversation implements ConversationModel {
} }
public getPrimaryDevicePubKey() { public getPrimaryDevicePubKey() {
return this.isPrimary return this.isPrimary ? this.id : TestUtils.generateFakePubKey().key;
? this.id
: TestUtils.generateFakePubKey().key;
} }
} }
const myconv = new MockPrivateConversation(false) ;

@ -1,8 +1,3 @@
// Integer Type - primarily for incremental values in ConversationModel etc
export type Integer = number & { __int__: void };
export const roundToInt = (num: number): Integer => Math.round(num) as Integer;
export type RenderTextCallbackType = (options: { export type RenderTextCallbackType = (options: {
text: string; text: string;
key: number; key: number;

Loading…
Cancel
Save