2021-08-30 19:53:24 +02:00
|
|
|
import { revisionService, setupDatabaseAndSynchronizer, db, switchClient, msleep } from '../testing/test-utils';
|
|
|
|
import SearchEngine from '../services/searchengine/SearchEngine';
|
|
|
|
import ResourceService from '../services/ResourceService';
|
|
|
|
import ItemChangeUtils from '../services/ItemChangeUtils';
|
|
|
|
import Note from '../models/Note';
|
|
|
|
import ItemChange from '../models/ItemChange';
|
2019-01-14 21:11:54 +02:00
|
|
|
|
2021-08-30 19:53:24 +02:00
|
|
|
let searchEngine: SearchEngine = null;
|
2019-01-14 21:11:54 +02:00
|
|
|
|
2023-02-20 17:02:29 +02:00
|
|
|
describe('models/ItemChange', () => {
|
2019-01-14 21:11:54 +02:00
|
|
|
|
2022-11-15 12:23:50 +02:00
|
|
|
beforeEach(async () => {
|
2019-01-14 21:11:54 +02:00
|
|
|
await setupDatabaseAndSynchronizer(1);
|
2019-11-12 18:16:39 +02:00
|
|
|
await switchClient(1);
|
2019-01-14 21:11:54 +02:00
|
|
|
searchEngine = new SearchEngine();
|
|
|
|
searchEngine.setDb(db());
|
|
|
|
});
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should delete old changes that have been processed', (async () => {
|
2021-05-21 15:17:21 +02:00
|
|
|
await Note.save({ title: 'abcd efgh' });
|
2019-01-14 21:11:54 +02:00
|
|
|
|
|
|
|
await ItemChange.waitForAllSaved();
|
|
|
|
|
|
|
|
expect(await ItemChange.lastChangeId()).toBe(1);
|
|
|
|
|
|
|
|
const resourceService = new ResourceService();
|
|
|
|
|
|
|
|
await searchEngine.syncTables();
|
2021-08-30 19:53:24 +02:00
|
|
|
|
2019-01-14 21:11:54 +02:00
|
|
|
// If we run this now, it should not delete any change because
|
|
|
|
// the resource service has not yet processed the change
|
2021-08-30 19:53:24 +02:00
|
|
|
await ItemChangeUtils.deleteProcessedChanges(0);
|
2019-01-14 21:11:54 +02:00
|
|
|
expect(await ItemChange.lastChangeId()).toBe(1);
|
|
|
|
|
|
|
|
await resourceService.indexNoteResources();
|
2021-08-30 19:53:24 +02:00
|
|
|
await ItemChangeUtils.deleteProcessedChanges(0);
|
2019-05-06 22:35:29 +02:00
|
|
|
expect(await ItemChange.lastChangeId()).toBe(1);
|
2019-01-14 21:11:54 +02:00
|
|
|
|
2019-05-06 22:35:29 +02:00
|
|
|
await revisionService().collectRevisions();
|
2021-08-30 19:53:24 +02:00
|
|
|
|
|
|
|
// If we don't set a TTL it will default to 90 days so it won't delete
|
|
|
|
// either.
|
2019-05-06 22:35:29 +02:00
|
|
|
await ItemChangeUtils.deleteProcessedChanges();
|
2021-08-30 19:53:24 +02:00
|
|
|
expect(await ItemChange.lastChangeId()).toBe(1);
|
|
|
|
|
|
|
|
// All changes should be at least 4 ms old now
|
|
|
|
await msleep(4);
|
|
|
|
|
|
|
|
// Now it should delete all changes older than 3 ms
|
|
|
|
await ItemChangeUtils.deleteProcessedChanges(3);
|
2019-01-14 21:11:54 +02:00
|
|
|
expect(await ItemChange.lastChangeId()).toBe(0);
|
|
|
|
}));
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|