Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test helpers 2 electric boogaloo #562

Draft
wants to merge 32 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ecbf809
postPurgeAllSegments, setUsername
mchangrh Oct 16, 2023
db28400
use more test helpers
mchangrh Oct 16, 2023
430ac8c
use gha native services
mchangrh Oct 16, 2023
b7f9423
add setUsername ban test
mchangrh Oct 16, 2023
7c4838c
half-broken getLockCategoriesByHash
mchangrh Oct 16, 2023
625dc43
replace done()
mchangrh Oct 17, 2023
afb39ba
fix missing HashedVideoID
mchangrh Oct 17, 2023
355c154
fix typos
mchangrh Oct 17, 2023
6a08de7
userAgentTest from array
mchangrh Oct 17, 2023
5ec0018
use queryGen, userGen
mchangrh Oct 17, 2023
7a76628
refactor getRandom to genRandom
mchangrh Oct 17, 2023
f7e0980
getSegmetInfo tests
mchangrh Oct 17, 2023
9238cc5
merge 404 clauses in getSegmentInfo
mchangrh Oct 17, 2023
db94918
simplify getSegmentInfo endpoint with parseParams
mchangrh Oct 17, 2023
4e51293
use ES5 proxies instead of arrays
mchangrh Oct 17, 2023
fe3f742
rewrite tests for voteOnSponsorTime
mchangrh Oct 17, 2023
ae4e670
add undovote tests
mchangrh Oct 17, 2023
c4161a7
add category vote tests
mchangrh Oct 17, 2023
f729585
use variables, 1 vote for categories
mchangrh Oct 17, 2023
aa90b6a
less votes for category re-flipping
mchangrh Oct 24, 2023
b29dea2
convert more functions
mchangrh Oct 24, 2023
bc191ea
add getUserInfoFree
mchangrh Nov 3, 2023
c664d83
update getSearchSegment, fix parseParams
mchangrh Nov 4, 2023
d7a4b67
update tests
mchangrh Nov 4, 2023
88a7435
add more small tests
mchangrh Nov 4, 2023
2969266
postSkipSegments, postSkipSegmentsLocked
mchangrh Nov 21, 2023
e393708
getSkipSegments tests
mchangrh Dec 12, 2023
be3490a
partial skipsegments byhash rewrite
mchangrh Dec 15, 2023
30f8693
insomnia fueled tests
mchangrh Dec 16, 2023
28a457c
test checkpoint
mchangrh Dec 21, 2023
94cd59e
finish getSkipSegmentsByHash test
mchangrh Dec 26, 2023
5062bef
Merge branch 'master' of github.com:ajayyy/SponsorBlockServer into te…
mchangrh May 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
finish getSkipSegmentsByHash test
merry christmas from japan ❤️
mchangrh committed Dec 26, 2023
commit 94cd59ebb3d70c8d639d2c9d9e1385444b1d299d
307 changes: 104 additions & 203 deletions test/cases/getSkipSegmentsByHash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { db } from "../../src/databases/databases";
import { partialDeepEquals, arrayPartialDeepEquals } from "../utils/partialDeepEquals";
import { partialDeepEquals } from "../utils/partialDeepEquals";
import { getHash } from "../../src/utils/getHash";
import { ImportMock, } from "ts-mock-imports";
import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
@@ -42,10 +42,6 @@ const defaultResponseSegment = {
description: ""
};

interface segmentInsertSegmentParams extends insertSegmentParams {
segment: [number, number] | number[],
}

describe("getSkipSegmentsByHash", () => {
const endpoint = "/api/skipSegments";
const videoIDs = multiGenProxy("video", "getSegmentsByHash");
@@ -60,16 +56,8 @@ describe("getSkipSegmentsByHash", () => {
return result;
}
});
console.log(hashedVideoIDs[0].videoID)
console.log(hashedVideoIDs[0].hashedVideoID)
console.log(hashedVideoIDs[0].hashPrefix)
console.log(`${hashedVideoIDs[0].hashPrefix}noMatchHash`)

const requiredSegmentVidHash = getHash("requiredSegmentsVid", 1);
const requiredSegmentHashVidHash = getHash("requiredSegmentsHashVid", 1);
const differentCategoryVidHash = "7fac44d1ee3257ec7f18953e2b5f991828de6854ad57193d1027c530981a89c0";
const nonMusicOverlapVidHash = "306151f778f9bfd19872b3ccfc83cbab37c4f370717436bfd85e0a624cd8ba3c";
const fullCategoryVidHash = "278fa987eebfe07ae3a4a60cf0663989ad874dd0c1f0430831d63c2001567e6f";
const insertSegments: insertSegmentParams[] = [
// videoID 0
{ videoID: hashedVideoIDs[0].videoID, startTime: 1, endTime: 10, UUID: "getSegmentsByHash-01" },
@@ -160,6 +148,16 @@ describe("getSkipSegmentsByHash", () => {
assert.ok(partialDeepEquals(data, expectedArray, true));
};

const assertSegmentsArray = async(hashPrefix: string, expectedArray: { segments: any[]}[][], axiosConfig: AxiosRequestConfig) => {
// fetch segments
const res = await client.get(`${endpoint}/${hashPrefix}`, axiosConfig);
assert.strictEqual(res.status, 200);
const data = (res.data as Array<any>).sort((a, b) => a.videoID.localeCompare(b.videoID));
const arrayMatch = expectedArray.some(exp => partialDeepEquals(data, exp));
assert.ok(arrayMatch);
return assert.strictEqual(data[0].segments.length, expectedArray[0][0].segments.length);
};

before(async () => {
for (const segment of insertSegments) {
await insertSegment(db, segment);
@@ -242,95 +240,64 @@ describe("getSkipSegmentsByHash", () => {
return assertSegmentsEqual(prefix, ["requiredSegmentsVid-2", "requiredSegmentsVid-3"], { params: { requiredSegments: `["requiredSegmentsVid-2","requiredSegmentsVid-3"]` } });
});

it("Should be able to get specific segments with repeating requiredSegment", (done) => {
it("Should be able to get specific segments with repeating requiredSegment", () => {
const prefix = requiredSegmentVidHash.substring(0, 5);
client.get(`${endpoint}/${prefix}?requiredSegment=requiredSegmentsVid-2&requiredSegment=requiredSegmentsVid-3`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = (res.data as Array<any>).sort((a, b) => a.videoID.localeCompare(b.videoID));
assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segments.length, 2);
const expected = [{
segments: [{
UUID: "requiredSegmentsVid-2"
}, {
UUID: "requiredSegmentsVid-3"
}]
}];
assert.ok(partialDeepEquals(data, expected));
done();
})
.catch(err => done(err));
const requiredSegments = ["requiredSegmentsVid-2","requiredSegmentsVid-3"];
return assertSegmentsEqual(prefix, requiredSegments, { params: { requiredSegment: requiredSegments } });
});

it("Should be able to get overlapping chapter segments if very different", (done) => {
client.get(`${endpoint}/7258?category=chapter&actionType=chapter`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = (res.data as Array<any>).sort((a, b) => a.videoID.localeCompare(b.videoID));
assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
UUID: "chapterVid-hash-1",
description: "Chapter 1"
}, {
UUID: "chapterVid-hash-2",
description: "Chapter 2"
}]
}];
const expected2 = [{
segments: [{
UUID: "chapterVid-hash-1",
description: "Chapter 1"
}, {
UUID: "chapterVid-hash-3",
description: "Chapter 3"
}]
}];

assert.ok(partialDeepEquals(data, expected, false) || partialDeepEquals(data, expected2));
assert.strictEqual(data[0].segments.length, 2);
done();
})
.catch(err => done(err));
it("Should be able to get overlapping chapter segments if very different", () => {
const expectedArray = [
[{
segments: [{
UUID: "chapterVid-hash-1",
description: "Chapter 1"
}, {
UUID: "chapterVid-hash-2",
description: "Chapter 2"
}]
}],
[{
segments: [{
UUID: "chapterVid-hash-1",
description: "Chapter 1"
}, {
UUID: "chapterVid-hash-3",
description: "Chapter 3"
}]
}]
];
return assertSegmentsArray("7258", expectedArray, { params: { category: "chapter", actionType: "chapter" } });
});

it("Should be able to get mute segment with small skip segment in middle", (done) => {
client.get(`${endpoint}/6613?actionType=skip&actionType=mute`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = (res.data as Array<any>).sort((a, b) => a.videoID.localeCompare(b.videoID));
assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
UUID: "longMuteVid-hash-3",
actionType: "mute"
}, {
UUID: "longMuteVid-hash-2",
actionType: "skip"
}, {
UUID: "longMuteVid-hash-1",
actionType: "skip"
}]
}];
const expected2 = [{
segments: [{
UUID: "longMuteVid-hash-4",
actionType: "mute"
}, {
UUID: "longMuteVid-hash-2",
actionType: "skip"
}, {
UUID: "longMuteVid-hash-1",
actionType: "skip"
}]
}];

assert.ok(arrayPartialDeepEquals(data, expected) || arrayPartialDeepEquals(data, expected2));
assert.strictEqual(data[0].segments.length, 3);
done();
})
.catch(err => done(err));
it("Should be able to get mute segment with small skip segment in middle", () => {
const expectedArray = [
[{
segments: [{
UUID: "longMuteVid-hash-3",
actionType: "mute"
}, {
UUID: "longMuteVid-hash-2",
actionType: "skip"
}, {
UUID: "longMuteVid-hash-1",
actionType: "skip"
}]
}],
[{
segments: [{
UUID: "longMuteVid-hash-4",
actionType: "mute"
}, {
UUID: "longMuteVid-hash-2",
actionType: "skip"
}, {
UUID: "longMuteVid-hash-1",
actionType: "skip"
}]
}]
];
return assertSegmentsArray("6613", expectedArray, { params: { actionType: ["skip", "mute"] } });
});

// This behavior was causing unintended consequence, uncommend when a solution is found
@@ -353,121 +320,55 @@ describe("getSkipSegmentsByHash", () => {
// .catch(err => done(err));
// });

it("Should be able to get overlapping segments where one is non music and one is other", (done) => {
client.get(`${endpoint}/3061?categories=["sponsor","music_offtopic"]`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = (res.data as Array<any>).sort((a, b) => a.videoID.localeCompare(b.videoID));
assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
category: "sponsor"
}, {
category: "music_offtopic"
}]
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data[0].segments.length, 2);
done();
})
.catch(err => done(err));
it("Should be able to get overlapping segments where one is non music and one is other", () => {
return assertSegmentsEqual("3061", ["nonMusicOverlapVid-1", "nonMusicOverlapVid-2"], { params: { categories: `["sponsor","music_offtopic"]` } });
});

it("Should be able to get mute segment with small skip segment in middle (2)", (done) => {
client.get(`${endpoint}/ab0c?actionType=skip&actionType=mute`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = (res.data as Array<any>).sort((a, b) => a.videoID.localeCompare(b.videoID));
assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
UUID: "longMuteVid-2-hash-1",
actionType: "mute"
}, {
UUID: "longMuteVid-2-hash-2",
actionType: "skip"
}]
}];
const expected2 = [{
segments: [{
UUID: "longMuteVid-2-hash-3",
actionType: "mute"
}, {
UUID: "longMuteVid-2-hash-2",
actionType: "skip"
}]
}];
const expected3 = [{
segments: [{
UUID: "longMuteVid-2-hash-4",
actionType: "mute"
}, {
UUID: "longMuteVid-2-hash-2",
actionType: "skip"
}]
}];

assert.ok(partialDeepEquals(data, expected, false) || partialDeepEquals(data, expected2) || partialDeepEquals(data, expected3));
assert.strictEqual(data[0].segments.length, 2);
done();
})
.catch(err => done(err));
it("Should be able to get mute segment with small skip segment in middle (2)", () => {
const expectedArrays = [
[{
segments: [{
UUID: "longMuteVid-2-hash-1",
actionType: "mute"
}, {
UUID: "longMuteVid-2-hash-2",
actionType: "skip"
}]
}],
[{
segments: [{
UUID: "longMuteVid-2-hash-3",
actionType: "mute"
}, {
UUID: "longMuteVid-2-hash-2",
actionType: "skip"
}]
}],
[{
segments: [{
UUID: "longMuteVid-2-hash-4",
actionType: "mute"
}, {
UUID: "longMuteVid-2-hash-2",
actionType: "skip"
}]
}]
];
return assertSegmentsArray("ab0c", expectedArrays, { params: { actionType: ["skip", "mute"] } });
});

it("Should only return one segment when fetching full video segments", (done) => {
client.get(`${endpoint}/278f`, { params: { category: ["sponsor", "selfpromo"], actionType: "full" } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = (res.data as Array<any>).sort((a, b) => a.videoID.localeCompare(b.videoID));
assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segments.length, 1);
assert.strictEqual(data[0].segments[0].category, "selfpromo");
done();
})
.catch(err => done(err));
it("Should only return one segment when fetching full video segments", () => {
return assertSegmentsEqual("278f", ["fullCategoryVid-2"], { params: { category: ["sponsor", "selfpromo"], actionType: "full" } });
});

it("Should be able to get specific segments with partial requiredSegments", (done) => {
it("Should be able to get specific segments with partial requiredSegments", () => {
const requiredSegment1 = "fbf0af454059733c8822f6a4ac8ec568e0787f8c0a5ee915dd5b05e0d7a9a388";
const requiredSegment2 = "7e1ebc5194551d2d0a606d64f675e5a14952e4576b2959f8c9d51e316c14f8da";
const prefix = requiredSegmentHashVidHash.substring(0, 5);
client.get(`${endpoint}/${prefix}?requiredSegments=["${requiredSegment1.slice(0,8)}","${requiredSegment2.slice(0,8)}"]`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = (res.data as Array<any>).sort((a, b) => a.videoID.localeCompare(b.videoID));
assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
UUID: requiredSegment1
}, {
UUID: requiredSegment2
}]
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data[0].segments.length, 2);
done();
})
.catch(err => done(err));
return assertSegmentsEqual("32ef", [requiredSegment1, requiredSegment2], { params: { requiredSegments: `["${requiredSegment1.slice(0,8)}","${requiredSegment2.slice(0,8)}"]` } });
});

it("Should be able to get single segment with requiredSegments", (done) => {
const requiredSegment1 = "fbf0af454059733c8822f6a4ac8ec568e0787f8c0a5ee915dd5b05e0d7a9a388";
const prefix = requiredSegmentHashVidHash.substring(0, 5);
client.get(`${endpoint}/${prefix}?requiredSegment=${requiredSegment1}`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = (res.data as Array<any>).sort((a, b) => a.videoID.localeCompare(b.videoID));
assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
UUID: requiredSegment1
}]
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data[0].segments.length, 1);
done();
})
.catch(err => done(err));
it("Should be able to get single segment with requiredSegments", () => {
const requiredSegment = "fbf0af454059733c8822f6a4ac8ec568e0787f8c0a5ee915dd5b05e0d7a9a388";
return assertSegmentsEqual("32ef", [requiredSegment], { params: { requiredSegment: requiredSegment } });
});

});