BlueGit

projects-timfalken-com microchess

Files

microchess / web / tests / colors.php

colors.php

colors.php 3.6 KiB Download Raw
1<?php
2declare(strict_types=1);
3require dirname(__DIR__).'/lib/PieceColors.php';
4
5// Stub query()/db() unused by pure resolve helpers.
6$checks=0;
7function check(bool $condition,string $label): void {global $checks;$checks++;if(!$condition)throw new RuntimeException($label);}
8
9function 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'));
15check($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'));
19check($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'));
23check($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'));
26check($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
32check($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);
36check($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);
39check($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'));
43check($r===['white'=>'Black','black'=>'White'],'Cross-coloured customs with no clash');
44
45// effectivePreferred
46check(effectivePreferredColor(prefs('default','Red','Blue'),'White')==='White','Default mode uses rules colour');
47check(effectivePreferredColor(prefs('custom','Red','Blue'),'White')==='Red','Custom mode uses preferred');
48
49// nextDistinct
50check(nextDistinctPieceColor('White','Black')==='Red','Cycle White skip Black -> Red');
51check(nextDistinctPieceColor('Yellow','White')==='Black','Cycle Yellow wrap skip White -> Black');
52check(nextDistinctPieceColor('Red','Blue')==='Green','Cycle Red skip Blue -> Green');
53
54// Validation
55try {validatePieceColors('nope','White','Black');check(false,'bad mode');} catch(DomainException){check(true,'bad mode rejected');}
56try {validatePieceColors('custom','Purple','Black');check(false,'bad colour');} catch(DomainException){check(true,'bad colour rejected');}
57try {validatePieceColors('custom','Red','Red');check(false,'same colours');} catch(DomainException){check(true,'same colours rejected');}
58validatePieceColors('custom','Red','Blue');
59check(true,'valid custom accepted');
60
61echo "Colors: $checks assertions passed.\n";