Merge pull request #146 from zkldi:zkldi/issue99

zkldi/issue99
This commit is contained in:
zkldi
2021-05-29 12:09:09 +01:00
committed by GitHub
48 changed files with 278 additions and 158 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
files: "src/**/*.test.ts"
test-env: NODE_ENV=test
jobs: 1 # disable parallel to avoid db clashes
timeout: 500 # might need to be increased
timeout: 60 # might need to be increased
ts: true
before: "./src/test-utils/setup.ts"
check-coverage: true
+3 -3
View File
@@ -61,7 +61,7 @@ dependencies:
express-session: 1.17.1
fast-json-stable-hash: 1.0.2
fast-xml-parser: 3.19.0
kamaitachi-common: github.com/zkldi/Kamaitachi-common/5d9c31ce0b9b309182d40191ae3000a3e9083336
kamaitachi-common: github.com/zkldi/Kamaitachi-common/6f1ca95f0432ae61eef586bb3535ac5a159ad764
mongodb: 3.6.6
monk: 7.3.4
multer: 1.4.2
@@ -4344,8 +4344,8 @@ packages:
engines: {node: '>=6'}
dev: true
github.com/zkldi/Kamaitachi-common/5d9c31ce0b9b309182d40191ae3000a3e9083336:
resolution: {tarball: https://codeload.github.com/zkldi/Kamaitachi-common/tar.gz/5d9c31ce0b9b309182d40191ae3000a3e9083336}
github.com/zkldi/Kamaitachi-common/6f1ca95f0432ae61eef586bb3535ac5a159ad764:
resolution: {tarball: https://codeload.github.com/zkldi/Kamaitachi-common/tar.gz/6f1ca95f0432ae61eef586bb3535ac5a159ad764}
name: kamaitachi-common
version: 0.1.0
dependencies:
+15
View File
@@ -0,0 +1,15 @@
import t from "tap";
import { CloseRedisPubSub, RedisPub, RedisSub } from "./redis-IPC";
t.test("Basic PUB/SUB testing", (t) => {
t.setTimeout(2000);
RedisSub("class-update", (d) => {
t.equal(d.userID, 1);
t.pass();
t.end();
});
RedisPub("class-update", { userID: 1 } as any);
});
t.teardown(CloseRedisPubSub);
+80
View File
@@ -0,0 +1,80 @@
import redis from "redis";
import { RedisIPCChannels, RedisIPCData } from "kamaitachi-common";
import CreateLogCtx from "../../lib/logger/logger";
const logger = CreateLogCtx(__filename);
type RedisSubCallback<T extends RedisIPCChannels> = (data: RedisIPCData[T]) => void;
type SubCallbacks = {
[K in RedisIPCChannels]: RedisSubCallback<K>[];
};
const SubCallbacks: Partial<SubCallbacks> = {};
// Redis doesn't allow one client to subscribe and publish
// There's little to no overhead to having these three clients,
// so - that's why its like this.
const SubClient = redis.createClient();
const PubClient = redis.createClient();
const PREFIX = "KTBSV";
export function RedisPub<T extends RedisIPCChannels>(channel: T, data: RedisIPCData[T]) {
PubClient.publish(`${PREFIX}-${channel}`, JSON.stringify(data));
}
export function RedisSub<T extends RedisIPCChannels>(channel: T, callback: RedisSubCallback<T>) {
if (SubCallbacks[channel]) {
// @ts-expect-error It's complaining that the T in channel might be a different T to the T in callback
// this is obviously nonsense.
SubCallbacks[channel]!.push(callback);
logger.debug(`Pushed callback ${callback.name} to channel ${channel}.`);
} else {
// @ts-expect-error see above.
SubCallbacks[channel] = [callback];
SubClient.subscribe(`${PREFIX}-${channel}`);
logger.debug(`Added first callback ${callback.name} to channel ${channel}.`);
}
}
SubClient.on("message", (channel, strData) => {
if (!channel.startsWith("KTBSV-")) {
return; // not our business
}
const ktChannel = channel.slice("KTBSV-".length) as RedisIPCChannels;
if (!Object.prototype.hasOwnProperty.call(SubCallbacks, ktChannel)) {
return; // no callbacks to call
}
const jsData = JSON.parse(strData);
for (const cb of SubCallbacks[ktChannel]!) {
try {
cb(jsData);
} catch (err) {
logger.error(`Error calling callback ${cb.name} for channel ${ktChannel}`, { err });
}
}
});
// Awful...
export function CloseRedisPubSub() {
return new Promise<void>((resolve, reject) => {
PubClient.quit((err) => {
if (err) {
reject(err);
}
SubClient.quit((err) => {
if (err) {
reject(err);
}
resolve();
});
});
});
}
+2 -2
View File
@@ -1,5 +1,5 @@
import t from "tap";
import { CloseMongoConnection } from "../../external/mongo/db";
import { CloseAllConnections } from "../../test-utils/close-connections";
import CreateLogCtx, { Transports } from "./logger";
t.test("Logger Tests", (t) => {
@@ -22,4 +22,4 @@ t.test("Logger Tests", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,6 +1,6 @@
import Pr from "prudence";
import t from "tap";
import db, { CloseMongoConnection } from "../../../../external/mongo/db";
import db from "../../../../external/mongo/db";
import CreateLogCtx from "../../../logger/logger";
import { prAssert } from "../../../../test-utils/asserts";
import {
@@ -13,6 +13,7 @@ import { CreateCalculatedData, CalculateLampRating, CalculateRating } from "./ca
import deepmerge from "deepmerge";
import ResetDBState from "../../../../test-utils/reset-db-state";
import { GetDefaultTierlist } from "../../../../utils/tierlist";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
const mockLogger = CreateLogCtx(__filename);
@@ -292,4 +293,4 @@ t.test("#CalculateLampRating", async (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,5 +1,5 @@
import t from "tap";
import db, { CloseMongoConnection } from "../../../../external/mongo/db";
import db from "../../../../external/mongo/db";
import { AnyChartDocument, Difficulties, Lamps, ScoreDocument } from "kamaitachi-common";
import {
CalculateBPI,
@@ -19,6 +19,7 @@ import {
Testing511SPA,
TestingIIDXSPDryScore,
} from "../../../../test-utils/test-data";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
t.test("#CalculateBPI", (t) => {
t.test("AA BPI tests", (t) => {
@@ -312,4 +313,4 @@ t.test("#KaidenPercentile", async (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,6 +1,5 @@
import Pr from "prudence";
import t from "tap";
import { CloseMongoConnection } from "../../../../external/mongo/db";
import CreateLogCtx from "../../../logger/logger";
import { prAssert } from "../../../../test-utils/asserts";
import {
@@ -9,6 +8,7 @@ import {
TestingSDVXSingleDryScore,
} from "../../../../test-utils/test-data";
import { CreateGameSpecific } from "./game-specific";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
/**
@@ -127,4 +127,4 @@ t.test("#CreateGameSpecific", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,7 +1,7 @@
import t from "tap";
import { CreateImportLoggerAndID } from "./import-logger";
import { PublicUserDocument } from "kamaitachi-common";
import { CloseMongoConnection } from "../../../../external/mongo/db";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
t.test("#CreateImportLoggerAndID", (t) => {
const { importID, logger } = CreateImportLoggerAndID(
@@ -19,4 +19,4 @@ t.test("#CreateImportLoggerAndID", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,13 +1,7 @@
import t from "tap";
import db, { CloseMongoConnection } from "../../../../external/mongo/db";
import db from "../../../../external/mongo/db";
import ResetDBState from "../../../../test-utils/reset-db-state";
import {
GetRelevantFolderGoals,
GetRelevantGoals,
UpdateGoalsForUser,
GetAndUpdateUsersGoals,
ProcessGoal,
} from "./goals";
import { GetRelevantFolderGoals, GetRelevantGoals, UpdateGoalsForUser, ProcessGoal } from "./goals";
import { GoalDocument, UserGoalDocument } from "kamaitachi-common";
import { CreateFolderChartLookup } from "../../../../utils/folder";
import {
@@ -21,6 +15,7 @@ import {
import deepmerge from "deepmerge";
import CreateLogCtx from "../../../logger/logger";
import crypto from "crypto";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -422,4 +417,4 @@ t.test("#ProcessGoal", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -2,6 +2,7 @@ import { integer, Game, GoalDocument, UserGoalDocument } from "kamaitachi-common
import { EvaluateGoalForUser } from "../../../../utils/goal";
import db from "../../../../external/mongo/db";
import { KtLogger } from "../../../logger/logger";
import { RedisPub } from "../../../../external/redis/redis-IPC";
/**
* Update a user's progress on all of their set goals.
@@ -87,30 +88,11 @@ export async function ProcessGoal(
return;
}
// if the user has changed their progress on the goal
if (userGoal.progress !== res.progress) {
// if the user has improved their progress on the goal
// if userGoal.progress is null, then res.progress must be non-null, and therefore an improvement.
if (
userGoal.progress === null ||
(res.progress !== null && userGoal.progress < res.progress)
) {
// @todo #99 emit something
}
} else if (userGoal.outOf === res.outOf) {
// if the users progress hasn't changed AND the outOf hasn't
// then nothing has changed.
// the outOf check is to account for things such as folder sizes changing underfoot
// which would always require an update.
// nothing has changed
if (userGoal.progress === res.progress && userGoal.outOf === res.outOf) {
return;
}
// if this is a newly-achieved goal
if (res.achieved && !userGoal.achieved) {
// @todo #99 emit something
}
const newData = {
progress: res.progress,
progressHuman: res.progressHuman,
@@ -119,6 +101,24 @@ export async function ProcessGoal(
achieved: res.achieved,
};
const oldData = {
progress: userGoal.progress,
progressHuman: userGoal.progressHuman,
outOf: userGoal.outOf,
outOfHuman: userGoal.outOfHuman,
achieved: userGoal.achieved,
};
// if this is a newly-achieved goal
if (res.achieved && !userGoal.achieved) {
RedisPub("goal-achieved", {
userID,
goalID: goal.goalID,
old: oldData,
new: newData,
});
}
const bulkWrite = {
updateOne: {
filter: { _id: userGoal._id! },
@@ -139,13 +139,7 @@ export async function ProcessGoal(
bwrite: bulkWrite,
import: {
goalID: goal.goalID,
old: {
progress: userGoal.progress,
progressHuman: userGoal.progressHuman,
outOf: userGoal.outOf,
outOfHuman: userGoal.outOfHuman,
achieved: userGoal.achieved,
},
old: oldData,
new: newData,
},
};
@@ -1,11 +1,12 @@
import t from "tap";
import db, { CloseMongoConnection } from "../../../../external/mongo/db";
import db from "../../../../external/mongo/db";
import CreateLogCtx from "../../../logger/logger";
import ResetDBState from "../../../../test-utils/reset-db-state";
import { TestingIIDXSPMilestone } from "../../../../test-utils/test-data";
import { UpdateUsersMilestones } from "./milestones";
import { GoalImportInfo } from "kamaitachi-common";
import deepmerge from "deepmerge";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -353,4 +354,4 @@ t.test("#UpdateUsersMilestones", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -11,6 +11,7 @@ import {
import db from "../../../../external/mongo/db";
import { BulkWriteUpdateOneOperation } from "mongodb";
import { CalculateMilestoneOutOf, GetGoalIDsFromMilestone } from "../../../../utils/milestone";
import { RedisPub, RedisSub } from "../../../../external/redis/redis-IPC";
/**
* Processes and updates a user's milestones from their Goal Import Info (i.e. what is returned
@@ -113,22 +114,27 @@ export async function UpdateUsersMilestones(
},
});
const milestoneInfo = {
milestoneID: userMilestone.milestoneID,
old: {
progress: userMilestone.progress,
achieved: userMilestone.achieved,
},
new: {
progress,
achieved,
},
};
if (progress !== userMilestone.progress) {
importMilestoneInfo.push({
milestoneID: userMilestone.milestoneID,
old: {
progress: userMilestone.progress,
achieved: userMilestone.achieved,
},
new: {
progress,
achieved,
},
});
importMilestoneInfo.push(milestoneInfo);
}
if (achieved && !userMilestone.achieved) {
// @todo #99 emit some sort of event
RedisPub("milestone-achieved", {
userID,
...milestoneInfo,
});
}
}
@@ -1,5 +1,5 @@
import t from "tap";
import db, { CloseMongoConnection } from "../../../../external/mongo/db";
import db from "../../../../external/mongo/db";
import ResetDBState from "../../../../test-utils/reset-db-state";
import { GetKTDataJSON, Testing511SPA, TestingIIDXSPScore } from "../../../../test-utils/test-data";
import { CreatePBDoc } from "./create-pb-doc";
@@ -7,6 +7,7 @@ import deepmerge from "deepmerge";
import CreateLogCtx, { KtLogger } from "../../../logger/logger";
import { lamps } from "kamaitachi-common/js/config";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
let IIDXScore = TestingIIDXSPScore;
@@ -169,4 +170,4 @@ t.test("#CreatePBDoc", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,11 +1,12 @@
import t from "tap";
import db, { CloseMongoConnection } from "../../../../external/mongo/db";
import db from "../../../../external/mongo/db";
import CreateLogCtx from "../../../logger/logger";
import ResetDBState from "../../../../test-utils/reset-db-state";
import { Testing511SPA, TestingIIDXSPScore } from "../../../../test-utils/test-data";
import { ProcessPBs } from "./process-pbs";
import deepmerge from "deepmerge";
import crypto from "crypto";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -58,4 +59,4 @@ t.test("#ProcessPBs", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,6 +1,7 @@
import { ScoreDocument } from "kamaitachi-common";
import t from "tap";
import db, { CloseMongoConnection } from "../../../../external/mongo/db";
import db from "../../../../external/mongo/db";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
import ResetDBState from "../../../../test-utils/reset-db-state";
import { InsertQueue, QueueScoreInsert } from "./insert-score";
@@ -107,4 +108,4 @@ t.test("#QueueScoreInsert, #InsertQueue", async (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,6 +1,6 @@
import { ScoreDocument } from "kamaitachi-common";
import t from "tap";
import { CloseMongoConnection } from "../../../../external/mongo/db";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
import ResetDBState from "../../../../test-utils/reset-db-state";
import { Testing511SPA, TestingIIDXSPDryScore } from "../../../../test-utils/test-data";
import { CreateScoreID, GetWithScoreID } from "./score-id";
@@ -93,4 +93,4 @@ t.test("#CreateScoreID", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,9 +1,9 @@
import { UserGameStats } from "kamaitachi-common";
import t from "tap";
import { CloseMongoConnection } from "../../../../external/mongo/db";
import CreateLogCtx from "../../../logger/logger";
import ResetDBState from "../../../../test-utils/reset-db-state";
import { CalculateClassDeltas, UpdateUGSClasses } from "./classes";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -48,7 +48,7 @@ t.test("#CalculateClassDeltas", (t) => {
t.beforeEach(ResetDBState);
t.test("Should return improved classes from null", (t) => {
const res = CalculateClassDeltas("SP", { dan: 18 }, null, logger);
const res = CalculateClassDeltas("SP", { dan: 18 }, null, 1, logger);
t.strictSame(res, [
{
@@ -67,6 +67,7 @@ t.test("#CalculateClassDeltas", (t) => {
"SP",
{ dan: 18 },
{ classes: {} } as UserGameStats,
1,
logger
);
@@ -87,6 +88,7 @@ t.test("#CalculateClassDeltas", (t) => {
"SP",
{ dan: 18 },
({ classes: { dan: 17 } } as unknown) as UserGameStats,
1,
logger
);
@@ -107,6 +109,7 @@ t.test("#CalculateClassDeltas", (t) => {
"SP",
{ dan: 18 },
({ classes: { dan: 18 } } as unknown) as UserGameStats,
1,
logger
);
@@ -120,6 +123,7 @@ t.test("#CalculateClassDeltas", (t) => {
"SP",
{ dan: 16 },
({ classes: { dan: 18 } } as unknown) as UserGameStats,
1,
logger
);
@@ -131,4 +135,4 @@ t.test("#CalculateClassDeltas", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -4,6 +4,7 @@ import { KtLogger } from "../../../logger/logger";
import { CalculateGitadoraColour, CalculateJubeatColour } from "./builtin-class-handlers";
import { ReturnClassIfGreater } from "../../../../utils/class";
import { GameClasses } from "kamaitachi-common/js/game-classes";
import { RedisPub } from "../../../../external/redis/redis-IPC";
type ScoreClasses = Partial<GameClasses<IDStrings>>;
@@ -117,9 +118,10 @@ export function CalculateClassDeltas(
playtype: Playtypes[Game],
classes: ScoreClasses,
userGameStats: UserGameStats | null,
userID: integer,
logger: KtLogger
): ClassDelta[] {
const deltas = [];
const deltas: ClassDelta[] = [];
for (const s in classes) {
const classSet = s as keyof GameClasses<IDStrings>;
@@ -135,22 +137,27 @@ export function CalculateClassDeltas(
if (isGreater === false) {
continue;
} else if (isGreater === null) {
// @todo #99 REDISIPC-New Class Achieved
deltas.push({
set: classSet,
playtype,
old: null,
new: classVal,
});
} else {
// @todo #99 REDISIPC-Class Improved!
deltas.push({
set: classSet,
playtype,
old: userGameStats!.classes[classSet]!,
new: classVal,
});
let delta: ClassDelta;
if (isGreater === null) {
delta = {
set: classSet,
playtype,
old: null,
new: classVal,
};
} else {
delta = {
set: classSet,
playtype,
old: userGameStats!.classes[classSet]!,
new: classVal,
};
}
RedisPub("class-update", { userID, ...delta });
deltas.push(delta);
}
} catch (err) {
logger.error(err);
@@ -1,11 +1,12 @@
import t from "tap";
import db, { CloseMongoConnection } from "../../../../external/mongo/db";
import db from "../../../../external/mongo/db";
import ResetDBState from "../../../../test-utils/reset-db-state";
import { TestingIIDXSPScorePB } from "../../../../test-utils/test-data";
import { CalculateCustomRatings, CalculateRatings } from "./rating";
import deepmerge from "deepmerge";
import CreateLogCtx from "../../../logger/logger";
import crypto from "crypto";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -142,4 +143,4 @@ t.test("#CalculateCustomRatings", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,12 +1,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import t from "tap";
import db, { CloseMongoConnection } from "../../../../external/mongo/db";
import db from "../../../../external/mongo/db";
import CreateLogCtx from "../../../logger/logger";
import ResetDBState from "../../../../test-utils/reset-db-state";
import { UpdateUsersGamePlaytypeStats } from "./update-ugs";
import deepmerge from "deepmerge";
import crypto from "crypto";
import { TestingIIDXSPScorePB } from "../../../../test-utils/test-data";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -212,4 +213,4 @@ t.test("#UpdateUsersGamePlaytypeStats", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -41,7 +41,7 @@ export async function UpdateUsersGamePlaytypeStats(
logger.debug(`Calculating Class Deltas...`);
const deltas = CalculateClassDeltas(playtype, classes, userGameStats, logger);
const deltas = CalculateClassDeltas(playtype, classes, userGameStats, userID, logger);
logger.debug(`Had ${deltas.length} deltas.`);
@@ -5,10 +5,10 @@ import { GetKTDataJSON, Testing511Song, Testing511SPA } from "../../../../../tes
import { ResolveChartFromSong, ResolveMatchTypeToKTData, ConverterBatchManual } from "./converter";
import deepmerge from "deepmerge";
import { EscapeStringRegexp } from "../../../../../utils/misc";
import db, { CloseMongoConnection } from "../../../../../external/mongo/db";
import { Game } from "kamaitachi-common";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import { InvalidScoreFailure } from "../../../framework/common/converter-failures";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
const baseBatchManualScore = {
score: 500,
@@ -394,4 +394,4 @@ t.test("#ConverterFn", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,11 +1,11 @@
import t from "tap";
import { CloseMongoConnection } from "../../../../../external/mongo/db";
import CreateLogCtx from "../../../../logger/logger";
import ScoreImportFatalError from "../../../framework/score-importing/score-import-error";
import { ParseBatchManualFromObject as ParserFn } from "./parser";
import { BatchManual } from "./types";
import { EscapeStringRegexp } from "../../../../../utils/misc";
import deepmerge from "deepmerge";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
const mockErr = (...msg: string[]) =>
(({
@@ -472,4 +472,4 @@ t.test("#ParserFn", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,5 +1,5 @@
import t from "tap";
import db, { CloseMongoConnection } from "../../../../../external/mongo/db";
import db from "../../../../../external/mongo/db";
import CreateLogCtx from "../../../../logger/logger";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import ConverterFn, { EamScoreConverter, EamScoreConverterWrapper } from "./converter";
@@ -12,6 +12,7 @@ import {
InvalidScoreFailure,
KTDataNotFoundFailure,
} from "../../../framework/common/converter-failures";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -373,4 +374,4 @@ t.todo("#ConverterFn", async (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -2,12 +2,12 @@ import t from "tap";
import CreateLogCtx from "../../../../logger/logger";
import ScoreImportFatalError from "../../../framework/score-importing/score-import-error";
import GenericParseEamIIDXCSV, { NaiveCSVParse, ResolveHeaders } from "./parser";
import { CloseMongoConnection } from "../../../../../external/mongo/db";
import {
TestingIIDXEamusementCSV26,
TestingIIDXEamusementCSV27,
} from "../../../../../test-utils/test-data";
import { MockMulterFile } from "../../../../../test-utils/mock-multer";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -304,4 +304,4 @@ t.test("#ParseEamusementCSV", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import t from "tap";
import db, { CloseMongoConnection } from "../../../../../external/mongo/db";
import db from "../../../../../external/mongo/db";
import CreateLogCtx from "../../../../logger/logger";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import {
@@ -12,6 +12,7 @@ import {
import { ConvertFileMerIIDX } from "./converter";
import deepmerge from "deepmerge";
import { MerScore } from "./types";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -136,4 +137,4 @@ t.test("#ConvertFileMerIIDX", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,12 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import t from "tap";
import { CloseMongoConnection } from "../../../../../external/mongo/db";
import CreateLogCtx from "../../../../logger/logger";
import { MockMulterFile } from "../../../../../test-utils/mock-multer";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import { GetKTDataBuffer, GetKTDataJSON } from "../../../../../test-utils/test-data";
import { ParseMerIIDX } from "./parser";
import deepmerge from "deepmerge";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -143,4 +143,4 @@ t.test("#ParseMerIIDX", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import t from "tap";
import { CloseMongoConnection } from "../../../../../external/mongo/db";
import CreateLogCtx from "../../../../logger/logger";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import {
@@ -12,6 +11,7 @@ import {
import { ConvertFileS3, ParseDifficulty, ResolveS3Lamp } from "./converter";
import { S3Score } from "./types";
import deepmerge from "deepmerge";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -222,4 +222,4 @@ t.test("#ResolveS3Lamp", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,12 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import t from "tap";
import { GetMilisecondsSince } from "../../../../../utils/misc";
import { CloseMongoConnection } from "../../../../../external/mongo/db";
import CreateLogCtx from "../../../../logger/logger";
import { MockMulterFile } from "../../../../../test-utils/mock-multer";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import { GetKTDataBuffer } from "../../../../../test-utils/test-data";
import { ParseSolidStateXML } from "./parser";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -187,7 +186,7 @@ t.test("#ParseSolidStateXML", (t) => {
});
t.test("Should reject billion laughs", (t) => {
const time = process.hrtime.bigint();
t.setTimeout(5000);
t.throws(
() =>
@@ -202,15 +201,11 @@ t.test("#ParseSolidStateXML", (t) => {
{ message: /Invalid S3 XML/u } as any
);
const end = GetMilisecondsSince(time);
t.ok(end < 10_000, "Should take less than 10 seconds");
t.end();
});
t.test("Should not expand specifically crafted billion laughs", (t) => {
const time = process.hrtime.bigint();
t.setTimeout(5000);
const res = ParseSolidStateXML(
MockMulterFile(
@@ -224,14 +219,10 @@ t.test("#ParseSolidStateXML", (t) => {
// @ts-expect-error shush
t.equal(res.iterable[0].songname, "&lol9;", "Should not expand billion laughs.");
const end = GetMilisecondsSince(time);
t.ok(end < 10_000, "Should take less than 10 seconds");
t.end();
});
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,10 +1,11 @@
import t from "tap";
import db, { CloseMongoConnection } from "../../../../../external/mongo/db";
import db from "../../../../../external/mongo/db";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import CreateLogCtx from "../../../../logger/logger";
import { ConverterIRBarbatos } from "./converter";
import deepmerge from "deepmerge";
import { barbScore } from "../../../../../test-utils/test-data";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -111,4 +112,4 @@ t.test("#ConverterIRBarbatos", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,5 +1,5 @@
import t from "tap";
import { CloseMongoConnection } from "../../../../../external/mongo/db";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import { barbScore } from "../../../../../test-utils/test-data";
import CreateLogCtx from "../../../../logger/logger";
@@ -33,4 +33,4 @@ t.test("#ParseBarbatosSingle", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,5 +1,5 @@
import t from "tap";
import { CloseMongoConnection } from "../../../../../external/mongo/db";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import CreateLogCtx from "../../../../logger/logger";
import { FerStaticClassHandler } from "./class-handler";
@@ -63,4 +63,4 @@ t.test("#FerStaticClassHandler", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,5 +1,5 @@
import t from "tap";
import { CloseMongoConnection } from "../../../../../external/mongo/db";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import { GetKTDataJSON } from "../../../../../test-utils/test-data";
import CreateLogCtx from "../../../../logger/logger";
@@ -181,4 +181,4 @@ t.test("#ParseFervidexStatic", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import t from "tap";
import db, { CloseMongoConnection } from "../../../../../external/mongo/db";
import db from "../../../../../external/mongo/db";
import CreateLogCtx from "../../../../logger/logger";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import { GetKTDataJSON, Testing511Song, Testing511SPA } from "../../../../../test-utils/test-data";
@@ -15,6 +15,7 @@ import {
} from "./converter";
import { FervidexScore } from "./types";
import deepmerge from "deepmerge";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -225,4 +226,4 @@ t.test("#ConverterIRFervidex", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,11 +1,12 @@
import t from "tap";
import db, { CloseMongoConnection } from "../../../../../external/mongo/db";
import db from "../../../../../external/mongo/db";
import { ConverterIRUSC, DeriveLamp, DeriveNoteMod } from "./converter";
import d from "deepmerge";
import { uscChart, uscScore } from "../../../../../test-utils/test-data";
import CreateLogCtx from "../../../../logger/logger";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import { USCClientScore } from "../../../../../server/router/ir/usc/usc";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -122,4 +123,4 @@ t.test("#ConverterIRUSC", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,5 +1,5 @@
import t from "tap";
import { CloseMongoConnection } from "../../../../../external/mongo/db";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
import { uscChart, uscScore } from "../../../../../test-utils/test-data";
import CreateLogCtx from "../../../../logger/logger";
import { ParseIRUSC } from "./parser";
@@ -37,4 +37,4 @@ t.test("#ParseIRUSC", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,8 +1,9 @@
import { CreateInviteCode, AddNewInvite, ReinstateInvite, ValidateCaptcha } from "./auth";
import t from "tap";
import db, { CloseMongoConnection } from "../../../../../external/mongo/db";
import db from "../../../../../external/mongo/db";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import { MockFetch } from "../../../../../test-utils/mock-fetch";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
t.test("#ReinstateInvite", (t) => {
t.beforeEach(ResetDBState);
@@ -74,4 +75,4 @@ t.test("#ValidateCaptcha", async (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,9 +1,9 @@
import t from "tap";
import { ValidateAuthToken, ValidateIRClientVersion } from "./auth";
import expMiddlewareMock from "express-request-mock";
import { CloseMongoConnection } from "../../../../external/mongo/db";
import ResetDBState from "../../../../test-utils/reset-db-state";
import { SYMBOL_KtchiData } from "../../../../lib/constants/ktchi";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
t.test("#ValidateIRClientVersion", (t) => {
t.test("Should reject clients that are not supported", async (t) => {
@@ -117,4 +117,4 @@ t.test("#ValidateAuthToken", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,11 +1,12 @@
import t from "tap";
import db, { CloseMongoConnection } from "../../../../../external/mongo/db";
import db from "../../../../../external/mongo/db";
import ResetDBState from "../../../../../test-utils/reset-db-state";
import { GetKTDataJSON } from "../../../../../test-utils/test-data";
import { KtchiPBScoreToBeatorajaFormat } from "./convert-scores";
import { ScoreDocument, PBScoreDocument } from "kamaitachi-common";
import { Random20Hex } from "../../../../../utils/misc";
import deepmerge from "deepmerge";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
const gazerChart = GetKTDataJSON("./kamaitachi/bms-gazer-chart.json");
@@ -187,4 +188,4 @@ t.test("#KtchiPBScoreToBeatorajaFormat", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
+3 -2
View File
@@ -1,10 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import t from "tap";
import db, { CloseMongoConnection } from "../../../../external/mongo/db";
import db from "../../../../external/mongo/db";
import ResetDBState from "../../../../test-utils/reset-db-state";
import { CreatePOSTScoresResponseBody, KtchiScoreToServerScore } from "./usc";
import { ChartDocument, PBScoreDocument, ScoreDocument } from "kamaitachi-common";
import deepmerge from "deepmerge";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
const mockScorePB: PBScoreDocument<"usc:Single"> = {
chartID: "USC_CHART_ID",
@@ -419,4 +420,4 @@ t.test("#CreatePOSTScoresResponseBody", async (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
@@ -1,4 +1,5 @@
import { CloseMongoConnection } from "../external/mongo/db";
import { CloseRedisPubSub } from "../external/redis/redis-IPC";
import { CloseRedisConnection } from "../external/redis/redis-store";
import { CloseServerConnection } from "./mock-api";
@@ -6,4 +7,5 @@ export async function CloseAllConnections() {
await CloseMongoConnection();
await CloseServerConnection();
await CloseRedisConnection();
await CloseRedisPubSub();
}
+8 -2
View File
@@ -2,6 +2,7 @@ import { Game, integer, IDStrings, Playtypes, UserGameStats } from "kamaitachi-c
import db from "../external/mongo/db";
import CreateLogCtx from "../lib/logger/logger";
import { GameClassSets } from "kamaitachi-common/js/game-classes";
import { RedisPub } from "../external/redis/redis-IPC";
const logger = CreateLogCtx(__filename);
@@ -67,11 +68,16 @@ export async function UpdateClassIfGreater(
}
if (isGreater === null) {
// @todo #99 REDIS-IPC new class achieved
RedisPub("class-update", { userID, new: classVal, old: null, set: classSet });
return null;
} else {
// @todo #99 REDIS-IPC class improved
RedisPub("class-update", {
userID,
new: classVal,
old: userGameStats!.classes[classSet]!,
set: classSet,
});
return true;
}
+3 -2
View File
@@ -1,7 +1,8 @@
import { GetNextCounterValue } from "./db";
import t from "tap";
import db, { CloseMongoConnection } from "../external/mongo/db";
import db from "../external/mongo/db";
import ResetDBState from "../test-utils/reset-db-state";
import { CloseAllConnections } from "../test-utils/close-connections";
t.test("#GetNextCounterValue", (t) => {
t.beforeEach(ResetDBState);
@@ -27,4 +28,4 @@ t.test("#GetNextCounterValue", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
+3 -2
View File
@@ -8,9 +8,10 @@ import {
GetFolderCharts,
ResolveFolderToCharts,
} from "./folder";
import db, { CloseMongoConnection } from "../external/mongo/db";
import db from "../external/mongo/db";
import ResetDBState from "../test-utils/reset-db-state";
import deepmerge from "deepmerge";
import { CloseAllConnections } from "../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -208,4 +209,4 @@ t.test("#GetFolderChartIDs", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
+3 -2
View File
@@ -1,5 +1,5 @@
import t from "tap";
import db, { CloseMongoConnection } from "../external/mongo/db";
import db from "../external/mongo/db";
import CreateLogCtx from "../lib/logger/logger";
import ResetDBState from "../test-utils/reset-db-state";
import { EvaluateGoalForUser } from "./goal";
@@ -12,6 +12,7 @@ import {
TestingIIDXSPScorePB,
} from "../test-utils/test-data";
import { CreateFolderChartLookup } from "./folder";
import { CloseAllConnections } from "../test-utils/close-connections";
const logger = CreateLogCtx(__filename);
@@ -296,4 +297,4 @@ t.test("#EvaluateGoalForUser", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
+3 -2
View File
@@ -1,5 +1,6 @@
import t from "tap";
import db, { CloseMongoConnection } from "../external/mongo/db";
import db from "../external/mongo/db";
import { CloseAllConnections } from "../test-utils/close-connections";
import ResetDBState from "../test-utils/reset-db-state";
import { GetScoresFromSession } from "./session";
@@ -27,4 +28,4 @@ t.test("#GetScoresFromSession", async (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);
+2 -2
View File
@@ -1,9 +1,9 @@
import t from "tap";
import { CloseMongoConnection } from "../external/mongo/db";
import { PRUDENCE_PRIVATE_USER, PRUDENCE_PUBLIC_USER } from "../external/mongo/schemas";
import { prAssert } from "../test-utils/asserts";
import { GetUserCaseInsensitive, PRIVATEINFO_GetUserCaseInsensitive, FormatUserDoc } from "./user";
import { PublicUserDocument } from "kamaitachi-common";
import { CloseAllConnections } from "../test-utils/close-connections";
t.test("#GetUserCaseInsensitive", (t) => {
t.test("Should return the user for an exact username", async (t) => {
@@ -85,4 +85,4 @@ t.test("#FormatUserDoc", (t) => {
t.end();
});
t.teardown(CloseMongoConnection);
t.teardown(CloseAllConnections);