|
1
|
|
|
import { describe, it, expect, beforeEach } from 'vitest'; |
|
2
|
|
|
import { state, resetBoxes, toggleBox, calculateBinaryValue } from '../../../src/core/state'; |
|
3
|
|
|
|
|
4
|
|
|
describe('Disabled Box Logic (Unit Tests)', () => { |
|
5
|
|
|
beforeEach(() => { |
|
6
|
|
|
resetBoxes(); |
|
7
|
|
|
}); |
|
8
|
|
|
|
|
9
|
|
|
describe('2048 Box Disable Conditions', () => { |
|
10
|
|
|
it('should identify when 2048 should be disabled (any other box active)', () => { |
|
11
|
|
|
// No boxes active - 2048 should NOT be disabled |
|
12
|
|
|
const isAnyOtherActive1 = state.boxes.slice(1).some(box => box); |
|
13
|
|
|
expect(isAnyOtherActive1).toBe(false); |
|
14
|
|
|
|
|
15
|
|
|
// Click box 11 (value 1) |
|
16
|
|
|
toggleBox(11); |
|
17
|
|
|
|
|
18
|
|
|
// Now any other box is active - 2048 should be disabled |
|
19
|
|
|
const isAnyOtherActive2 = state.boxes.slice(1).some(box => box); |
|
20
|
|
|
expect(isAnyOtherActive2).toBe(true); |
|
21
|
|
|
}); |
|
22
|
|
|
|
|
23
|
|
|
it('should identify when 2048 should be enabled (no other boxes active)', () => { |
|
24
|
|
|
// Click and unclick box 11 |
|
25
|
|
|
toggleBox(11); |
|
26
|
|
|
toggleBox(11); |
|
27
|
|
|
|
|
28
|
|
|
// No boxes active |
|
29
|
|
|
const isAnyOtherActive = state.boxes.slice(1).some(box => box); |
|
30
|
|
|
expect(isAnyOtherActive).toBe(false); |
|
31
|
|
|
}); |
|
32
|
|
|
|
|
33
|
|
|
it('should correctly identify multiple other boxes active', () => { |
|
34
|
|
|
toggleBox(1); // 1024 |
|
35
|
|
|
toggleBox(2); // 512 |
|
36
|
|
|
toggleBox(11); // 1 |
|
37
|
|
|
|
|
38
|
|
|
const isAnyOtherActive = state.boxes.slice(1).some(box => box); |
|
39
|
|
|
expect(isAnyOtherActive).toBe(true); |
|
40
|
|
|
}); |
|
41
|
|
|
}); |
|
42
|
|
|
|
|
43
|
|
|
describe('Other Boxes Disable Conditions', () => { |
|
44
|
|
|
it('should identify when other boxes should be disabled (2048 active)', () => { |
|
45
|
|
|
// 2048 not active - others should NOT be disabled |
|
46
|
|
|
const is2048Active1 = state.boxes[0]; |
|
47
|
|
|
expect(is2048Active1).toBe(false); |
|
48
|
|
|
|
|
49
|
|
|
// Activate 2048 |
|
50
|
|
|
toggleBox(0); |
|
51
|
|
|
|
|
52
|
|
|
// Now 2048 is active - others should be disabled |
|
53
|
|
|
const is2048Active2 = state.boxes[0]; |
|
54
|
|
|
expect(is2048Active2).toBe(true); |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
it('should identify when other boxes should be enabled (2048 not active)', () => { |
|
58
|
|
|
// Click and unclick 2048 |
|
59
|
|
|
toggleBox(0); |
|
60
|
|
|
toggleBox(0); |
|
61
|
|
|
|
|
62
|
|
|
const is2048Active = state.boxes[0]; |
|
63
|
|
|
expect(is2048Active).toBe(false); |
|
64
|
|
|
}); |
|
65
|
|
|
}); |
|
66
|
|
|
|
|
67
|
|
|
describe('Disable Logic Edge Cases', () => { |
|
68
|
|
|
it('should not disable active boxes themselves', () => { |
|
69
|
|
|
// When box 1024 is active and 2048 gets disabled, |
|
70
|
|
|
// box 1024 itself should remain enabled (isActive check) |
|
71
|
|
|
toggleBox(1); // Activate 1024 |
|
72
|
|
|
|
|
73
|
|
|
const is2048Active = state.boxes[0]; |
|
74
|
|
|
const isBox1024Active = state.boxes[1]; |
|
75
|
|
|
|
|
76
|
|
|
expect(is2048Active).toBe(false); // 2048 not active |
|
77
|
|
|
expect(isBox1024Active).toBe(true); // 1024 is active |
|
78
|
|
|
|
|
79
|
|
|
// Logic: box 1024 should NOT be disabled even though 2048 rule applies |
|
80
|
|
|
// because isActive = true for box 1024 |
|
81
|
|
|
}); |
|
82
|
|
|
|
|
83
|
|
|
it('should handle all boxes active except 2048', () => { |
|
84
|
|
|
// Activate all boxes except first (2048) |
|
85
|
|
|
for (let i = 1; i < 12; i++) { |
|
86
|
|
|
toggleBox(i); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
const is2048Active = state.boxes[0]; |
|
90
|
|
|
const isAnyOtherActive = state.boxes.slice(1).some(box => box); |
|
91
|
|
|
|
|
92
|
|
|
expect(is2048Active).toBe(false); |
|
93
|
|
|
expect(isAnyOtherActive).toBe(true); |
|
94
|
|
|
|
|
95
|
|
|
// 2048 should be disabled |
|
96
|
|
|
const shouldDisable2048 = isAnyOtherActive && !is2048Active; |
|
97
|
|
|
expect(shouldDisable2048).toBe(true); |
|
98
|
|
|
}); |
|
99
|
|
|
|
|
100
|
|
|
it('should handle only 2048 active', () => { |
|
101
|
|
|
toggleBox(0); // Only 2048 |
|
102
|
|
|
|
|
103
|
|
|
const is2048Active = state.boxes[0]; |
|
104
|
|
|
const isAnyOtherActive = state.boxes.slice(1).some(box => box); |
|
105
|
|
|
|
|
106
|
|
|
expect(is2048Active).toBe(true); |
|
107
|
|
|
expect(isAnyOtherActive).toBe(false); |
|
108
|
|
|
|
|
109
|
|
|
// All other boxes should be disabled (except themselves if active) |
|
110
|
|
|
for (let i = 1; i < 12; i++) { |
|
111
|
|
|
const isActive = state.boxes[i]; |
|
112
|
|
|
const shouldDisable = is2048Active && !isActive; |
|
113
|
|
|
expect(shouldDisable).toBe(true); |
|
114
|
|
|
} |
|
115
|
|
|
}); |
|
116
|
|
|
}); |
|
117
|
|
|
|
|
118
|
|
|
describe('Value Range Prevention', () => { |
|
119
|
|
|
it('should never exceed 2048 with disabled logic', () => { |
|
120
|
|
|
// Clicking 2048 alone = 2048 (valid) |
|
121
|
|
|
toggleBox(0); |
|
122
|
|
|
expect(calculateBinaryValue()).toBe(2048); |
|
123
|
|
|
|
|
124
|
|
|
// Can't click others when 2048 is active (disabled logic prevents it) |
|
125
|
|
|
// So value will never exceed 2048 |
|
126
|
|
|
}); |
|
127
|
|
|
|
|
128
|
|
|
it('should prevent 2048 + any other combination', () => { |
|
129
|
|
|
// If somehow both were active (shouldn't happen with disabled logic) |
|
130
|
|
|
state.boxes[0] = true; // 2048 |
|
131
|
|
|
state.boxes[11] = true; // 1 |
|
132
|
|
|
|
|
133
|
|
|
const value = calculateBinaryValue(); |
|
134
|
|
|
expect(value).toBe(2049); // Would be out of range |
|
135
|
|
|
|
|
136
|
|
|
// But disabled logic prevents this state in UI |
|
137
|
|
|
}); |
|
138
|
|
|
|
|
139
|
|
|
it('should allow maximum combination without 2048', () => { |
|
140
|
|
|
// All boxes except 2048 |
|
141
|
|
|
for (let i = 1; i < 12; i++) { |
|
142
|
|
|
toggleBox(i); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
const value = calculateBinaryValue(); |
|
146
|
|
|
expect(value).toBe(2047); // Maximum without 2048 |
|
147
|
|
|
expect(value).toBeLessThanOrEqual(2048); |
|
148
|
|
|
}); |
|
149
|
|
|
}); |
|
150
|
|
|
}); |
|
151
|
|
|
|