2019-07-30 09:35:42 +02:00
|
|
|
/* eslint-disable no-unused-vars, require-atomic-updates */
|
|
|
|
|
2018-10-11 18:18:24 +02:00
|
|
|
|
2020-11-05 18:58:23 +02:00
|
|
|
const time = require('@joplinapp/lib/time').default;
|
|
|
|
const { asyncTest, fileContentEqual, setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync } = require('./test-utils.js');
|
|
|
|
const Folder = require('@joplinapp/lib/models/Folder.js');
|
|
|
|
const Note = require('@joplinapp/lib/models/Note.js');
|
|
|
|
const Resource = require('@joplinapp/lib/models/Resource.js');
|
|
|
|
const BaseModel = require('@joplinapp/lib/BaseModel').default;
|
|
|
|
const shim = require('@joplinapp/lib/shim').default;
|
2018-10-11 18:18:24 +02:00
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason, p) => {
|
|
|
|
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
|
|
|
|
});
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
const testImagePath = `${__dirname}/../tests/support/photo.jpg`;
|
2019-05-12 12:38:33 +02:00
|
|
|
|
2018-10-11 18:18:24 +02:00
|
|
|
describe('models_Resource', function() {
|
|
|
|
|
|
|
|
beforeEach(async (done) => {
|
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await switchClient(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have a "done" fetch_status when created locally', asyncTest(async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
2019-05-12 12:38:33 +02:00
|
|
|
await shim.attachFileToNote(note1, testImagePath);
|
2020-03-14 01:46:14 +02:00
|
|
|
const resource1 = (await Resource.all())[0];
|
|
|
|
const ls = await Resource.localState(resource1);
|
2018-11-13 02:45:08 +02:00
|
|
|
expect(ls.fetch_status).toBe(Resource.FETCH_STATUS_DONE);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should have a default local state', asyncTest(async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
2019-05-12 12:38:33 +02:00
|
|
|
await shim.attachFileToNote(note1, testImagePath);
|
2020-03-14 01:46:14 +02:00
|
|
|
const resource1 = (await Resource.all())[0];
|
|
|
|
const ls = await Resource.localState(resource1);
|
2018-11-13 02:45:08 +02:00
|
|
|
expect(!ls.id).toBe(true);
|
|
|
|
expect(ls.resource_id).toBe(resource1.id);
|
|
|
|
expect(ls.fetch_status).toBe(Resource.FETCH_STATUS_DONE);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should save and delete local state', asyncTest(async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
2019-05-12 12:38:33 +02:00
|
|
|
await shim.attachFileToNote(note1, testImagePath);
|
2020-03-14 01:46:14 +02:00
|
|
|
const resource1 = (await Resource.all())[0];
|
2018-11-13 02:45:08 +02:00
|
|
|
await Resource.setLocalState(resource1, { fetch_status: Resource.FETCH_STATUS_IDLE });
|
|
|
|
|
|
|
|
let ls = await Resource.localState(resource1);
|
|
|
|
expect(!!ls.id).toBe(true);
|
|
|
|
expect(ls.fetch_status).toBe(Resource.FETCH_STATUS_IDLE);
|
|
|
|
|
|
|
|
await Resource.delete(resource1.id);
|
|
|
|
ls = await Resource.localState(resource1);
|
|
|
|
expect(!ls.id).toBe(true);
|
2018-10-11 18:18:24 +02:00
|
|
|
}));
|
|
|
|
|
2019-05-12 12:38:33 +02:00
|
|
|
it('should resize the resource if the image is below the required dimensions', asyncTest(async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
2019-05-12 12:38:33 +02:00
|
|
|
const previousMax = Resource.IMAGE_MAX_DIMENSION;
|
|
|
|
Resource.IMAGE_MAX_DIMENSION = 5;
|
|
|
|
await shim.attachFileToNote(note1, testImagePath);
|
|
|
|
Resource.IMAGE_MAX_DIMENSION = previousMax;
|
2020-03-14 01:46:14 +02:00
|
|
|
const resource1 = (await Resource.all())[0];
|
2019-05-12 12:38:33 +02:00
|
|
|
|
|
|
|
const originalStat = await shim.fsDriver().stat(testImagePath);
|
|
|
|
const newStat = await shim.fsDriver().stat(Resource.fullPath(resource1));
|
|
|
|
|
|
|
|
expect(newStat.size < originalStat.size).toBe(true);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should not resize the resource if the image is below the required dimensions', asyncTest(async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
2019-05-12 12:38:33 +02:00
|
|
|
await shim.attachFileToNote(note1, testImagePath);
|
2020-03-14 01:46:14 +02:00
|
|
|
const resource1 = (await Resource.all())[0];
|
2019-05-12 12:38:33 +02:00
|
|
|
|
|
|
|
const originalStat = await shim.fsDriver().stat(testImagePath);
|
|
|
|
const newStat = await shim.fsDriver().stat(Resource.fullPath(resource1));
|
|
|
|
|
|
|
|
expect(originalStat.size).toBe(newStat.size);
|
|
|
|
}));
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|