Skip to content

Commit

Permalink
Solve unit tests errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregor Herdmann committed May 4, 2017
1 parent bba5e90 commit 0fc2acb
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 66 deletions.
57 changes: 32 additions & 25 deletions app/script/conversation/ConversationMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ z.conversation.ConversationMapper = class ConversationMapper {
/**
* Convert a JSON conversation into a conversation entity.
* @param {Object} json - Conversation data
* @returns {z.entity.Conversation} Mapped conversation entity
* @returns {Conversation} Mapped conversation entity
*/
map_conversation(json) {
const conversation_ets = this.map_conversations([(json != null ? json.data : undefined) || json]);
return conversation_ets[0];
const json_data_array = json ? (json.data ? [json.data] : [json]) : [json];
const [conversation_et] = this.map_conversations(json_data_array);
return conversation_et;
}

/**
* Convert multiple JSON conversations into a conversation entities.
* @param {Object} json - Conversation data
* @returns {Array<z.entity.Conversation>} Mapped conversation entities
* @returns {Array<Conversation>} Mapped conversation entities
*/
map_conversations(json) {
return json.map((conversation) => this._create_conversation_et(conversation));
Expand All @@ -54,16 +55,16 @@ z.conversation.ConversationMapper = class ConversationMapper {
* @example data: {"name":"ThisIsMyNewConversationName"}
* @todo make utility?
*
* @param {z.entity.Conversation} conversation_et - Conversation to be updated
* @param {data} data - Conversation data
* @returns {z.entity.Conversation} Updated conversation entity
* @param {Conversation} conversation_et - Conversation to be updated
* @param {Object} conversation_data - Conversation data
* @returns {Conversation} Updated conversation entity
*/
update_properties(conversation_et, data) {
for (const key in data) {
update_properties(conversation_et, conversation_data) {
for (const key in conversation_data) {
if (key !== 'id' && conversation_et.hasOwnProperty(key)) {
const value = conversation_et[key];
const value = conversation_data[key];

if (conversation_et[key] !== undefined) {
if (value !== undefined) {
if (ko.isObservable(conversation_et[key])) {
conversation_et[key](value);
} else {
Expand All @@ -79,9 +80,9 @@ z.conversation.ConversationMapper = class ConversationMapper {
/**
* Update the membership properties of a conversation.
*
* @param {z.entity.Conversation} conversation_et - Conversation to be updated
* @param {Conversation} conversation_et - Conversation to be updated
* @param {Object} self - Conversation self data
* @returns {z.entity.Conversation} Updated conversation entity
* @returns {Conversation} Updated conversation entity
*/
update_self_status(conversation_et, self) {
if (conversation_et) {
Expand Down Expand Up @@ -140,30 +141,36 @@ z.conversation.ConversationMapper = class ConversationMapper {
* Creates a conversation entity from JSON data.
*
* @private
* @param {Object} data - Either locally stored or backend data
* @returns {z.entity.Conversation} Mapped conversation entity
* @param {Object} conversation_data - Either locally stored or backend data
* @returns {Conversation} Mapped conversation entity
*/
_create_conversation_et(data) {
if (data === undefined) {
_create_conversation_et(conversation_data) {
if (conversation_data === undefined) {
throw new Error('Cannot create conversation entity without data');
}

let conversation_et = new z.entity.Conversation(data.id);
const {id, creator, members, name, type, others} = conversation_data;
let conversation_et = new z.entity.Conversation(id);

conversation_et.creator = creator;
conversation_et.type(type);
conversation_et.name(name ? name : '');

conversation_et.creator = data.creator;
conversation_et.type(data.type);
conversation_et.name(data.name ? data.name : '');
conversation_et = this.update_self_status(conversation_et, (data.members != null ? data.members.self : undefined) || data);
if (members) {
conversation_et = this.update_self_status(conversation_et, members.self);
} else {
conversation_et = this.update_self_status(conversation_et, conversation_data);
}

if (!conversation_et.last_event_timestamp()) {
conversation_et.last_event_timestamp(Date.now());
}

// all users that are still active
if (data.others) {
conversation_et.participating_user_ids(data.others);
if (others) {
conversation_et.participating_user_ids(others);
} else {
const participating_user_ids = data.members.others
const participating_user_ids = members.others
.filter((other) => other.status === z.conversation.ConversationStatus.CURRENT_MEMBER)
.map((other) => other.id);

Expand Down
45 changes: 30 additions & 15 deletions app/script/conversation/ConversationRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -1653,25 +1653,26 @@ z.conversation.ConversationRepository = class ConversationRepository {
*
* @private
* @param {Object} user_client_map - User client map
* @param {Function} client_callback - Function to be executed on clients first
* @param {Function} user_callback - Function to be executed on users at the end
* @param {Function} client_fn - Function to be executed on clients first
* @param {Function} user_fn - Function to be executed on users at the end
* @returns {Array} Function array
*/
_map_user_client_map(user_client_map, client_callback, user_callback) {
_map_user_client_map(user_client_map, client_fn, user_fn) {
const result = [];
const user_ids = Object.keys(user_client_map);

user_client_map.forEach(function(user_id) {
user_ids.forEach(function(user_id) {
if (user_client_map.hasOwnProperty(user_id)) {
const client_ids = user_client_map[user_id];

if (_.isFunction(client_callback)) {
if (_.isFunction(client_fn)) {
client_ids.forEach((function(client_id) {
result.push(client_callback(user_id, client_id));
result.push(client_fn(user_id, client_id));
}));
}

if (_.isFunction(user_callback)) {
result.push(user_callback(user_id));
if (_.isFunction(user_fn)) {
result.push(user_fn(user_id));
}
}
});
Expand Down Expand Up @@ -2814,17 +2815,18 @@ z.conversation.ConversationRepository = class ConversationRepository {
* @returns {Promise} Resolves when the event was handled
*/
_on_message_hidden(event_json) {
if (event_json.from !== this.user_repository.self().id) {
const {data: event_data, from} = event_json;
if (from !== this.user_repository.self().id) {
return Promise.reject(new Error('Cannot hide message: Sender is not self user'));
}

return this.get_conversation_by_id_async(event_json.data.conversation_id)
return this.get_conversation_by_id_async(event_data.conversation_id)
.then((conversation_et) => {
amplify.publish(z.event.WebApp.CONVERSATION.MESSAGE.REMOVED, event_json.data.message_id);
return this._delete_message_by_id(conversation_et, event_json.data.message_id);
amplify.publish(z.event.WebApp.CONVERSATION.MESSAGE.REMOVED, event_data.message_id);
return this._delete_message_by_id(conversation_et, event_data.message_id);
})
.catch((error) => {
this.logger.info(`Failed to delete message for conversation '${event_json.conversation}'`, error);
this.logger.info(`Failed to delete message '${event_data.message_id}' for conversation '${event_data.conversation_id}'`, error);
throw error;
});
}
Expand Down Expand Up @@ -3204,11 +3206,24 @@ z.conversation.ConversationRepository = class ConversationRepository {
* @param {Message} message_et - Message to delete
* @returns {Promise} Resolves when message was deleted
*/
_delete_message_by_id(conversation_et, message_et) {
_delete_message(conversation_et, message_et) {
conversation_et.remove_message_by_id(message_et.id);
return this.conversation_service.delete_message_with_key_from_db(message_et.primary_key);
}

/**
* Delete message from UI and database. Primary key is used to delete message in database.
*
* @private
* @param {Conversation} conversation_et - Conversation that contains the message
* @param {string} message_id - ID of message to delete
* @returns {Promise} Resolves when message was deleted
*/
_delete_message_by_id(conversation_et, message_id) {
conversation_et.remove_message_by_id(message_id);
return this.conversation_service.delete_message_from_db(conversation_et.id, message_id);
}

/**
* Delete messages from UI an database.
*
Expand Down Expand Up @@ -3352,7 +3367,7 @@ z.conversation.ConversationRepository = class ConversationRepository {
const first_asset = original_message_et.get_first_asset();

if (!first_asset.previews().length) {
return this._delete_message_by_id(conversation_et, original_message_et.id);
return this._delete_message(conversation_et, original_message_et);
}
})
.then(() => event_json);
Expand Down
17 changes: 8 additions & 9 deletions app/script/conversation/ConversationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ z.conversation.ConversationService = class ConversationService {

/**
* Construct a new Conversation Service.
* @param {z.service.BackendClient} client - Client for the API calls
* @param {z.storage.StorageService} storage_service - Service for all storage interactions
* @param {BackendClient} client - Client for the API calls
* @param {StorageService} storage_service - Service for all storage interactions
*/
constructor(client, storage_service) {
this.client = client;
Expand Down Expand Up @@ -122,6 +122,7 @@ z.conversation.ConversationService = class ConversationService {
* Get a conversation by ID.
*
* @see https://staging-nginz-https.zinfra.io/swagger-ui/#!/conversations/conversation
*
* @param {string} conversation_id - ID of conversation to get
* @returns {Promise} Resolves with the server response
*/
Expand Down Expand Up @@ -413,7 +414,7 @@ z.conversation.ConversationService = class ConversationService {
* @param {number} [limit=Number.MAX_SAFE_INTEGER] - Amount of events to load
* @returns {Promise} Resolves with the retrieved records
*/
load_preceding_events_from_db(conversation_id, lower_bound = new Date(0), upper_bound = new Date(), limit) {
load_preceding_events_from_db(conversation_id, lower_bound = new Date(0), upper_bound = new Date(), limit = Number.MAX_SAFE_INTEGER) {
if (!_.isDate(lower_bound) || !_.isDate(upper_bound)) {
throw new Error(`Lower bound (${typeof lower_bound}) and upper bound (${typeof upper_bound}) must be of type 'Date'.`);
} else if (lower_bound.getTime() > upper_bound.getTime()) {
Expand Down Expand Up @@ -465,7 +466,7 @@ z.conversation.ConversationService = class ConversationService {

/**
* Saves a list of conversation records in the local database.
* @param {Array<z.entity.Conversation>} conversations - Conversation entity
* @param {Array<Conversation>} conversations - Conversation entity
* @returns {Promise<Array>} Resolves with a list of conversation records
*/
save_conversations_in_db(conversations) {
Expand All @@ -477,7 +478,7 @@ z.conversation.ConversationService = class ConversationService {

/**
* Saves a conversation entity in the local database.
* @param {z.entity.Conversation} conversation_et - Conversation entity
* @param {Conversation} conversation_et - Conversation entity
* @returns {Promise} Resolves with the conversation entity
*/
save_conversation_state_in_db(conversation_et) {
Expand All @@ -501,9 +502,7 @@ z.conversation.ConversationService = class ConversationService {

return this.load_events_with_category_from_db(conversation_id, category_min, category_max)
.then((events) => {
return events.filter((event) => {
z.search.FullTextSearch.search(event.data.content, query);
});
return events.filter(({data: event_data}) => z.search.FullTextSearch.search(event_data.content, query));
});
}

Expand Down Expand Up @@ -574,7 +573,7 @@ z.conversation.ConversationService = class ConversationService {
/**
* Update a message in the database.
*
* @param {z.entity.Message} message_et - Message event to update in the database
* @param {Message} message_et - Message event to update in the database
* @param {Object} [changes={}] - Changes to update message with
* @param {string} conversation_id - ID of conversation
* @returns {Promise} Resolves when the message was updated in database
Expand Down
17 changes: 9 additions & 8 deletions app/script/conversation/EventMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ z.conversation.EventMapper = class EventMapper {
message_et = this._map_event_member_update(event);
break;
case z.event.Client.CONVERSATION.MISSED_MESSAGES:
message_et = this._map_event_missed_messages(event);
message_et = this._map_event_missed_messages();
break;
case z.event.Backend.CONVERSATION.RENAME:
message_et = this._map_event_rename(event);
Expand Down Expand Up @@ -157,6 +157,7 @@ z.conversation.EventMapper = class EventMapper {
return message_et;
}


//##############################################################################
// Event mappers
//##############################################################################
Expand Down Expand Up @@ -485,8 +486,8 @@ z.conversation.EventMapper = class EventMapper {
_map_asset_link_preview(link_preview) {
if (link_preview) {
const link_preview_et = new z.entity.LinkPreview();
const {article = {}, image, permanent_url, summary, title, url, url_offset, meta_data} = link_preview;
const {image: article_image, title: article_title, summary: article_summary, permanent_url: article_permanent_url} = article;
const {image, permanent_url, summary, title, url, url_offset, meta_data} = link_preview;
const {image: article_image, title: article_title, summary: article_summary, permanent_url: article_permanent_url} = link_preview.article || {};

link_preview_et.title = title || article_title;
link_preview_et.summary = summary || article_summary;
Expand Down Expand Up @@ -520,7 +521,7 @@ z.conversation.EventMapper = class EventMapper {
* @param {Array} [link_previews=[]] - base64 encoded proto previews
* @returns {Array<LinkPreview>} Array of mapped link previews
*/
_map_asset_link_previews(link_previews) {
_map_asset_link_previews(link_previews = []) {
return link_previews
.map((encoded_link_preview) => z.proto.LinkPreview.decode64(encoded_link_preview))
.map((link_preview) => this._map_asset_link_preview(link_preview))
Expand All @@ -531,12 +532,12 @@ z.conversation.EventMapper = class EventMapper {
* Maps JSON data of text asset into asset entity
*
* @private
* @param {Object} data - Asset data received as JSON
* @param {Object} event_data - Asset data received as JSON
* @returns {Text} Text asset entity
*/
_map_asset_text(data) {
const {id, content, message, nonce, previews} = data;
const asset_et = new z.entity.Text(id, content || message);
_map_asset_text(event_data) {
const {id, content, message, nonce, previews} = event_data;
const asset_et = new z.entity.Text(id, (content || message));

asset_et.nonce = nonce;
asset_et.previews(this._map_asset_link_previews(previews));
Expand Down
6 changes: 2 additions & 4 deletions test/unit_tests/conversation/ConversationMapperSpec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ describe 'Conversation Mapper', ->
data = {name: 'New foo bar conversation name', id: 'd5a39ffb-6ce3-4cc8-9048-0123456789abc', creator: creator_id}
updated_conversation_et = conversation_mapper.update_properties conversation_et, data

expect(updated_conversation_et.name()).toBe 'New foo bar conversation name'
expect(updated_conversation_et.id).not.toBe 'd5a39ffb-6ce3-4cc8-9048-0123456789abc'
expect(updated_conversation_et.creator).toBe creator_id
expect(updated_conversation_et.name()).toBe data.name
expect(updated_conversation_et.id).not.toBe data.id
expect(updated_conversation_et.creator).toBe data.creator

describe 'update_self_status', ->
Expand Down Expand Up @@ -233,7 +232,6 @@ describe 'Conversation Mapper', ->
expect(merged_data.others[0]).toBe remote_data.members.others[0].id
expect(merged_data.status).toBe remote_data.members.self.status
expect(merged_data.type).toBe remote_data.type
expect(merged_data.last_event_timestamp).toBe new Date(remote_data.last_event_time).getTime()

it 'updates local archive and muted timestamps if time of remote data is newer', ->
#@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# grunt test_init && grunt test_run:conversation/ConversationRepository
#@formatter:off
describe 'z.conversation.ConversationRepository', ->
describe 'ConversationRepository', ->
test_factory = new TestFactory()

client = test_factory.client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# grunt test_init && grunt test_run:conversation/ConversationServiceNoCompound

describe 'z.conversation.ConversationServiceNoCompound', ->
describe 'ConversationServiceNoCompound', ->
conversation_mapper = null
conversation_service = null
server = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# grunt test_init && grunt test_run:conversation/ConversationService

describe 'z.conversation.ConversationService', ->
describe 'ConversationService', ->
conversation_mapper = null
conversation_service = null
server = null
Expand Down
4 changes: 2 additions & 2 deletions test/unit_tests/conversation/EventMapperSpec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ describe 'Event Mapper', ->
message_ets = event_mapper.map_json_events [good_message, bad_message], conversation_et
expect(message_ets.length).toBe 1

describe '_map_system_event_unable_to_decrypt', ->
describe '_map_event_unable_to_decrypt', ->
it 'maps a message from a decrypt error event', ->
# @formatter:off
event = {"category": 0, "conversation": "fb1c051a-3ce3-46c5-bbc2-0153b6076af0", "error": "We received a message with session tag 'a8859a310a0c374a3da67e3a0f871145', but we don't have a session for this tag. (c0a70d96aaeb87b6)", "error_code": "205 (c0a70d96aaeb87b6)", "from": "2bde49aa-bdb5-458f-98cf-7d3552b10916", "id": "cb4972e0-9586-42a2-90cc-1798ec0cb648", "primary_key": 9, "time": "2017-04-03T12:58:04.301Z", "type": "conversation.unable-to-decrypt"}
# @formatter:on
message_et = event_mapper._map_system_event_unable_to_decrypt event
message_et = event_mapper._map_event_unable_to_decrypt event
expect(message_et.error_code).toBe '205'
expect(message_et.client_id).toBe 'c0a70d96aaeb87b6'

0 comments on commit 0fc2acb

Please sign in to comment.