mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-14 07:27:01 +03:00
everything up to purgeAllSegments
This commit is contained in:
@@ -1,40 +1,43 @@
|
||||
import assert from "assert";
|
||||
|
||||
import {strictEqual, ok} from "assert";
|
||||
import { db } from "../../src/databases/databases";
|
||||
import { getHash } from "../../src/utils/getHash";
|
||||
import { archiveDownvoteSegment } from "../../src/cronjob/downvoteSegmentArchiveJob";
|
||||
import { DBSegment } from "../../src/types/segments.model";
|
||||
|
||||
const oct2021 = new Date("October 1, 2021").getTime();
|
||||
const nov2021 = new Date("November 1, 2021").getTime();
|
||||
const dec2021 = new Date("December 17, 2021").getTime();
|
||||
const dec2022 = new Date("December 17, 2022").getTime();
|
||||
|
||||
const records = [
|
||||
["testtesttest", 1, 11, 2, 0, "1-uuid-0", "testman", new Date("December 17, 2021").getTime(), 50, "sponsor", "skip", "ytb", 100, 0, 0, getHash("testtesttest", 1)],
|
||||
["testtesttest2", 1, 11, 2, 0, "1-uuid-0-1", "testman", new Date("December 17, 2021").getTime(), 50, "sponsor", "skip", "ytb", 120, 0, 0, getHash("testtesttest2", 1)],
|
||||
["testtesttest", 12, 14, 2, 0, "1-uuid-0-2", "testman", new Date("December 17, 2021").getTime(), 50, "sponsor", "mute", "ytb", 100, 0, 0, getHash("testtesttest", 1)],
|
||||
["testtesttest", 20, 33, 2, 0, "1-uuid-2", "testman", new Date("December 17, 2021").getTime(), 50, "intro", "skip", "ytb", 101, 0, 0, getHash("testtesttest", 1)],
|
||||
["testtesttest,test", 1, 11, 2, 0, "1-uuid-1", "testman", new Date("December 17, 2021").getTime(), 50, "sponsor", "skip", "ytb", 140, 0, 0, getHash("testtesttest,test", 1)],
|
||||
["dsajVideo0", 0, 0, 2, 0, "dsaj00", "dsajUser", dec2021, 0, 0, 0],
|
||||
["dsajVideo0", 0, 0, 2, 0, "dsaj01", "dsajUser", dec2021, 0, 0, 0],
|
||||
["dsajVideo0", 0, 0, 2, 0, "dsaj02", "dsajUser", dec2021, 0, 0, 0],
|
||||
["dsajVideo0", 0, 0, 2, 0, "dsaj03", "dsajUser", dec2021, 0, 0, 0],
|
||||
["dsajVideo0", 0, 0, 2, 0, "dsaj04", "dsajUser", dec2021, 0, 0, 0,],
|
||||
|
||||
["test3", 1, 11, 2, 0, "1-uuid-4", "testman", new Date("December 17, 2021").getTime(), 50, "sponsor", "skip", "ytb", 200, 0, 0, getHash("test3", 1)],
|
||||
["test3", 7, 22, -3, 0, "1-uuid-5", "testman", new Date("December 17, 2021").getTime(), 50, "sponsor", "skip", "ytb", 300, 0, 0, getHash("test3", 1)],
|
||||
["dsajVideo1", 0, 0, 2, 0, "dsaj10", "dsajUser", dec2021, 0, 0, 0],
|
||||
["dsajVideo1", 0, 0, -3, 0, "dsaj11", "dsajUser", dec2021, 0, 0, 0],
|
||||
|
||||
["multiple", 1, 11, 2, 0, "1-uuid-6", "testman", new Date("December 17, 2021").getTime(), 50, "intro", "skip", "ytb", 400, 0, 0, getHash("multiple", 1)],
|
||||
["multiple", 20, 33, -4, 0, "1-uuid-7", "testman", new Date("October 1, 2021").getTime(), 50, "intro", "skip", "ytb", 500, 0, 0, getHash("multiple", 1)],
|
||||
["dsajVideo2", 0, 0, 2, 0, "dsaj20", "dsajUser", dec2021, 0, 0, 0],
|
||||
["dsajVideo2", 0, 0, -4, 0, "dsaj21", "dsajUser", oct2021, 0, 0, 0],
|
||||
|
||||
["locked", 20, 33, 2, 1, "1-uuid-locked-8", "testman", new Date("December 17, 2021").getTime(), 50, "intro", "skip", "ytb", 230, 0, 0, getHash("locked", 1)],
|
||||
["locked", 20, 34, 100000, 0, "1-uuid-9", "testman", new Date("December 17, 2021").getTime(), 50, "intro", "skip", "ytb", 190, 0, 0, getHash("locked", 1)],
|
||||
["dsajVideo3", 0, 0, 2, 1, "dsaj30", "dsajUser", dec2021, 0, 0, 0],
|
||||
["dsajVideo3", 0, 0, 100000, 0, "dsaj31", "dsajUser", dec2021, 0, 0, 0],
|
||||
|
||||
["onlyHiddenSegments", 20, 34, 100000, 0, "onlyHiddenSegments", "testman", new Date("December 17, 2021").getTime(), 50, "sponsor", "skip", "ytb", 190, 1, 0, getHash("onlyHiddenSegments", 1)],
|
||||
["dsajVideo4", 0, 0, 100000, 0, "dsaj40", "dsajUser", dec2021, 0, 1, 0],
|
||||
|
||||
["requiredSegmentVid-raw", 60, 70, 2, 0, "requiredSegmentVid-raw-1", "testman", new Date("December 17, 2021").getTime(), 50, "sponsor", "skip", "ytb", 0, 0, 0, getHash("requiredSegmentVid-raw", 1)],
|
||||
["requiredSegmentVid-raw", 60, 70, -1, 0, "requiredSegmentVid-raw-2", "testman", new Date("December 17, 2021").getTime(), 50, "sponsor", "skip", "ytb", 0, 0, 0, getHash("requiredSegmentVid-raw", 1)],
|
||||
["requiredSegmentVid-raw", 80, 90, -2, 0, "requiredSegmentVid-raw-3", "testman", new Date("November 17, 2021").getTime(), 50, "sponsor", "skip", "ytb", 0, 0, 0, getHash("requiredSegmentVid-raw", 1)],
|
||||
["requiredSegmentVid-raw", 80, 90, 2, 0, "requiredSegmentVid-raw-4", "testman", new Date("December 17, 2021").getTime(), 50, "sponsor", "skip", "ytb", 0, 0, 0, getHash("requiredSegmentVid-raw", 1)]
|
||||
["dsajVideo5", 0, 0, 2, 0, "dsaj50", "dsajUser", dec2021, 0, 0, 0],
|
||||
["dsajVideo5", 0, 0, -1, 0, "dsaj51", "dsajUser", dec2021, 0, 0, 0],
|
||||
["dsajVideo5", 0, 0, -2, 0, "dsaj52", "dsajUser", nov2021, 0, 0, 0],
|
||||
["dsajVideo5", 0, 0, 2, 0, "dsaj53", "dsajUser", dec2021, 0, 0, 0]
|
||||
];
|
||||
|
||||
describe("downvoteSegmentArchiveJob", () => {
|
||||
beforeEach(async () => {
|
||||
const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", views, category, "actionType", "service", "videoDuration", "hidden", "shadowHidden", "hashedVideoID") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
||||
const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "hidden", "shadowHidden") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
||||
|
||||
for (let i = 0; i < records.length; i += 1) {
|
||||
await db.prepare("run", query, records[i]);
|
||||
for (const record of records) {
|
||||
await db.prepare("run", query, record);
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -42,7 +45,7 @@ describe("downvoteSegmentArchiveJob", () => {
|
||||
|
||||
it("Should update the database version when starting the application", async () => {
|
||||
const version = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
|
||||
assert.ok(version >= 21, "version should be greater or equal to 21");
|
||||
ok(version >= 21, "version should be greater or equal to 21");
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
@@ -50,12 +53,10 @@ describe("downvoteSegmentArchiveJob", () => {
|
||||
await db.prepare("run", 'DELETE FROM "archivedSponsorTimes"');
|
||||
});
|
||||
|
||||
const getArchivedSegment = (): Promise<DBSegment[]> => {
|
||||
return db.prepare("all", 'SELECT * FROM "archivedSponsorTimes"');
|
||||
};
|
||||
const getArchivedSegment = (): Promise<DBSegment[]> => db.prepare("all", 'SELECT * FROM "archivedSponsorTimes"');
|
||||
|
||||
const getSegmentsInMainTable = (dayLimit: number, voteLimit: number, now: number): Promise<DBSegment[]> => {
|
||||
return db.prepare(
|
||||
const getSegmentsInMainTable = (dayLimit: number, voteLimit: number, now: number): Promise<DBSegment[]> =>
|
||||
db.prepare(
|
||||
"all",
|
||||
'SELECT * FROM "sponsorTimes" WHERE "votes" < ? AND (? - "timeSubmitted") > ?',
|
||||
[
|
||||
@@ -64,109 +65,107 @@ describe("downvoteSegmentArchiveJob", () => {
|
||||
dayLimit * 86400000,
|
||||
]
|
||||
);
|
||||
};
|
||||
|
||||
const countSegmentInMainTable = (): Promise<number> => {
|
||||
return db.prepare(
|
||||
const countSegmentInMainTable = (): Promise<number> =>
|
||||
db.prepare(
|
||||
"get",
|
||||
'SELECT COUNT(*) as count FROM "sponsorTimes"'
|
||||
).then(res => res.count);
|
||||
};
|
||||
|
||||
it("Should archive all records match", async () => {
|
||||
const dayLimit = 20;
|
||||
const voteLimit = 0;
|
||||
const time = new Date("December 17, 2022").getTime();
|
||||
const time = dec2022;
|
||||
const res = await archiveDownvoteSegment(dayLimit, voteLimit, time);
|
||||
assert.strictEqual(res, 0, "Expection in archiveDownvoteSegment");
|
||||
strictEqual(res, 0, "Expection in archiveDownvoteSegment");
|
||||
|
||||
// check segments in archived table
|
||||
const archivedSegment = await getArchivedSegment();
|
||||
assert.strictEqual(archivedSegment.length, 4, `Incorrect segment in archiveTable: ${archivedSegment.length} instead of 4`);
|
||||
strictEqual(archivedSegment.length, 4, `Incorrect segment in archiveTable: ${archivedSegment.length} instead of 4`);
|
||||
|
||||
// check segments not in main table
|
||||
const segments = await getSegmentsInMainTable(dayLimit, voteLimit, time);
|
||||
assert.strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
|
||||
strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
|
||||
|
||||
// check number segments remain in main table
|
||||
assert.strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
|
||||
strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
|
||||
});
|
||||
|
||||
it("Should archive records with vote < -1 match", async () => {
|
||||
const dayLimit = 20;
|
||||
const voteLimit = -1;
|
||||
const time = new Date("December 17, 2022").getTime();
|
||||
const time = dec2022;
|
||||
const res = await archiveDownvoteSegment(dayLimit, voteLimit, time);
|
||||
assert.strictEqual(res, 0, "");
|
||||
strictEqual(res, 0, "");
|
||||
|
||||
// check segments in archived table
|
||||
const archivedSegment = await getArchivedSegment();
|
||||
assert.strictEqual(archivedSegment.length, 3, `Incorrect segment in archiveTable: ${archivedSegment.length} instead of 3`);
|
||||
strictEqual(archivedSegment.length, 3, `Incorrect segment in archiveTable: ${archivedSegment.length} instead of 3`);
|
||||
|
||||
// check segments not in main table
|
||||
const segments = await getSegmentsInMainTable(dayLimit, voteLimit, time);
|
||||
assert.strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
|
||||
strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
|
||||
|
||||
// check number segments remain in main table
|
||||
assert.strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
|
||||
strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
|
||||
});
|
||||
|
||||
it("Should archive records with vote < -2 and day < 30 match", async () => {
|
||||
const dayLimit = 30;
|
||||
const voteLimit = -2;
|
||||
const time = new Date("December 17, 2021").getTime();
|
||||
const time = dec2021;
|
||||
const res = await archiveDownvoteSegment(dayLimit, voteLimit, time);
|
||||
assert.strictEqual(res, 0, "");
|
||||
strictEqual(res, 0, "");
|
||||
|
||||
// check segments in archived table
|
||||
const archivedSegment = await getArchivedSegment();
|
||||
assert.strictEqual(archivedSegment.length, 1, `Incorrect segment in archiveTable: ${archivedSegment.length} instead of 1`);
|
||||
strictEqual(archivedSegment.length, 1, `Incorrect segment in archiveTable: ${archivedSegment.length} instead of 1`);
|
||||
|
||||
assert.strictEqual(archivedSegment[0].votes, -4, `Incorrect segment vote in archiveTable: ${archivedSegment[0].votes} instead of -4`);
|
||||
strictEqual(archivedSegment[0].votes, -4, `Incorrect segment vote in archiveTable: ${archivedSegment[0].votes} instead of -4`);
|
||||
|
||||
// check segments not in main table
|
||||
const segments = await getSegmentsInMainTable(dayLimit, voteLimit, time);
|
||||
assert.strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
|
||||
strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
|
||||
|
||||
// check number segments remain in main table
|
||||
assert.strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
|
||||
strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
|
||||
});
|
||||
|
||||
it("Should archive records with vote < -2 and day < 300 match", async () => {
|
||||
const dayLimit = 300;
|
||||
const voteLimit = -2;
|
||||
const time = new Date("December 17, 2022").getTime();
|
||||
const time = dec2022;
|
||||
const res = await archiveDownvoteSegment(dayLimit, voteLimit, time);
|
||||
assert.strictEqual(res, 0, "");
|
||||
strictEqual(res, 0, "");
|
||||
|
||||
// check segments in archived table
|
||||
const archivedSegment = await getArchivedSegment();
|
||||
assert.strictEqual(archivedSegment.length, 2, `Incorrect segment in archiveTable: ${archivedSegment.length} instead of 2`);
|
||||
strictEqual(archivedSegment.length, 2, `Incorrect segment in archiveTable: ${archivedSegment.length} instead of 2`);
|
||||
|
||||
// check segments not in main table
|
||||
const segments = await getSegmentsInMainTable(dayLimit, voteLimit, time);
|
||||
assert.strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
|
||||
strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
|
||||
|
||||
// check number segments remain in main table
|
||||
assert.strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
|
||||
strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
|
||||
});
|
||||
|
||||
it("Should not archive any", async () => {
|
||||
const dayLimit = 300;
|
||||
const voteLimit = -2;
|
||||
const time = new Date("December 17, 2021").getTime();
|
||||
const time = dec2021;
|
||||
const res = await archiveDownvoteSegment(dayLimit, voteLimit, time);
|
||||
assert.strictEqual(res, 0, "");
|
||||
strictEqual(res, 0, "");
|
||||
|
||||
// check segments in archived table
|
||||
const archivedSegment = await getArchivedSegment();
|
||||
assert.strictEqual(archivedSegment.length, 0, `Incorrect segment in archiveTable: ${archivedSegment.length} instead of 0`);
|
||||
strictEqual(archivedSegment.length, 0, `Incorrect segment in archiveTable: ${archivedSegment.length} instead of 0`);
|
||||
|
||||
// check segments not in main table
|
||||
const segments = await getSegmentsInMainTable(dayLimit, voteLimit, time);
|
||||
assert.strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
|
||||
strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
|
||||
|
||||
// check number segments remain in main table
|
||||
assert.strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
|
||||
strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user