Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
83 changes: 83 additions & 0 deletions __test__/queue.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { agent } from 'supertest';
import app from '../app.js';
import { db } from '../src/services/db.js';
import { QueueService } from '../src/services/queue.js';
const request = agent(app);

describe('Queue routes', () => {
beforeAll(() => {
// Authenticate mock steam user (mock strategy)
return request.get('/auth/steam/return').expect(302);
});

it('should create a queue and return URL', async () => {
const payload = [ { maxPlayers: 4, private: false } ];
const res = await request
.post('/queue/')
.set('Content-Type', 'application/json')
.send(payload)
.expect(200);

expect(res.body.url).toMatch(/\/queue\//);
// Save the slug for subsequent tests
const slug = res.body.url.split('/').pop();
expect(slug).toBeDefined();
// store on global for other tests
global.__TEST_QUEUE_SLUG = slug;
});

it('should add users to the queue and create teams when full', async () => {
const slug = global.__TEST_QUEUE_SLUG;
// Add 4 users; the first is the creator (already added) so add 3 more
// Using the mockProfile steam id and some fake ids for others
const extraUsers = ['76561198025644195','76561198025644196','76561198025644197'];
// Add users directly to the queue service to simulate distinct steam IDs (route uses req.user)
for (const id of extraUsers) {
await QueueService.addUserToQueue(slug, id);
}

// Now trigger team creation from the service (would normally be called by the route once full)
const result = await QueueService.createTeamsFromQueue(slug);
expect(result).toBeDefined();
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(2);

// Check DB for created teams; there should be at least 2 inserted with team_auth_names
const teams = await db.query('SELECT id FROM team WHERE name LIKE ?', [`team_%`]);
expect(teams.length).toBeGreaterThanOrEqual(2);
const teamId = teams[0].id;
const auths = await db.query('SELECT auth FROM team_auth_names WHERE team_id = ?', [teamId]);
expect(auths.length).toBeGreaterThan(0);
});

describe('rating normalization', () => {
test('uses median when some ratings are present and adds jitter for missing', () => {
const realRandom = Math.random;
Math.random = () => 0.5;

const players = [
{ steamId: '1', timestamp: 1, hltvRating: 2 },
{ steamId: '2', timestamp: 2, hltvRating: 4 },
{ steamId: '3', timestamp: 3, hltvRating: undefined },
];
const out = QueueService.normalizePlayerRatings(players);
expect(out.find(p => p.steamId === '3').hltvRating).toBeCloseTo(3);

Math.random = realRandom;
});

test('all missing ratings fall back to 1.0 +/- jitter', () => {
const realRandom = Math.random;
Math.random = () => 0.25;

const players = [
{ steamId: 'a', timestamp: 1, hltvRating: undefined },
{ steamId: 'b', timestamp: 2, hltvRating: undefined }
];
const out = QueueService.normalizePlayerRatings(players);
expect(out[0].hltvRating).toBeCloseTo(0.975);

Math.random = realRandom;
});
});
});
2 changes: 2 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import matchesRouter from "./src/routes/matches/matches.js";
import matchServerRouter from "./src/routes/matches/matchserver.js";
import playerstatsRouter from "./src/routes/playerstats/playerstats.js";
import playerstatsextraRouter from "./src/routes/playerstats/extrastats.js";
import queuerouter from "./src/routes/queue.js";
import seasonsRouter from "./src/routes/seasons.js";
import serversRouter from "./src/routes/servers.js";
import teamsRouter from "./src/routes/teams.js";
Expand Down Expand Up @@ -143,6 +144,7 @@ app.use("/matches", matchesRouter, matchServerRouter);
app.use("/mapstats", mapstatsRouter);
app.use("/playerstats", playerstatsRouter);
app.use("/playerstatsextra", playerstatsextraRouter);
app.use("/queue", queuerouter);
app.use("/seasons", seasonsRouter);
app.use("/match", legacyAPICalls);
app.use("/leaderboard", leaderboardRouter);
Expand Down
3 changes: 2 additions & 1 deletion config/development.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"uploadDemos": false,
"localLoginEnabled": true,
"redisUrl": "redis://:super_secure@localhost:6379",
"redisTTL": 86400
"redisTTL": 86400,
"queueTTL": 3600
},
"development": {
"driver": "mysql",
Expand Down
3 changes: 2 additions & 1 deletion config/production.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"uploadDemos": $UPLOADDEMOS,
"localLoginEnabled": $LOCALLOGINS,
"redisUrl": "$REDISURL",
"redisTTL": $REDISTTL
"redisTTL": $REDISTTL,
"queueTTL": $QUEUETTL
},
"production": {
"driver": "mysql",
Expand Down
3 changes: 2 additions & 1 deletion config/test.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"uploadDemos": false,
"localLoginEnabled": true,
"redisUrl": "redis://:super_secure@localhost:6379",
"redisTTL": 86400
"redisTTL": 86400,
"queueTTL": 3600
},
"test": {
"driver": "mysql",
Expand Down
16 changes: 16 additions & 0 deletions jest_config/jest.queue.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
process.env.NODE_ENV = "test";
module.exports = {
preset: 'ts-jest/presets/js-with-ts-esm',
resolver: "jest-ts-webcompat-resolver",
clearMocks: true,
globalTeardown: "./test-teardown-globals.cjs",
testEnvironment: "node",
roots: [
"../__test__"
],
testMatch: [
"**/__test__/queue.test.js",
"**/@(queue.)+(spec|test).[tj]s?(x)"
],
verbose: false,
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@
"migrate-drop-prod": "MYSQL_FLAGS=\"-CONNECT_WITH_DB\" db-migrate --env production --config config/production.json db:drop get5",
"migrate-drop-test": "MYSQL_FLAGS=\"-CONNECT_WITH_DB\" db-migrate --env test --config config/test.json db:drop get5test",
"prod": "NODE_ENV=production yarn migrate-create-prod && yarn migrate-prod-upgrade",
"test": "yarn build && NODE_ENV=test && yarn test:setup-user && yarn migrate-drop-test && yarn migrate-create-test && yarn migrate-test-upgrade && yarn test:user && yarn test:gameservers && yarn test:teams && yarn test:matches && yarn test:seasons && yarn test:vetoes && yarn test:mapstats && yarn test:playerstats && yarn test:vetosides && yarn test:maplists",
"test": "yarn build && NODE_ENV=test && yarn test:setup-user && yarn migrate-drop-test && yarn migrate-create-test && yarn migrate-test-upgrade && yarn test:user && yarn test:gameservers && yarn test:teams && yarn test:matches && yarn test:seasons && yarn test:vetoes && yarn test:mapstats && yarn test:playerstats && yarn test:vetosides && yarn test:maplists && yarn test:queue",
"test:gameservers": "yarn test:removeID && NODE_OPTIONS=--experimental-vm-modules jest --testTimeout=10000 --detectOpenHandles --config ./jest_config/jest.gameservers.config.cjs",
"test:mapstats": "NODE_OPTIONS=--experimental-vm-modules jest --testTimeout=10000 --detectOpenHandles --config ./jest_config/jest.mapstats.config.cjs",
"test:maplists": "NODE_OPTIONS=--experimental-vm-modules jest --testTimeout=10000 --detectOpenHandles --config ./jest_config/jest.maplist.config.cjs",
"test:matches": "NODE_OPTIONS=--experimental-vm-modules jest --testTimeout=10000 --detectOpenHandles --config ./jest_config/jest.matches.config.cjs",
"test:queue": "NODE_OPTIONS=--experimental-vm-modules jest --testTimeout=10000 --detectOpenHandles --config ./jest_config/jest.queue.config.cjs",
"test:playerstats": "NODE_OPTIONS=--experimental-vm-modules jest --testTimeout=10000 --detectOpenHandles --config ./jest_config/jest.playerstats.config.cjs",
"test:removeID": "sed -i -e 's.\"steam_ids\": \"[0-9][0-9]*\".\"steam_ids\": \"super_admins,go,here\".g' ./config/test.json",
"test:seasons": "NODE_OPTIONS=--experimental-vm-modules jest --testTimeout=10000 --detectOpenHandles --config ./jest_config/jest.seasons.config.cjs",
Expand Down
Loading