SponsorBlockServer/test/cases/segmentShift.ts

298 lines
10 KiB
TypeScript
Raw Normal View History

2021-01-06 01:43:28 +01:00
import fetch from 'node-fetch';
2020-10-17 20:56:54 +02:00
import {Done, getbaseURL} from '../utils';
import {db} from '../../src/databases/databases';
import {getHash} from '../../src/utils/getHash';
import {IDatabase} from '../../src/databases/IDatabase';
2021-03-07 06:21:56 +01:00
async function dbSponsorTimesAdd(db: IDatabase, videoID: string, startTime: number, endTime: number, UUID: string, category: string) {
2020-10-17 20:56:54 +02:00
const votes = 0,
userID = 0,
timeSubmitted = 0,
views = 0,
shadowHidden = 0,
hashedVideoID = `hash_${UUID}`;
2021-03-07 06:21:56 +01:00
await db.prepare("run", `INSERT INTO
2021-05-07 01:51:11 +02:00
"sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID",
"userID", "timeSubmitted", "views", "category", "shadowHidden", "hashedVideoID")
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID]);
2020-10-17 20:56:54 +02:00
}
2021-03-02 03:37:35 +01:00
async function dbSponsorTimesSetByUUID(db: IDatabase, UUID: string, startTime: number, endTime: number) {
2021-03-07 06:21:56 +01:00
await db.prepare('run', `UPDATE "sponsorTimes" SET "startTime" = ?, "endTime" = ? WHERE "UUID" = ?`, [startTime, endTime, UUID]);
2020-10-17 20:56:54 +02:00
}
2021-03-02 03:37:35 +01:00
async function dbSponsorTimesCompareExpect(db: IDatabase, expect: any) {
2020-10-17 20:56:54 +02:00
for (let i = 0, len = expect.length; i < len; i++) {
const expectSeg = expect[i];
2021-03-07 06:21:56 +01:00
let seg = await db.prepare('get', `SELECT "startTime", "endTime" FROM "sponsorTimes" WHERE "UUID" = ?`, [expectSeg.UUID]);
2020-10-17 20:56:54 +02:00
if ('removed' in expect) {
if (expect.removed === true && seg.votes === -2) {
return;
} else {
return `${expectSeg.UUID} doesnt got removed`;
}
}
if (seg.startTime !== expectSeg.startTime) {
return `${expectSeg.UUID} startTime is incorrect. seg.startTime is ${seg.startTime} expected ${expectSeg.startTime}`;
}
if (seg.endTime !== expectSeg.endTime) {
return `${expectSeg.UUID} endTime is incorrect. seg.endTime is ${seg.endTime} expected ${expectSeg.endTime}`;
}
}
return;
}
describe('segmentShift', function () {
const privateVipUserID = 'VIPUser-segmentShift';
const vipUserID = getHash(privateVipUserID);
const baseURL = getbaseURL();
2021-03-07 06:21:56 +01:00
before(async function () {
2020-10-17 20:56:54 +02:00
// startTime and endTime get set in beforeEach for consistency
2021-03-07 06:21:56 +01:00
await dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid01', 'intro');
await dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid02', 'sponsor');
await dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid03', 'interaction');
await dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid04', 'outro');
2021-05-07 01:51:11 +02:00
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [vipUserID]);
2020-10-17 20:56:54 +02:00
});
2021-06-22 00:59:56 +02:00
beforeEach(async function () {
2020-10-17 20:56:54 +02:00
// resetting startTime and endTime to reuse them
2021-06-22 00:59:56 +02:00
await dbSponsorTimesSetByUUID(db, 'vsegshifttest01uuid01', 0, 10);
await dbSponsorTimesSetByUUID(db, 'vsegshifttest01uuid02', 60, 90);
await dbSponsorTimesSetByUUID(db, 'vsegshifttest01uuid03', 40, 45);
await dbSponsorTimesSetByUUID(db, 'vsegshifttest01uuid04', 120, 140);
2020-10-17 20:56:54 +02:00
});
it('Reject none VIP user', function (done: Done) {
2021-01-06 01:43:28 +01:00
fetch(`${baseURL}/api/segmentShift`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
2020-10-17 20:56:54 +02:00
videoID: 'vsegshift01',
userID: 'segshift_randomuser001',
startTime: 20,
endTime: 30,
2021-01-06 01:43:28 +01:00
}),
})
2021-03-02 03:37:35 +01:00
.then(async res => {
2021-01-06 01:43:28 +01:00
done(res.status === 403 ? undefined : res.status);
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
it('Shift is outside segments', function (done: Done) {
2021-01-06 01:43:28 +01:00
fetch(`${baseURL}/api/segmentShift`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
2020-10-17 20:56:54 +02:00
videoID: 'vsegshift01',
userID: privateVipUserID,
startTime: 20,
endTime: 30,
2021-01-06 01:43:28 +01:00
}),
})
2021-03-02 03:37:35 +01:00
.then(async res => {
2021-01-06 01:43:28 +01:00
if (res.status !== 200) return done(`Status code was ${res.status}`);
2020-10-17 20:56:54 +02:00
const expect = [
{
UUID: 'vsegshifttest01uuid01',
startTime: 0,
endTime: 10,
},
{
UUID: 'vsegshifttest01uuid02',
startTime: 50,
endTime: 80,
},
{
UUID: 'vsegshifttest01uuid03',
startTime: 30,
endTime: 35,
},
{
UUID: 'vsegshifttest01uuid04',
startTime: 110,
endTime: 130,
},
];
2021-03-02 03:37:35 +01:00
done(await dbSponsorTimesCompareExpect(db, expect));
2021-01-06 01:43:28 +01:00
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
it('Shift is inside segment', function (done: Done) {
2021-01-06 01:43:28 +01:00
fetch(`${baseURL}/api/segmentShift`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
2020-10-17 20:56:54 +02:00
videoID: 'vsegshift01',
userID: privateVipUserID,
startTime: 65,
endTime: 75,
2021-01-06 01:43:28 +01:00
}),
})
2021-03-02 03:37:35 +01:00
.then(async res => {
2021-01-06 01:43:28 +01:00
if (res.status !== 200) return done(`Status code was ${res.status}`);
2020-10-17 20:56:54 +02:00
const expect = [
{
UUID: 'vsegshifttest01uuid01',
startTime: 0,
endTime: 10,
},
{
UUID: 'vsegshifttest01uuid02',
startTime: 60,
endTime: 80,
},
{
UUID: 'vsegshifttest01uuid03',
startTime: 40,
endTime: 45,
},
{
UUID: 'vsegshifttest01uuid04',
startTime: 110,
endTime: 130,
},
];
2021-03-02 03:37:35 +01:00
done(await dbSponsorTimesCompareExpect(db, expect));
2021-01-06 01:43:28 +01:00
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
it('Shift is overlaping startTime of segment', function (done: Done) {
2021-01-06 01:43:28 +01:00
fetch(`${baseURL}/api/segmentShift`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
2020-10-17 20:56:54 +02:00
videoID: 'vsegshift01',
userID: privateVipUserID,
startTime: 32,
endTime: 42,
2021-01-06 01:43:28 +01:00
}),
})
2021-03-02 03:37:35 +01:00
.then(async res => {
2021-01-06 01:43:28 +01:00
if (res.status !== 200) return done(`Status code was ${res.status}`);
2020-10-17 20:56:54 +02:00
const expect = [
{
UUID: 'vsegshifttest01uuid01',
startTime: 0,
endTime: 10,
},
{
UUID: 'vsegshifttest01uuid02',
startTime: 50,
endTime: 80,
},
{
UUID: 'vsegshifttest01uuid03',
startTime: 32,
endTime: 35,
},
{
UUID: 'vsegshifttest01uuid04',
startTime: 110,
endTime: 130,
},
];
2021-03-02 03:37:35 +01:00
done(await dbSponsorTimesCompareExpect(db, expect));
2021-01-06 01:43:28 +01:00
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
it('Shift is overlaping endTime of segment', function (done: Done) {
2021-01-06 01:43:28 +01:00
fetch(`${baseURL}/api/segmentShift`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
2020-10-17 20:56:54 +02:00
videoID: 'vsegshift01',
userID: privateVipUserID,
startTime: 85,
endTime: 95,
2021-01-06 01:43:28 +01:00
}),
})
2021-03-02 03:37:35 +01:00
.then(async res => {
2021-01-06 01:43:28 +01:00
if (res.status !== 200) return done(`Status code was ${res.status}`);
2020-10-17 20:56:54 +02:00
const expect = [
{
UUID: 'vsegshifttest01uuid01',
startTime: 0,
endTime: 10,
},
{
UUID: 'vsegshifttest01uuid02',
startTime: 60,
endTime: 85,
},
{
UUID: 'vsegshifttest01uuid03',
startTime: 40,
endTime: 45,
},
{
UUID: 'vsegshifttest01uuid04',
startTime: 110,
endTime: 130,
},
];
2021-03-02 03:37:35 +01:00
done(await dbSponsorTimesCompareExpect(db, expect));
2021-01-06 01:43:28 +01:00
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
it('Shift is overlaping segment', function (done: Done) {
2021-01-06 01:43:28 +01:00
fetch(`${baseURL}/api/segmentShift`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
2020-10-17 20:56:54 +02:00
videoID: 'vsegshift01',
userID: privateVipUserID,
startTime: 35,
endTime: 55,
2021-01-06 01:43:28 +01:00
}),
})
2021-03-02 03:37:35 +01:00
.then(async res => {
2021-01-06 01:43:28 +01:00
if (res.status !== 200) return done(`Status code was ${res.status}`);
2020-10-17 20:56:54 +02:00
const expect = [
{
UUID: 'vsegshifttest01uuid01',
startTime: 0,
endTime: 10,
},
{
UUID: 'vsegshifttest01uuid02',
startTime: 40,
endTime: 70,
},
{
UUID: 'vsegshifttest01uuid03',
startTime: 40,
endTime: 45,
removed: true,
},
{
UUID: 'vsegshifttest01uuid04',
startTime: 100,
endTime: 120,
},
];
2021-03-02 03:37:35 +01:00
done(await dbSponsorTimesCompareExpect(db, expect));
2021-01-06 01:43:28 +01:00
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
});