| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- /**
- * End-to-end tests for mini-games scoring flows
- * Tests: 1a2b and Sudoku game completion and API integration
- */
- const { test, expect } = require('@playwright/test');
- test.describe('Mini-Games E2E Tests', () => {
-
- test.describe('1a2b Game', () => {
- test('should complete game and submit score on win', () => {
- // Test flow:
- // 1. Navigate to game page
- // 2. Start game with valid input
- // 3. Complete game by guessing correctly
- // 4. Verify score calculation
- // 5. Verify API call to completeMiniGame
-
- // Mock scenario
- const gameState = {
- answer: '1234',
- userGuess: '1234',
- remainingGuesses: 10,
- score: 100
- };
-
- // Assertions
- expect(gameState.score).toBe(100);
- expect(gameState.remainingGuesses).toBe(10);
- });
- test('should award 100 points on first-guess win', () => {
- // Test immediate win scenario
- const firstGuessScore = 100;
- expect(firstGuessScore).toBe(100);
- });
- test('should calculate score based on guesses used', () => {
- // Score formula: max(100 - guessesUsed * 8, 60)
- const calculateScore = (guessesUsed) => Math.max(100 - guessesUsed * 8, 60);
-
- expect(calculateScore(1)).toBe(92);
- expect(calculateScore(5)).toBe(60);
- expect(calculateScore(10)).toBe(60);
- });
- test('should give minimum score on loss', () => {
- const lossScore = 30;
- expect(lossScore).toBe(30);
- });
- test('should handle missing childId gracefully', () => {
- const childId = null;
- const canSubmit = childId !== null;
- expect(canSubmit).toBe(false);
- });
- });
- test.describe('Sudoku Game', () => {
- test('should calculate base score by difficulty', () => {
- const baseScore = {
- easy: 60,
- medium: 75,
- hard: 90
- };
-
- expect(baseScore.easy).toBe(60);
- expect(baseScore.medium).toBe(75);
- expect(baseScore.hard).toBe(90);
- });
- test('should add time bonus to base score', () => {
- // Time bonus: max(40 - floor(time / 10), 0)
- const calculateTimeBonus = (time) => Math.max(40 - Math.floor(time / 10), 0);
-
- expect(calculateTimeBonus(50)).toBe(35);
- expect(calculateTimeBonus(100)).toBe(30);
- expect(calculateTimeBonus(400)).toBe(0);
- });
- test('should calculate total score correctly', () => {
- const calculateTotalScore = (difficulty, time) => {
- const baseScore = { easy: 60, medium: 75, hard: 90 };
- const base = baseScore[difficulty] || 60;
- const timeBonus = Math.max(40 - Math.floor(time / 10), 0);
- return base + timeBonus;
- };
-
- // Easy, 30 seconds
- expect(calculateTotalScore('easy', 30)).toBe(97);
-
- // Medium, 60 seconds
- expect(calculateTotalScore('medium', 60)).toBe(109);
-
- // Hard, 120 seconds
- expect(calculateTotalScore('hard', 120)).toBe(118);
- });
- test('should submit score via completeMiniGame API', () => {
- // API call verification
- const apiCall = {
- childId: 'child123',
- gameCode: 'sudoku',
- completionTime: 60,
- score: 109
- };
-
- expect(apiCall.gameCode).toBe('sudoku');
- expect(apiCall.score).toBeGreaterThan(60);
- });
- });
- test.describe('API Integration', () => {
- test('should call completeMiniGame with correct parameters', () => {
- // Mock API call
- const mockApiCall = (childId, gameCode, completionTime, score) => {
- return {
- childId,
- gameCode,
- completionTime,
- score,
- timestamp: Date.now()
- };
- };
-
- const result = mockApiCall('child1', 'schulte', 45, 85);
-
- expect(result.childId).toBe('child1');
- expect(result.gameCode).toBe('schulte');
- expect(result.completionTime).toBe(45);
- expect(result.score).toBe(85);
- });
- test('should handle API response correctly', () => {
- // Mock successful response
- const mockResponse = {
- code: 200,
- data: {
- pointsEarned: 8,
- newBalance: 108
- }
- };
-
- expect(mockResponse.code).toBe(200);
- expect(mockResponse.data.pointsEarned).toBe(8);
- expect(mockResponse.data.newBalance).toBe(108);
- });
- test('should handle API error gracefully', () => {
- // Mock error response
- const mockErrorResponse = {
- code: 500,
- message: 'Server error'
- };
-
- expect(mockErrorResponse.code).toBe(500);
- expect(mockErrorResponse.message).toBeDefined();
- });
- });
- test.describe('UI Display', () => {
- test('should display score in result modal', () => {
- const resultModal = {
- showResult: true,
- score: 100,
- time: 30
- };
-
- expect(resultModal.showResult).toBe(true);
- expect(resultModal.score).toBe(100);
- });
- test('should show points earned toast', () => {
- const pointsEarned = 10;
- const toastMessage = `获得${pointsEarned}积分!`;
-
- expect(toastMessage).toContain('10');
- expect(toastMessage).toContain('积分');
- });
- });
- });
|