fix typings for getSubmissionUUID, update getStatus

This commit is contained in:
Michael C 2023-09-30 19:08:23 -04:00
parent d8b93dec00
commit 467443a03f
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
4 changed files with 79 additions and 51 deletions

View file

@ -1,14 +1,14 @@
import { getHash } from "./getHash";
import { HashedValue } from "../types/hash.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(
videoID: VideoID,
category: Category,
actionType: ActionType,
description: string,
userID: UserID,
userID: HashedUserID,
startTime: number,
endTime: number,
service: Service

View file

@ -3,7 +3,6 @@ import assert from "assert";
import { client } from "../utils/httpClient";
import { insertLock } from "../utils/queryGen";
import { multiGenRandomValue } from "../utils/getRandom";
import { partialDeepEquals } from "../utils/partialDeepEquals";
const endpoint = "/api/lockCategories";
const defaultActionTypes = ["skip", "mute"];

View file

@ -11,7 +11,7 @@ describe("getStatus", () => {
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)
.then(res => {
assert.strictEqual(res.status, 200);
@ -22,106 +22,86 @@ describe("getStatus", () => {
assert.ok(data.startTime);
assert.ok(data.processTime >= 0);
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`)
.then(res => {
assert.strictEqual(res.status, 200);
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`)
.then(res => {
assert.strictEqual(res.status, 200);
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`)
.then(res => {
assert.strictEqual(res.status, 200);
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`)
.then(res => {
assert.strictEqual(res.status, 200);
const now = Date.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`)
.then(res => {
assert.strictEqual(res.status, 200);
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`)
.then(res => {
assert.strictEqual(res.status, 200);
assert.ok(Number(res.data[0]) >= 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();
client.get(`${endpoint}/statusRequests`)
return client.get(`${endpoint}/statusRequests`)
.then(res => {
assert.strictEqual(res.status, 200);
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();
client.get(endpoint)
return client.get(endpoint)
.then(res => {
assert.strictEqual(res.status, 200);
const data = res.data;
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();
client.get(endpoint)
return client.get(endpoint)
.then(res => {
assert.strictEqual(res.status, 200);
const data = res.data;
assert.ok(data.redisProcessTime >= 0);
done();
})
.catch(err => done(err));
});
});
it("Should return commit unkown if not present", (done) => {

View file

@ -1,12 +1,61 @@
import { getSubmissionUUID } from "../../src/utils/getSubmissionUUID";
import assert from "assert";
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", () => {
it("Should return the hashed value", () => {
assert.strictEqual(
getSubmissionUUID("video001" as VideoID, "sponsor" as Category, "skip" as ActionType, "", "testuser001" as UserID, 13.33337, 42.000001, Service.YouTube),
"2a473bca993dd84d8c2f6a4785989b20948dfe0c12c00f6f143bbda9ed561dca7");
it("Should return the hashed value identical to manually generated value", () => {
const segment: segment = {
videoID: "video001" as VideoID,
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);
});
});