170 lines
4.9 KiB
TypeScript
170 lines
4.9 KiB
TypeScript
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { NotFoundException } from '@nestjs/common';
|
|
import { WechatGameService } from './wechat-game.service';
|
|
import { GameConfigRepository } from './repositories/game-config.repository';
|
|
import { LevelRepository } from './repositories/level.repository';
|
|
import { GameConfig } from './entities/game-config.entity';
|
|
import { Level } from './entities/level.entity';
|
|
|
|
describe('WechatGameService', () => {
|
|
let service: WechatGameService;
|
|
|
|
const mockGameConfig: GameConfig = {
|
|
id: 'config-uuid-1',
|
|
configKey: 'game_speed',
|
|
configValue: '1.5',
|
|
description: 'Game speed multiplier',
|
|
isActive: true,
|
|
createdAt: new Date('2026-01-01'),
|
|
updatedAt: new Date('2026-01-01'),
|
|
};
|
|
|
|
const mockLevel: Level = {
|
|
id: 'level-1',
|
|
imageUrl: 'https://example.com/meme1.jpg',
|
|
answer: '答案一',
|
|
hint1: '提示1',
|
|
hint2: '提示2',
|
|
hint3: null,
|
|
sortOrder: 0,
|
|
timeLimit: 60,
|
|
createdAt: new Date('2026-01-01'),
|
|
updatedAt: new Date('2026-01-01'),
|
|
};
|
|
|
|
const mockLevel2: Level = {
|
|
id: 'level-2',
|
|
imageUrl: 'https://example.com/meme2.jpg',
|
|
answer: '答案二',
|
|
hint1: '提示A',
|
|
hint2: null,
|
|
hint3: null,
|
|
sortOrder: 1,
|
|
timeLimit: null,
|
|
createdAt: new Date('2026-01-01'),
|
|
updatedAt: new Date('2026-01-01'),
|
|
};
|
|
|
|
const mockGameConfigRepository = {
|
|
findActiveConfigs: jest.fn(),
|
|
findByKey: jest.fn(),
|
|
};
|
|
|
|
const mockLevelRepository = {
|
|
findAllOrdered: jest.fn(),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
WechatGameService,
|
|
{ provide: GameConfigRepository, useValue: mockGameConfigRepository },
|
|
{ provide: LevelRepository, useValue: mockLevelRepository },
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<WechatGameService>(WechatGameService);
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
describe('getAllConfigs', () => {
|
|
it('should return all active configs', async () => {
|
|
mockGameConfigRepository.findActiveConfigs.mockResolvedValue([
|
|
mockGameConfig,
|
|
]);
|
|
|
|
const result = await service.getAllConfigs();
|
|
|
|
expect(result.configs).toHaveLength(1);
|
|
expect(result.total).toBe(1);
|
|
expect(result.configs[0].configKey).toBe('game_speed');
|
|
expect(result.configs[0].configValue).toBe('1.5');
|
|
});
|
|
|
|
it('should return empty array when no configs found', async () => {
|
|
mockGameConfigRepository.findActiveConfigs.mockResolvedValue([]);
|
|
|
|
const result = await service.getAllConfigs();
|
|
|
|
expect(result.configs).toHaveLength(0);
|
|
expect(result.total).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('getConfigByKey', () => {
|
|
it('should return config by key', async () => {
|
|
mockGameConfigRepository.findByKey.mockResolvedValue(mockGameConfig);
|
|
|
|
const result = await service.getConfigByKey('game_speed');
|
|
|
|
expect(result.configKey).toBe('game_speed');
|
|
expect(result.configValue).toBe('1.5');
|
|
expect(mockGameConfigRepository.findByKey).toHaveBeenCalledWith(
|
|
'game_speed',
|
|
);
|
|
});
|
|
|
|
it('should throw NotFoundException when config not found', async () => {
|
|
mockGameConfigRepository.findByKey.mockResolvedValue(null);
|
|
|
|
await expect(service.getConfigByKey('nonexistent')).rejects.toThrow(
|
|
NotFoundException,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('getAllLevels', () => {
|
|
it('should return all levels with 1-indexed level numbers', async () => {
|
|
mockLevelRepository.findAllOrdered.mockResolvedValue([
|
|
mockLevel,
|
|
mockLevel2,
|
|
]);
|
|
|
|
const result = await service.getAllLevels();
|
|
|
|
expect(result.levels).toHaveLength(2);
|
|
expect(result.total).toBe(2);
|
|
expect(result.levels[0].level).toBe(1);
|
|
expect(result.levels[0].id).toBe('level-1');
|
|
expect(result.levels[0].answer).toBe('答案一');
|
|
expect(result.levels[1].level).toBe(2);
|
|
expect(result.levels[1].id).toBe('level-2');
|
|
});
|
|
|
|
it('should return empty array when no levels exist', async () => {
|
|
mockLevelRepository.findAllOrdered.mockResolvedValue([]);
|
|
|
|
const result = await service.getAllLevels();
|
|
|
|
expect(result.levels).toHaveLength(0);
|
|
expect(result.total).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('getLevelById', () => {
|
|
it('should return level with correct level number', async () => {
|
|
mockLevelRepository.findAllOrdered.mockResolvedValue([
|
|
mockLevel,
|
|
mockLevel2,
|
|
]);
|
|
|
|
const result = await service.getLevelById('level-2');
|
|
|
|
expect(result.id).toBe('level-2');
|
|
expect(result.level).toBe(2);
|
|
expect(result.answer).toBe('答案二');
|
|
});
|
|
|
|
it('should throw NotFoundException when level not found', async () => {
|
|
mockLevelRepository.findAllOrdered.mockResolvedValue([mockLevel]);
|
|
|
|
await expect(service.getLevelById('nonexistent')).rejects.toThrow(
|
|
NotFoundException,
|
|
);
|
|
});
|
|
});
|
|
});
|