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 18:01:47 +03:00
|
|
|
getResourceById(id, type) {
|
2022-08-19 23:39:18 +03:00
|
|
|
if (id === 'invalid') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
id,
|
2022-08-24 18:01:47 +03:00
|
|
|
type,
|
|
|
|
url: 'https://absolute/dir/path',
|
|
|
|
title: 'Title'
|
2022-08-19 23:39:18 +03:00
|
|
|
};
|
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 () {
|
|
|
|
const history = new UrlHistory([]);
|
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-22 18:16:18 +03:00
|
|
|
const history = new UrlHistory([{path: '/dir/not-last', time: 123}, {path: '/dir/test/', time: 123}]);
|
|
|
|
should(attributionBuilder.getAttribution(history)).match({type: 'url', id: null, url: '/test/'});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns last post', function () {
|
|
|
|
const history = new UrlHistory([
|
2022-08-22 18:16:18 +03:00
|
|
|
{path: '/dir/my-post', time: 123},
|
|
|
|
{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 () {
|
|
|
|
const history = new UrlHistory([
|
2022-08-22 18:16:18 +03:00
|
|
|
{path: '/dir/my-post', time: 123},
|
|
|
|
{path: '/dir/my-page', 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 page if no posts', function () {
|
|
|
|
const history = new UrlHistory([
|
2022-08-22 18:16:18 +03:00
|
|
|
{path: '/dir/other', time: 123},
|
|
|
|
{path: '/dir/my-page', 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: 'page', id: 845, url: '/my-page'});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns all null for invalid histories', function () {
|
|
|
|
const history = new UrlHistory('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 () {
|
|
|
|
const history = new UrlHistory([]);
|
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 18:01:47 +03:00
|
|
|
should(await attributionBuilder.build({type: 'post', id: '123', url: '/post'}).getResource()).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 18:01:47 +03:00
|
|
|
should(await attributionBuilder.build({type: 'url', id: null, url: '/url'}).getResource()).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 18:01:47 +03:00
|
|
|
should(await attributionBuilder.build({type: 'post', id: 'invalid', url: '/post'}).getResource()).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
|
|
|
});
|