Skip to content

Commit 467443a

Browse files
committed
fix typings for getSubmissionUUID, update getStatus
1 parent d8b93de commit 467443a

File tree

4 files changed

+79
-51
lines changed

4 files changed

+79
-51
lines changed

src/utils/getSubmissionUUID.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { getHash } from "./getHash";
22
import { HashedValue } from "../types/hash.model";
33
import { ActionType, VideoID, Service, Category } from "../types/segments.model";
4-
import { UserID } from "../types/user.model";
4+
import { HashedUserID } from "../types/user.model";
55

66
export function getSubmissionUUID(
77
videoID: VideoID,
88
category: Category,
99
actionType: ActionType,
1010
description: string,
11-
userID: UserID,
11+
userID: HashedUserID,
1212
startTime: number,
1313
endTime: number,
1414
service: Service

test/cases/getLockCategories.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import assert from "assert";
33
import { client } from "../utils/httpClient";
44
import { insertLock } from "../utils/queryGen";
55
import { multiGenRandomValue } from "../utils/getRandom";
6-
import { partialDeepEquals } from "../utils/partialDeepEquals";
76

87
const endpoint = "/api/lockCategories";
98
const defaultActionTypes = ["skip", "mute"];

test/cases/getStatus.ts

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe("getStatus", () => {
1111
dbVersion = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
1212
});
1313

14-
it("Should be able to get status", (done) => {
14+
it("Should be able to get status", () =>
1515
client.get(endpoint)
1616
.then(res => {
1717
assert.strictEqual(res.status, 200);
@@ -22,106 +22,86 @@ describe("getStatus", () => {
2222
assert.ok(data.startTime);
2323
assert.ok(data.processTime >= 0);
2424
assert.ok(data.loadavg.length == 2);
25-
done();
2625
})
27-
.catch(err => done(err));
28-
});
26+
);
2927

30-
it("Should be able to get uptime only", (done) => {
28+
it("Should be able to get uptime only", () =>
3129
client.get(`${endpoint}/uptime`)
3230
.then(res => {
3331
assert.strictEqual(res.status, 200);
3432
assert.ok(Number(res.data) >= 1); // uptime should be greater than 1s
35-
done();
3633
})
37-
.catch(err => done(err));
38-
});
34+
);
3935

40-
it("Should be able to get commit only", (done) => {
36+
it("Should be able to get commit only", () =>
4137
client.get(`${endpoint}/commit`)
4238
.then(res => {
4339
assert.strictEqual(res.status, 200);
4440
assert.strictEqual(res.data, "test"); // commit should be test
45-
done();
4641
})
47-
.catch(err => done(err));
48-
});
42+
);
4943

50-
it("Should be able to get db only", (done) => {
44+
it("Should be able to get db only", () =>
5145
client.get(`${endpoint}/db`)
5246
.then(res => {
5347
assert.strictEqual(res.status, 200);
5448
assert.strictEqual(Number(res.data), Number(dbVersion)); // commit should be test
55-
done();
5649
})
57-
.catch(err => done(err));
58-
});
50+
);
5951

60-
it("Should be able to get startTime only", (done) => {
52+
it("Should be able to get startTime only", () =>
6153
client.get(`${endpoint}/startTime`)
6254
.then(res => {
6355
assert.strictEqual(res.status, 200);
6456
const now = Date.now();
6557
assert.ok(Number(res.data) <= now); // startTime should be more than now
66-
done();
6758
})
68-
.catch(err => done(err));
69-
});
59+
);
7060

71-
it("Should be able to get processTime only", (done) => {
61+
it("Should be able to get processTime only", () =>
7262
client.get(`${endpoint}/processTime`)
7363
.then(res => {
7464
assert.strictEqual(res.status, 200);
7565
assert.ok(Number(res.data) >= 0);
76-
done();
7766
})
78-
.catch(err => done(err));
79-
});
67+
);
8068

81-
it("Should be able to get loadavg only", (done) => {
69+
it("Should be able to get loadavg only", () =>
8270
client.get(`${endpoint}/loadavg`)
8371
.then(res => {
8472
assert.strictEqual(res.status, 200);
8573
assert.ok(Number(res.data[0]) >= 0);
8674
assert.ok(Number(res.data[1]) >= 0);
87-
done();
8875
})
89-
.catch(err => done(err));
90-
});
76+
);
9177

92-
it("Should be able to get statusRequests only", function (done) {
78+
it("Should be able to get statusRequests only", function () {
9379
if (!config.redis?.enabled) this.skip();
94-
client.get(`${endpoint}/statusRequests`)
80+
return client.get(`${endpoint}/statusRequests`)
9581
.then(res => {
9682
assert.strictEqual(res.status, 200);
9783
assert.ok(Number(res.data) > 1);
98-
done();
99-
})
100-
.catch(err => done(err));
84+
});
10185
});
10286

103-
it("Should be able to get status with statusRequests", function (done) {
87+
it("Should be able to get status with statusRequests", function () {
10488
if (!config.redis?.enabled) this.skip();
105-
client.get(endpoint)
89+
return client.get(endpoint)
10690
.then(res => {
10791
assert.strictEqual(res.status, 200);
10892
const data = res.data;
10993
assert.ok(data.statusRequests > 2);
110-
done();
111-
})
112-
.catch(err => done(err));
94+
});
11395
});
11496

115-
it("Should be able to get redis latency", function (done) {
97+
it("Should be able to get redis latency", function () {
11698
if (!config.redis?.enabled) this.skip();
117-
client.get(endpoint)
99+
return client.get(endpoint)
118100
.then(res => {
119101
assert.strictEqual(res.status, 200);
120102
const data = res.data;
121103
assert.ok(data.redisProcessTime >= 0);
122-
done();
123-
})
124-
.catch(err => done(err));
104+
});
125105
});
126106

127107
it("Should return commit unkown if not present", (done) => {

test/cases/getSubmissionUUID.ts

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,61 @@
11
import { getSubmissionUUID } from "../../src/utils/getSubmissionUUID";
22
import assert from "assert";
33
import { ActionType, VideoID, Service, Category } from "../../src/types/segments.model";
4-
import { UserID } from "../../src/types/user.model";
4+
import { HashedUserID } from "../../src/types/user.model";
5+
import { getHash } from "../../src/utils/getHash";
6+
import { HashedValue } from "../../src/types/hash.model";
7+
import { genAnonUser } from "../utils/genUser";
8+
import { genRandomValue } from "../utils/getRandom";
9+
10+
function testHash (segment: segment, version: number): HashedValue {
11+
const manualHash = getHash(Object.values(segment).join(""), 1) as HashedValue;
12+
const generatedHash = getSubmissionUUID(segment.videoID, segment.category, segment.actionType, segment.description, segment.userID, segment.startTime, segment.endTime, segment.service);
13+
assert.strictEqual(version, Number(generatedHash.at(-1)), "version should match passed in version");
14+
assert.strictEqual(`${manualHash}${version}`, generatedHash);
15+
return generatedHash;
16+
}
17+
18+
interface segment {
19+
videoID: VideoID,
20+
startTime: number,
21+
endTime: number,
22+
userID: HashedUserID,
23+
description: string,
24+
category: Category,
25+
actionType: ActionType,
26+
service: Service
27+
}
28+
29+
const version = 7;
530

631
describe("getSubmissionUUID", () => {
7-
it("Should return the hashed value", () => {
8-
assert.strictEqual(
9-
getSubmissionUUID("video001" as VideoID, "sponsor" as Category, "skip" as ActionType, "", "testuser001" as UserID, 13.33337, 42.000001, Service.YouTube),
10-
"2a473bca993dd84d8c2f6a4785989b20948dfe0c12c00f6f143bbda9ed561dca7");
32+
it("Should return the hashed value identical to manually generated value", () => {
33+
const segment: segment = {
34+
videoID: "video001" as VideoID,
35+
startTime: 13.33337,
36+
endTime: 42.000001,
37+
userID: "testuser001" as HashedUserID,
38+
description: "",
39+
category: "sponsor" as Category,
40+
actionType: "skip" as ActionType,
41+
service: Service.YouTube
42+
};
43+
const testedHash = testHash(segment, version);
44+
// test against baked hash
45+
assert.strictEqual(testedHash, "2a473bca993dd84d8c2f6a4785989b20948dfe0c12c00f6f143bbda9ed561dca7");
46+
});
47+
it ("Should return identical hash for randomly generated values", () => {
48+
const user = genAnonUser();
49+
const segment: segment = {
50+
videoID: genRandomValue("video", "getUUID") as VideoID,
51+
startTime: Math.random()*1000,
52+
endTime: Math.random()*500,
53+
userID: user.pubID,
54+
description: genRandomValue("description", "getUUID"),
55+
category: "sponsor" as Category,
56+
actionType: "skip" as ActionType,
57+
service: Service.YouTube
58+
};
59+
testHash(segment, version);
1160
});
1261
});

0 commit comments

Comments
 (0)