Files
shanhe-puzzle/tests/results.mjs
T

17 lines
7.1 KiB
JavaScript

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=[];p.on('pageerror',e=>errors.push(e.message));
await p.addInitScript(()=>{let seed=785;Math.random=()=>{seed=(seed*1664525+1013904223)>>>0;return seed/4294967296;};window.calls=[];window.xhs={miniTool:{writeTempFile:async args=>{window.exportData=args.data;window.calls.push('write');return {filePath:'test://fresh-card.png'};},postNote:async args=>{window.posted=args;window.calls.push('post');},saveImageToPhotosAlbum:async args=>{window.saved=args;window.calls.push('save');}}};});
await p.clock.install();await p.goto('http://localhost:4173');await p.waitForFunction(()=>window.shanheDebug);await p.locator('[data-speed="3"]').click();await p.locator('[data-bounce="2"]').click();await p.locator('#start').click();await p.clock.runFor(19500);const before=await p.evaluate(()=>shanheDebug.state());assert.equal(before.status,'playing');await p.locator('#up').tap();await p.clock.runFor(100);assert.ok((await p.evaluate(()=>shanheDebug.state())).shakeY<0);await p.locator('#pause').tap();const paused=await p.evaluate(()=>shanheDebug.state());await p.clock.runFor(60000);assert.equal((await p.evaluate(()=>shanheDebug.state())).elapsed,paused.elapsed);await p.getByRole('button',{name:'结束旅程',exact:true}).tap();const ended=await p.evaluate(()=>shanheDebug.state());assert.equal(ended.screen,'result');assert.equal(ended.status,'ended');assert.equal(ended.result.bounceName,'弹力全开');assert.equal(ended.result.speedName,'山河起飞');assert.equal(ended.result.score,paused.score);assert.equal(ended.result.elapsed,paused.elapsed);assert.equal(ended.result.history.at(-1).ms,paused.elapsed);assert.equal(ended.result.history.at(-1).score,paused.score);assert.ok(ended.result.history.every((v,i,a)=>!i||v.ms>=a[i-1].ms));assert.equal(ended.result.dropped,ended.dropped);assert.ok(ended.dropped<34,'结局截图保留未完成状态');await p.screenshot({path:'artifacts/result-mobile.png'});
await p.locator('#share-note').tap();await p.waitForFunction(()=>window.posted);const posted=await p.evaluate(()=>window.posted);assert.equal(posted.pageType,'photo_publish');assert.equal(posted.mediaInfo.image_resources[0].url,'test://fresh-card.png');assert.ok(posted.content.includes('弹力全开')&&posted.content.includes('山河起飞'));assert.ok(posted.title.length<=20&&posted.content.length<=1000);const image=await p.evaluate(()=>window.exportData);fs.writeFileSync('artifacts/result-card.png',Buffer.from(image.split(',')[1],'base64'));await p.locator('#save-card').tap();await p.waitForFunction(()=>window.saved);assert.deepEqual(await p.evaluate(()=>window.calls),['write','post','write','save']);
// Failure/cancellation must leave the card usable; no-SDK must not throw.
await p.evaluate(()=>{window.xhs.miniTool.postNote=async()=>{throw Error('cancelled');};});await p.locator('#share-note').tap();await p.waitForFunction(()=>!document.querySelector('#share-note').disabled);assert.equal(await p.locator('#result-screen').isVisible(),true);await p.evaluate(()=>{window.xhs=null;});await p.locator('#share-note').tap();assert.ok((await p.locator('#toast').textContent()).includes('小红书小工具'));
for(const viewport of [{width:320,height:568},{width:375,height:667},{width:740,height:390}]){await p.setViewportSize(viewport);await p.waitForFunction(()=>{const r=document.querySelector('.result-actions').getBoundingClientRect();return r.bottom<=innerHeight;});assert.ok(await p.evaluate(()=>document.documentElement.scrollHeight<=innerHeight+1));const rect=await p.locator('#result-card').boundingBox();assert.ok(rect.width>20&&rect.height>20);}
await p.setViewportSize({width:320,height:568});await p.addStyleTag({content:':root{--safe-area-inset-top:44px;--safe-area-inset-bottom:34px}'});await p.evaluate(()=>dispatchEvent(new Event('resize')));assert.ok(await p.evaluate(()=>document.documentElement.scrollHeight<=innerHeight+1));assert.ok(await p.locator('.result-actions').evaluate(e=>e.getBoundingClientRect().bottom<=innerHeight-34));
await p.locator('#replay').tap();assert.equal((await p.evaluate(()=>shanheDebug.state())).dropped,1);assert.equal((await p.evaluate(()=>shanheDebug.state())).result,null);await p.locator('#pause').tap();await p.getByRole('button',{name:'结束旅程',exact:true}).tap();assert.equal((await p.evaluate(()=>shanheDebug.state())).result.score,0);await p.locator('#result-back').tap();assert.equal((await p.evaluate(()=>shanheDebug.state())).screen,'entry');assert.deepEqual(errors,[]);
fs.writeFileSync('artifacts/results-report.json',JSON.stringify({passed:true,scope:'Browser regression with mocked native SDK; not real publish/save acceptance',result:ended.result,exportBytes:Buffer.from(image.split(',')[1],'base64').length,errors},null,2));console.log('成绩图真实结局、三档弹性记录、暂停计时、折线终点、图片原生API参数/失败降级及手机布局通过');
// Automatic loss uses the full released map; victory uses a deliberately small
// two-piece level fixture, exercising the real engine -> app -> image flow.
await p.goto('http://localhost:4173');await p.locator('[data-speed="3"]').tap();await p.locator('#start').tap();await p.clock.runFor(90000);const lost=await p.evaluate(()=>shanheDebug.state());assert.equal(lost.screen,'result');assert.equal(lost.result.reason,'lost');assert.equal(lost.result.history.at(-1).score,lost.score);await p.screenshot({path:'artifacts/result-lost.png'});
const winPage=await context.newPage();winPage.on('pageerror',e=>errors.push(e.message));await winPage.clock.install();const region=(id,y)=>({id,name:'测试'+id,center:[50,y+50],label:[50,y+50],rings:[[[0,y],[100,y],[100,y+100],[0,y+100]]]});const fixture={levels:[{id:'100000',name:'两块通关测试',width:1000,height:1000,regions:[region('0',100),region('1',0)],edges:[[0,1,0]]}]};await winPage.route('**/maps.js',route=>route.fulfill({contentType:'text/javascript',body:'Math.random=function(){return .5;};window.MAP_DATA='+JSON.stringify(fixture)+';'}));await winPage.goto('http://localhost:4173');await winPage.locator('[data-speed="3"]').tap();await winPage.locator('#start').tap();await winPage.clock.runFor(10000);const won=await winPage.evaluate(()=>shanheDebug.state());assert.equal(won.screen,'result');assert.equal(won.result.reason,'won');assert.equal(won.result.score,3300);assert.equal(won.result.history.at(-1).score,3300);await winPage.screenshot({path:'artifacts/result-won-fixture.png'});assert.deepEqual(errors,[]);console.log('真实地图自然判负、两块测试关卡自然通关均自动生成结局图,通关曲线包含奖励');const report=JSON.parse(fs.readFileSync('artifacts/results-report.json'));report.automaticEndings={lost:{reason:lost.result.reason,score:lost.score},wonFixture:{reason:won.result.reason,score:won.score}};fs.writeFileSync('artifacts/results-report.json',JSON.stringify(report,null,2));await browser.close();