SponsorBlockServer/test/cases/downvoteSegmentArchiveJob.ts

172 lines
7.9 KiB
TypeScript
Raw Normal View History

2021-09-23 00:52:35 +02:00
import { strictEqual, ok } from "assert";
2021-07-12 08:43:46 +02:00
import { db } from "../../src/databases/databases";
import { archiveDownvoteSegment } from "../../src/cronjob/downvoteSegmentArchiveJob";
import { DBSegment } from "../../src/types/segments.model";
2021-09-05 01:23:43 +02:00
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 = [
2021-09-05 01:23:43 +02:00
["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,],
2021-09-05 01:23:43 +02:00
["dsajVideo1", 0, 0, 2, 0, "dsaj10", "dsajUser", dec2021, 0, 0, 0],
["dsajVideo1", 0, 0, -3, 0, "dsaj11", "dsajUser", dec2021, 0, 0, 0],
2021-09-05 01:23:43 +02:00
["dsajVideo2", 0, 0, 2, 0, "dsaj20", "dsajUser", dec2021, 0, 0, 0],
["dsajVideo2", 0, 0, -4, 0, "dsaj21", "dsajUser", oct2021, 0, 0, 0],
2021-09-05 01:23:43 +02:00
["dsajVideo3", 0, 0, 2, 1, "dsaj30", "dsajUser", dec2021, 0, 0, 0],
["dsajVideo3", 0, 0, 100000, 0, "dsaj31", "dsajUser", dec2021, 0, 0, 0],
2021-09-05 01:23:43 +02:00
["dsajVideo4", 0, 0, 100000, 0, "dsaj40", "dsajUser", dec2021, 0, 1, 0],
2021-09-05 01:23:43 +02:00
["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]
];
2021-07-12 08:43:46 +02:00
describe("downvoteSegmentArchiveJob", () => {
beforeEach(async () => {
2021-09-05 01:23:43 +02:00
const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "hidden", "shadowHidden") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
2021-09-05 01:23:43 +02:00
for (const record of records) {
await db.prepare("run", query, record);
}
return;
});
2021-07-12 08:43:46 +02:00
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;
2021-09-05 01:23:43 +02:00
ok(version >= 21, "version should be greater or equal to 21");
2021-07-10 06:26:18 +02:00
});
afterEach(async () => {
2021-07-12 08:43:46 +02:00
await db.prepare("run", 'DELETE FROM "sponsorTimes"');
await db.prepare("run", 'DELETE FROM "archivedSponsorTimes"');
});
2021-09-05 01:23:43 +02:00
const getArchivedSegment = (): Promise<DBSegment[]> => db.prepare("all", 'SELECT * FROM "archivedSponsorTimes"');
2021-09-05 01:23:43 +02:00
const getSegmentsInMainTable = (dayLimit: number, voteLimit: number, now: number): Promise<DBSegment[]> =>
db.prepare(
2021-07-12 08:43:46 +02:00
"all",
2021-07-10 06:26:18 +02:00
'SELECT * FROM "sponsorTimes" WHERE "votes" < ? AND (? - "timeSubmitted") > ?',
[
voteLimit,
now,
dayLimit * 86400000,
]
);
2021-09-05 01:23:43 +02:00
const countSegmentInMainTable = (): Promise<number> =>
db.prepare(
2021-07-12 08:43:46 +02:00
"get",
'SELECT COUNT(*) as count FROM "sponsorTimes"'
).then(res => res.count);
2021-07-12 08:43:46 +02:00
it("Should archive all records match", async () => {
const dayLimit = 20;
const voteLimit = 0;
2021-09-05 01:23:43 +02:00
const time = dec2022;
const res = await archiveDownvoteSegment(dayLimit, voteLimit, time);
2021-09-05 01:23:43 +02:00
strictEqual(res, 0, "Expection in archiveDownvoteSegment");
// check segments in archived table
const archivedSegment = await getArchivedSegment();
2021-09-05 01:23:43 +02:00
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);
2021-09-05 01:23:43 +02:00
strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
// check number segments remain in main table
2021-09-05 01:23:43 +02:00
strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
});
2021-07-12 08:43:46 +02:00
it("Should archive records with vote < -1 match", async () => {
const dayLimit = 20;
const voteLimit = -1;
2021-09-05 01:23:43 +02:00
const time = dec2022;
const res = await archiveDownvoteSegment(dayLimit, voteLimit, time);
2021-09-05 01:23:43 +02:00
strictEqual(res, 0, "");
// check segments in archived table
const archivedSegment = await getArchivedSegment();
2021-09-05 01:23:43 +02:00
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);
2021-09-05 01:23:43 +02:00
strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
// check number segments remain in main table
2021-09-05 01:23:43 +02:00
strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
});
2021-07-12 08:43:46 +02:00
it("Should archive records with vote < -2 and day < 30 match", async () => {
const dayLimit = 30;
const voteLimit = -2;
2021-09-05 01:23:43 +02:00
const time = dec2021;
const res = await archiveDownvoteSegment(dayLimit, voteLimit, time);
2021-09-05 01:23:43 +02:00
strictEqual(res, 0, "");
// check segments in archived table
const archivedSegment = await getArchivedSegment();
2021-09-05 01:23:43 +02:00
strictEqual(archivedSegment.length, 1, `Incorrect segment in archiveTable: ${archivedSegment.length} instead of 1`);
2021-09-05 01:23:43 +02:00
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);
2021-09-05 01:23:43 +02:00
strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
// check number segments remain in main table
2021-09-05 01:23:43 +02:00
strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
});
2021-07-12 08:43:46 +02:00
it("Should archive records with vote < -2 and day < 300 match", async () => {
const dayLimit = 300;
const voteLimit = -2;
2021-09-05 01:23:43 +02:00
const time = dec2022;
const res = await archiveDownvoteSegment(dayLimit, voteLimit, time);
2021-09-05 01:23:43 +02:00
strictEqual(res, 0, "");
// check segments in archived table
const archivedSegment = await getArchivedSegment();
2021-09-05 01:23:43 +02:00
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);
2021-09-05 01:23:43 +02:00
strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
// check number segments remain in main table
2021-09-05 01:23:43 +02:00
strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
});
2021-07-12 08:43:46 +02:00
it("Should not archive any", async () => {
const dayLimit = 300;
const voteLimit = -2;
2021-09-05 01:23:43 +02:00
const time = dec2021;
const res = await archiveDownvoteSegment(dayLimit, voteLimit, time);
2021-09-05 01:23:43 +02:00
strictEqual(res, 0, "");
// check segments in archived table
const archivedSegment = await getArchivedSegment();
2021-09-05 01:23:43 +02:00
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);
2021-09-05 01:23:43 +02:00
strictEqual(segments.length, 0, `Incorrect segment in main table: ${segments.length} instead of 0`);
// check number segments remain in main table
2021-09-05 01:23:43 +02:00
strictEqual(await countSegmentInMainTable(), records.length - archivedSegment.length ,"Incorrect segment remain in main table");
});
});