Merge pull request #541 from mchangrh/etagTest

add etag and other tests
This commit is contained in:
Ajay Ramachandran
2023-02-22 01:38:41 -05:00
committed by GitHub
42 changed files with 1536 additions and 1070 deletions

View File

@@ -1,4 +1,5 @@
export function createMemoryCache(memoryFn: (...args: any[]) => void, cacheTimeMs: number): any {
/* istanbul ignore if */
if (isNaN(cacheTimeMs)) cacheTimeMs = 0;
// holds the promise results

13
src/utils/getCWSUsers.ts Normal file
View File

@@ -0,0 +1,13 @@
import axios from "axios";
import { Logger } from "../utils/logger";
export const getCWSUsers = (extID: string): Promise<number | undefined> =>
axios.post(`https://chrome.google.com/webstore/ajax/detail?pv=20210820&id=${extID}`)
.then(res => res.data.split("\n")[2])
.then(data => JSON.parse(data))
.then(data => (data[1][1][0][23]).replaceAll(/,|\+/g,""))
.then(data => parseInt(data))
.catch((err) => {
Logger.error(`Error getting chrome users - ${err}`);
return 0;
});

View File

@@ -28,7 +28,7 @@ async function getFromRedis<T extends string>(key: HashedValue): Promise<T & Has
Logger.debug(`Got data from redis: ${reply}`);
return reply as T & HashedValue;
}
} catch (err) {
} catch (err) /* istanbul ignore next */ {
Logger.error(err as string);
}
}
@@ -37,7 +37,7 @@ async function getFromRedis<T extends string>(key: HashedValue): Promise<T & Has
const data = getHash(key, cachedHashTimes);
if (!config.redis?.disableHashCache) {
redis.set(redisKey, data).catch((err) => Logger.error(err));
redis.set(redisKey, data).catch(/* istanbul ignore next */ (err) => Logger.error(err));
}
return data as T & HashedValue;

View File

@@ -70,15 +70,16 @@ export async function getPlayerData (videoID: string, ignoreCache = false): Prom
}
try {
const data = await getFromITube(videoID)
.catch(err => {
.catch(/* istanbul ignore next */ err => {
Logger.warn(`InnerTube API Error for ${videoID}: ${err}`);
return Promise.reject(err);
});
DiskCache.set(cacheKey, data)
.then(() => Logger.debug(`InnerTube API: video information cache set for: ${videoID}`))
.catch((err: any) => Logger.warn(err));
.catch(/* istanbul ignore next */ (err: any) => Logger.warn(err));
return data;
} catch (err) {
/* istanbul ignore next */
return Promise.reject(err);
}
}

View File

@@ -11,7 +11,7 @@ export const isUserTempVIP = async (hashedUserID: HashedUserID, videoID: VideoID
try {
const reply = await redis.get(tempVIPKey(hashedUserID));
return reply && reply == channelID;
} catch (e) {
} catch (e) /* istanbul ignore next */ {
Logger.error(e as string);
return false;
}

View File

@@ -45,6 +45,7 @@ class Logger {
};
constructor() {
/* istanbul ignore if */
if (config.mode === "development") {
this._settings.INFO = true;
this._settings.DEBUG = true;
@@ -73,9 +74,11 @@ class Logger {
let color = colors.Bright;
if (level === LogLevel.ERROR) color = colors.FgRed;
/* istanbul ignore if */
if (level === LogLevel.WARN) color = colors.FgYellow;
let levelStr = level.toString();
/* istanbul ignore if */
if (levelStr.length === 4) {
levelStr += " "; // ensure logs are aligned
}

View File

@@ -73,3 +73,6 @@ export const parseActionTypes = (req: Request, fallback: ActionType[]): ActionTy
export const parseRequiredSegments = (req: Request): SegmentUUID[] | undefined =>
syntaxErrorWrapper(getRequiredSegments, req, []); // never fall back
export const validateCategories = (categories: string[]): boolean =>
categories.every((category: string) => config.categoryList.includes(category));

View File

@@ -135,17 +135,21 @@ if (config.redis?.enabled) {
.then((reply) => resolve(reply))
.catch((err) => reject(err))
);
/* istanbul ignore next */
client.on("error", function(error) {
lastClientFail = Date.now();
Logger.error(`Redis Error: ${error}`);
});
/* istanbul ignore next */
client.on("reconnect", () => {
Logger.info("Redis: trying to reconnect");
});
/* istanbul ignore next */
readClient?.on("error", function(error) {
lastReadFail = Date.now();
Logger.error(`Redis Read-Only Error: ${error}`);
});
/* istanbul ignore next */
readClient?.on("reconnect", () => {
Logger.info("Redis Read-Only: trying to reconnect");
});