50 lines
5.5 KiB
JavaScript
50 lines
5.5 KiB
JavaScript
import {chooseProvince,setDifficulty} from './helpers.mjs';
|
|
import {chromium} from 'playwright';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
const browser=await chromium.launch({executablePath:'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',headless:true});
|
|
const context=await browser.newContext({viewport:{width:390,height:844},isMobile:true,hasTouch:true,deviceScaleFactor:2});
|
|
const p=await context.newPage(),errors=[],results=[];
|
|
p.on('pageerror',e=>errors.push(e.message));
|
|
await p.addInitScript(()=>{let seed=785;Math.random=()=>{seed=(seed*1664525+1013904223)>>>0;return seed/4294967296;};});
|
|
await p.goto('http://localhost:4173');await p.clock.install();
|
|
await setDifficulty(p,{speed:3});await p.locator('#start').tap();await p.clock.runFor(12000);
|
|
const state=()=>p.evaluate(()=>shanheDebug.state()),cdp=await context.newCDPSession(p);
|
|
const touch=async(type,x,y)=>cdp.send('Input.dispatchTouchEvent',{type,touchPoints:x===undefined?[]:[{x,y,id:1}]});
|
|
let board=await p.locator('#board').boundingBox();
|
|
assert.equal(await p.locator('#board').evaluate(e=>getComputedStyle(e).touchAction),'none');
|
|
assert.deepEqual(await p.locator('.controls button').evaluateAll(buttons=>buttons.map(b=>b.id)),['pause']);
|
|
const row=await p.locator('.controls').boundingBox();
|
|
for(const id of ['pause']){const r=await p.locator('#'+id).boundingBox();assert.ok(r.y>=row.y&&r.y+r.height<=row.y+row.height+1);}
|
|
assert.equal(await p.locator('#board-name,.board-tag,#left,#right,#up,#back,.game-tools').count(),0);
|
|
const icon=await p.locator('#pause').evaluate(e=>({text:e.textContent.trim(),label:e.getAttribute('aria-label'),w:e.offsetWidth,h:e.offsetHeight,r:getComputedStyle(e).borderRadius}));assert.equal(icon.text,'');assert.equal(icon.w,icon.h);assert.equal(icon.r,'50%');assert.ok(icon.label.includes('暂停'));
|
|
const x=board.x+board.width*.35,y=board.y+board.height*.72;
|
|
const initial=await state();
|
|
await touch('touchStart',x,y);await touch('touchMove',x+2,y+1);await p.clock.runFor(32);await touch('touchEnd');
|
|
assert.equal((await state()).swipeCount,initial.swipeCount,'轻触和手抖不算滑动');
|
|
// No force is delivered while the finger moves or remains pressed.
|
|
await touch('touchStart',x,y);
|
|
for(let i=1;i<=6;i++){await touch('touchMove',x+i*16,y-i*8);await p.clock.runFor(24);}
|
|
const pending=await state();assert.ok(pending.swiping);assert.equal(pending.swipeCount,initial.swipeCount);assert.equal(pending.shakeCount,initial.shakeCount);
|
|
await touch('touchEnd');assert.equal((await state()).swipeCount,initial.swipeCount+1);assert.equal((await state()).swiping,false);
|
|
await p.clock.runFor(48);const released=await state();assert.ok(released.shakeOffset!==0||released.shakeY!==0);await p.screenshot({path:'artifacts/swipe-playing.png'});
|
|
await p.clock.runFor(600);assert.equal((await state()).swipeCount,released.swipeCount);assert.notDeepEqual((await state()).regions,released.regions,'松手后继续靠惯性和重力运动');
|
|
results.push('一次轻划仅在松手时触发一次晃动,移动中不施力,松手后自由运动');
|
|
// A long slow drag cannot steer pieces. Neither can a flick held before release.
|
|
board=await p.locator('#board').boundingBox();let count=(await state()).swipeCount;
|
|
await touch('touchStart',x,y);for(let i=1;i<=8;i++){await p.clock.runFor(100);await touch('touchMove',x+i*8,y);}await touch('touchEnd');assert.equal((await state()).swipeCount,count,'慢拖不触发');
|
|
await touch('touchStart',x,y);await touch('touchMove',x+90,y-20);await p.clock.runFor(600);assert.equal((await state()).swipeCount,count);await touch('touchEnd');assert.equal((await state()).swipeCount,count,'划后按住不能储存冲量');
|
|
results.push('慢拖、按住以及划后停住再松手均不施力,无持续牵引');
|
|
board=await p.locator('#board').boundingBox();
|
|
await touch('touchStart',board.x+80,board.y+board.height*.6);await touch('touchMove',board.x+140,board.y+board.height*.6-20);await p.clock.runFor(32);assert.ok((await state()).swiping);
|
|
await touch('touchCancel');assert.equal((await state()).swiping,false);const canceled=(await state()).swipeCount;await p.clock.runFor(300);assert.equal((await state()).swipeCount,canceled);
|
|
results.push('系统取消触摸释放指针捕获并清理待处理输入');
|
|
board=await p.locator('#board').boundingBox();
|
|
await touch('touchStart',board.x+80,board.y+board.height*.55);await touch('touchMove',board.x+125,board.y+board.height*.55-25);await p.clock.runFor(32);
|
|
// A keyboard pause while the finger is still down must discard pending movement.
|
|
await p.keyboard.press(' ');const paused=await state();assert.equal(paused.status,'paused');assert.equal(paused.swiping,false);await touch('touchMove',board.x+180,board.y+board.height*.5);await touch('touchEnd');await p.clock.runFor(1000);assert.equal((await state()).swipeCount,paused.swipeCount);assert.deepEqual((await state()).regions,paused.regions);
|
|
await p.getByRole('button',{name:'继续游戏',exact:true}).tap();await p.clock.runFor(100);assert.equal((await state()).swipeCount,paused.swipeCount);await p.locator('#pause').tap();await p.getByRole('button',{name:'结束旅程',exact:true}).tap();assert.equal((await state()).screen,'result');
|
|
results.push('滑动中暂停清空手势,暂停冻结局面,继续后不重放旧输入;底部旅程操作可结束并生成成绩');
|
|
assert.deepEqual(errors,[]);fs.writeFileSync('artifacts/swipe-report.json',JSON.stringify({scope:'Modern Chrome / CDP touch input, not native container acceptance',passed:true,results,errors},null,2));
|
|
console.log(results.join('\n'));await browser.close();
|