mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-17 13:08:49 +03:00
@@ -1,5 +1,5 @@
|
||||
import { db } from "../../src/databases/databases";
|
||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||
import { partialDeepEquals, arrayPartialDeepEquals } from "../utils/partialDeepEquals";
|
||||
import { getHash } from "../../src/utils/getHash";
|
||||
import { ImportMock, } from "ts-mock-imports";
|
||||
import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
|
||||
@@ -434,7 +434,7 @@ describe("getSkipSegmentsByHash", () => {
|
||||
}]
|
||||
}];
|
||||
|
||||
assert.ok(partialDeepEquals(data, expected, false) || partialDeepEquals(data, expected2, false));
|
||||
assert.ok(arrayPartialDeepEquals(data, expected) || arrayPartialDeepEquals(data, expected2));
|
||||
assert.strictEqual(data[0].segments.length, 3);
|
||||
done();
|
||||
})
|
||||
|
||||
@@ -3,7 +3,7 @@ import { getHash } from "../../../src/utils/getHash";
|
||||
import assert from "assert";
|
||||
import { client } from "../../utils/httpClient";
|
||||
import { AxiosResponse } from "axios";
|
||||
import { partialDeepEquals } from "../../utils/partialDeepEquals";
|
||||
import { partialDeepEquals, arrayPartialDeepEquals } from "../../utils/partialDeepEquals";
|
||||
|
||||
const endpoint = "/api/ratings/rate";
|
||||
const getRating = (hash: string, params?: unknown): Promise<AxiosResponse> => client.get(`${endpoint}/${hash}`, { params });
|
||||
@@ -58,6 +58,9 @@ describe("getRating", () => {
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
/*
|
||||
This test will fail if tests are already ran with redis.
|
||||
*/
|
||||
it("Should be able to bulk fetch", (done) => {
|
||||
getBulkRating([videoOnePartialHash, videoTwoPartialHash])
|
||||
.then(res => {
|
||||
@@ -80,7 +83,7 @@ describe("getRating", () => {
|
||||
count: 10,
|
||||
hash: videoOneIDHash,
|
||||
}];
|
||||
assert.ok(partialDeepEquals(res.data, expected));
|
||||
assert.ok(arrayPartialDeepEquals(res.data, expected));
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
|
||||
32
test/cases/redisTest.ts
Normal file
32
test/cases/redisTest.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { config } from "../../src/config";
|
||||
import redis from "../../src/utils/redis";
|
||||
import crypto from "crypto";
|
||||
import assert from "assert";
|
||||
|
||||
const genRandom = (bytes=8) => crypto.pseudoRandomBytes(bytes).toString("hex");
|
||||
|
||||
const randKey1 = genRandom();
|
||||
const randValue1 = genRandom();
|
||||
const randKey2 = genRandom(16);
|
||||
|
||||
describe("redis test", function() {
|
||||
before(async function() {
|
||||
if (!config.redis) this.skip();
|
||||
await redis.setAsync(randKey1, randValue1);
|
||||
});
|
||||
it("Should get stored value", (done) => {
|
||||
redis.getAsync(randKey1)
|
||||
.then(res => {
|
||||
if (res.err) assert.fail(res.err);
|
||||
assert.strictEqual(res.reply, randValue1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("Should not be able to get not stored value", (done) => {
|
||||
redis.getAsync(randKey2)
|
||||
.then(res => {
|
||||
if (res.reply || res.err ) assert.fail("Value should not be found")
|
||||
done();
|
||||
});
|
||||
})
|
||||
});
|
||||
@@ -9,6 +9,7 @@ import { initDb } from "../src/databases/databases";
|
||||
import { ImportMock } from "ts-mock-imports";
|
||||
import * as rateLimitMiddlewareModule from "../src/middleware/requestRateLimit";
|
||||
import rateLimit from "express-rate-limit";
|
||||
import redis from "../src/utils/redis";
|
||||
|
||||
async function init() {
|
||||
ImportMock.mockFunction(rateLimitMiddlewareModule, "rateLimitMiddleware", rateLimit({
|
||||
@@ -56,6 +57,7 @@ async function init() {
|
||||
mocha.run((failures) => {
|
||||
mockServer.close();
|
||||
server.close();
|
||||
redis.close(true);
|
||||
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
|
||||
});
|
||||
});
|
||||
|
||||
@@ -22,6 +22,12 @@ export const partialDeepEquals = (actual: Record<string, any>, expected: Record<
|
||||
return true;
|
||||
};
|
||||
|
||||
export const arrayPartialDeepEquals = (actual: Array<any>, expected: Array<any>): boolean => {
|
||||
for (const value of expected)
|
||||
if (!actual.some(a => partialDeepEquals(a, value, false))) return false;
|
||||
return true;
|
||||
};
|
||||
|
||||
export const arrayDeepEquals = (actual: Record<string, any>, expected: Record<string, any>, print = true): boolean => {
|
||||
if (actual.length !== expected.length) return false;
|
||||
let flag = true;
|
||||
|
||||
Reference in New Issue
Block a user