File size: 9,055 Bytes
100a6dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { describe, it, expect, vi, beforeEach } from 'vitest';
import axios from 'axios';
import {
  makeMove,
  isPromotionRequired,
  getPromotionRequirement,
  createPromotionMove,
  type GameResponse,
  type PromotionRequirement,
  type GameState
} from './api';

// Mock axios
vi.mock('axios');
const mockedAxios = vi.mocked(axios);

describe('API Service - Promotion Support', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  const mockGameState: GameState = {
    fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
    turn: 'white',
    game_state: 'active',
    legal_moves: ['e2e4', 'e2e3'],
    in_check: false,
    move_count: 1
  };

  describe('makeMove function', () => {
    it('should make a normal move successfully', async () => {
      const mockResponse: GameResponse = {
        status: 'success',
        board_state: mockGameState,
        player_move: 'e2e4'
      };

      mockedAxios.post.mockResolvedValueOnce({ data: mockResponse });

      const result = await makeMove('e2e4');

      expect(mockedAxios.post).toHaveBeenCalledWith('/api/game/move', { move: 'e2e4' });
      expect(result).toEqual(mockResponse);
    });

    it('should make a promotion move with piece specified', async () => {
      const mockResponse: GameResponse = {
        status: 'success',
        board_state: mockGameState,
        player_move: 'e7e8q'
      };

      mockedAxios.post.mockResolvedValueOnce({ data: mockResponse });

      const result = await makeMove('e7e8', 'queen');

      expect(mockedAxios.post).toHaveBeenCalledWith('/api/game/move', { move: 'e7e8q' });
      expect(result).toEqual(mockResponse);
    });

    it('should handle promotion required error response', async () => {
      const errorResponse = {
        status: 'promotion_required',
        message: 'Pawn promotion requires piece selection',
        board_state: mockGameState,
        promotion_details: {
          from: 'e7',
          to: 'e8',
          available_pieces: ['queen', 'rook', 'bishop', 'knight']
        }
      };

      const axiosError = {
        isAxiosError: true,
        response: {
          status: 400,
          data: errorResponse
        }
      };

      mockedAxios.post.mockRejectedValueOnce(axiosError);
      mockedAxios.isAxiosError.mockReturnValueOnce(true);

      const result = await makeMove('e7e8');

      expect(result.status).toBe('promotion_required');
      expect(result.promotion_required).toEqual({
        from: 'e7',
        to: 'e8',
        available_pieces: ['queen', 'rook', 'bishop', 'knight']
      });
    });

    it('should handle promotion required error with minimal data', async () => {
      const errorResponse = {
        status: 'promotion_required',
        message: 'Pawn promotion requires piece selection'
      };

      const axiosError = {
        isAxiosError: true,
        response: {
          status: 400,
          data: errorResponse
        }
      };

      mockedAxios.post.mockRejectedValueOnce(axiosError);
      mockedAxios.isAxiosError.mockReturnValueOnce(true);

      const result = await makeMove('e7e8');

      expect(result.status).toBe('promotion_required');
      expect(result.promotion_required).toEqual({
        from: 'e7',
        to: 'e8',
        available_pieces: ['queen', 'rook', 'bishop', 'knight']
      });
    });

    it('should re-throw non-promotion errors', async () => {
      const axiosError = {
        isAxiosError: true,
        response: {
          status: 500,
          data: { message: 'Internal server error' }
        }
      };

      mockedAxios.post.mockRejectedValueOnce(axiosError);
      mockedAxios.isAxiosError.mockReturnValueOnce(true);

      await expect(makeMove('e2e4')).rejects.toEqual(axiosError);
    });

    it('should re-throw non-axios errors', async () => {
      const networkError = new Error('Network error');

      mockedAxios.post.mockRejectedValueOnce(networkError);
      mockedAxios.isAxiosError.mockReturnValueOnce(false);

      await expect(makeMove('e2e4')).rejects.toEqual(networkError);
    });

    it('should handle different promotion pieces correctly', async () => {
      const testCases = [
        { piece: 'queen', expected: 'e7e8q' },
        { piece: 'rook', expected: 'e7e8r' },
        { piece: 'bishop', expected: 'e7e8b' },
        { piece: 'knight', expected: 'e7e8n' }
      ] as const;

      for (const testCase of testCases) {
        const mockResponse: GameResponse = {
          status: 'success',
          board_state: mockGameState,
          player_move: testCase.expected
        };

        mockedAxios.post.mockResolvedValueOnce({ data: mockResponse });

        await makeMove('e7e8', testCase.piece);

        expect(mockedAxios.post).toHaveBeenCalledWith('/api/game/move', { move: testCase.expected });
      }
    });
  });

  describe('isPromotionRequired function', () => {
    it('should return true when promotion is required', () => {
      const response: GameResponse = {
        status: 'promotion_required',
        board_state: mockGameState,
        promotion_required: {
          from: 'e7',
          to: 'e8',
          available_pieces: ['queen', 'rook', 'bishop', 'knight']
        }
      };

      expect(isPromotionRequired(response)).toBe(true);
    });

    it('should return false when promotion is not required', () => {
      const response: GameResponse = {
        status: 'success',
        board_state: mockGameState
      };

      expect(isPromotionRequired(response)).toBe(false);
    });

    it('should return false when status is promotion_required but no promotion_required field', () => {
      const response: GameResponse = {
        status: 'promotion_required',
        board_state: mockGameState
      };

      expect(isPromotionRequired(response)).toBe(false);
    });
  });

  describe('getPromotionRequirement function', () => {
    it('should return promotion requirement when present', () => {
      const promotionReq: PromotionRequirement = {
        from: 'e7',
        to: 'e8',
        available_pieces: ['queen', 'rook', 'bishop', 'knight']
      };

      const response: GameResponse = {
        status: 'promotion_required',
        board_state: mockGameState,
        promotion_required: promotionReq
      };

      expect(getPromotionRequirement(response)).toEqual(promotionReq);
    });

    it('should return null when no promotion requirement', () => {
      const response: GameResponse = {
        status: 'success',
        board_state: mockGameState
      };

      expect(getPromotionRequirement(response)).toBeNull();
    });
  });

  describe('createPromotionMove function', () => {
    it('should create correct UCI notation for different pieces', () => {
      expect(createPromotionMove('e7', 'e8', 'queen')).toBe('e7e8q');
      expect(createPromotionMove('e7', 'e8', 'rook')).toBe('e7e8r');
      expect(createPromotionMove('e7', 'e8', 'bishop')).toBe('e7e8b');
      expect(createPromotionMove('e7', 'e8', 'knight')).toBe('e7e8n');
    });

    it('should handle different squares correctly', () => {
      expect(createPromotionMove('a7', 'a8', 'queen')).toBe('a7a8q');
      expect(createPromotionMove('h2', 'h1', 'rook')).toBe('h2h1r');
      expect(createPromotionMove('d7', 'd8', 'bishop')).toBe('d7d8b');
    });
  });

  describe('Integration scenarios', () => {
    it('should handle complete promotion flow', async () => {
      // First call - promotion required
      const promotionRequiredError = {
        isAxiosError: true,
        response: {
          status: 400,
          data: {
            status: 'promotion_required',
            message: 'Pawn promotion requires piece selection',
            promotion_details: {
              from: 'e7',
              to: 'e8',
              available_pieces: ['queen', 'rook', 'bishop', 'knight']
            }
          }
        }
      };

      mockedAxios.post.mockRejectedValueOnce(promotionRequiredError);
      mockedAxios.isAxiosError.mockReturnValueOnce(true);

      const firstResult = await makeMove('e7e8');
      expect(isPromotionRequired(firstResult)).toBe(true);

      const promotionReq = getPromotionRequirement(firstResult);
      expect(promotionReq).toBeTruthy();
      expect(promotionReq!.available_pieces).toContain('queen');

      // Second call - with promotion piece
      const successResponse: GameResponse = {
        status: 'success',
        board_state: mockGameState,
        player_move: 'e7e8q'
      };

      mockedAxios.post.mockResolvedValueOnce({ data: successResponse });

      const secondResult = await makeMove('e7e8', 'queen');
      expect(secondResult.status).toBe('success');
      expect(secondResult.player_move).toBe('e7e8q');
    });
  });
});