Add casual mode endpoint

This commit is contained in:
Ajay
2025-02-05 03:38:55 -05:00
parent ab9cab8ff5
commit 07435b9af1
13 changed files with 409 additions and 16 deletions

View File

@@ -3,7 +3,7 @@ import assert from "assert";
import { getHash } from "../../src/utils/getHash";
import { db } from "../../src/databases/databases";
import { Service } from "../../src/types/segments.model";
import { BrandingUUID, ThumbnailResult, TitleResult } from "../../src/types/branding.model";
import { BrandingUUID, CasualVote, ThumbnailResult, TitleResult } from "../../src/types/branding.model";
import { partialDeepEquals } from "../utils/partialDeepEquals";
describe("getBranding", () => {
@@ -14,6 +14,8 @@ describe("getBranding", () => {
const videoIDRandomTime = "videoID5";
const videoIDUnverified = "videoID6";
const videoIDvidDuration = "videoID7";
const videoIDCasual = "videoIDCasual";
const videoIDCasualDownvoted = "videoIDCasualDownvoted";
const videoID1Hash = getHash(videoID1, 1).slice(0, 4);
const videoID2LockedHash = getHash(videoID2Locked, 1).slice(0, 4);
@@ -22,6 +24,8 @@ describe("getBranding", () => {
const videoIDRandomTimeHash = getHash(videoIDRandomTime, 1).slice(0, 4);
const videoIDUnverifiedHash = getHash(videoIDUnverified, 1).slice(0, 4);
const videoIDvidDurationHash = getHash(videoIDUnverified, 1).slice(0, 4);
const videoIDCasualHash = getHash(videoIDCasual, 1).slice(0, 4);
const videoIDCasualDownvotedHash = getHash(videoIDCasualDownvoted, 1).slice(0, 4);
const endpoint = "/api/branding";
const getBranding = (params: Record<string, any>) => client({
@@ -43,6 +47,7 @@ describe("getBranding", () => {
const thumbnailTimestampsQuery = `INSERT INTO "thumbnailTimestamps" ("UUID", "timestamp") VALUES (?, ?)`;
const thumbnailVotesQuery = `INSERT INTO "thumbnailVotes" ("UUID", "votes", "locked", "shadowHidden", "downvotes", "removed") VALUES (?, ?, ?, ?, ?, ?)`;
const segmentQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden", "description", "hashedVideoID") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
const insertCasualVotesQuery = `INSERT INTO "casualVotes" ("UUID", "videoID", "service", "hashedVideoID", "category", "upvotes", "downvotes", "timeSubmitted") VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
await Promise.all([
db.prepare("run", titleQuery, [videoID1, "title1", 0, "userID1", Service.YouTube, videoID1Hash, 1, "UUID1"]),
@@ -143,6 +148,12 @@ describe("getBranding", () => {
db.prepare("run", segmentQuery, [videoIDvidDuration, 0, 6, 0, 0, "uuidvd6", "testman", 15, 0, "sponsor", "skip", "YouTube", 21.37, 0, 0, "", videoIDvidDurationHash]), // not the oldest visible
db.prepare("run", segmentQuery, [videoIDvidDuration, 0, 7, -2, 0, "uuidvd7", "testman", 16, 0, "sponsor", "skip", "YouTube", 21.38, 0, 0, "", videoIDvidDurationHash]), // downvoted, not the oldest
]);
await Promise.all([
db.prepare("run", insertCasualVotesQuery, ["postBrandCasual1", videoIDCasual, Service.YouTube, videoIDCasualHash, "clever", 1, 0, Date.now()]),
db.prepare("run", insertCasualVotesQuery, ["postBrandCasual2", videoIDCasualDownvoted, Service.YouTube, videoIDCasualDownvotedHash, "clever", 1, 1, Date.now()]),
db.prepare("run", insertCasualVotesQuery, ["postBrandCasual3", videoIDCasualDownvoted, Service.YouTube, videoIDCasualDownvotedHash, "other", 4, 1, Date.now()])
]);
});
it("should get top titles and thumbnails", async () => {
@@ -335,9 +346,28 @@ describe("getBranding", () => {
assert.strictEqual(result2.data[videoIDvidDuration].videoDuration, correctDuration);
});
it("should get casual votes", async () => {
await checkVideo(videoIDCasual, videoIDCasualHash, true, {
casualVotes: [{
id: "clever",
count: 1
}]
});
});
it("should not get casual votes with downvotes", async () => {
await checkVideo(videoIDCasualDownvoted, videoIDCasualDownvotedHash, true, {
casualVotes: [{
id: "other",
count: 3
}]
});
});
async function checkVideo(videoID: string, videoIDHash: string, fetchAll: boolean, expected: {
titles: TitleResult[],
thumbnails: ThumbnailResult[]
titles?: TitleResult[],
thumbnails?: ThumbnailResult[],
casualVotes?: CasualVote[]
}) {
const result1 = await getBranding({ videoID, fetchAll });
const result2 = await getBrandingByHash(videoIDHash, { fetchAll });

132
test/cases/postCasual.ts Normal file
View File

@@ -0,0 +1,132 @@
import { db } from "../../src/databases/databases";
import { client } from "../utils/httpClient";
import assert from "assert";
import { Service } from "../../src/types/segments.model";
describe("postCasual", () => {
const userID1 = `PostCasualUser1${".".repeat(16)}`;
const userID2 = `PostCasualUser2${".".repeat(16)}`;
const userID3 = `PostCasualUser3${".".repeat(16)}`;
const endpoint = "/api/casual";
const postCasual = (data: Record<string, any>) => client({
method: "POST",
url: endpoint,
data
});
const queryCasualVotesByVideo = (videoID: string, all = false) => db.prepare(all ? "all" : "get", `SELECT * FROM "casualVotes" WHERE "videoID" = ? ORDER BY "timeSubmitted" DESC`, [videoID]);
it("submit casual vote", async () => {
const videoID = "postCasual1";
const res = await postCasual({
category: "clever",
userID: userID1,
service: Service.YouTube,
videoID
});
assert.strictEqual(res.status, 200);
const dbVotes = await queryCasualVotesByVideo(videoID);
assert.strictEqual(dbVotes.category, "clever");
assert.strictEqual(dbVotes.upvotes, 1);
assert.strictEqual(dbVotes.downvotes, 0);
});
it("submit same casual vote again", async () => {
const videoID = "postCasual1";
const res = await postCasual({
category: "clever",
userID: userID1,
service: Service.YouTube,
videoID
});
assert.strictEqual(res.status, 200);
const dbVotes = await queryCasualVotesByVideo(videoID);
assert.strictEqual(dbVotes.category, "clever");
assert.strictEqual(dbVotes.upvotes, 1);
assert.strictEqual(dbVotes.downvotes, 0);
});
it("submit casual upvote", async () => {
const videoID = "postCasual1";
const res = await postCasual({
category: "clever",
userID: userID2,
service: Service.YouTube,
videoID
});
assert.strictEqual(res.status, 200);
const dbVotes = await queryCasualVotesByVideo(videoID);
assert.strictEqual(dbVotes.category, "clever");
assert.strictEqual(dbVotes.upvotes, 2);
assert.strictEqual(dbVotes.downvotes, 0);
});
it("submit casual downvote from same user", async () => {
const videoID = "postCasual1";
const res = await postCasual({
category: "clever",
downvote: true,
userID: userID1,
service: Service.YouTube,
videoID
});
assert.strictEqual(res.status, 200);
const dbVotes = await queryCasualVotesByVideo(videoID);
assert.strictEqual(dbVotes.category, "clever");
assert.strictEqual(dbVotes.upvotes, 1);
assert.strictEqual(dbVotes.downvotes, 1);
});
it("submit casual downvote from different user", async () => {
const videoID = "postCasual1";
const res = await postCasual({
category: "clever",
downvote: true,
userID: userID3,
service: Service.YouTube,
videoID
});
assert.strictEqual(res.status, 200);
const dbVotes = await queryCasualVotesByVideo(videoID);
assert.strictEqual(dbVotes.category, "clever");
assert.strictEqual(dbVotes.upvotes, 1);
assert.strictEqual(dbVotes.downvotes, 2);
});
it("submit casual upvote from same user", async () => {
const videoID = "postCasual1";
const res = await postCasual({
category: "clever",
downvote: false,
userID: userID3,
service: Service.YouTube,
videoID
});
assert.strictEqual(res.status, 200);
const dbVotes = await queryCasualVotesByVideo(videoID);
assert.strictEqual(dbVotes.category, "clever");
assert.strictEqual(dbVotes.upvotes, 2);
assert.strictEqual(dbVotes.downvotes, 1);
});
});