2018-03-14 02:01:23 +00:00
|
|
|
|
const { assert } = require('chai');
|
2018-04-10 22:09:29 +00:00
|
|
|
|
const sinon = require('sinon');
|
2018-03-14 02:01:23 +00:00
|
|
|
|
|
|
|
|
|
const Message = require('../../../js/modules/types/message');
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const {
|
|
|
|
|
stringToArrayBuffer,
|
|
|
|
|
} = require('../../../js/modules/string_to_array_buffer');
|
2018-03-14 02:01:23 +00:00
|
|
|
|
|
|
|
|
|
describe('Message', () => {
|
2018-04-04 22:57:18 +00:00
|
|
|
|
describe('createAttachmentDataWriter', () => {
|
2018-04-04 01:10:34 +00:00
|
|
|
|
it('should ignore messages that didn’t go through attachment migration', async () => {
|
|
|
|
|
const input = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
|
|
|
|
schemaVersion: 2,
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
|
|
|
|
schemaVersion: 2,
|
|
|
|
|
};
|
|
|
|
|
const writeExistingAttachmentData = () => {};
|
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const actual = await Message.createAttachmentDataWriter(
|
|
|
|
|
writeExistingAttachmentData
|
|
|
|
|
)(input);
|
2018-04-04 01:10:34 +00:00
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should ignore messages without attachments', async () => {
|
|
|
|
|
const input = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
|
|
|
|
schemaVersion: 4,
|
|
|
|
|
attachments: [],
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
|
|
|
|
schemaVersion: 4,
|
|
|
|
|
attachments: [],
|
|
|
|
|
};
|
|
|
|
|
const writeExistingAttachmentData = () => {};
|
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const actual = await Message.createAttachmentDataWriter(
|
|
|
|
|
writeExistingAttachmentData
|
|
|
|
|
)(input);
|
2018-04-04 01:10:34 +00:00
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should write attachments to file system on original path', async () => {
|
|
|
|
|
const input = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
|
|
|
|
schemaVersion: 4,
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
path: 'ab/abcdefghi',
|
|
|
|
|
data: stringToArrayBuffer('It’s easy if you try'),
|
|
|
|
|
},
|
|
|
|
|
],
|
2018-04-04 01:10:34 +00:00
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
|
|
|
|
schemaVersion: 4,
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
path: 'ab/abcdefghi',
|
|
|
|
|
},
|
|
|
|
|
],
|
2018-04-04 01:10:34 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const writeExistingAttachmentData = attachment => {
|
2018-04-04 01:10:34 +00:00
|
|
|
|
assert.equal(attachment.path, 'ab/abcdefghi');
|
2018-04-27 21:25:04 +00:00
|
|
|
|
assert.deepEqual(
|
|
|
|
|
attachment.data,
|
|
|
|
|
stringToArrayBuffer('It’s easy if you try')
|
|
|
|
|
);
|
2018-04-04 01:10:34 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const actual = await Message.createAttachmentDataWriter(
|
|
|
|
|
writeExistingAttachmentData
|
|
|
|
|
)(input);
|
2018-04-04 01:10:34 +00:00
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
2018-04-20 21:55:33 +00:00
|
|
|
|
|
|
|
|
|
it('should process quote attachment thumbnails', async () => {
|
|
|
|
|
const input = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
|
|
|
|
schemaVersion: 4,
|
|
|
|
|
attachments: [],
|
|
|
|
|
quote: {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
thumbnail: {
|
|
|
|
|
path: 'ab/abcdefghi',
|
|
|
|
|
data: stringToArrayBuffer('It’s easy if you try'),
|
|
|
|
|
},
|
2018-04-20 21:55:33 +00:00
|
|
|
|
},
|
2018-04-27 21:25:04 +00:00
|
|
|
|
],
|
2018-04-20 21:55:33 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
|
|
|
|
schemaVersion: 4,
|
|
|
|
|
attachments: [],
|
|
|
|
|
quote: {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
thumbnail: {
|
|
|
|
|
path: 'ab/abcdefghi',
|
|
|
|
|
},
|
2018-04-20 21:55:33 +00:00
|
|
|
|
},
|
2018-04-27 21:25:04 +00:00
|
|
|
|
],
|
2018-04-20 21:55:33 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const writeExistingAttachmentData = attachment => {
|
2018-04-20 21:55:33 +00:00
|
|
|
|
assert.equal(attachment.path, 'ab/abcdefghi');
|
2018-04-27 21:25:04 +00:00
|
|
|
|
assert.deepEqual(
|
|
|
|
|
attachment.data,
|
|
|
|
|
stringToArrayBuffer('It’s easy if you try')
|
|
|
|
|
);
|
2018-04-20 21:55:33 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const actual = await Message.createAttachmentDataWriter(
|
|
|
|
|
writeExistingAttachmentData
|
|
|
|
|
)(input);
|
2018-04-20 21:55:33 +00:00
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
2018-04-04 01:10:34 +00:00
|
|
|
|
});
|
|
|
|
|
|
2018-03-14 15:45:35 +00:00
|
|
|
|
describe('initializeSchemaVersion', () => {
|
2018-03-14 02:01:23 +00:00
|
|
|
|
it('should ignore messages with previously inherited schema', () => {
|
|
|
|
|
const input = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
|
|
|
|
schemaVersion: 2,
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
|
|
|
|
schemaVersion: 2,
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-14 15:45:35 +00:00
|
|
|
|
const actual = Message.initializeSchemaVersion(input);
|
2018-03-14 02:01:23 +00:00
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
context('for message without attachments', () => {
|
|
|
|
|
it('should initialize schema version to zero', () => {
|
|
|
|
|
const input = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
|
|
|
|
attachments: [],
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
|
|
|
|
attachments: [],
|
|
|
|
|
schemaVersion: 0,
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-14 15:45:35 +00:00
|
|
|
|
const actual = Message.initializeSchemaVersion(input);
|
2018-03-14 02:01:23 +00:00
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
context('for message with attachments', () => {
|
|
|
|
|
it('should inherit existing attachment schema version', () => {
|
|
|
|
|
const input = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
contentType: 'image/jpeg',
|
|
|
|
|
fileName: 'lennon.jpg',
|
|
|
|
|
schemaVersion: 7,
|
|
|
|
|
},
|
|
|
|
|
],
|
2018-03-14 02:01:23 +00:00
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
body: 'Imagine there is no heaven…',
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
contentType: 'image/jpeg',
|
|
|
|
|
fileName: 'lennon.jpg',
|
|
|
|
|
},
|
|
|
|
|
],
|
2018-03-14 02:01:23 +00:00
|
|
|
|
schemaVersion: 7,
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-14 15:45:35 +00:00
|
|
|
|
const actual = Message.initializeSchemaVersion(input);
|
2018-03-14 02:01:23 +00:00
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-03-14 03:45:19 +00:00
|
|
|
|
|
|
|
|
|
describe('upgradeSchema', () => {
|
|
|
|
|
it('should upgrade an unversioned message to the latest version', async () => {
|
|
|
|
|
const input = {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
contentType: 'application/json',
|
|
|
|
|
data: stringToArrayBuffer('It’s easy if you try'),
|
|
|
|
|
fileName: 'test\u202Dfig.exe',
|
|
|
|
|
size: 1111,
|
|
|
|
|
},
|
|
|
|
|
],
|
2018-03-14 03:45:19 +00:00
|
|
|
|
schemaVersion: 0,
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
contentType: 'application/json',
|
|
|
|
|
path: 'abc/abcdefg',
|
|
|
|
|
fileName: 'test\uFFFDfig.exe',
|
|
|
|
|
size: 1111,
|
|
|
|
|
},
|
|
|
|
|
],
|
Index messages with attachments using a boolean
When indexing message attachment metadata using numeric indexes such as:
```javascript
{
conversationId: '+12223334455',
received_at: 123,
attachments: […],
numAttachments: 2,
},
{
conversationId: '+12223334455',
received_at: 456,
attachments: [],
numAttachments: 0,
}
{
conversationId: '+12223334455',
received_at: 789,
attachments: [],
numAttachments: 1,
}
```
It creates an index as follows:
```
[conversationId, received_at, numAttachments]
['+12223334455', 123, 2]
['+12223334455', 456, 0]
['+12223334455', 789, 1]
```
This means a query such as…
```
lowerBound: ['+12223334455', 0, 1 ]
upperBound: ['+12223334455', Number.MAX_VALUE, Number.MAX_VALUE]
```
…will return all three original entries because they span the `received_at`
from `0` through `Number.MAX_VALUE`. One workaround is to index booleans using
`1 | undefined` where `1` is included in the index and `undefined` is not, but
that way we lose the ability to query for the `false` value. Instead, we flip
adjust the index to `[conversationId, hasAttachments, received_at]` and can
then query messages with attachments using
```
[conversationId, 1 /* hasAttachments */, 0 /* received_at */]
[conversationId, 1 /* hasAttachments */, Number.MAX_VALUE /* received_at */]
```
2018-04-14 01:47:06 +00:00
|
|
|
|
hasAttachments: 1,
|
2018-04-25 18:15:06 +00:00
|
|
|
|
hasVisualMediaAttachments: undefined,
|
Index messages with attachments using a boolean
When indexing message attachment metadata using numeric indexes such as:
```javascript
{
conversationId: '+12223334455',
received_at: 123,
attachments: […],
numAttachments: 2,
},
{
conversationId: '+12223334455',
received_at: 456,
attachments: [],
numAttachments: 0,
}
{
conversationId: '+12223334455',
received_at: 789,
attachments: [],
numAttachments: 1,
}
```
It creates an index as follows:
```
[conversationId, received_at, numAttachments]
['+12223334455', 123, 2]
['+12223334455', 456, 0]
['+12223334455', 789, 1]
```
This means a query such as…
```
lowerBound: ['+12223334455', 0, 1 ]
upperBound: ['+12223334455', Number.MAX_VALUE, Number.MAX_VALUE]
```
…will return all three original entries because they span the `received_at`
from `0` through `Number.MAX_VALUE`. One workaround is to index booleans using
`1 | undefined` where `1` is included in the index and `undefined` is not, but
that way we lose the ability to query for the `false` value. Instead, we flip
adjust the index to `[conversationId, hasAttachments, received_at]` and can
then query messages with attachments using
```
[conversationId, 1 /* hasAttachments */, 0 /* received_at */]
[conversationId, 1 /* hasAttachments */, Number.MAX_VALUE /* received_at */]
```
2018-04-14 01:47:06 +00:00
|
|
|
|
hasFileAttachments: 1,
|
2018-03-14 03:45:19 +00:00
|
|
|
|
schemaVersion: Message.CURRENT_SCHEMA_VERSION,
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const expectedAttachmentData = stringToArrayBuffer(
|
|
|
|
|
'It’s easy if you try'
|
|
|
|
|
);
|
2018-03-15 00:45:33 +00:00
|
|
|
|
const context = {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
writeNewAttachmentData: async attachmentData => {
|
2018-03-15 00:45:33 +00:00
|
|
|
|
assert.deepEqual(attachmentData, expectedAttachmentData);
|
|
|
|
|
return 'abc/abcdefg';
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const actual = await Message.upgradeSchema(input, context);
|
2018-03-14 03:45:19 +00:00
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
context('with multiple upgrade steps', () => {
|
|
|
|
|
it('should return last valid message when any upgrade step fails', async () => {
|
|
|
|
|
const input = {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
contentType: 'application/json',
|
|
|
|
|
data: null,
|
|
|
|
|
fileName: 'test\u202Dfig.exe',
|
|
|
|
|
size: 1111,
|
|
|
|
|
},
|
|
|
|
|
],
|
2018-03-14 03:45:19 +00:00
|
|
|
|
schemaVersion: 0,
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
contentType: 'application/json',
|
|
|
|
|
data: null,
|
|
|
|
|
fileName: 'test\u202Dfig.exe',
|
|
|
|
|
size: 1111,
|
|
|
|
|
},
|
|
|
|
|
],
|
2018-03-14 03:45:19 +00:00
|
|
|
|
hasUpgradedToVersion1: true,
|
|
|
|
|
schemaVersion: 1,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const v1 = async message =>
|
|
|
|
|
Object.assign({}, message, { hasUpgradedToVersion1: true });
|
|
|
|
|
const v2 = async () => {
|
|
|
|
|
throw new Error('boom');
|
|
|
|
|
};
|
|
|
|
|
const v3 = async message =>
|
|
|
|
|
Object.assign({}, message, { hasUpgradedToVersion3: true });
|
|
|
|
|
|
2018-03-14 15:45:35 +00:00
|
|
|
|
const toVersion1 = Message._withSchemaVersion(1, v1);
|
|
|
|
|
const toVersion2 = Message._withSchemaVersion(2, v2);
|
|
|
|
|
const toVersion3 = Message._withSchemaVersion(3, v3);
|
2018-03-14 03:45:19 +00:00
|
|
|
|
|
|
|
|
|
const upgradeSchema = async message =>
|
|
|
|
|
toVersion3(await toVersion2(await toVersion1(message)));
|
|
|
|
|
|
|
|
|
|
const actual = await upgradeSchema(input);
|
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should skip out-of-order upgrade steps', async () => {
|
|
|
|
|
const input = {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
contentType: 'application/json',
|
|
|
|
|
data: null,
|
|
|
|
|
fileName: 'test\u202Dfig.exe',
|
|
|
|
|
size: 1111,
|
|
|
|
|
},
|
|
|
|
|
],
|
2018-03-14 03:45:19 +00:00
|
|
|
|
schemaVersion: 0,
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
contentType: 'application/json',
|
|
|
|
|
data: null,
|
|
|
|
|
fileName: 'test\u202Dfig.exe',
|
|
|
|
|
size: 1111,
|
|
|
|
|
},
|
|
|
|
|
],
|
2018-03-14 03:45:19 +00:00
|
|
|
|
schemaVersion: 2,
|
|
|
|
|
hasUpgradedToVersion1: true,
|
|
|
|
|
hasUpgradedToVersion2: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const v1 = async attachment =>
|
|
|
|
|
Object.assign({}, attachment, { hasUpgradedToVersion1: true });
|
|
|
|
|
const v2 = async attachment =>
|
|
|
|
|
Object.assign({}, attachment, { hasUpgradedToVersion2: true });
|
|
|
|
|
const v3 = async attachment =>
|
|
|
|
|
Object.assign({}, attachment, { hasUpgradedToVersion3: true });
|
|
|
|
|
|
2018-03-14 15:45:35 +00:00
|
|
|
|
const toVersion1 = Message._withSchemaVersion(1, v1);
|
|
|
|
|
const toVersion2 = Message._withSchemaVersion(2, v2);
|
|
|
|
|
const toVersion3 = Message._withSchemaVersion(3, v3);
|
2018-03-14 03:45:19 +00:00
|
|
|
|
|
|
|
|
|
// NOTE: We upgrade to 3 before 2, i.e. the pipeline should abort:
|
|
|
|
|
const upgradeSchema = async attachment =>
|
|
|
|
|
toVersion2(await toVersion3(await toVersion1(attachment)));
|
|
|
|
|
|
|
|
|
|
const actual = await upgradeSchema(input);
|
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-03-14 15:45:35 +00:00
|
|
|
|
describe('_withSchemaVersion', () => {
|
2018-03-14 03:45:19 +00:00
|
|
|
|
it('should require a version number', () => {
|
|
|
|
|
const toVersionX = () => {};
|
|
|
|
|
assert.throws(
|
2018-03-14 15:45:35 +00:00
|
|
|
|
() => Message._withSchemaVersion(toVersionX, 2),
|
2018-04-11 19:44:52 +00:00
|
|
|
|
"'schemaVersion' is invalid"
|
2018-03-14 03:45:19 +00:00
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should require an upgrade function', () => {
|
|
|
|
|
assert.throws(
|
2018-03-14 15:45:35 +00:00
|
|
|
|
() => Message._withSchemaVersion(2, 3),
|
2018-04-11 19:44:52 +00:00
|
|
|
|
"'upgrade' must be a function"
|
2018-03-14 03:45:19 +00:00
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should skip upgrading if message has already been upgraded', async () => {
|
|
|
|
|
const upgrade = async message =>
|
|
|
|
|
Object.assign({}, message, { foo: true });
|
2018-03-14 15:45:35 +00:00
|
|
|
|
const upgradeWithVersion = Message._withSchemaVersion(3, upgrade);
|
2018-03-14 03:45:19 +00:00
|
|
|
|
|
|
|
|
|
const input = {
|
|
|
|
|
id: 'guid-guid-guid-guid',
|
|
|
|
|
schemaVersion: 4,
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
id: 'guid-guid-guid-guid',
|
|
|
|
|
schemaVersion: 4,
|
|
|
|
|
};
|
|
|
|
|
const actual = await upgradeWithVersion(input);
|
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return original message if upgrade function throws', async () => {
|
|
|
|
|
const upgrade = async () => {
|
|
|
|
|
throw new Error('boom!');
|
|
|
|
|
};
|
2018-03-14 15:45:35 +00:00
|
|
|
|
const upgradeWithVersion = Message._withSchemaVersion(3, upgrade);
|
2018-03-14 03:45:19 +00:00
|
|
|
|
|
|
|
|
|
const input = {
|
|
|
|
|
id: 'guid-guid-guid-guid',
|
|
|
|
|
schemaVersion: 0,
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
id: 'guid-guid-guid-guid',
|
|
|
|
|
schemaVersion: 0,
|
|
|
|
|
};
|
|
|
|
|
const actual = await upgradeWithVersion(input);
|
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return original message if upgrade function returns null', async () => {
|
|
|
|
|
const upgrade = async () => null;
|
2018-03-14 15:45:35 +00:00
|
|
|
|
const upgradeWithVersion = Message._withSchemaVersion(3, upgrade);
|
2018-03-14 03:45:19 +00:00
|
|
|
|
|
|
|
|
|
const input = {
|
|
|
|
|
id: 'guid-guid-guid-guid',
|
|
|
|
|
schemaVersion: 0,
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
id: 'guid-guid-guid-guid',
|
|
|
|
|
schemaVersion: 0,
|
|
|
|
|
};
|
|
|
|
|
const actual = await upgradeWithVersion(input);
|
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-04-10 22:09:29 +00:00
|
|
|
|
|
|
|
|
|
describe('_mapQuotedAttachments', () => {
|
|
|
|
|
it('handles message with no quote', async () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const upgradeAttachment = sinon
|
|
|
|
|
.stub()
|
|
|
|
|
.throws(new Error("Shouldn't be called"));
|
2018-04-10 22:09:29 +00:00
|
|
|
|
const upgradeVersion = Message._mapQuotedAttachments(upgradeAttachment);
|
|
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
|
body: 'hey there!',
|
|
|
|
|
};
|
|
|
|
|
const result = await upgradeVersion(message);
|
|
|
|
|
assert.deepEqual(result, message);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('handles quote with no attachments', async () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const upgradeAttachment = sinon
|
|
|
|
|
.stub()
|
|
|
|
|
.throws(new Error("Shouldn't be called"));
|
2018-04-10 22:09:29 +00:00
|
|
|
|
const upgradeVersion = Message._mapQuotedAttachments(upgradeAttachment);
|
|
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
|
body: 'hey there!',
|
|
|
|
|
quote: {
|
|
|
|
|
text: 'hey!',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
body: 'hey there!',
|
|
|
|
|
quote: {
|
|
|
|
|
text: 'hey!',
|
|
|
|
|
attachments: [],
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const result = await upgradeVersion(message);
|
|
|
|
|
assert.deepEqual(result, expected);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('handles zero attachments', async () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const upgradeAttachment = sinon
|
|
|
|
|
.stub()
|
|
|
|
|
.throws(new Error("Shouldn't be called"));
|
2018-04-10 22:09:29 +00:00
|
|
|
|
const upgradeVersion = Message._mapQuotedAttachments(upgradeAttachment);
|
|
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
|
body: 'hey there!',
|
|
|
|
|
quote: {
|
|
|
|
|
text: 'hey!',
|
|
|
|
|
attachments: [],
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const result = await upgradeVersion(message);
|
|
|
|
|
assert.deepEqual(result, message);
|
|
|
|
|
});
|
|
|
|
|
|
2018-04-13 22:45:37 +00:00
|
|
|
|
it('handles attachments with no thumbnail', async () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const upgradeAttachment = sinon
|
|
|
|
|
.stub()
|
|
|
|
|
.throws(new Error("Shouldn't be called"));
|
2018-04-13 22:45:37 +00:00
|
|
|
|
const upgradeVersion = Message._mapQuotedAttachments(upgradeAttachment);
|
|
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
|
body: 'hey there!',
|
|
|
|
|
quote: {
|
|
|
|
|
text: 'hey!',
|
|
|
|
|
attachments: [],
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const result = await upgradeVersion(message);
|
|
|
|
|
assert.deepEqual(result, message);
|
|
|
|
|
});
|
|
|
|
|
|
2018-04-23 23:43:25 +00:00
|
|
|
|
it('eliminates thumbnails with no data fielkd', async () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const upgradeAttachment = sinon
|
|
|
|
|
.stub()
|
|
|
|
|
.throws(new Error("Shouldn't be called"));
|
2018-04-23 23:43:25 +00:00
|
|
|
|
const upgradeVersion = Message._mapQuotedAttachments(upgradeAttachment);
|
|
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
|
body: 'hey there!',
|
|
|
|
|
quote: {
|
|
|
|
|
text: 'hey!',
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
fileName: 'cat.gif',
|
|
|
|
|
contentType: 'image/gif',
|
|
|
|
|
thumbnail: {
|
|
|
|
|
fileName: 'failed to download!',
|
|
|
|
|
},
|
2018-04-23 23:43:25 +00:00
|
|
|
|
},
|
2018-04-27 21:25:04 +00:00
|
|
|
|
],
|
2018-04-23 23:43:25 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
body: 'hey there!',
|
|
|
|
|
quote: {
|
|
|
|
|
text: 'hey!',
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
contentType: 'image/gif',
|
|
|
|
|
fileName: 'cat.gif',
|
|
|
|
|
},
|
|
|
|
|
],
|
2018-04-23 23:43:25 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const result = await upgradeVersion(message);
|
|
|
|
|
assert.deepEqual(result, expected);
|
|
|
|
|
});
|
|
|
|
|
|
2018-04-10 22:09:29 +00:00
|
|
|
|
it('calls provided async function for each quoted attachment', async () => {
|
2018-04-13 02:02:20 +00:00
|
|
|
|
const upgradeAttachment = sinon.stub().resolves({
|
2018-04-10 22:09:29 +00:00
|
|
|
|
path: '/new/path/on/disk',
|
2018-04-13 02:02:20 +00:00
|
|
|
|
});
|
2018-04-10 22:09:29 +00:00
|
|
|
|
const upgradeVersion = Message._mapQuotedAttachments(upgradeAttachment);
|
|
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
|
body: 'hey there!',
|
|
|
|
|
quote: {
|
|
|
|
|
text: 'hey!',
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
thumbnail: {
|
|
|
|
|
data: 'data is here',
|
|
|
|
|
},
|
2018-04-13 22:45:37 +00:00
|
|
|
|
},
|
2018-04-27 21:25:04 +00:00
|
|
|
|
],
|
2018-04-10 22:09:29 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const expected = {
|
|
|
|
|
body: 'hey there!',
|
|
|
|
|
quote: {
|
|
|
|
|
text: 'hey!',
|
2018-04-27 21:25:04 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
thumbnail: {
|
|
|
|
|
path: '/new/path/on/disk',
|
|
|
|
|
},
|
2018-04-13 22:45:37 +00:00
|
|
|
|
},
|
2018-04-27 21:25:04 +00:00
|
|
|
|
],
|
2018-04-10 22:09:29 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const result = await upgradeVersion(message);
|
|
|
|
|
assert.deepEqual(result, expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-03-14 02:01:23 +00:00
|
|
|
|
});
|