mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2026-04-24 02:18:15 +01:00
Add Attachment.migrateDataToFileSystem
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
require('mocha-testcheck').install();
|
||||
|
||||
const stringToArrayBuffer = require('string-to-arraybuffer');
|
||||
const { assert } = require('chai');
|
||||
|
||||
const Attachment = require('../../../js/modules/types/attachment');
|
||||
@@ -101,4 +102,81 @@ describe('Attachment', () => {
|
||||
assert.deepEqual(actual, expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('migrateDataToFileSystem', () => {
|
||||
it('should write data to disk and store relative path to it', async () => {
|
||||
const input = {
|
||||
contentType: 'image/jpeg',
|
||||
data: stringToArrayBuffer('Above us only sky'),
|
||||
fileName: 'foo.jpg',
|
||||
size: 1111,
|
||||
};
|
||||
|
||||
const expected = {
|
||||
contentType: 'image/jpeg',
|
||||
path: 'abc/abcdefgh123456789',
|
||||
fileName: 'foo.jpg',
|
||||
size: 1111,
|
||||
};
|
||||
|
||||
const expectedAttachmentData = stringToArrayBuffer('Above us only sky');
|
||||
const writeAttachmentData = async (attachmentData) => {
|
||||
assert.deepEqual(attachmentData, expectedAttachmentData);
|
||||
return 'abc/abcdefgh123456789';
|
||||
};
|
||||
|
||||
const actual = await Attachment.migrateDataToFileSystem(
|
||||
input,
|
||||
{ writeAttachmentData }
|
||||
);
|
||||
assert.deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('should skip over (invalid) attachments without data', async () => {
|
||||
const input = {
|
||||
contentType: 'image/jpeg',
|
||||
fileName: 'foo.jpg',
|
||||
size: 1111,
|
||||
};
|
||||
|
||||
const expected = {
|
||||
contentType: 'image/jpeg',
|
||||
fileName: 'foo.jpg',
|
||||
size: 1111,
|
||||
};
|
||||
|
||||
const writeAttachmentData = async () =>
|
||||
'abc/abcdefgh123456789';
|
||||
|
||||
const actual = await Attachment.migrateDataToFileSystem(
|
||||
input,
|
||||
{ writeAttachmentData }
|
||||
);
|
||||
assert.deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('should throw error if data is not valid', async () => {
|
||||
const input = {
|
||||
contentType: 'image/jpeg',
|
||||
data: 42,
|
||||
fileName: 'foo.jpg',
|
||||
size: 1111,
|
||||
};
|
||||
|
||||
const writeAttachmentData = async () =>
|
||||
'abc/abcdefgh123456789';
|
||||
|
||||
try {
|
||||
await Attachment.migrateDataToFileSystem(input, { writeAttachmentData });
|
||||
} catch (error) {
|
||||
assert.strictEqual(
|
||||
error.message,
|
||||
'Expected `attachment.data` to be an array buffer; got: number'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
assert.fail('Unreachable');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user