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;
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
attributionBuilder = new AttributionBuilder({
|
|
|
|
urlTranslator: {
|
|
|
|
getTypeAndId(path) {
|
|
|
|
if (path === '/my-post') {
|
|
|
|
return {
|
|
|
|
id: 123,
|
|
|
|
type: 'post'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (path === '/my-page') {
|
|
|
|
return {
|
|
|
|
id: 845,
|
|
|
|
type: 'page'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return;
|
2022-08-19 23:39:18 +03:00
|
|
|
},
|
2022-08-24 17:11:25 +03:00
|
|
|
getResourceById(id) {
|
2022-08-19 23:39:18 +03:00
|
|
|
if (id === 'invalid') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
id,
|
2022-08-24 17:11:25 +03:00
|
|
|
get() {
|
|
|
|
return 'Title';
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns empty if empty history', function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create([]);
|
2022-08-19 23:39:18 +03:00
|
|
|
should(attributionBuilder.getAttribution(history)).match({id: null, type: null, url: null});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns last url', function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create([{path: '/dir/not-last', time: 123}, {path: '/dir/test/', time: 123}]);
|
2022-08-22 18:16:18 +03:00
|
|
|
should(attributionBuilder.getAttribution(history)).match({type: 'url', id: null, url: '/test/'});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns last post', function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create([
|
|
|
|
{path: '/dir/my-post', time: 123},
|
2022-08-22 18:16:18 +03:00
|
|
|
{path: '/dir/test', time: 124},
|
|
|
|
{path: '/dir/unknown-page', time: 125}
|
2022-08-18 18:38:42 +03:00
|
|
|
]);
|
2022-08-19 23:39:18 +03:00
|
|
|
should(attributionBuilder.getAttribution(history)).match({type: 'post', id: 123, url: '/my-post'});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns last post even when it found pages', function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create([
|
|
|
|
{path: '/dir/my-post', time: 123},
|
|
|
|
{path: '/dir/my-page', time: 124},
|
2022-08-22 18:16:18 +03:00
|
|
|
{path: '/dir/unknown-page', time: 125}
|
2022-08-18 18:38:42 +03:00
|
|
|
]);
|
2022-08-19 23:39:18 +03:00
|
|
|
should(attributionBuilder.getAttribution(history)).match({type: 'post', id: 123, url: '/my-post'});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns last page if no posts', function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create([
|
|
|
|
{path: '/dir/other', time: 123},
|
|
|
|
{path: '/dir/my-page', time: 124},
|
2022-08-22 18:16:18 +03:00
|
|
|
{path: '/dir/unknown-page', time: 125}
|
2022-08-18 18:38:42 +03:00
|
|
|
]);
|
2022-08-19 23:39:18 +03:00
|
|
|
should(attributionBuilder.getAttribution(history)).match({type: 'page', id: 845, url: '/my-page'});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns all null for invalid histories', function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create('invalid');
|
2022-08-19 23:39:18 +03:00
|
|
|
should(attributionBuilder.getAttribution(history)).match({
|
2022-08-18 18:38:42 +03:00
|
|
|
type: null,
|
|
|
|
id: null,
|
|
|
|
url: null
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns all null for empty histories', function () {
|
2022-08-25 19:21:08 +03:00
|
|
|
const history = UrlHistory.create([]);
|
2022-08-19 23:39:18 +03:00
|
|
|
should(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-08-19 23:39:18 +03:00
|
|
|
title: 'Title'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
});
|