|
| 1 | +import { Test, TestingModule } from "@nestjs/testing"; |
| 2 | +import { INestApplication } from "@nestjs/common"; |
| 3 | +import { WsAdapter } from "@nestjs/platform-ws"; |
| 4 | +import { AppModule } from "../src/app.module"; |
| 5 | +import { HasuraService } from "../src/hasura/hasura.service"; |
| 6 | +import { TypeSenseService } from "../src/type-sense/type-sense.service"; |
| 7 | +import { SystemService } from "../src/system/system.service"; |
| 8 | +import { DiscordBotService } from "../src/discord-bot/discord-bot.service"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Creates a mock Hasura query result via Proxy. Collections return empty |
| 12 | + * arrays; aggregate queries return count > 0 to skip data-generation |
| 13 | + * bootstrap logic (e.g. MatchesModule.generatePlayerRatings). |
| 14 | + */ |
| 15 | +function createMockQueryResult(): any { |
| 16 | + return new Proxy( |
| 17 | + {}, |
| 18 | + { |
| 19 | + get(_, prop) { |
| 20 | + if (typeof prop === "string") { |
| 21 | + if (prop.includes("aggregate")) { |
| 22 | + return { aggregate: { count: 1 } }; |
| 23 | + } |
| 24 | + return []; |
| 25 | + } |
| 26 | + return undefined; |
| 27 | + }, |
| 28 | + }, |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +export const mockHasuraService = { |
| 33 | + query: jest |
| 34 | + .fn() |
| 35 | + .mockImplementation(() => Promise.resolve(createMockQueryResult())), |
| 36 | + mutation: jest.fn().mockResolvedValue({}), |
| 37 | + setup: jest.fn().mockResolvedValue(undefined), |
| 38 | + checkSecret: jest.fn().mockReturnValue(false), |
| 39 | + getHasuraHeaders: jest.fn().mockResolvedValue({}), |
| 40 | +}; |
| 41 | + |
| 42 | +export const mockTypeSenseService = { |
| 43 | + setup: jest.fn().mockResolvedValue(undefined), |
| 44 | + updatePlayer: jest.fn().mockResolvedValue(undefined), |
| 45 | + removePlayer: jest.fn().mockResolvedValue(undefined), |
| 46 | + upsertCvars: jest.fn().mockResolvedValue(undefined), |
| 47 | + resetCvars: jest.fn().mockResolvedValue(undefined), |
| 48 | + createPlayerCollection: jest.fn().mockResolvedValue(undefined), |
| 49 | + createCvarsCollection: jest.fn().mockResolvedValue(undefined), |
| 50 | +}; |
| 51 | + |
| 52 | +export const mockSystemService = { |
| 53 | + detectFeatures: jest.fn().mockResolvedValue(undefined), |
| 54 | + getSetting: jest |
| 55 | + .fn() |
| 56 | + .mockImplementation((_name, defaultValue) => |
| 57 | + Promise.resolve(defaultValue), |
| 58 | + ), |
| 59 | +}; |
| 60 | + |
| 61 | +export const mockDiscordBotService = { |
| 62 | + setup: jest.fn().mockResolvedValue(undefined), |
| 63 | + login: jest.fn().mockResolvedValue(undefined), |
| 64 | + client: null, |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * Creates a NestJS test application with external services mocked out. |
| 69 | + * |
| 70 | + * Mocked services (not available in test CI): |
| 71 | + * - HasuraService → prevents GraphQL fetches to non-existent Hasura |
| 72 | + * - TypeSenseService → prevents infinite retry loop to typesense:8108 |
| 73 | + * - SystemService → prevents K8s config loading + infinite detectFeatures loop |
| 74 | + * - DiscordBotService → prevents Discord API calls |
| 75 | + * |
| 76 | + * Uses WsAdapter to match src/main.ts bootstrap. |
| 77 | + */ |
| 78 | +export async function createTestApp(): Promise<INestApplication> { |
| 79 | + const moduleFixture: TestingModule = await Test.createTestingModule({ |
| 80 | + imports: [AppModule], |
| 81 | + }) |
| 82 | + .overrideProvider(HasuraService) |
| 83 | + .useValue(mockHasuraService) |
| 84 | + .overrideProvider(TypeSenseService) |
| 85 | + .useValue(mockTypeSenseService) |
| 86 | + .overrideProvider(SystemService) |
| 87 | + .useValue(mockSystemService) |
| 88 | + .overrideProvider(DiscordBotService) |
| 89 | + .useValue(mockDiscordBotService) |
| 90 | + .compile(); |
| 91 | + |
| 92 | + const app = moduleFixture.createNestApplication(); |
| 93 | + app.useWebSocketAdapter(new WsAdapter(app)); |
| 94 | + await app.init(); |
| 95 | + |
| 96 | + return app; |
| 97 | +} |
0 commit comments