|
| 1 | +import { GameTree, GameNode } from 'src/types' |
| 2 | +import { Chess } from 'chess.ts' |
| 3 | + |
| 4 | +/** |
| 5 | + * Test to verify that evaluation chart generation starts from the correct position |
| 6 | + * This test validates the fix for issue #118 where the position evaluation graph |
| 7 | + * was showing pre-opening moves that the player didn't actually play. |
| 8 | + */ |
| 9 | +describe('useOpeningDrillController evaluation chart generation', () => { |
| 10 | + // Helper function to simulate the extractNodeAnalysis logic |
| 11 | + const extractNodeAnalysisFromPosition = ( |
| 12 | + startingNode: GameNode, |
| 13 | + playerColor: 'white' | 'black', |
| 14 | + ) => { |
| 15 | + const moveAnalyses: Array<{ |
| 16 | + move: string |
| 17 | + san: string |
| 18 | + fen: string |
| 19 | + isPlayerMove: boolean |
| 20 | + evaluation: number |
| 21 | + moveNumber: number |
| 22 | + }> = [] |
| 23 | + const evaluationChart: Array<{ |
| 24 | + moveNumber: number |
| 25 | + evaluation: number |
| 26 | + isPlayerMove: boolean |
| 27 | + }> = [] |
| 28 | + |
| 29 | + const extractNodeAnalysis = ( |
| 30 | + node: GameNode, |
| 31 | + path: GameNode[] = [], |
| 32 | + ): void => { |
| 33 | + const currentPath = [...path, node] |
| 34 | + |
| 35 | + if (node.move && node.san) { |
| 36 | + const moveIndex = currentPath.length - 2 |
| 37 | + const isPlayerMove = |
| 38 | + playerColor === 'white' ? moveIndex % 2 === 0 : moveIndex % 2 === 1 |
| 39 | + |
| 40 | + // Mock evaluation data |
| 41 | + const evaluation = Math.random() * 200 - 100 // Random evaluation between -100 and 100 |
| 42 | + |
| 43 | + const moveAnalysis = { |
| 44 | + move: node.move, |
| 45 | + san: node.san, |
| 46 | + fen: node.fen, |
| 47 | + isPlayerMove, |
| 48 | + evaluation, |
| 49 | + moveNumber: Math.ceil((moveIndex + 1) / 2), |
| 50 | + } |
| 51 | + |
| 52 | + moveAnalyses.push(moveAnalysis) |
| 53 | + |
| 54 | + const evaluationPoint = { |
| 55 | + moveNumber: moveAnalysis.moveNumber, |
| 56 | + evaluation, |
| 57 | + isPlayerMove, |
| 58 | + } |
| 59 | + |
| 60 | + evaluationChart.push(evaluationPoint) |
| 61 | + } |
| 62 | + |
| 63 | + if (node.children.length > 0) { |
| 64 | + extractNodeAnalysis(node.children[0], currentPath) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + extractNodeAnalysis(startingNode) |
| 69 | + return { moveAnalyses, evaluationChart } |
| 70 | + } |
| 71 | + |
| 72 | + it('should start analysis from opening end node rather than game root', () => { |
| 73 | + // Create a game tree representing: 1. e4 e5 2. Nf3 Nc6 (opening) 3. Bb5 a6 (drill moves) |
| 74 | + const chess = new Chess() |
| 75 | + const gameTree = new GameTree(chess.fen()) |
| 76 | + |
| 77 | + // Add opening moves (these should NOT be included in evaluation chart) |
| 78 | + chess.move('e4') |
| 79 | + const e4Node = gameTree.addMainMove( |
| 80 | + gameTree.getRoot(), |
| 81 | + chess.fen(), |
| 82 | + 'e2e4', |
| 83 | + 'e4', |
| 84 | + )! |
| 85 | + |
| 86 | + chess.move('e5') |
| 87 | + const e5Node = gameTree.addMainMove(e4Node, chess.fen(), 'e7e5', 'e5')! |
| 88 | + |
| 89 | + chess.move('Nf3') |
| 90 | + const nf3Node = gameTree.addMainMove(e5Node, chess.fen(), 'g1f3', 'Nf3')! |
| 91 | + |
| 92 | + chess.move('Nc6') |
| 93 | + const nc6Node = gameTree.addMainMove(nf3Node, chess.fen(), 'b8c6', 'Nc6')! // This is the opening end |
| 94 | + |
| 95 | + // Add drill moves (these SHOULD be included in evaluation chart) |
| 96 | + chess.move('Bb5') |
| 97 | + const bb5Node = gameTree.addMainMove(nc6Node, chess.fen(), 'f1b5', 'Bb5')! |
| 98 | + |
| 99 | + chess.move('a6') |
| 100 | + const a6Node = gameTree.addMainMove(bb5Node, chess.fen(), 'a7a6', 'a6')! |
| 101 | + |
| 102 | + // Test starting from game root (old behavior - should include all moves) |
| 103 | + const { moveAnalyses: rootAnalyses, evaluationChart: rootChart } = |
| 104 | + extractNodeAnalysisFromPosition(gameTree.getRoot(), 'white') |
| 105 | + |
| 106 | + // Test starting from opening end (new behavior - should only include drill moves) |
| 107 | + const { |
| 108 | + moveAnalyses: openingEndAnalyses, |
| 109 | + evaluationChart: openingEndChart, |
| 110 | + } = extractNodeAnalysisFromPosition(nc6Node, 'white') |
| 111 | + |
| 112 | + // Verify that starting from root includes all moves (including opening) |
| 113 | + expect(rootAnalyses).toHaveLength(6) // e4, e5, Nf3, Nc6, Bb5, a6 |
| 114 | + expect(rootChart).toHaveLength(6) |
| 115 | + |
| 116 | + // Verify that starting from opening end only includes post-opening moves |
| 117 | + // Note: This includes the last opening move (Nc6) which provides context for the evaluation chart |
| 118 | + expect(openingEndAnalyses).toHaveLength(3) // Nc6 (last opening move), Bb5, a6 |
| 119 | + expect(openingEndChart).toHaveLength(3) |
| 120 | + |
| 121 | + // Verify the moves are correct - the first should be the last opening move, then drill moves |
| 122 | + expect(openingEndAnalyses[0].san).toBe('Nc6') // Last opening move |
| 123 | + expect(openingEndAnalyses[1].san).toBe('Bb5') // First drill move |
| 124 | + expect(openingEndAnalyses[1].isPlayerMove).toBe(true) // White's move |
| 125 | + expect(openingEndAnalyses[2].san).toBe('a6') // Second drill move |
| 126 | + expect(openingEndAnalyses[2].isPlayerMove).toBe(false) // Black's move |
| 127 | + |
| 128 | + // Verify evaluation chart matches move analyses |
| 129 | + expect(openingEndChart[0].moveNumber).toBe(openingEndAnalyses[0].moveNumber) |
| 130 | + expect(openingEndChart[1].moveNumber).toBe(openingEndAnalyses[1].moveNumber) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should handle the case where opening end node is null', () => { |
| 134 | + const chess = new Chess() |
| 135 | + const gameTree = new GameTree(chess.fen()) |
| 136 | + |
| 137 | + // Add some moves |
| 138 | + chess.move('e4') |
| 139 | + const e4Node = gameTree.addMainMove( |
| 140 | + gameTree.getRoot(), |
| 141 | + chess.fen(), |
| 142 | + 'e2e4', |
| 143 | + 'e4', |
| 144 | + )! |
| 145 | + |
| 146 | + // Test with null opening end node (should fallback to root) |
| 147 | + const startingNode = null || gameTree.getRoot() // Simulates the fallback logic |
| 148 | + const { moveAnalyses, evaluationChart } = extractNodeAnalysisFromPosition( |
| 149 | + startingNode, |
| 150 | + 'white', |
| 151 | + ) |
| 152 | + |
| 153 | + expect(moveAnalyses).toHaveLength(1) |
| 154 | + expect(evaluationChart).toHaveLength(1) |
| 155 | + expect(moveAnalyses[0].san).toBe('e4') |
| 156 | + }) |
| 157 | +}) |
0 commit comments