2023-11-20 14:00:51 +03:00
|
|
|
import assert from 'assert/strict';
|
2023-05-03 20:58:03 +03:00
|
|
|
import sinon from 'sinon';
|
|
|
|
import {PostRevisions} from '../src';
|
2023-04-17 13:12:55 +03:00
|
|
|
|
2023-04-17 16:14:33 +03:00
|
|
|
const config = {
|
2023-04-21 18:04:54 +03:00
|
|
|
max_revisions: 10,
|
|
|
|
revision_interval_ms: 1000
|
2023-04-17 16:14:33 +03:00
|
|
|
};
|
|
|
|
|
2023-05-03 20:58:03 +03:00
|
|
|
function makePostLike(data: any = {}) {
|
|
|
|
return Object.assign({
|
|
|
|
id: 'fakeid',
|
|
|
|
lexical: 'blah',
|
|
|
|
html: 'blah',
|
|
|
|
author_id: 'fakeauthorid',
|
|
|
|
feature_image: data.feature_image || null,
|
|
|
|
feature_image_alt: data.feature_image_alt || null,
|
|
|
|
feature_image_caption: data.feature_image_caption || null,
|
|
|
|
title: 'Title',
|
|
|
|
reason: 'reason',
|
|
|
|
post_status: 'published'
|
|
|
|
}, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeRevision(data: any = {}) {
|
|
|
|
return Object.assign({
|
|
|
|
post_id: 'fakeid',
|
2023-05-12 15:13:51 +03:00
|
|
|
created_at_ts: data.created_at_ts || Date.now(),
|
2023-05-03 20:58:03 +03:00
|
|
|
lexical: 'blah',
|
|
|
|
html: 'blah',
|
|
|
|
author_id: 'fakeauthorid',
|
|
|
|
feature_image: data.feature_image || null,
|
|
|
|
feature_image_alt: data.feature_image_alt || null,
|
|
|
|
feature_image_caption: data.feature_image_caption || null,
|
|
|
|
title: 'Title',
|
|
|
|
reason: 'reason',
|
|
|
|
post_status: 'published'
|
|
|
|
}, data);
|
|
|
|
}
|
|
|
|
|
2023-04-17 16:14:33 +03:00
|
|
|
describe('PostRevisions', function () {
|
|
|
|
describe('shouldGenerateRevision', function () {
|
|
|
|
it('should return true if there are no revisions', function () {
|
2023-05-03 20:58:03 +03:00
|
|
|
const postRevisions = new PostRevisions({config, model: {}});
|
2023-04-17 16:14:33 +03:00
|
|
|
|
2023-04-21 16:36:35 +03:00
|
|
|
const expected = {value: true, reason: 'initial_revision'};
|
2023-05-03 20:58:03 +03:00
|
|
|
const actual = postRevisions.shouldGenerateRevision(makePostLike(), []);
|
2023-04-17 16:14:33 +03:00
|
|
|
|
2023-04-21 16:36:35 +03:00
|
|
|
assert.deepEqual(actual, expected);
|
2023-04-17 16:14:33 +03:00
|
|
|
});
|
|
|
|
|
2023-04-17 18:14:43 +03:00
|
|
|
it('should return false if the current and previous html values are the same', function () {
|
2023-05-03 20:58:03 +03:00
|
|
|
const postRevisions = new PostRevisions({config, model: {}});
|
2023-04-17 16:14:33 +03:00
|
|
|
|
2023-04-21 16:36:35 +03:00
|
|
|
const expected = {value: false};
|
2023-05-03 20:58:03 +03:00
|
|
|
const actual = postRevisions.shouldGenerateRevision(makePostLike({
|
2023-04-17 18:14:43 +03:00
|
|
|
lexical: 'current',
|
|
|
|
html: 'blah'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), [makeRevision({
|
2023-04-17 16:14:33 +03:00
|
|
|
lexical: 'blah'
|
2023-05-03 20:58:03 +03:00
|
|
|
})]);
|
2023-04-17 16:14:33 +03:00
|
|
|
|
2023-04-21 16:36:35 +03:00
|
|
|
assert.deepEqual(actual, expected);
|
2023-04-17 16:14:33 +03:00
|
|
|
});
|
|
|
|
|
2023-04-21 12:04:05 +03:00
|
|
|
it('should return true if forceRevision is true and the lexical has changed since the latest revision', function () {
|
2023-05-03 20:58:03 +03:00
|
|
|
const postRevisions = new PostRevisions({config, model: {}});
|
2023-04-17 16:14:33 +03:00
|
|
|
|
2023-04-21 16:36:35 +03:00
|
|
|
const expected = {value: true, reason: 'explicit_save'};
|
2023-05-03 20:58:03 +03:00
|
|
|
const actual = postRevisions.shouldGenerateRevision(makePostLike({
|
2023-04-17 16:14:33 +03:00
|
|
|
lexical: 'blah'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), [{
|
2023-04-21 12:04:05 +03:00
|
|
|
lexical: 'blah2'
|
2023-05-03 20:58:03 +03:00
|
|
|
}, {
|
|
|
|
lexical: 'blah3'
|
|
|
|
}].map(makeRevision), {
|
|
|
|
forceRevision: true,
|
|
|
|
isPublished: false
|
2023-04-21 12:04:05 +03:00
|
|
|
});
|
2023-04-17 16:14:33 +03:00
|
|
|
|
2023-04-21 16:36:35 +03:00
|
|
|
assert.deepEqual(actual, expected);
|
2023-04-17 16:14:33 +03:00
|
|
|
});
|
2023-04-18 16:15:26 +03:00
|
|
|
|
2023-04-21 12:04:05 +03:00
|
|
|
it('should return true if the current and previous title values are different and forceRevision is true', function () {
|
2023-05-03 20:58:03 +03:00
|
|
|
const postRevisions = new PostRevisions({config, model: {}});
|
2023-04-18 16:15:26 +03:00
|
|
|
|
2023-04-21 16:36:35 +03:00
|
|
|
const expected = {value: true, reason: 'explicit_save'};
|
2023-05-03 20:58:03 +03:00
|
|
|
const actual = postRevisions.shouldGenerateRevision(makePostLike({
|
2023-04-18 16:15:26 +03:00
|
|
|
lexical: 'blah',
|
|
|
|
html: 'blah',
|
|
|
|
title: 'blah2'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), [{
|
2023-04-21 18:04:54 +03:00
|
|
|
lexical: 'blah',
|
|
|
|
title: 'not blah'
|
2023-05-03 20:58:03 +03:00
|
|
|
}].map(makeRevision), {
|
|
|
|
forceRevision: true,
|
|
|
|
isPublished: false
|
2023-04-21 12:04:05 +03:00
|
|
|
});
|
|
|
|
|
2023-04-21 16:36:35 +03:00
|
|
|
assert.deepEqual(actual, expected);
|
2023-04-21 12:04:05 +03:00
|
|
|
});
|
|
|
|
|
2023-04-21 16:53:35 +03:00
|
|
|
it('should return true if the current and previous feature_image values are different and forceRevision is true', function () {
|
2023-05-03 20:58:03 +03:00
|
|
|
const postRevisions = new PostRevisions({config, model: {}});
|
2023-04-21 12:04:05 +03:00
|
|
|
|
2023-04-21 16:53:35 +03:00
|
|
|
const expected = {value: true, reason: 'explicit_save'};
|
2023-05-03 20:58:03 +03:00
|
|
|
const actual = postRevisions.shouldGenerateRevision(makePostLike({
|
2023-04-21 12:04:05 +03:00
|
|
|
lexical: 'blah',
|
|
|
|
html: 'blah',
|
2023-04-21 16:53:35 +03:00
|
|
|
title: 'blah',
|
|
|
|
feature_image: 'new'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), [{
|
2023-04-21 16:53:35 +03:00
|
|
|
lexical: 'blah',
|
|
|
|
html: 'blah',
|
|
|
|
title: 'blah',
|
|
|
|
feature_image: null
|
2023-05-03 20:58:03 +03:00
|
|
|
}].map(makeRevision), {
|
|
|
|
forceRevision: true,
|
|
|
|
isPublished: false
|
2023-04-21 16:53:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
});
|
|
|
|
|
2023-05-03 10:20:34 +03:00
|
|
|
it('should return true if post is unpublished and forceRevision is true', function () {
|
2023-05-03 20:58:03 +03:00
|
|
|
const postRevisions = new PostRevisions({config, model: {}});
|
2023-05-03 10:20:34 +03:00
|
|
|
|
|
|
|
const expected = {value: true, reason: 'unpublished'};
|
|
|
|
|
2023-05-03 20:58:03 +03:00
|
|
|
const actual = postRevisions.shouldGenerateRevision(makePostLike({
|
2023-05-03 10:20:34 +03:00
|
|
|
lexical: 'blah',
|
|
|
|
html: 'blah',
|
|
|
|
title: 'blah2'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), [makeRevision({
|
2023-05-03 10:20:34 +03:00
|
|
|
lexical: 'blah'
|
2023-05-03 20:58:03 +03:00
|
|
|
})], {
|
2023-05-03 10:20:34 +03:00
|
|
|
isPublished: false,
|
|
|
|
forceRevision: true,
|
|
|
|
newStatus: 'draft',
|
|
|
|
olderStatus: 'published'
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
});
|
|
|
|
|
2023-04-21 16:53:35 +03:00
|
|
|
it('should always return true if isPublished is true', function () {
|
2023-05-03 20:58:03 +03:00
|
|
|
const postRevisions = new PostRevisions({config, model: {}});
|
2023-04-21 16:53:35 +03:00
|
|
|
|
|
|
|
const expected = {value: true, reason: 'published'};
|
2023-05-03 20:58:03 +03:00
|
|
|
const actual = postRevisions.shouldGenerateRevision(makePostLike({
|
2023-04-21 12:04:05 +03:00
|
|
|
lexical: 'blah',
|
|
|
|
html: 'blah',
|
|
|
|
title: 'blah2'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), [{
|
2023-04-21 12:04:05 +03:00
|
|
|
lexical: 'blah'
|
2023-05-03 20:58:03 +03:00
|
|
|
}].map(makeRevision), {
|
|
|
|
forceRevision: false,
|
2023-04-21 12:04:05 +03:00
|
|
|
isPublished: true
|
|
|
|
});
|
2023-04-18 16:15:26 +03:00
|
|
|
|
2023-04-21 16:36:35 +03:00
|
|
|
assert.deepEqual(actual, expected);
|
2023-04-18 16:15:26 +03:00
|
|
|
});
|
2023-04-21 18:04:54 +03:00
|
|
|
|
|
|
|
it('should return true if the latest revision was more than the interval', function () {
|
2023-05-03 20:58:03 +03:00
|
|
|
const postRevisions = new PostRevisions({config, model: {}});
|
2023-04-21 18:04:54 +03:00
|
|
|
|
|
|
|
const expected = {value: true, reason: 'background_save'};
|
2023-05-03 20:58:03 +03:00
|
|
|
const actual = postRevisions.shouldGenerateRevision(makePostLike({
|
2023-04-21 18:04:54 +03:00
|
|
|
lexical: 'blah',
|
|
|
|
html: 'blah',
|
|
|
|
title: 'blah'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), [{
|
|
|
|
lexical: 'blah2',
|
2023-04-21 18:04:54 +03:00
|
|
|
created_at_ts: Date.now() - 2000
|
2023-05-03 20:58:03 +03:00
|
|
|
}].map(makeRevision), {});
|
2023-04-21 18:04:54 +03:00
|
|
|
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
});
|
2023-04-17 16:14:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getRevisions', function () {
|
|
|
|
it('returns the original revisions if there is no previous', async function () {
|
2023-05-03 20:58:03 +03:00
|
|
|
const postRevisions = new PostRevisions({config, model: {}});
|
2023-05-12 15:13:51 +03:00
|
|
|
const now = Date.now();
|
2023-04-17 16:14:33 +03:00
|
|
|
|
|
|
|
const expected = [{
|
2023-05-12 15:13:51 +03:00
|
|
|
lexical: 'blah',
|
|
|
|
created_at_ts: now
|
2023-05-03 20:58:03 +03:00
|
|
|
}].map(makeRevision);
|
|
|
|
|
|
|
|
const actual = await postRevisions.getRevisions(makePostLike({}), [{
|
2023-05-12 15:13:51 +03:00
|
|
|
lexical: 'blah',
|
|
|
|
created_at_ts: now
|
2023-05-03 20:58:03 +03:00
|
|
|
}].map(makeRevision));
|
2023-04-17 16:14:33 +03:00
|
|
|
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the original revisions if the current and previous', async function () {
|
2023-05-03 20:58:03 +03:00
|
|
|
const postRevisions = new PostRevisions({config, model: {}});
|
2023-05-12 15:13:51 +03:00
|
|
|
const now = Date.now();
|
2023-04-17 16:14:33 +03:00
|
|
|
|
|
|
|
const expected = [{
|
2023-05-12 15:13:51 +03:00
|
|
|
lexical: 'revision',
|
|
|
|
created_at_ts: now
|
2023-05-03 20:58:03 +03:00
|
|
|
}].map(makeRevision);
|
|
|
|
|
|
|
|
const actual = await postRevisions.getRevisions(makePostLike({
|
2023-04-17 18:14:43 +03:00
|
|
|
lexical: 'blah',
|
|
|
|
html: 'blah'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), [{
|
2023-05-12 15:13:51 +03:00
|
|
|
lexical: 'revision',
|
|
|
|
created_at_ts: now
|
2023-05-03 20:58:03 +03:00
|
|
|
}].map(makeRevision));
|
2023-04-17 16:14:33 +03:00
|
|
|
|
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
});
|
|
|
|
|
2023-04-18 16:15:26 +03:00
|
|
|
it('returns one revision when there are no existing revisions', async function () {
|
2023-05-03 20:58:03 +03:00
|
|
|
const postRevisions = new PostRevisions({config, model: {}});
|
2023-04-17 16:14:33 +03:00
|
|
|
|
2023-05-03 20:58:03 +03:00
|
|
|
const actual = await postRevisions.getRevisions(makePostLike({
|
2023-04-17 16:14:33 +03:00
|
|
|
id: '1',
|
2023-04-17 18:14:43 +03:00
|
|
|
lexical: 'current',
|
2023-04-18 16:15:26 +03:00
|
|
|
html: 'current',
|
|
|
|
author_id: '123',
|
|
|
|
title: 'foo bar baz'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), []);
|
2023-04-17 16:14:33 +03:00
|
|
|
|
2023-04-17 18:52:51 +03:00
|
|
|
assert.equal(actual.length, 1);
|
|
|
|
assert.equal(actual[0].lexical, 'current');
|
2023-04-18 16:15:26 +03:00
|
|
|
assert.equal(actual[0].author_id, '123');
|
|
|
|
assert.equal(actual[0].title, 'foo bar baz');
|
2023-04-17 16:14:33 +03:00
|
|
|
});
|
|
|
|
|
2023-04-21 16:36:35 +03:00
|
|
|
it('does not limit the number of revisions if under the max_revisions count', async function () {
|
2023-04-17 16:14:33 +03:00
|
|
|
const postRevisions = new PostRevisions({
|
|
|
|
config: {
|
2023-05-03 20:58:03 +03:00
|
|
|
max_revisions: 2,
|
|
|
|
revision_interval_ms: config.revision_interval_ms
|
|
|
|
},
|
|
|
|
model: {}
|
2023-04-17 16:14:33 +03:00
|
|
|
});
|
|
|
|
|
2023-05-03 20:58:03 +03:00
|
|
|
const revisions = await postRevisions.getRevisions(makePostLike({
|
2023-04-17 16:14:33 +03:00
|
|
|
id: '1',
|
2023-04-17 18:14:43 +03:00
|
|
|
lexical: 'current',
|
|
|
|
html: 'current'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), []);
|
2023-04-17 16:14:33 +03:00
|
|
|
|
2023-05-03 20:58:03 +03:00
|
|
|
const actual = await postRevisions.getRevisions(makePostLike({
|
2023-04-17 16:14:33 +03:00
|
|
|
id: '1',
|
2023-04-17 18:14:43 +03:00
|
|
|
lexical: 'new',
|
|
|
|
html: 'new'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), revisions, {
|
2023-04-21 12:04:05 +03:00
|
|
|
forceRevision: true
|
|
|
|
});
|
2023-04-17 16:14:33 +03:00
|
|
|
|
|
|
|
assert.equal(actual.length, 2);
|
|
|
|
});
|
2023-04-21 16:36:35 +03:00
|
|
|
|
|
|
|
it('limits the number of revisions to the max_revisions count', async function () {
|
|
|
|
const postRevisions = new PostRevisions({
|
|
|
|
config: {
|
2023-05-03 20:58:03 +03:00
|
|
|
max_revisions: 1,
|
|
|
|
revision_interval_ms: config.revision_interval_ms
|
|
|
|
},
|
|
|
|
model: {}
|
2023-04-21 16:36:35 +03:00
|
|
|
});
|
|
|
|
|
2023-05-03 20:58:03 +03:00
|
|
|
const revisions = await postRevisions.getRevisions(makePostLike({
|
2023-04-21 16:36:35 +03:00
|
|
|
id: '1',
|
|
|
|
lexical: 'current',
|
|
|
|
html: 'current'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), []);
|
2023-04-21 16:36:35 +03:00
|
|
|
|
2023-05-03 20:58:03 +03:00
|
|
|
const actual = await postRevisions.getRevisions(makePostLike({
|
2023-04-21 16:36:35 +03:00
|
|
|
id: '1',
|
|
|
|
lexical: 'new',
|
|
|
|
html: 'new'
|
2023-05-03 20:58:03 +03:00
|
|
|
}), revisions, {
|
2023-04-21 16:36:35 +03:00
|
|
|
forceRevision: true
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(actual.length, 1);
|
|
|
|
});
|
2023-04-17 13:12:55 +03:00
|
|
|
});
|
2023-04-19 16:00:45 +03:00
|
|
|
|
|
|
|
describe('removeAuthor', function () {
|
|
|
|
it('removes the provided author from post revisions', async function () {
|
|
|
|
const authorId = 'abc123';
|
|
|
|
const options = {
|
|
|
|
transacting: {}
|
|
|
|
};
|
|
|
|
const revisions = [
|
|
|
|
{
|
|
|
|
id: 'revision123',
|
|
|
|
post_id: 'post123',
|
|
|
|
author_id: 'author123'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'revision456',
|
|
|
|
post_id: 'post123',
|
|
|
|
author_id: 'author123'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'revision789',
|
|
|
|
post_id: 'post123',
|
|
|
|
author_id: 'author456'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
const modelStub = {
|
|
|
|
findAll: sinon.stub().resolves({
|
|
|
|
toJSON: () => revisions
|
|
|
|
}),
|
|
|
|
bulkEdit: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
const postRevisions = new PostRevisions({
|
2023-05-03 20:58:03 +03:00
|
|
|
config,
|
2023-04-19 16:00:45 +03:00
|
|
|
model: modelStub
|
|
|
|
});
|
|
|
|
|
|
|
|
await postRevisions.removeAuthorFromRevisions(authorId, options);
|
|
|
|
|
|
|
|
assert.equal(modelStub.bulkEdit.calledOnce, true);
|
|
|
|
|
|
|
|
const bulkEditArgs = modelStub.bulkEdit.getCall(0).args;
|
|
|
|
|
|
|
|
assert.deepEqual(bulkEditArgs[0], ['revision123', 'revision456', 'revision789']);
|
|
|
|
assert.equal(bulkEditArgs[1], 'post_revisions');
|
|
|
|
assert.deepEqual(bulkEditArgs[2], {
|
|
|
|
data: {
|
|
|
|
author_id: null
|
|
|
|
},
|
|
|
|
column: 'id',
|
|
|
|
transacting: options.transacting,
|
|
|
|
throwErrors: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does nothing if there are no post revisions by the provided author', async function () {
|
|
|
|
const modelStub = {
|
|
|
|
findAll: sinon.stub().resolves({
|
|
|
|
toJSON: () => []
|
|
|
|
}),
|
|
|
|
bulkEdit: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
const postRevisions = new PostRevisions({
|
2023-05-03 20:58:03 +03:00
|
|
|
config,
|
2023-04-19 16:00:45 +03:00
|
|
|
model: modelStub
|
|
|
|
});
|
|
|
|
|
|
|
|
await postRevisions.removeAuthorFromRevisions('abc123', {
|
|
|
|
transacting: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(modelStub.bulkEdit.calledOnce, false);
|
|
|
|
});
|
|
|
|
});
|
2023-04-17 13:12:55 +03:00
|
|
|
});
|