2023-04-14 10:28:53 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-08-02 09:23:48 +03:00
|
|
|
import { normalizeVisits, processStatsFromVisits } from '../../../shlink-web-component/src/visits/services/VisitsParser';
|
|
|
|
import type { OrphanVisit, Visit, VisitsStats } from '../../../shlink-web-component/src/visits/types';
|
2018-08-25 00:38:37 +03:00
|
|
|
|
|
|
|
describe('VisitsParser', () => {
|
2020-09-04 19:43:26 +03:00
|
|
|
const visits: Visit[] = [
|
2023-04-14 10:28:53 +03:00
|
|
|
fromPartial({
|
2018-08-25 00:38:37 +03:00
|
|
|
userAgent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0',
|
|
|
|
referer: 'https://google.com',
|
2022-12-23 23:16:17 +03:00
|
|
|
potentialBot: false,
|
2018-08-25 00:38:37 +03:00
|
|
|
visitLocation: {
|
|
|
|
countryName: 'Spain',
|
2019-01-07 13:53:14 +03:00
|
|
|
cityName: 'Zaragoza',
|
2020-04-10 15:59:12 +03:00
|
|
|
latitude: 123.45,
|
|
|
|
longitude: -543.21,
|
2018-08-25 00:38:37 +03:00
|
|
|
},
|
2020-09-04 19:43:26 +03:00
|
|
|
}),
|
2023-04-14 10:28:53 +03:00
|
|
|
fromPartial({
|
2018-08-25 00:38:37 +03:00
|
|
|
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0',
|
|
|
|
referer: 'https://google.com',
|
2022-12-23 23:16:17 +03:00
|
|
|
potentialBot: false,
|
2018-08-25 00:38:37 +03:00
|
|
|
visitLocation: {
|
|
|
|
countryName: 'United States',
|
2019-01-07 13:53:14 +03:00
|
|
|
cityName: 'New York',
|
2020-04-10 15:59:12 +03:00
|
|
|
latitude: 1029,
|
|
|
|
longitude: 6758,
|
2018-08-25 00:38:37 +03:00
|
|
|
},
|
2020-09-04 19:43:26 +03:00
|
|
|
}),
|
2023-04-14 10:28:53 +03:00
|
|
|
fromPartial({
|
2018-08-25 00:38:37 +03:00
|
|
|
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
|
2022-12-23 23:16:17 +03:00
|
|
|
potentialBot: false,
|
2018-08-25 00:38:37 +03:00
|
|
|
visitLocation: {
|
|
|
|
countryName: 'Spain',
|
2020-09-19 11:31:23 +03:00
|
|
|
cityName: '',
|
2018-08-25 00:38:37 +03:00
|
|
|
},
|
2020-09-04 19:43:26 +03:00
|
|
|
}),
|
2023-04-14 10:28:53 +03:00
|
|
|
fromPartial({
|
2018-08-25 00:38:37 +03:00
|
|
|
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
|
|
|
|
referer: 'https://m.facebook.com',
|
2022-12-23 23:16:17 +03:00
|
|
|
potentialBot: false,
|
2018-08-25 00:38:37 +03:00
|
|
|
visitLocation: {
|
|
|
|
countryName: 'Spain',
|
2019-01-07 13:53:14 +03:00
|
|
|
cityName: 'Zaragoza',
|
2020-04-10 15:59:12 +03:00
|
|
|
latitude: 123.45,
|
|
|
|
longitude: -543.21,
|
2018-08-25 00:38:37 +03:00
|
|
|
},
|
2020-09-04 19:43:26 +03:00
|
|
|
}),
|
2023-04-14 10:28:53 +03:00
|
|
|
fromPartial({
|
2018-08-25 00:38:37 +03:00
|
|
|
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41',
|
2021-06-13 12:07:32 +03:00
|
|
|
potentialBot: true,
|
2020-09-04 19:43:26 +03:00
|
|
|
}),
|
2018-08-25 00:38:37 +03:00
|
|
|
];
|
2021-02-27 22:03:51 +03:00
|
|
|
const orphanVisits: OrphanVisit[] = [
|
2023-04-14 10:28:53 +03:00
|
|
|
fromPartial({
|
2021-02-27 22:03:51 +03:00
|
|
|
type: 'base_url',
|
|
|
|
visitedUrl: 'foo',
|
|
|
|
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0',
|
|
|
|
referer: 'https://google.com',
|
2022-12-23 23:16:17 +03:00
|
|
|
potentialBot: false,
|
2021-02-27 22:03:51 +03:00
|
|
|
visitLocation: {
|
|
|
|
countryName: 'United States',
|
|
|
|
cityName: 'New York',
|
|
|
|
latitude: 1029,
|
|
|
|
longitude: 6758,
|
|
|
|
},
|
|
|
|
}),
|
2023-04-14 10:28:53 +03:00
|
|
|
fromPartial({
|
2021-02-27 22:03:51 +03:00
|
|
|
type: 'regular_404',
|
|
|
|
visitedUrl: 'bar',
|
2021-06-13 12:07:32 +03:00
|
|
|
potentialBot: true,
|
2021-02-27 22:03:51 +03:00
|
|
|
}),
|
2023-04-14 10:28:53 +03:00
|
|
|
fromPartial({
|
2021-02-27 22:03:51 +03:00
|
|
|
type: 'invalid_short_url',
|
2021-03-28 22:03:46 +03:00
|
|
|
visitedUrl: 'bar',
|
2021-02-27 22:03:51 +03:00
|
|
|
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
|
|
|
|
referer: 'https://m.facebook.com',
|
|
|
|
visitLocation: {
|
|
|
|
countryName: 'Spain',
|
|
|
|
cityName: 'Zaragoza',
|
|
|
|
latitude: 123.45,
|
|
|
|
longitude: -543.21,
|
|
|
|
},
|
2021-06-13 12:07:32 +03:00
|
|
|
potentialBot: false,
|
2021-02-27 22:03:51 +03:00
|
|
|
}),
|
|
|
|
];
|
2018-08-25 00:38:37 +03:00
|
|
|
|
2019-03-04 20:14:45 +03:00
|
|
|
describe('processStatsFromVisits', () => {
|
2020-09-04 19:43:26 +03:00
|
|
|
let stats: VisitsStats;
|
2019-03-04 20:14:45 +03:00
|
|
|
|
|
|
|
beforeAll(() => {
|
2020-04-10 15:59:12 +03:00
|
|
|
stats = processStatsFromVisits(normalizeVisits(visits));
|
2019-03-04 20:14:45 +03:00
|
|
|
});
|
|
|
|
|
2018-08-25 00:38:37 +03:00
|
|
|
it('properly parses OS stats', () => {
|
2019-03-04 20:14:45 +03:00
|
|
|
const { os } = stats;
|
|
|
|
|
|
|
|
expect(os).toEqual({
|
2018-08-26 00:39:27 +03:00
|
|
|
Linux: 3,
|
|
|
|
Windows: 1,
|
2020-04-10 20:16:44 +03:00
|
|
|
macOS: 1,
|
2018-08-25 00:38:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('properly parses browser stats', () => {
|
2019-03-04 20:14:45 +03:00
|
|
|
const { browsers } = stats;
|
|
|
|
|
|
|
|
expect(browsers).toEqual({
|
2018-08-26 00:39:27 +03:00
|
|
|
Firefox: 2,
|
|
|
|
Chrome: 2,
|
|
|
|
Opera: 1,
|
2018-08-25 00:38:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('properly parses referrer stats', () => {
|
2019-03-04 20:14:45 +03:00
|
|
|
const { referrers } = stats;
|
|
|
|
|
|
|
|
expect(referrers).toEqual({
|
2022-03-26 14:17:42 +03:00
|
|
|
Direct: 2,
|
2018-08-25 00:38:37 +03:00
|
|
|
'google.com': 2,
|
|
|
|
'm.facebook.com': 1,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('properly parses countries stats', () => {
|
2019-03-04 20:14:45 +03:00
|
|
|
const { countries } = stats;
|
|
|
|
|
|
|
|
expect(countries).toEqual({
|
2022-03-26 14:17:42 +03:00
|
|
|
Spain: 3,
|
2018-08-25 00:38:37 +03:00
|
|
|
'United States': 1,
|
2022-03-26 14:17:42 +03:00
|
|
|
Unknown: 1,
|
2018-08-25 00:38:37 +03:00
|
|
|
});
|
|
|
|
});
|
2019-01-07 13:53:14 +03:00
|
|
|
|
|
|
|
it('properly parses cities stats', () => {
|
2019-03-04 20:14:45 +03:00
|
|
|
const { cities } = stats;
|
|
|
|
|
|
|
|
expect(cities).toEqual({
|
2022-03-26 14:17:42 +03:00
|
|
|
Zaragoza: 2,
|
2019-01-07 13:53:14 +03:00
|
|
|
'New York': 1,
|
2022-03-26 14:17:42 +03:00
|
|
|
Unknown: 2,
|
2019-01-07 13:53:14 +03:00
|
|
|
});
|
|
|
|
});
|
2019-01-09 09:59:56 +03:00
|
|
|
|
|
|
|
it('properly parses cities stats with lat and long', () => {
|
2019-03-04 20:14:45 +03:00
|
|
|
const { citiesForMap } = stats;
|
2019-01-09 09:59:56 +03:00
|
|
|
const zaragozaLat = 123.45;
|
|
|
|
const zaragozaLong = -543.21;
|
|
|
|
const newYorkLat = 1029;
|
|
|
|
const newYorkLong = 6758;
|
|
|
|
|
2019-03-04 20:14:45 +03:00
|
|
|
expect(citiesForMap).toEqual({
|
2022-03-26 14:17:42 +03:00
|
|
|
Zaragoza: {
|
2019-01-09 09:59:56 +03:00
|
|
|
cityName: 'Zaragoza',
|
|
|
|
count: 2,
|
2022-03-26 14:17:42 +03:00
|
|
|
latLong: [zaragozaLat, zaragozaLong],
|
2019-01-09 09:59:56 +03:00
|
|
|
},
|
|
|
|
'New York': {
|
|
|
|
cityName: 'New York',
|
|
|
|
count: 1,
|
2022-03-26 14:17:42 +03:00
|
|
|
latLong: [newYorkLat, newYorkLong],
|
2019-01-09 09:59:56 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2021-03-28 22:03:46 +03:00
|
|
|
|
|
|
|
it('properly parses visited URL stats', () => {
|
|
|
|
const { visitedUrls } = processStatsFromVisits(normalizeVisits(orphanVisits));
|
|
|
|
|
|
|
|
expect(visitedUrls).toEqual({
|
|
|
|
foo: 1,
|
|
|
|
bar: 2,
|
|
|
|
});
|
|
|
|
});
|
2019-01-09 09:59:56 +03:00
|
|
|
});
|
2020-04-10 12:59:53 +03:00
|
|
|
|
|
|
|
describe('normalizeVisits', () => {
|
|
|
|
it('properly parses the list of visits', () => {
|
|
|
|
expect(normalizeVisits(visits)).toEqual([
|
|
|
|
{
|
|
|
|
browser: 'Firefox',
|
|
|
|
os: 'Windows',
|
|
|
|
referer: 'google.com',
|
|
|
|
country: 'Spain',
|
|
|
|
city: 'Zaragoza',
|
|
|
|
date: undefined,
|
2020-04-10 15:59:12 +03:00
|
|
|
latitude: 123.45,
|
|
|
|
longitude: -543.21,
|
2021-06-13 12:07:32 +03:00
|
|
|
potentialBot: false,
|
2020-04-10 12:59:53 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
browser: 'Firefox',
|
2020-04-10 20:16:44 +03:00
|
|
|
os: 'macOS',
|
2020-04-10 12:59:53 +03:00
|
|
|
referer: 'google.com',
|
|
|
|
country: 'United States',
|
|
|
|
city: 'New York',
|
|
|
|
date: undefined,
|
2020-04-10 15:59:12 +03:00
|
|
|
latitude: 1029,
|
|
|
|
longitude: 6758,
|
2021-06-13 12:07:32 +03:00
|
|
|
potentialBot: false,
|
2020-04-10 12:59:53 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
browser: 'Chrome',
|
|
|
|
os: 'Linux',
|
|
|
|
referer: 'Direct',
|
|
|
|
country: 'Spain',
|
|
|
|
city: 'Unknown',
|
|
|
|
date: undefined,
|
2020-04-10 15:59:12 +03:00
|
|
|
latitude: undefined,
|
|
|
|
longitude: undefined,
|
2021-06-13 12:07:32 +03:00
|
|
|
potentialBot: false,
|
2020-04-10 12:59:53 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
browser: 'Chrome',
|
|
|
|
os: 'Linux',
|
|
|
|
referer: 'm.facebook.com',
|
|
|
|
country: 'Spain',
|
|
|
|
city: 'Zaragoza',
|
|
|
|
date: undefined,
|
2020-04-10 15:59:12 +03:00
|
|
|
latitude: 123.45,
|
|
|
|
longitude: -543.21,
|
2021-06-13 12:07:32 +03:00
|
|
|
potentialBot: false,
|
2020-04-10 12:59:53 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
browser: 'Opera',
|
|
|
|
os: 'Linux',
|
|
|
|
referer: 'Direct',
|
|
|
|
country: 'Unknown',
|
|
|
|
city: 'Unknown',
|
|
|
|
date: undefined,
|
2020-04-10 15:59:12 +03:00
|
|
|
latitude: undefined,
|
|
|
|
longitude: undefined,
|
2021-06-13 12:07:32 +03:00
|
|
|
potentialBot: true,
|
2020-04-10 12:59:53 +03:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2021-02-27 22:03:51 +03:00
|
|
|
|
|
|
|
it('properly parses the list of orphan visits', () => {
|
|
|
|
expect(normalizeVisits(orphanVisits)).toEqual([
|
|
|
|
{
|
|
|
|
browser: 'Firefox',
|
|
|
|
os: 'macOS',
|
|
|
|
referer: 'google.com',
|
|
|
|
country: 'United States',
|
|
|
|
city: 'New York',
|
|
|
|
date: undefined,
|
|
|
|
latitude: 1029,
|
|
|
|
longitude: 6758,
|
|
|
|
type: 'base_url',
|
|
|
|
visitedUrl: 'foo',
|
2021-06-13 12:07:32 +03:00
|
|
|
potentialBot: false,
|
2021-02-27 22:03:51 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'regular_404',
|
|
|
|
visitedUrl: 'bar',
|
|
|
|
browser: 'Others',
|
|
|
|
city: 'Unknown',
|
|
|
|
country: 'Unknown',
|
|
|
|
date: undefined,
|
|
|
|
latitude: undefined,
|
|
|
|
longitude: undefined,
|
|
|
|
os: 'Others',
|
|
|
|
referer: 'Direct',
|
2021-06-13 12:07:32 +03:00
|
|
|
potentialBot: true,
|
2021-02-27 22:03:51 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
browser: 'Chrome',
|
|
|
|
os: 'Linux',
|
|
|
|
referer: 'm.facebook.com',
|
|
|
|
country: 'Spain',
|
|
|
|
city: 'Zaragoza',
|
|
|
|
date: undefined,
|
|
|
|
latitude: 123.45,
|
|
|
|
longitude: -543.21,
|
|
|
|
type: 'invalid_short_url',
|
2021-03-28 22:03:46 +03:00
|
|
|
visitedUrl: 'bar',
|
2021-06-13 12:07:32 +03:00
|
|
|
potentialBot: false,
|
2021-02-27 22:03:51 +03:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2020-04-10 12:59:53 +03:00
|
|
|
});
|
2018-08-25 00:38:37 +03:00
|
|
|
});
|