mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-06 19:47:00 +03:00
fix typings for getSubmissionUUID, update getStatus
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
import { getHash } from "./getHash";
|
import { getHash } from "./getHash";
|
||||||
import { HashedValue } from "../types/hash.model";
|
import { HashedValue } from "../types/hash.model";
|
||||||
import { ActionType, VideoID, Service, Category } from "../types/segments.model";
|
import { ActionType, VideoID, Service, Category } from "../types/segments.model";
|
||||||
import { UserID } from "../types/user.model";
|
import { HashedUserID } from "../types/user.model";
|
||||||
|
|
||||||
export function getSubmissionUUID(
|
export function getSubmissionUUID(
|
||||||
videoID: VideoID,
|
videoID: VideoID,
|
||||||
category: Category,
|
category: Category,
|
||||||
actionType: ActionType,
|
actionType: ActionType,
|
||||||
description: string,
|
description: string,
|
||||||
userID: UserID,
|
userID: HashedUserID,
|
||||||
startTime: number,
|
startTime: number,
|
||||||
endTime: number,
|
endTime: number,
|
||||||
service: Service
|
service: Service
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import assert from "assert";
|
|||||||
import { client } from "../utils/httpClient";
|
import { client } from "../utils/httpClient";
|
||||||
import { insertLock } from "../utils/queryGen";
|
import { insertLock } from "../utils/queryGen";
|
||||||
import { multiGenRandomValue } from "../utils/getRandom";
|
import { multiGenRandomValue } from "../utils/getRandom";
|
||||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
|
||||||
|
|
||||||
const endpoint = "/api/lockCategories";
|
const endpoint = "/api/lockCategories";
|
||||||
const defaultActionTypes = ["skip", "mute"];
|
const defaultActionTypes = ["skip", "mute"];
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ describe("getStatus", () => {
|
|||||||
dbVersion = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
|
dbVersion = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to get status", (done) => {
|
it("Should be able to get status", () =>
|
||||||
client.get(endpoint)
|
client.get(endpoint)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
@@ -22,106 +22,86 @@ describe("getStatus", () => {
|
|||||||
assert.ok(data.startTime);
|
assert.ok(data.startTime);
|
||||||
assert.ok(data.processTime >= 0);
|
assert.ok(data.processTime >= 0);
|
||||||
assert.ok(data.loadavg.length == 2);
|
assert.ok(data.loadavg.length == 2);
|
||||||
done();
|
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
);
|
||||||
});
|
|
||||||
|
|
||||||
it("Should be able to get uptime only", (done) => {
|
it("Should be able to get uptime only", () =>
|
||||||
client.get(`${endpoint}/uptime`)
|
client.get(`${endpoint}/uptime`)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
assert.ok(Number(res.data) >= 1); // uptime should be greater than 1s
|
assert.ok(Number(res.data) >= 1); // uptime should be greater than 1s
|
||||||
done();
|
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
);
|
||||||
});
|
|
||||||
|
|
||||||
it("Should be able to get commit only", (done) => {
|
it("Should be able to get commit only", () =>
|
||||||
client.get(`${endpoint}/commit`)
|
client.get(`${endpoint}/commit`)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
assert.strictEqual(res.data, "test"); // commit should be test
|
assert.strictEqual(res.data, "test"); // commit should be test
|
||||||
done();
|
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
);
|
||||||
});
|
|
||||||
|
|
||||||
it("Should be able to get db only", (done) => {
|
it("Should be able to get db only", () =>
|
||||||
client.get(`${endpoint}/db`)
|
client.get(`${endpoint}/db`)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
assert.strictEqual(Number(res.data), Number(dbVersion)); // commit should be test
|
assert.strictEqual(Number(res.data), Number(dbVersion)); // commit should be test
|
||||||
done();
|
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
);
|
||||||
});
|
|
||||||
|
|
||||||
it("Should be able to get startTime only", (done) => {
|
it("Should be able to get startTime only", () =>
|
||||||
client.get(`${endpoint}/startTime`)
|
client.get(`${endpoint}/startTime`)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
assert.ok(Number(res.data) <= now); // startTime should be more than now
|
assert.ok(Number(res.data) <= now); // startTime should be more than now
|
||||||
done();
|
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
);
|
||||||
});
|
|
||||||
|
|
||||||
it("Should be able to get processTime only", (done) => {
|
it("Should be able to get processTime only", () =>
|
||||||
client.get(`${endpoint}/processTime`)
|
client.get(`${endpoint}/processTime`)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
assert.ok(Number(res.data) >= 0);
|
assert.ok(Number(res.data) >= 0);
|
||||||
done();
|
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
);
|
||||||
});
|
|
||||||
|
|
||||||
it("Should be able to get loadavg only", (done) => {
|
it("Should be able to get loadavg only", () =>
|
||||||
client.get(`${endpoint}/loadavg`)
|
client.get(`${endpoint}/loadavg`)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
assert.ok(Number(res.data[0]) >= 0);
|
assert.ok(Number(res.data[0]) >= 0);
|
||||||
assert.ok(Number(res.data[1]) >= 0);
|
assert.ok(Number(res.data[1]) >= 0);
|
||||||
done();
|
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
);
|
||||||
});
|
|
||||||
|
|
||||||
it("Should be able to get statusRequests only", function (done) {
|
it("Should be able to get statusRequests only", function () {
|
||||||
if (!config.redis?.enabled) this.skip();
|
if (!config.redis?.enabled) this.skip();
|
||||||
client.get(`${endpoint}/statusRequests`)
|
return client.get(`${endpoint}/statusRequests`)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
assert.ok(Number(res.data) > 1);
|
assert.ok(Number(res.data) > 1);
|
||||||
done();
|
});
|
||||||
})
|
|
||||||
.catch(err => done(err));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to get status with statusRequests", function (done) {
|
it("Should be able to get status with statusRequests", function () {
|
||||||
if (!config.redis?.enabled) this.skip();
|
if (!config.redis?.enabled) this.skip();
|
||||||
client.get(endpoint)
|
return client.get(endpoint)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const data = res.data;
|
const data = res.data;
|
||||||
assert.ok(data.statusRequests > 2);
|
assert.ok(data.statusRequests > 2);
|
||||||
done();
|
});
|
||||||
})
|
|
||||||
.catch(err => done(err));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to get redis latency", function (done) {
|
it("Should be able to get redis latency", function () {
|
||||||
if (!config.redis?.enabled) this.skip();
|
if (!config.redis?.enabled) this.skip();
|
||||||
client.get(endpoint)
|
return client.get(endpoint)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const data = res.data;
|
const data = res.data;
|
||||||
assert.ok(data.redisProcessTime >= 0);
|
assert.ok(data.redisProcessTime >= 0);
|
||||||
done();
|
});
|
||||||
})
|
|
||||||
.catch(err => done(err));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should return commit unkown if not present", (done) => {
|
it("Should return commit unkown if not present", (done) => {
|
||||||
|
|||||||
@@ -1,12 +1,61 @@
|
|||||||
import { getSubmissionUUID } from "../../src/utils/getSubmissionUUID";
|
import { getSubmissionUUID } from "../../src/utils/getSubmissionUUID";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { ActionType, VideoID, Service, Category } from "../../src/types/segments.model";
|
import { ActionType, VideoID, Service, Category } from "../../src/types/segments.model";
|
||||||
import { UserID } from "../../src/types/user.model";
|
import { HashedUserID } from "../../src/types/user.model";
|
||||||
|
import { getHash } from "../../src/utils/getHash";
|
||||||
|
import { HashedValue } from "../../src/types/hash.model";
|
||||||
|
import { genAnonUser } from "../utils/genUser";
|
||||||
|
import { genRandomValue } from "../utils/getRandom";
|
||||||
|
|
||||||
|
function testHash (segment: segment, version: number): HashedValue {
|
||||||
|
const manualHash = getHash(Object.values(segment).join(""), 1) as HashedValue;
|
||||||
|
const generatedHash = getSubmissionUUID(segment.videoID, segment.category, segment.actionType, segment.description, segment.userID, segment.startTime, segment.endTime, segment.service);
|
||||||
|
assert.strictEqual(version, Number(generatedHash.at(-1)), "version should match passed in version");
|
||||||
|
assert.strictEqual(`${manualHash}${version}`, generatedHash);
|
||||||
|
return generatedHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface segment {
|
||||||
|
videoID: VideoID,
|
||||||
|
startTime: number,
|
||||||
|
endTime: number,
|
||||||
|
userID: HashedUserID,
|
||||||
|
description: string,
|
||||||
|
category: Category,
|
||||||
|
actionType: ActionType,
|
||||||
|
service: Service
|
||||||
|
}
|
||||||
|
|
||||||
|
const version = 7;
|
||||||
|
|
||||||
describe("getSubmissionUUID", () => {
|
describe("getSubmissionUUID", () => {
|
||||||
it("Should return the hashed value", () => {
|
it("Should return the hashed value identical to manually generated value", () => {
|
||||||
assert.strictEqual(
|
const segment: segment = {
|
||||||
getSubmissionUUID("video001" as VideoID, "sponsor" as Category, "skip" as ActionType, "", "testuser001" as UserID, 13.33337, 42.000001, Service.YouTube),
|
videoID: "video001" as VideoID,
|
||||||
"2a473bca993dd84d8c2f6a4785989b20948dfe0c12c00f6f143bbda9ed561dca7");
|
startTime: 13.33337,
|
||||||
|
endTime: 42.000001,
|
||||||
|
userID: "testuser001" as HashedUserID,
|
||||||
|
description: "",
|
||||||
|
category: "sponsor" as Category,
|
||||||
|
actionType: "skip" as ActionType,
|
||||||
|
service: Service.YouTube
|
||||||
|
};
|
||||||
|
const testedHash = testHash(segment, version);
|
||||||
|
// test against baked hash
|
||||||
|
assert.strictEqual(testedHash, "2a473bca993dd84d8c2f6a4785989b20948dfe0c12c00f6f143bbda9ed561dca7");
|
||||||
|
});
|
||||||
|
it ("Should return identical hash for randomly generated values", () => {
|
||||||
|
const user = genAnonUser();
|
||||||
|
const segment: segment = {
|
||||||
|
videoID: genRandomValue("video", "getUUID") as VideoID,
|
||||||
|
startTime: Math.random()*1000,
|
||||||
|
endTime: Math.random()*500,
|
||||||
|
userID: user.pubID,
|
||||||
|
description: genRandomValue("description", "getUUID"),
|
||||||
|
category: "sponsor" as Category,
|
||||||
|
actionType: "skip" as ActionType,
|
||||||
|
service: Service.YouTube
|
||||||
|
};
|
||||||
|
testHash(segment, version);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user