| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | require dirname(__DIR__).'/lib/PieceColors.php'; |
| 4 | |
| 5 | // Stub query()/db() unused by pure resolve helpers. |
| 6 | $checks=0; |
| 7 | function check(bool $condition,string $label): void {global $checks;$checks++;if(!$condition)throw new RuntimeException($label);} |
| 8 | |
| 9 | function prefs(string $mode,string $preferred,string $alternative): array { |
| 10 | return ['color_mode'=>$mode,'preferred_color'=>$preferred,'alternative_color'=>$alternative]; |
| 11 | } |
| 12 | |
| 13 | // No conflict: both default on opposite rules sides. |
| 14 | $r=resolvePieceSkins(prefs('default','White','Black'),prefs('default','White','Black')); |
| 15 | check($r===['white'=>'White','black'=>'Black'],'Both default keep rules colours'); |
| 16 | |
| 17 | // No conflict: custom preferreds differ. |
| 18 | $r=resolvePieceSkins(prefs('custom','Red','Blue'),prefs('custom','Green','Yellow')); |
| 19 | check($r===['white'=>'Red','black'=>'Green'],'Different custom preferreds'); |
| 20 | |
| 21 | // Default vs custom conflict: default keeps rules colour; custom uses alternative. |
| 22 | $r=resolvePieceSkins(prefs('default','White','Black'),prefs('custom','White','Red')); |
| 23 | check($r===['white'=>'White','black'=>'Red'],'Black custom yields to alternative when conflicting with default White'); |
| 24 | |
| 25 | $r=resolvePieceSkins(prefs('custom','Black','Blue'),prefs('default','White','Black')); |
| 26 | check($r===['white'=>'Blue','black'=>'Black'],'White custom yields to alternative when conflicting with default Black'); |
| 27 | |
| 28 | // Default vs custom with broken alternative equal to default colour: cycle further. |
| 29 | $r=resolvePieceSkins(prefs('default','White','Black'),prefs('custom','White','White')); |
| 30 | // validatePieceColors would reject equal prefs; resolution still cycles if alternative equals. |
| 31 | // Here alternative equals preferred (invalid), but also equals whiteWant; nextDistinct from White skip White -> Black |
| 32 | check($r['white']==='White' && $r['black']!=='White','Cycled alternative when equal to default colour'); |
| 33 | |
| 34 | // Both custom same preferred: coin flip 1 => white keeps, black takes alternative |
| 35 | $r=resolvePieceSkins(prefs('custom','Red','Blue'),prefs('custom','Red','Green'),fn()=>1); |
| 36 | check($r===['white'=>'Red','black'=>'Green'],'Both custom conflict: white keeps when coin=1'); |
| 37 | |
| 38 | $r=resolvePieceSkins(prefs('custom','Red','Blue'),prefs('custom','Red','Green'),fn()=>0); |
| 39 | check($r===['white'=>'Blue','black'=>'Red'],'Both custom conflict: black keeps when coin=0'); |
| 40 | |
| 41 | // Custom preferred matches other side's rules/default without being same mode conflict path already covered. |
| 42 | $r=resolvePieceSkins(prefs('custom','Black','Red'),prefs('custom','White','Blue')); |
| 43 | check($r===['white'=>'Black','black'=>'White'],'Cross-coloured customs with no clash'); |
| 44 | |
| 45 | // effectivePreferred |
| 46 | check(effectivePreferredColor(prefs('default','Red','Blue'),'White')==='White','Default mode uses rules colour'); |
| 47 | check(effectivePreferredColor(prefs('custom','Red','Blue'),'White')==='Red','Custom mode uses preferred'); |
| 48 | |
| 49 | // nextDistinct |
| 50 | check(nextDistinctPieceColor('White','Black')==='Red','Cycle White skip Black -> Red'); |
| 51 | check(nextDistinctPieceColor('Yellow','White')==='Black','Cycle Yellow wrap skip White -> Black'); |
| 52 | check(nextDistinctPieceColor('Red','Blue')==='Green','Cycle Red skip Blue -> Green'); |
| 53 | |
| 54 | // Validation |
| 55 | try {validatePieceColors('nope','White','Black');check(false,'bad mode');} catch(DomainException){check(true,'bad mode rejected');} |
| 56 | try {validatePieceColors('custom','Purple','Black');check(false,'bad colour');} catch(DomainException){check(true,'bad colour rejected');} |
| 57 | try {validatePieceColors('custom','Red','Red');check(false,'same colours');} catch(DomainException){check(true,'same colours rejected');} |
| 58 | validatePieceColors('custom','Red','Blue'); |
| 59 | check(true,'valid custom accepted'); |
| 60 | |
| 61 | echo "Colors: $checks assertions passed.\n"; |
projects-timfalken-com microchess