|
1 | | -import { updateCount } from "./updateGlobalCounts"; |
2 | | -import redis from "redis-mock"; |
3 | | - |
4 | | -describe("updateCounts", () => { |
5 | | - test("create a new count", async () => { |
6 | | - const redisClient = redis.createClient(); |
7 | | - |
8 | | - await updateCount({ |
9 | | - redisClient, |
10 | | - getNewEntities: async () => [{ createdAt: new Date() }], |
11 | | - lastUpdatedAtKey: "test-0-lastUpdatedAt", |
12 | | - countKey: "test-0-count", |
13 | | - }); |
14 | | - |
15 | | - expect(await redisClient.get("test-0-count")).toBe("1"); |
16 | | - expect(await redisClient.get("test-0-lastUpdatedAt")).toBe(new Date().toISOString()); |
17 | | - }); |
18 | | - |
19 | | - test("update an existing count", async () => { |
20 | | - const redisClient = redis.createClient(); |
21 | | - |
22 | | - await redisClient.set("test-1-count", "1"); |
23 | | - await redisClient.set("test-1-lastUpdatedAt", new Date().toISOString()); |
24 | | - |
25 | | - await updateCount({ |
26 | | - redisClient, |
27 | | - getNewEntities: async () => [{ createdAt: new Date() }], |
28 | | - lastUpdatedAtKey: "test-1-lastUpdatedAt", |
29 | | - countKey: "test-1-count", |
30 | | - }); |
31 | | - |
32 | | - expect(await redisClient.get("test-1-count")).toBe("2"); |
33 | | - expect(await redisClient.get("test-1-lastUpdatedAt")).toBe(new Date().toISOString()); |
34 | | - }); |
| 1 | +import { updateClansCount, updateGameServersCount, updateGameTypesCount, updateMapsCount, updatePlayersCount } from "./updateGlobalCounts"; |
| 2 | +import Redis from "ioredis-mock"; |
| 3 | +import { prismaMock } from "../../test/mockPrisma"; |
| 4 | +import { Clan, GameServer, GameType, Map, Player, RankMethod } from "@prisma/client"; |
| 5 | +import { getGlobalCounts } from "@teerank/teerank"; |
| 6 | + |
| 7 | +const mockPlayer: Player = { |
| 8 | + createdAt: new Date(), |
| 9 | + name: "test", |
| 10 | + updatedAt: new Date(), |
| 11 | + lastSeenAt: new Date(), |
| 12 | + clanName: "test", |
| 13 | + playTime: BigInt(1), |
| 14 | +} |
| 15 | + |
| 16 | +const mockClan: Clan = { |
| 17 | + createdAt: new Date(), |
| 18 | + name: "test", |
| 19 | + updatedAt: new Date(), |
| 20 | + playTime: BigInt(1), |
| 21 | + activePlayerCount: 1, |
| 22 | +} |
| 23 | + |
| 24 | +const mockMap: Map = { |
| 25 | + createdAt: new Date(), |
| 26 | + name: "test", |
| 27 | + playTime: BigInt(1), |
| 28 | + playerCount: 1, |
| 29 | + clanCount: 1, |
| 30 | + gameServerCount: 1, |
| 31 | + id: 1, |
| 32 | + gameTypeName: "test", |
| 33 | +} |
| 34 | + |
| 35 | +const mockGameType: GameType = { |
| 36 | + createdAt: new Date(), |
| 37 | + name: "test", |
| 38 | + playTime: BigInt(1), |
| 39 | + playerCount: 1, |
| 40 | + clanCount: 1, |
| 41 | + gameServerCount: 1, |
| 42 | + rankMethod: RankMethod.ELO, |
| 43 | + mapCount: 1, |
| 44 | +} |
| 45 | + |
| 46 | +const mockGameServer: GameServer = { |
| 47 | + createdAt: new Date(), |
| 48 | + updatedAt: new Date(), |
| 49 | + lastSeenAt: new Date(), |
| 50 | + playTime: BigInt(1), |
| 51 | + failureCount: 1, |
| 52 | + ip: "127.0.0.1", |
| 53 | + port: 1, |
| 54 | + masterServerId: 1, |
| 55 | + id: 1, |
| 56 | +} |
| 57 | + |
| 58 | +const redis = new Redis(); |
| 59 | + |
| 60 | +beforeEach(async () => { |
| 61 | + await redis.flushall(); |
| 62 | +}); |
| 63 | + |
| 64 | +test("updatePlayersCount", async () => { |
| 65 | + prismaMock.player.findMany.mockResolvedValue([mockPlayer]); |
| 66 | + |
| 67 | + await updatePlayersCount(redis, new Date(0)); |
| 68 | + await updatePlayersCount(redis, new Date(0)); |
| 69 | + |
| 70 | + const globalCounts = await getGlobalCounts(redis); |
| 71 | + expect(globalCounts.players).toBe(2); |
| 72 | +}); |
| 73 | + |
| 74 | +test("updateClansCount", async () => { |
| 75 | + prismaMock.clan.findMany.mockResolvedValue([mockClan]); |
| 76 | + |
| 77 | + await updateClansCount(redis, new Date(0)); |
| 78 | + await updateClansCount(redis, new Date(0)); |
| 79 | + |
| 80 | + const globalCounts = await getGlobalCounts(redis); |
| 81 | + expect(globalCounts.clans).toBe(2); |
| 82 | +}); |
| 83 | + |
| 84 | +test("updateGameServersCount", async () => { |
| 85 | + prismaMock.gameServer.findMany.mockResolvedValue([mockGameServer]); |
| 86 | + |
| 87 | + await updateGameServersCount(redis, new Date(0)); |
| 88 | + await updateGameServersCount(redis, new Date(0)); |
| 89 | + |
| 90 | + const globalCounts = await getGlobalCounts(redis); |
| 91 | + expect(globalCounts.gameServers).toBe(2); |
| 92 | +}); |
| 93 | + |
| 94 | +test("updateMapsCount", async () => { |
| 95 | + prismaMock.map.findMany.mockResolvedValue([mockMap]); |
| 96 | + |
| 97 | + await updateMapsCount(redis, new Date(0)); |
| 98 | + await updateMapsCount(redis, new Date(0)); |
| 99 | + |
| 100 | + const globalCounts = await getGlobalCounts(redis); |
| 101 | + expect(globalCounts.maps).toBe(2); |
| 102 | +}); |
| 103 | + |
| 104 | +test("updateGameTypesCount", async () => { |
| 105 | + prismaMock.gameType.findMany.mockResolvedValue([mockGameType]); |
| 106 | + |
| 107 | + await updateGameTypesCount(redis, new Date(0)); |
| 108 | + await updateGameTypesCount(redis, new Date(0)); |
| 109 | + |
| 110 | + const globalCounts = await getGlobalCounts(redis); |
| 111 | + expect(globalCounts.gameTypes).toBe(2); |
35 | 112 | }); |
0 commit comments