mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-10 13:37:01 +03:00
move utils to seperate files and folder, update imports
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import fetch from "node-fetch";
|
||||
import {getbaseURL, Done} from "../utils";
|
||||
import { Done} from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import assert from "assert";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fetch from "node-fetch";
|
||||
import {Done, getbaseURL} from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import assert from "assert";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fetch from "node-fetch";
|
||||
import {Done, getbaseURL} from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import assert from "assert";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fetch from "node-fetch";
|
||||
import {Done, getbaseURL} from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import {deepStrictEqual} from "assert";
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import fetch from "node-fetch";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {Done, getbaseURL} from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import assert from "assert";
|
||||
|
||||
describe("getSearchSegments", () => {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import fetch from "node-fetch";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {Done, getbaseURL, partialDeepEquals} from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||
import assert from "assert";
|
||||
|
||||
const ENOENTID = "0".repeat(64);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import fetch from "node-fetch";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {Done, getbaseURL, partialDeepEquals} from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||
import assert from "assert";
|
||||
|
||||
describe("getSkipSegments", () => {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import fetch from "node-fetch";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {Done, getbaseURL, partialDeepEquals, postJSON} from "../utils";
|
||||
import { Done, postJSON } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import {ImportMock,} from "ts-mock-imports";
|
||||
import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import assert from "assert";
|
||||
import fetch from "node-fetch";
|
||||
import {Done, getbaseURL} from "../utils";
|
||||
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {db} from "../../src/databases/databases";
|
||||
let dbVersion: number;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fetch from "node-fetch";
|
||||
import {Done, getbaseURL} from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import assert from "assert";
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import fetch from "node-fetch";
|
||||
import {Done, getbaseURL, partialDeepEquals} from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import assert from "assert";
|
||||
|
||||
126
test/cases/getUserStats.ts
Normal file
126
test/cases/getUserStats.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
import fetch from "node-fetch";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||
import { db } from "../../src/databases/databases";
|
||||
import { getHash } from "../../src/utils/getHash";
|
||||
import assert from "assert";
|
||||
|
||||
describe("getUserStats", () => {
|
||||
before(async () => {
|
||||
const insertUserNameQuery = 'INSERT INTO "userNames" ("userID", "userName") VALUES(?, ?)';
|
||||
await db.prepare("run", insertUserNameQuery, [getHash("getuserstats_user_01"), "Username user 01"]);
|
||||
|
||||
const sponsorTimesQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
||||
await db.prepare("run", sponsorTimesQuery, ["getuserstats1", 0, 60, 0, "getuserstatsuuid1", getHash("getuserstats_user_01"), 1, 1, "sponsor", 0]);
|
||||
await db.prepare("run", sponsorTimesQuery, ["getuserstats1", 0, 60, 0, "getuserstatsuuid2", getHash("getuserstats_user_01"), 2, 2, "selfpromo", 0]);
|
||||
await db.prepare("run", sponsorTimesQuery, ["getuserstats1", 0, 60, 0, "getuserstatsuuid3", getHash("getuserstats_user_01"), 3, 3, "interaction", 0]);
|
||||
await db.prepare("run", sponsorTimesQuery, ["getuserstats1", 0, 60, 0, "getuserstatsuuid4", getHash("getuserstats_user_01"), 4, 4, "intro", 0]);
|
||||
await db.prepare("run", sponsorTimesQuery, ["getuserstats1", 0, 60, 0, "getuserstatsuuid5", getHash("getuserstats_user_01"), 5, 5, "outro", 0]);
|
||||
await db.prepare("run", sponsorTimesQuery, ["getuserstats1", 0, 60, 0, "getuserstatsuuid6", getHash("getuserstats_user_01"), 6, 6, "preview", 0]);
|
||||
await db.prepare("run", sponsorTimesQuery, ["getuserstats1", 0, 60, 0, "getuserstatsuuid7", getHash("getuserstats_user_01"), 7, 7, "music_offtopic", 0]);
|
||||
await db.prepare("run", sponsorTimesQuery, ["getuserstats1", 11, 11, 0, "getuserstatsuuid8", getHash("getuserstats_user_01"), 8, 8, "poi_highlight", 0]);
|
||||
await db.prepare("run", sponsorTimesQuery, ["getuserstats1", 0, 60, -2, "getuserstatsuuid9", getHash("getuserstats_user_02"), 8, 2, "sponsor", 0]);
|
||||
|
||||
});
|
||||
|
||||
it("Should be able to get a 400 (No userID parameter)", (done: Done) => {
|
||||
fetch(`${getbaseURL()}/api/userStats`)
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 400);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get all user info", (done: Done) => {
|
||||
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_01&fetchCategoryStats=true&fetchActionTypeStats=true`)
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = {
|
||||
userName: "Username user 01",
|
||||
userID: getHash("getuserstats_user_01"),
|
||||
categoryCount: {
|
||||
sponsor: 1,
|
||||
selfpromo: 1,
|
||||
interaction: 1,
|
||||
intro: 1,
|
||||
outro: 1,
|
||||
preview: 1,
|
||||
music_offtopic: 1,
|
||||
poi_highlight: 1,
|
||||
},
|
||||
actionTypeCount: {
|
||||
mute: 0,
|
||||
skip: 8
|
||||
},
|
||||
overallStats: {
|
||||
minutesSaved: 28,
|
||||
segmentCount: 8
|
||||
}
|
||||
};
|
||||
const data = await res.json();
|
||||
assert.ok(partialDeepEquals(data, expected));
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get all zeroes for invalid userid", (done: Done) => {
|
||||
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_invalid`)
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const data = await res.json();
|
||||
for (const value in data.overallStats) {
|
||||
if (data[value]) {
|
||||
done(`returned non-zero for ${value}`);
|
||||
}
|
||||
}
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get all zeroes for only ignored segments", (done: Done) => {
|
||||
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_02`)
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const data = await res.json();
|
||||
for (const value in data.overallStats) {
|
||||
if (data[value]) {
|
||||
done(`returned non-zero for ${value}`);
|
||||
}
|
||||
}
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should not get extra stats if not requested", (done: Done) => {
|
||||
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_01`)
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const data = await res.json();
|
||||
// check for categoryCount
|
||||
if (data.categoryCount || data.actionTypeCount) {
|
||||
done("returned extra stats");
|
||||
}
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should get parts of extra stats if not requested", (done: Done) => {
|
||||
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_01&fetchActionTypeStats=true`)
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const data = await res.json();
|
||||
// check for categoryCount
|
||||
if (data.categoryCount && !data.actionTypeCount) {
|
||||
done("returned extra stats");
|
||||
}
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import fetch from "node-fetch";
|
||||
import {Done, getbaseURL, postJSON} from "../utils";
|
||||
import { Done, postJSON } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import assert from "assert";
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import fetch from "node-fetch";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {Done, getbaseURL, partialDeepEquals} from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||
import assert from "assert";
|
||||
|
||||
const endpoint = `${getbaseURL()}/api/getVideoSponsorTimes`;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import fetch from "node-fetch";
|
||||
import {Done, getbaseURL, partialDeepEquals} from "../utils";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import { partialDeepEquals } from "../utils/partialDeepEquals";import {db} from "../../src/databases/databases";
|
||||
import assert from "assert";
|
||||
|
||||
const videoID1 = "dQw4w9WgXcQ";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fetch from "node-fetch";
|
||||
import {Done, getbaseURL} from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import assert from "assert";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fetch from "node-fetch";
|
||||
import {Done, getbaseURL, postJSON} from "../utils";
|
||||
import {Done, postJSON} from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import {IDatabase} from "../../src/databases/IDatabase";
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import fetch from "node-fetch";
|
||||
import {config} from "../../src/config";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import {Done, getbaseURL, partialDeepEquals, postJSON} from "../utils";
|
||||
import { Done, postJSON } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {ImportMock} from "ts-mock-imports";
|
||||
import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import fetch from "node-fetch";
|
||||
import {Done, getbaseURL, partialDeepEquals, postJSON} from "../utils";
|
||||
import { Done, postJSON } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import assert from "assert";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fetch from "node-fetch";
|
||||
import {Done, getbaseURL, postJSON} from "../utils";
|
||||
import {Done, postJSON} from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import {IDatabase} from "../../src/databases/IDatabase";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fetch from "node-fetch";
|
||||
import { Done, getbaseURL } from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import { db, privateDB } from "../../src/databases/databases";
|
||||
import { getHash } from "../../src/utils/getHash";
|
||||
import assert from "assert";
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import fetch from "node-fetch";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {Done, getbaseURL} from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import assert from "assert";
|
||||
import { Category } from "../../src/types/segments.model";
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import assert from "assert";
|
||||
|
||||
import { partialDeepEquals } from "../utils";
|
||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||
|
||||
describe("Test utils ", () => {
|
||||
it("objectContain", async () => {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fetch from "node-fetch";
|
||||
import { getbaseURL, postJSON } from "../utils";
|
||||
import { postJSON } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import { getHash } from "../../src/utils/getHash";
|
||||
import { db } from "../../src/databases/databases";
|
||||
import assert from "assert";
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import fetch from "node-fetch";
|
||||
import {config} from "../../src/config";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {Done, getbaseURL} from "../utils";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import {ImportMock} from "ts-mock-imports";
|
||||
import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
|
||||
|
||||
5
test/utils/getBaseURL.ts
Normal file
5
test/utils/getBaseURL.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { config } from "../../src/config";
|
||||
|
||||
export function getbaseURL(): string {
|
||||
return `http://localhost:${config.port}`;
|
||||
}
|
||||
@@ -1,19 +1,10 @@
|
||||
import {config} from "../src/config";
|
||||
import { Logger } from "../src/utils/logger";
|
||||
import { Logger } from "../../src/utils/logger";
|
||||
|
||||
export function getbaseURL(): string {
|
||||
return `http://localhost:${config.port}`;
|
||||
function printActualExpected(actual: Record<string, any>, expected: Record<string, any>): void {
|
||||
Logger.error(`Actual: ${JSON.stringify(actual)}`);
|
||||
Logger.error(`Expected: ${JSON.stringify(expected)}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Duplicated from Mocha types. TypeScript doesn't infer that type by itself for some reason.
|
||||
*/
|
||||
export type Done = (err?: any) => void;
|
||||
|
||||
/**
|
||||
*
|
||||
* Check object contains expected properties
|
||||
*/
|
||||
export const partialDeepEquals = (actual: Record<string, any>, expected: Record<string, any>, print = true): boolean => {
|
||||
// loop over key, value of expected
|
||||
for (const [ key, value ] of Object.entries(expected)) {
|
||||
@@ -21,27 +12,13 @@ export const partialDeepEquals = (actual: Record<string, any>, expected: Record<
|
||||
if (Array.isArray(value) || typeof value === "object") {
|
||||
if (!partialDeepEquals(actual?.[key], value, false)) {
|
||||
if (print) printActualExpected(actual, expected);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (actual?.[key] !== value) {
|
||||
if (print) printActualExpected(actual, expected);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
function printActualExpected(actual: Record<string, any>, expected: Record<string, any>): void {
|
||||
Logger.error(`Actual: ${JSON.stringify(actual)}`);
|
||||
Logger.error(`Expected: ${JSON.stringify(expected)}`);
|
||||
}
|
||||
|
||||
export const postJSON = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
11
test/utils/utils.ts
Normal file
11
test/utils/utils.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Duplicated from Mocha types. TypeScript doesn't infer that type by itself for some reason.
|
||||
*/
|
||||
export type Done = (err?: any) => void;
|
||||
|
||||
export const postJSON = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user