22 lines
6.6 KiB
JavaScript
22 lines
6.6 KiB
JavaScript
import {chromium} from 'playwright';import assert from 'node:assert/strict';import fs from 'node:fs';import {execFileSync} from 'node:child_process';
|
|
const browser=await chromium.launch({executablePath:'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',headless:true}),context=await browser.newContext({viewport:{width:390,height:844},isMobile:true,hasTouch:true,deviceScaleFactor:2}),p=await context.newPage(),errors=[];p.on('pageerror',e=>errors.push(e.message));
|
|
try{
|
|
await p.bringToFront();await p.clock.install();await p.goto('http://localhost:4173');await p.evaluate(()=>{const original=PuzzleGame.Game.prototype.start;PuzzleGame.Game.prototype.start=function(){original.call(this);window.celebrationGame=this;this.engine.gravity.y=0;this.status='paused';};const cap=PuzzleVideo.Capture.prototype.capture;PuzzleVideo.Capture.prototype.capture=function(...args){window.celebrationCapture=this;return cap.apply(this,args);};});await p.locator('#start').tap();await p.clock.runFor(100);
|
|
// Controlled real-map setup, then use the actual merge/achievement machinery.
|
|
await p.evaluate(()=>{const g=celebrationGame,ids=PuzzleGame.REGIONAL_MILESTONES.find(m=>m.id==='east').codes.map(code=>g.level.regions.findIndex(r=>r.id===code));const pose={tx:0,ty:g.dangerY+80,angle:0};g.createGroup(ids.slice(0,-1),pose);g.createGroup(ids.slice(-1),pose);g.queue=g.queue.filter(i=>!ids.includes(i));g.pending=null;g.dropped=ids.length;g.clock=1000;g.status='playing';g.tryMerges();document.dispatchEvent(new Event('visibilitychange'));});await p.clock.runFor(450);
|
|
let state=await p.evaluate(()=>shanheDebug.state());assert.equal(state.screen,'game');assert.equal(state.celebration.kind,'region');assert.deepEqual(state.celebration.names,['华东']);assert.equal(state.milestones.length,1);assert.equal(state.milestones[0].count,8);await p.screenshot({path:'artifacts/celebration-east.png'});
|
|
const sampled=await p.evaluate(()=>{const frames=celebrationCapture.frames,frame=frames.find(f=>f.scene.celebrations.some(e=>e.kind==='region')&&f.scene.view.effectClock>f.scene.celebrations[0].started+150);return {present:!!frame,from:frames[0].at,times:frames.map(f=>({at:f.at,clock:f.scene.view.effectClock,effects:f.scene.celebrations.length})),state:shanheDebug.state().status,elapsed:shanheDebug.state().elapsed,info:celebrationCapture.info(),hidden:document.hidden,visibility:document.visibilityState};});assert.equal(sampled.present,true,JSON.stringify(sampled));assert.equal(sampled.from,0);
|
|
await p.locator('#pause').tap();const paused=await p.evaluate(()=>shanheDebug.state());await p.clock.runFor(5000);assert.equal((await p.evaluate(()=>shanheDebug.state())).elapsed,paused.elapsed);await p.getByRole('button',{name:'继续游戏',exact:true}).tap();await p.clock.runFor(2200);assert.equal((await p.evaluate(()=>shanheDebug.state())).celebration,null);
|
|
// Finish the same actual national map through real merges, freeze physical time,
|
|
// and verify a distinct victory presentation precedes the result page.
|
|
await p.evaluate(()=>{const g=celebrationGame;g.groups.forEach(group=>Matter.Composite.remove(g.engine.world,group.body));g.groups=[];g.queue=[];g.pending=null;const half=g.level.regions.map((_,i)=>i),first=half.splice(0,17),pose={tx:0,ty:150,angle:0};const a=g.createGroup(first,pose),b=g.createGroup(half,pose);g.dropped=34;g.merge(a,b,[first[0],half[0],0]);});state=await p.evaluate(()=>shanheDebug.state());const wonClock=state.elapsed,wonScore=state.score;assert.equal(state.status,'won');assert.equal(state.screen,'game');assert.equal(state.celebration.kind,'victory');assert.equal(state.milestones.length,7);await p.clock.runFor(700);assert.equal((await p.evaluate(()=>shanheDebug.state())).screen,'game');assert.equal((await p.evaluate(()=>shanheDebug.state())).elapsed,wonClock);await p.screenshot({path:'artifacts/celebration-victory.png'});
|
|
// A backgrounded victory resumes its presentation instead of being skipped.
|
|
await p.evaluate(()=>{Object.defineProperty(document,'hidden',{configurable:true,value:true});document.dispatchEvent(new Event('visibilitychange'));});const age=await p.evaluate(()=>shanheDebug.state().victoryAge);await p.clock.runFor(4000);assert.equal((await p.evaluate(()=>shanheDebug.state())).victoryAge,age);await p.evaluate(()=>{delete document.hidden;document.dispatchEvent(new Event('visibilitychange'));});await p.clock.runFor(1000);assert.equal((await p.evaluate(()=>shanheDebug.state())).screen,'game');await p.clock.runFor(1500);state=await p.evaluate(()=>shanheDebug.state());assert.equal(state.screen,'result');assert.equal(state.result.reason,'won');assert.equal(state.result.elapsed,wonClock);assert.equal(state.result.score,wonScore);assert.ok(state.video.capture.to>=wonClock+2600);assert.ok(state.video.capture.from===0);
|
|
const victorySamples=await p.evaluate(()=>celebrationCapture.frames.filter(f=>f.scene.celebrations.some(e=>e.kind==='victory')).length);assert.ok(victorySamples>20);
|
|
await p.clock.resume();await p.waitForFunction(()=>shanheDebug.state().video.settled);await p.locator('#video-action').tap();await p.waitForFunction(()=>shanheDebug.state().video.generated,{},{timeout:30000});
|
|
// Export only to a local test file, without invoking any real publishing account.
|
|
const output=await p.evaluate(async()=>{const cap=celebrationCapture;const output=await PuzzleVideo.generate(cap,document.querySelector('#result-card'),{cancelled:false},()=>{});return PuzzleVideo.dataURL(output.blob);});fs.writeFileSync('artifacts/celebrations-demo.mp4',Buffer.from(output.split(',')[1],'base64'));execFileSync('/opt/homebrew/bin/ffmpeg',['-v','error','-i','artifacts/celebrations-demo.mp4','-f','null','-']);
|
|
// Reduced-motion mode still shows the completion message.
|
|
await p.emulateMedia({reducedMotion:'reduce'});await p.locator('#replay').tap();await p.evaluate(()=>{const g=celebrationGame;g.clock=1000;g.status='playing';g.checkMilestones({ids:PuzzleGame.REGIONAL_MILESTONES.find(m=>m.id==='east').codes.map(code=>g.level.regions.findIndex(r=>r.id===code))});document.dispatchEvent(new Event('visibilitychange'));});await p.waitForTimeout(200);assert.equal((await p.evaluate(()=>shanheDebug.state())).celebration.kind,'region');await p.screenshot({path:'artifacts/celebration-reduced-motion.png'});assert.deepEqual(errors,[]);fs.writeFileSync('artifacts/celebrations-report.json',JSON.stringify({passed:true,scope:'Modern Chrome; controlled real-map groups and actual merge logic, not native performance acceptance',regionalCapture:sampled,victorySamples,wonClock,wonScore,errors},null,2));console.log('地区庆祝、暂停冻结、通关演出与后台恢复、真实计时/得分、完整视频及减少动态效果检查通过');
|
|
}finally{await context.close();await browser.close();}
|