1 // Copyright 2020-2021 Ian Jackson and contributors to Otter
2 // SPDX-License-Identifier: AGPL-3.0-or-later
3 // There is NO WARRANTY.
13 usual_wanted_tests!{Ctx, su}
15 const ALICE: &str = "1#1";
18 pub fn player_dasharray(player: &'static str) -> String {
19 let player: PlayerId = player.try_into().context(player)?;
20 let player: slotmap::KeyData = player.into();
21 let (player,_) = player.get_idx_version();
22 let player: usize = player.try_into().unwrap();
23 let player = player.try_into().unwrap();
24 let dasharray = player_num_dasharray(player);
25 dasharray.into_html_string()
31 let su = &mut self.su;
33 let hand_vpid = "6v1".to_owned(); // todo
34 let pawn_vpid = "7v1".to_owned(); // todo
37 w: &mut WindowGuard<'_>, pc: &str,
38 player: Option<&'static str>
42 let dasharray = player.map(player_dasharray).transpose()?;
43 let euse = w.find_element(By::Id(&w.vpidelem("piece", pc)?))?;
44 let epath = euse.find_element(By::Tag("path"))?;
45 let attr = epath.get_attribute("stroke-dasharray")?;
47 assert_eq!(attr, dasharray);
52 let mut w = su.w(&self.alice)?;
55 let hand = w.find_piece(&hand_vpid)?;
56 let hand_pieceid = hand.pieceid.to_string();
58 let hand_posg = hand.posg()?;
69 let top = w.execute_script(&format!(r##"
70 return defs_marker.previousElementSibling.dataset.piece;
72 assert_eq!( top.value().as_str().unwrap(),
81 let top = w.execute_script(&format!(r##"
82 return pieces_marker.nextElementSibling.dataset.piece;
84 assert_eq!( top.value().as_str().unwrap(),
92 chk(&mut w, &hand_pieceid, Some(ALICE))?;
98 let mut w = su.w(&self.bob)?;
99 chk(&mut w, &hand_vpid, Some(ALICE))?;
101 w.get(w.current_url()?)?;
102 chk(&mut w, &hand_vpid, Some(ALICE))?;
106 let mut w = su.w(&self.alice)?;
107 let pawn = w.find_piece(&pawn_vpid)?;
111 .move_w(&w, (hand_posg + PosC::new(20,0))?)?
117 for side in &[&self.alice, &self.bob] {
118 let mut w = su.w(side)?;
120 let log = w.retrieve_log((&mut |_: &'_ _| false) as LogIgnoreBeforeFn)?;
121 log.assert_no_conflicts();
125 let mut w = su.w(&self.alice)?;
127 let hand = w.find_piece(&hand_vpid)?;
132 .did("select hand")?;
138 .did("unclaim hand")?;
145 chk(&mut w, &hand_vpid, None)?;
148 let mut w = su.w(&self.bob)?;
149 chk(&mut w, &hand_vpid, None)?;
154 fn ungrab_race(&mut self){
155 let su = &mut self.su;
157 let p_alice = "7v1".to_owned(); // todo
158 let p_bob = "8v1".to_owned(); // todo
159 const DEST: Pos = PosC::new(50, 20);
162 let mut w = su.w(&self.alice)?;
165 .move_pc(&w, &p_alice)?
173 .did("alice, drag pawn over target")?;
178 let mut w = su.w(&self.bob)?;
181 .move_pc(&w, &p_bob)?
183 .move_w(&w, (DEST + PosC::new(2,0))?)?
186 .did("bob, drag pawn to target")?;
191 let mut w = su.w(&self.alice)?;
194 .move_pc(&w, &p_alice)?
197 .did("alice, drop pawn on target")?;
201 let mut chk_alice_on_top = |pl|{
202 let mut w = su.w(pl)?;
204 let pcs = w.pieces()?;
206 let vpid = w.piece_vpid(pc).unwrap();
207 pcs.iter().enumerate()
208 .find_map(|(ix, wp)| if wp.piece == vpid { Some(ix) } else { None })
212 dbgc!( find(&p_alice) ) > dbgc!( find(&p_bob) )
218 chk_alice_on_top(&self.alice).did("chk alice")?;
219 chk_alice_on_top(&self.bob ).did("chk bob" )?;
223 fn regrab_race(&mut self){
224 let su = &mut self.su;
225 let pawn_vpid = "7v1".to_owned(); // todo
226 const MIDHAND: Pos = PosC::new(40, 40);
227 const OUTHAND: Pos = PosC::new(20, 20);
230 let mut w = su.w(&self.bob)?;
234 .move_pc(&w, &pawn_vpid)?
236 .move_w(&w, (MIDHAND + Pos::new(-20,0))?)?
239 .move_w(&w, MIDHAND)?
251 let pauseable = su.otter_pauseable();
253 let mut w = su.w(&self.alice)?;
255 let pawn = w.find_piece(&pawn_vpid)?;
256 let start = pawn.posw()?;
258 let paused = pauseable.pause()?;
263 .move_w(&w, OUTHAND)?
269 .did("alice, drag out, and re-select")?;
276 for (who, gen) in &[(&self.alice, alice_gen),
277 (&self.bob, bob_gen )] {
281 let held = w.piece_held(&pawn_vpid)?;
282 assert_eq!( held.unwrap(), ALICE );
284 let log = w.retrieve_log(*gen)?;
285 assert_eq!( log.find_conflicts(), Vec::<String>::new() );
289 let mut w = su.w(&self.alice)?;
293 .did("alice, ungrasp for tidy up")?;
300 fn tests(UsualSetup { su, alice, bob, ..}: UsualSetup) {
301 let mut c = Ctx { su, alice, bob };
303 test!(c, "claim", c.claim() ?);
304 test!(c, "ungrab-race", c.ungrab_race() ?);
305 test!(c, "regrab-race", c.regrab_race() ?);
311 pub fn main() { as_usual(tests, module_path!())? }