SponsorBlockServer/test/cases/getSavedTimeForUser.ts

55 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-09-23 00:52:35 +02:00
import { db } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash";
import { deepStrictEqual } from "assert";
2021-09-22 23:50:06 +02:00
import { client } from "../utils/httpClient";
2022-09-21 05:22:21 +02:00
import assert from "assert";
// helpers
2021-09-22 23:50:06 +02:00
const endpoint = "/api/getSavedTimeForUser";
2022-09-21 05:22:21 +02:00
const getSavedTimeForUser = (userID: string) => client({
url: endpoint,
params: { userID }
});
2020-10-17 20:56:54 +02:00
2021-07-12 08:43:46 +02:00
describe("getSavedTimeForUser", () => {
2022-09-21 05:22:21 +02:00
const user1 = "getSavedTimeForUser1";
const user2 = "getSavedTimeforUser2";
const [ start, end, views ] = [1, 11, 50];
2021-03-02 03:37:35 +01:00
before(async () => {
2021-09-05 01:23:43 +02:00
const startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", "views", "shadowHidden") VALUES';
await db.prepare("run", `${startOfQuery}(?, ?, ?, ?, ?, ?, ?, ?, ?)`,
2022-09-21 05:22:21 +02:00
["getSavedTimeForUser", start, end, 2, "getSavedTimeUUID0", getHash(user1), 0, views, 0]);
2021-03-02 03:37:35 +01:00
return;
2020-10-17 20:56:54 +02:00
});
2022-09-21 05:22:21 +02:00
it("Should be able to get a saved time", (done) => {
getSavedTimeForUser(user1)
2021-09-22 23:50:06 +02:00
.then(res => {
2021-07-12 08:43:46 +02:00
// (end-start)*minute * views
2022-09-21 05:22:21 +02:00
const savedMinutes = ((end-start)/60) * views;
2021-07-12 08:43:46 +02:00
const expected = {
timeSaved: savedMinutes
};
2021-09-22 23:50:06 +02:00
deepStrictEqual(res.data, expected);
2021-07-12 08:43:46 +02:00
done();
})
.catch((err) => done(err));
2020-10-17 20:56:54 +02:00
});
2022-09-21 05:22:21 +02:00
it("Should return 404 if no submissions", (done) => {
getSavedTimeForUser(user2)
.then(res => {
assert.strictEqual(res.status, 404);
done();
})
.catch((err) => done(err));
});
it("Should return 400 if no userID", (done) => {
client({ url: endpoint })
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch((err) => done(err));
});
2020-10-17 20:56:54 +02:00
});