2022-08-18 18:38:42 +03:00
|
|
|
// Switch these lines once there are useful utils
|
|
|
|
// const testUtils = require('./utils');
|
|
|
|
require('./utils');
|
|
|
|
const UrlHistory = require('../lib/history');
|
|
|
|
const AttributionBuilder = require('../lib/attribution');
|
|
|
|
|
|
|
|
describe('AttributionBuilder', function () {
|
|
|
|
let attributionBuilder;
|
2022-08-25 19:21:41 +03:00
|
|
|
let now;
|
2022-08-18 18:38:42 +03:00
|
|
|
|
|
|
|
before(function () {
|
2022-08-25 19:21:41 +03:00
|
|
|
now = Date.now();
|
2022-08-18 18:38:42 +03:00
|
|
|
attributionBuilder = new AttributionBuilder({
|
|
|
|
urlTranslator: {
|
2022-09-14 22:50:54 +03:00
|
|
|
getResourceDetails(item) {
|
|
|
|
if (!item.path) {
|
|
|
|
if (item.id === 'invalid') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: item.id,
|
|
|
|
type: item.type,
|
|
|
|
url: `/${item.type}/${item.id}`
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const path = this.stripSubdirectoryFromPath(item.path);
|
|
|
|
|
2022-08-18 18:38:42 +03:00
|
|
|
if (path === '/my-post') {
|
|
|
|
return {
|
|
|
|
id: 123,
|
2022-09-14 22:50:54 +03:00
|
|
|
type: 'post',
|
|
|
|
url: path
|
2022-08-18 18:38:42 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (path === '/my-page') {
|
|
|
|
return {
|
|
|
|
id: 845,
|
2022-09-14 22:50:54 +03:00
|
|
|
type: 'page',
|
|
|
|
url: path
|
2022-08-18 18:38:42 +03:00
|
|
|
};
|
|
|
|
}
|
2022-09-14 22:50:54 +03:00
|
|
|
return {
|
|
|
|
id: null,
|
|
|
|
type: 'url',
|
|
|
|
url: path
|
|
|
|
};
|
2022-08-19 23:39:18 +03:00
|
|
|
},
|
2022-09-14 22:50:54 +03:00
|
|
|
getResourceById(id, type) {
|
2022-08-19 23:39:18 +03:00
|
|
|
if (id === 'invalid') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
id,
|
2022-09-14 22:50:54 +03:00
|
|
|
get(prop) {
|
|
|
|
if (prop === 'title' && type === 'author') {
|
|
|
|
// Simulate an author doesn't have a title
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (id === 'no-props') {
|
|
|
|
// Simulate a model without properties for branch coverage
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return prop;
|
2022-08-24 17:11:25 +03:00
|
|
|
}
|
2022-08-19 23:39:18 +03:00
|
|
|
};
|
2022-08-22 18:16:18 +03:00
|
|
|
},
|
2022-08-25 15:51:38 +03:00
|
|
|
getUrlTitle(url) {
|
|
|
|
return url;
|
|
|
|
},
|
2022-08-24 17:11:25 +03:00
|
|
|
getUrlByResourceId() {
|
|
|
|
return 'https://absolute/dir/path';
|
|
|
|
},
|
2022-08-22 18:16:18 +03:00
|
|
|
relativeToAbsolute(path) {
|
|
|
|
return 'https://absolute/dir' + path;
|
|
|
|
},
|
|
|
|
stripSubdirectoryFromPath(path) {
|
|
|
|
if (path.startsWith('/dir/')) {
|
|
|
|
return path.substring('/dir/'.length - 1);
|
|
|
|
}
|
|
|
|
return path;
|
2022-08-18 18:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-14 22:50:54 +03:00
|
|
|
it('Returns empty if empty history', async function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create([]);
|
2022-09-14 22:50:54 +03:00
|
|
|
should(await attributionBuilder.getAttribution(history)).match({id: null, type: null, url: null});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
|
2022-09-14 22:50:54 +03:00
|
|
|
it('Returns last url', async function () {
|
2022-08-25 19:21:41 +03:00
|
|
|
const history = UrlHistory.create([{path: '/dir/not-last', time: now + 123}, {path: '/dir/test/', time: now + 123}]);
|
2022-09-14 22:50:54 +03:00
|
|
|
should(await attributionBuilder.getAttribution(history)).match({type: 'url', id: null, url: '/test/'});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
|
2022-09-14 22:50:54 +03:00
|
|
|
it('Returns last post', async function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create([
|
2022-08-25 19:21:41 +03:00
|
|
|
{path: '/dir/my-post', time: now + 123},
|
|
|
|
{path: '/dir/test', time: now + 124},
|
|
|
|
{path: '/dir/unknown-page', time: now + 125}
|
2022-08-18 18:38:42 +03:00
|
|
|
]);
|
2022-09-14 22:50:54 +03:00
|
|
|
should(await attributionBuilder.getAttribution(history)).match({type: 'post', id: 123, url: '/my-post'});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
|
2022-09-14 22:50:54 +03:00
|
|
|
it('Returns last post even when it found pages', async function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create([
|
2022-08-25 19:21:41 +03:00
|
|
|
{path: '/dir/my-post', time: now + 123},
|
|
|
|
{path: '/dir/my-page', time: now + 124},
|
|
|
|
{path: '/dir/unknown-page', time: now + 125}
|
2022-08-18 18:38:42 +03:00
|
|
|
]);
|
2022-09-14 22:50:54 +03:00
|
|
|
should(await attributionBuilder.getAttribution(history)).match({type: 'post', id: 123, url: '/my-post'});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
|
2022-09-14 22:50:54 +03:00
|
|
|
it('Returns last page if no posts', async function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create([
|
2022-08-25 19:21:41 +03:00
|
|
|
{path: '/dir/other', time: now + 123},
|
|
|
|
{path: '/dir/my-page', time: now + 124},
|
|
|
|
{path: '/dir/unknown-page', time: now + 125}
|
2022-08-18 18:38:42 +03:00
|
|
|
]);
|
2022-09-14 22:50:54 +03:00
|
|
|
should(await attributionBuilder.getAttribution(history)).match({type: 'page', id: 845, url: '/my-page'});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns last post via id', async function () {
|
|
|
|
const history = UrlHistory.create([
|
|
|
|
{path: '/dir/other', time: now + 123},
|
|
|
|
{id: '123', type: 'post', time: now + 124},
|
|
|
|
{path: '/dir/unknown-page', time: now + 125}
|
|
|
|
]);
|
|
|
|
should(await attributionBuilder.getAttribution(history)).match({type: 'post', id: '123', url: '/post/123'});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns all null if only invalid ids', async function () {
|
|
|
|
const history = UrlHistory.create([
|
|
|
|
{id: 'invalid', type: 'post', time: now + 124},
|
|
|
|
{id: 'invalid', type: 'post', time: now + 124}
|
|
|
|
]);
|
|
|
|
should(await attributionBuilder.getAttribution(history)).match({
|
|
|
|
type: null,
|
|
|
|
id: null,
|
|
|
|
url: null
|
|
|
|
});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
|
2022-09-14 22:50:54 +03:00
|
|
|
it('Returns all null for invalid histories', async function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create('invalid');
|
2022-09-14 22:50:54 +03:00
|
|
|
should(await attributionBuilder.getAttribution(history)).match({
|
2022-08-18 18:38:42 +03:00
|
|
|
type: null,
|
|
|
|
id: null,
|
|
|
|
url: null
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-14 22:50:54 +03:00
|
|
|
it('Returns all null for empty histories', async function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create([]);
|
2022-09-14 22:50:54 +03:00
|
|
|
should(await attributionBuilder.getAttribution(history)).match({
|
2022-08-18 18:38:42 +03:00
|
|
|
type: null,
|
|
|
|
id: null,
|
|
|
|
url: null
|
|
|
|
});
|
|
|
|
});
|
2022-08-19 23:39:18 +03:00
|
|
|
|
|
|
|
it('Returns post resource', async function () {
|
2022-08-24 17:11:25 +03:00
|
|
|
should(await attributionBuilder.build({type: 'post', id: '123', url: '/post'}).fetchResource()).match({
|
2022-08-19 23:39:18 +03:00
|
|
|
type: 'post',
|
|
|
|
id: '123',
|
2022-08-22 18:16:18 +03:00
|
|
|
url: 'https://absolute/dir/path',
|
2022-09-14 22:50:54 +03:00
|
|
|
title: 'title'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns author resource', async function () {
|
|
|
|
should(await attributionBuilder.build({type: 'author', id: '123', url: '/author'}).fetchResource()).match({
|
|
|
|
type: 'author',
|
|
|
|
id: '123',
|
|
|
|
url: 'https://absolute/dir/path',
|
|
|
|
title: 'name'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns default url title for resource if no title or name', async function () {
|
|
|
|
should(await attributionBuilder.build({type: 'post', id: 'no-props', url: '/post'}).fetchResource()).match({
|
|
|
|
type: 'post',
|
|
|
|
id: 'no-props',
|
|
|
|
url: 'https://absolute/dir/path',
|
|
|
|
title: '/post'
|
2022-08-19 23:39:18 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns url resource', async function () {
|
2022-08-24 17:11:25 +03:00
|
|
|
should(await attributionBuilder.build({type: 'url', id: null, url: '/url'}).fetchResource()).match({
|
2022-08-19 23:39:18 +03:00
|
|
|
type: 'url',
|
|
|
|
id: null,
|
2022-08-22 18:16:18 +03:00
|
|
|
url: 'https://absolute/dir/url',
|
2022-08-19 23:39:18 +03:00
|
|
|
title: '/url'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns url resource if not found', async function () {
|
2022-08-24 17:11:25 +03:00
|
|
|
should(await attributionBuilder.build({type: 'post', id: 'invalid', url: '/post'}).fetchResource()).match({
|
2022-08-19 23:39:18 +03:00
|
|
|
type: 'url',
|
|
|
|
id: null,
|
2022-08-22 18:16:18 +03:00
|
|
|
url: 'https://absolute/dir/post',
|
2022-08-19 23:39:18 +03:00
|
|
|
title: '/post'
|
|
|
|
});
|
|
|
|
});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|