|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { PostgreSqlContainer, StartedPostgreSqlContainer } from '@testcontainers/postgresql'; |
| 3 | +import { getRepositoryToken, TypeOrmModule } from '@nestjs/typeorm'; |
| 4 | +import { INestApplication } from '@nestjs/common'; |
| 5 | +import { Constructor, CqrsModule, EventBus } from '@nestjs/cqrs'; |
| 6 | +import { DataSource, ObjectLiteral, Repository } from 'typeorm'; |
| 7 | +import { EntityClassOrSchema } from '@nestjs/typeorm/dist/interfaces/entity-class-or-schema.type'; |
| 8 | +import { Entities } from 'util/typeorm-config'; |
| 9 | +import { ClientsModule, RedisOptions, RmqOptions, Transport } from '@nestjs/microservices'; |
| 10 | +import { RedisContainer, StartedRedisContainer } from '@testcontainers/redis'; |
| 11 | +import { FantasyFunction1739896254921 } from 'database/migrations/1739896254921-FantasyFunction'; |
| 12 | +import { AppService } from 'app.service'; |
| 13 | +import { MetaService } from 'rest/meta/meta.service'; |
| 14 | +import { MatchService } from 'rest/match/match.service'; |
| 15 | +import { PlayerService } from 'rest/service/player.service'; |
| 16 | +import { MatchMapper } from 'rest/match/match.mapper'; |
| 17 | +import { MetaMapper } from 'rest/meta/meta.mapper'; |
| 18 | +import { InfoMapper } from 'rest/info/info.mapper'; |
| 19 | +import { InfoService } from 'rest/info/info.service'; |
| 20 | +import { MmrBucketService } from 'gameserver/mmr-bucket.service'; |
| 21 | +import { Mapper } from 'rest/mapper'; |
| 22 | +import { GameServerDomain } from 'gameserver'; |
| 23 | +import { ConfigModule } from '@nestjs/config'; |
| 24 | +import { RabbitMQContainer, StartedRabbitMQContainer } from '@testcontainers/rabbitmq'; |
| 25 | +import { WinstonWrapper } from 'util/logger'; |
| 26 | +import { CoreController } from 'core.controller'; |
| 27 | +import { RmqController } from 'rmq.controller'; |
| 28 | +import { QueryController } from 'query.controller'; |
| 29 | +import { MatchController } from 'rest/match/match.controller'; |
| 30 | +import { PlayerController } from 'rest/player.controller'; |
| 31 | +import { InfoController } from 'rest/info/info.controller'; |
| 32 | +import { MetaController } from 'rest/meta/meta.controller'; |
| 33 | +import { CrimeController } from 'rest/crime/crime.controller'; |
| 34 | +import SpyInstance = jest.SpyInstance; |
| 35 | + |
| 36 | +export interface TestEnvironment { |
| 37 | + module: TestingModule; |
| 38 | + app: INestApplication; |
| 39 | + containers: { |
| 40 | + pg: StartedPostgreSqlContainer; |
| 41 | + redis: StartedRedisContainer; |
| 42 | + rabbit: StartedRabbitMQContainer; |
| 43 | + }; |
| 44 | + ebus: EventBus; |
| 45 | + ebusSpy: SpyInstance; |
| 46 | + queryMocks: Record<string, jest.Mock>; |
| 47 | + |
| 48 | + service<R>(c: Constructor<R>): R; |
| 49 | + |
| 50 | + repo<R extends ObjectLiteral>(c: EntityClassOrSchema): Repository<R>; |
| 51 | +} |
| 52 | + |
| 53 | +export function useFullModule(): TestEnvironment { |
| 54 | + jest.setTimeout(120_000); |
| 55 | + |
| 56 | + const te: TestEnvironment = { |
| 57 | + module: undefined as unknown as any, |
| 58 | + containers: {} as unknown as any, |
| 59 | + ebus: {} as unknown as any, |
| 60 | + ebusSpy: {} as unknown as any, |
| 61 | + app: {} as unknown as any, |
| 62 | + service: {} as unknown as any, |
| 63 | + repo: {} as unknown as any, |
| 64 | + |
| 65 | + queryMocks: {}, |
| 66 | + }; |
| 67 | + |
| 68 | + afterEach(() => { |
| 69 | + te.ebusSpy.mockReset(); |
| 70 | + }); |
| 71 | + |
| 72 | + beforeAll(async () => { |
| 73 | + te.containers.pg = await new PostgreSqlContainer() |
| 74 | + .withUsername("username") |
| 75 | + .withPassword("password") |
| 76 | + .start(); |
| 77 | + |
| 78 | + te.containers.redis = await new RedisContainer() |
| 79 | + .withPassword("redispass") |
| 80 | + .start(); |
| 81 | + |
| 82 | + te.containers.rabbit = await new RabbitMQContainer() |
| 83 | + .start(); |
| 84 | + |
| 85 | + te.queryMocks = {}; |
| 86 | + |
| 87 | + te.module = await Test.createTestingModule({ |
| 88 | + imports: [ |
| 89 | + await ConfigModule.forRoot({ |
| 90 | + isGlobal: true |
| 91 | + }), |
| 92 | + CqrsModule.forRoot(), |
| 93 | + TypeOrmModule.forRoot({ |
| 94 | + host: te.containers.pg.getHost(), |
| 95 | + port: te.containers.pg.getFirstMappedPort(), |
| 96 | + |
| 97 | + type: "postgres", |
| 98 | + database: "postgres", |
| 99 | + // logging: true, |
| 100 | + |
| 101 | + username: te.containers.pg.getUsername(), |
| 102 | + password: te.containers.pg.getPassword(), |
| 103 | + entities: Entities, |
| 104 | + synchronize: true, |
| 105 | + dropSchema: true, |
| 106 | + ssl: false, |
| 107 | + }), |
| 108 | + TypeOrmModule.forFeature(Entities), |
| 109 | + ClientsModule.registerAsync([ |
| 110 | + { |
| 111 | + name: "QueryCore", |
| 112 | + useFactory(): RedisOptions { |
| 113 | + return { |
| 114 | + transport: Transport.REDIS, |
| 115 | + options: { |
| 116 | + port: te.containers.redis.getPort(), |
| 117 | + host: te.containers.redis.getHost(), |
| 118 | + password: te.containers.redis.getPassword(), |
| 119 | + }, |
| 120 | + }; |
| 121 | + }, |
| 122 | + inject: [], |
| 123 | + imports: [], |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "RMQ", |
| 127 | + useFactory(): RmqOptions { |
| 128 | + return { |
| 129 | + transport: Transport.RMQ, |
| 130 | + options: { |
| 131 | + urls: [ |
| 132 | + { |
| 133 | + hostname: te.containers.rabbit.getHost(), |
| 134 | + port: te.containers.rabbit.getFirstMappedPort(), |
| 135 | + protocol: "amqp", |
| 136 | + // username: te.containers.rabbit.getName(), |
| 137 | + // password: te.containers.rabbit.get(), |
| 138 | + }, |
| 139 | + ], |
| 140 | + queue: 'gameserver_commands', |
| 141 | + queueOptions: { |
| 142 | + durable: true, |
| 143 | + }, |
| 144 | + prefetchCount: 5, |
| 145 | + }, |
| 146 | + }; |
| 147 | + }, |
| 148 | + inject: [], |
| 149 | + imports: [], |
| 150 | + } |
| 151 | + ]), |
| 152 | + ], |
| 153 | + providers: [ |
| 154 | + AppService, |
| 155 | + MetaService, |
| 156 | + MatchService, |
| 157 | + PlayerService, |
| 158 | + MatchMapper, |
| 159 | + MetaMapper, |
| 160 | + InfoMapper, |
| 161 | + InfoService, |
| 162 | + MmrBucketService, |
| 163 | + Mapper, |
| 164 | + ...GameServerDomain, |
| 165 | + ], |
| 166 | + controllers: [ |
| 167 | + CoreController, |
| 168 | + RmqController, |
| 169 | + QueryController, |
| 170 | + MatchController, |
| 171 | + PlayerController, |
| 172 | + InfoController, |
| 173 | + MetaController, |
| 174 | + CrimeController, |
| 175 | + ] |
| 176 | + }).compile(); |
| 177 | + |
| 178 | + te.app = await te.module.createNestApplication({ |
| 179 | + logger: new WinstonWrapper( |
| 180 | + 'localhost', |
| 181 | + 7777, |
| 182 | + true, |
| 183 | + ), |
| 184 | + }); |
| 185 | + |
| 186 | + await te.app.listen(0) |
| 187 | + |
| 188 | + te.service = (con) => te.module.get(con); |
| 189 | + te.repo = (con) => te.module.get(getRepositoryToken(con)); |
| 190 | + te.ebus = te.module.get(EventBus); |
| 191 | + te.ebusSpy = jest.spyOn(te.ebus, "publish"); |
| 192 | + |
| 193 | + await new FantasyFunction1739896254921().up( |
| 194 | + te.service(DataSource).createQueryRunner(), |
| 195 | + ); |
| 196 | + console.log("Created fantasy score function") |
| 197 | + |
| 198 | + // Mocks: |
| 199 | + }); |
| 200 | + |
| 201 | + afterAll(async () => { |
| 202 | + await te.app.close(); |
| 203 | + await te.containers.pg.stop(); |
| 204 | + await te.containers.redis.stop(); |
| 205 | + }); |
| 206 | + |
| 207 | + return te; |
| 208 | +} |
| 209 | + |
| 210 | +export function testUser(): string { |
| 211 | + return Math.round(Math.random() * 1000000).toString(); |
| 212 | +} |
| 213 | + |
| 214 | +export async function sleep(ms: number) { |
| 215 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 216 | +} |
0 commit comments