SponsorBlockServer/test/cases/segmentShift.ts

293 lines
10 KiB
TypeScript
Raw Normal View History

2021-07-12 08:43:46 +02:00
import fetch from "node-fetch";
import {Done, getbaseURL} from "../utils";
import {db} from "../../src/databases/databases";
import {getHash} from "../../src/utils/getHash";
import {IDatabase} from "../../src/databases/IDatabase";
import assert from "assert";
2020-10-17 20:56:54 +02:00
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(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
2021-07-12 08:43:46 +02:00
[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-07-12 08:43:46 +02:00
await db.prepare("run", `UPDATE "sponsorTimes" SET "startTime" = ?, "endTime" = ? WHERE "UUID" = ?`, [startTime, endTime, UUID]);
2020-10-17 20:56:54 +02:00
}
2021-07-07 09:47:08 +02:00
async function dbSponsorTimesCompareExpect(db: IDatabase, expect: any): Promise<void> {
2020-10-17 20:56:54 +02:00
for (let i = 0, len = expect.length; i < len; i++) {
const expectSeg = expect[i];
2021-07-12 08:43:46 +02:00
const seg = await db.prepare("get", `SELECT "startTime", "endTime" FROM "sponsorTimes" WHERE "UUID" = ?`, [expectSeg.UUID]);
if ("removed" in expect) {
2021-07-07 09:47:08 +02:00
assert.ok(expect.removed);
assert.strictEqual(seg.votes, -2);
assert.deepStrictEqual(seg, expectSeg);
assert.strictEqual(seg.startTime, expectSeg.startTime);
assert.strictEqual(seg.endTime, expectSeg.endTime);
2020-10-17 20:56:54 +02:00
}
}
}
2021-07-12 08:43:46 +02:00
describe("segmentShift", function () {
const privateVipUserID = "VIPUser-segmentShift";
2020-10-17 20:56:54 +02:00
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-07-12 08:43:46 +02: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-07-12 08:43:46 +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
});
2021-07-12 08:43:46 +02:00
it("Reject none VIP user", function (done: Done) {
2021-01-06 01:43:28 +01:00
fetch(`${baseURL}/api/segmentShift`, {
2021-07-12 08:43:46 +02:00
method: "POST",
2021-01-06 01:43:28 +01:00
headers: {
2021-07-12 08:43:46 +02:00
"Content-Type": "application/json",
2021-01-06 01:43:28 +01:00
},
body: JSON.stringify({
2021-07-12 08:43:46 +02:00
videoID: "vsegshift01",
userID: "segshift_randomuser001",
2020-10-17 20:56:54 +02:00
startTime: 20,
endTime: 30,
2021-01-06 01:43:28 +01:00
}),
})
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 403);
done();
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
2021-07-12 08:43:46 +02:00
it("Shift is outside segments", function (done: Done) {
2021-01-06 01:43:28 +01:00
fetch(`${baseURL}/api/segmentShift`, {
2021-07-12 08:43:46 +02:00
method: "POST",
2021-01-06 01:43:28 +01:00
headers: {
2021-07-12 08:43:46 +02:00
"Content-Type": "application/json",
2021-01-06 01:43:28 +01:00
},
body: JSON.stringify({
2021-07-12 08:43:46 +02:00
videoID: "vsegshift01",
2020-10-17 20:56:54 +02:00
userID: privateVipUserID,
startTime: 20,
endTime: 30,
2021-01-06 01:43:28 +01:00
}),
})
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
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,
},
];
done(await dbSponsorTimesCompareExpect(db, expect));
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
2021-07-12 08:43:46 +02:00
it("Shift is inside segment", function (done: Done) {
2021-01-06 01:43:28 +01:00
fetch(`${baseURL}/api/segmentShift`, {
2021-07-12 08:43:46 +02:00
method: "POST",
2021-01-06 01:43:28 +01:00
headers: {
2021-07-12 08:43:46 +02:00
"Content-Type": "application/json",
2021-01-06 01:43:28 +01:00
},
body: JSON.stringify({
2021-07-12 08:43:46 +02:00
videoID: "vsegshift01",
2020-10-17 20:56:54 +02:00
userID: privateVipUserID,
startTime: 65,
endTime: 75,
2021-01-06 01:43:28 +01:00
}),
})
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
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,
},
];
done(await dbSponsorTimesCompareExpect(db, expect));
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
2021-07-12 08:43:46 +02:00
it("Shift is overlaping startTime of segment", function (done: Done) {
2021-01-06 01:43:28 +01:00
fetch(`${baseURL}/api/segmentShift`, {
2021-07-12 08:43:46 +02:00
method: "POST",
2021-01-06 01:43:28 +01:00
headers: {
2021-07-12 08:43:46 +02:00
"Content-Type": "application/json",
2021-01-06 01:43:28 +01:00
},
body: JSON.stringify({
2021-07-12 08:43:46 +02:00
videoID: "vsegshift01",
2020-10-17 20:56:54 +02:00
userID: privateVipUserID,
startTime: 32,
endTime: 42,
2021-01-06 01:43:28 +01:00
}),
})
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
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,
},
];
done(await dbSponsorTimesCompareExpect(db, expect));
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
2021-07-12 08:43:46 +02:00
it("Shift is overlaping endTime of segment", function (done: Done) {
2021-01-06 01:43:28 +01:00
fetch(`${baseURL}/api/segmentShift`, {
2021-07-12 08:43:46 +02:00
method: "POST",
2021-01-06 01:43:28 +01:00
headers: {
2021-07-12 08:43:46 +02:00
"Content-Type": "application/json",
2021-01-06 01:43:28 +01:00
},
body: JSON.stringify({
2021-07-12 08:43:46 +02:00
videoID: "vsegshift01",
2020-10-17 20:56:54 +02:00
userID: privateVipUserID,
startTime: 85,
endTime: 95,
2021-01-06 01:43:28 +01:00
}),
})
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
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,
},
];
done(await dbSponsorTimesCompareExpect(db, expect));
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
2021-07-12 08:43:46 +02:00
it("Shift is overlaping segment", function (done: Done) {
2021-01-06 01:43:28 +01:00
fetch(`${baseURL}/api/segmentShift`, {
2021-07-12 08:43:46 +02:00
method: "POST",
2021-01-06 01:43:28 +01:00
headers: {
2021-07-12 08:43:46 +02:00
"Content-Type": "application/json",
2021-01-06 01:43:28 +01:00
},
body: JSON.stringify({
2021-07-12 08:43:46 +02:00
videoID: "vsegshift01",
2020-10-17 20:56:54 +02:00
userID: privateVipUserID,
startTime: 35,
endTime: 55,
2021-01-06 01:43:28 +01:00
}),
})
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
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,
},
];
done(await dbSponsorTimesCompareExpect(db, expect));
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
});