File size: 5,235 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
let enhancedFailed=0;
const fs=require('fs');const path=require('path');const{pathToFileURL}=require('url');const{chromium}=require('playwright');
function a(x,m){if(!x)throw new Error(m)}function eq(x,y,m){a(require("node:util").isDeepStrictEqual(x,y),`${m}: got ${JSON.stringify(x)} want ${JSON.stringify(y)}`)}async function tc(id,f){try{await f();console.log(id+' PASS')}catch(e){console.error(id+' FAIL: '+e.message);enhancedFailed++}}
(async()=>{const b=await chromium.launch({headless:true,args:['--no-sandbox','--disable-dev-shm-usage']});const p=await b.newPage();await p.goto(pathToFileURL(path.join(process.cwd(),'index.html')).href);
// ENHANCED_CASE_1: corner, edge and center cells count all and only their eight-neighborhood mines.
await tc('ENHANCED_CASE_1',async()=>{const s=await p.evaluate(()=>__game.init({rows:4,cols:5,mines:[[0,0],[0,2],[1,1],[2,3],[3,4]]}));const at=(r,c)=>s.cells[r*s.cols+c].adj;eq([at(0,1),at(1,2),at(2,2),at(3,3)],[3,3,2,2],'adjacency counts')});
// ENHANCED_CASE_2: flood fill crosses connected zeros and reveals number boundaries without crossing a mine wall.
await tc('ENHANCED_CASE_2',async()=>{const s=await p.evaluate(()=>{__game.init({rows:5,cols:5,mines:[[0,2],[1,2],[2,2],[3,2],[4,2]]});return __game.reveal(2,0)});const shown=[];s.cells.forEach((c,i)=>{if(c.revealed)shown.push([Math.floor(i/5),i%5])});eq(shown,[[0,0],[0,1],[1,0],[1,1],[2,0],[2,1],[3,0],[3,1],[4,0],[4,1]],'flood set');eq(s.status,'playing','flood status')});
// ENHANCED_CASE_3: flag toggling is reversible, flagged reveal is a no-op, and losing reveals every mine.
await tc('ENHANCED_CASE_3',async()=>{const r=await p.evaluate(()=>{__game.init({rows:3,cols:4,mines:[[0,0],[2,3]]});const flagged=__game.toggleFlag(1,1);const blocked=__game.reveal(1,1);const unflagged=__game.toggleFlag(1,1);const lost=__game.reveal(0,0);return{flagged,blocked,unflagged,lost}});a(r.flagged.cells[5].flagged&&!r.flagged.cells[5].revealed,'flag failed');eq(r.blocked.cells[5],r.flagged.cells[5],'flagged reveal');a(!r.unflagged.cells[5].flagged,'unflag failed');eq(r.lost.status,'lost','loss status');a(r.lost.cells[0].revealed&&r.lost.cells[11].revealed,'not all mines revealed')});
// ENHANCED_CASE_4: selecting a size changes the UI board; new games randomize mine locations.
await tc('ENHANCED_CASE_4',async()=>{const sizes=[['8x8',8],['12x12',12],['5x5',5]];for(const [option,n] of sizes){const choice=await p.locator('[data-testid="grid-size"] option').evaluateAll((options,n)=>options.find(item=>item.textContent.replace(/\s+/g,'').replace('×','x')===n+'x'+n)?.value,n);a(choice!==undefined,'missing size choice');await p.locator('[data-testid="grid-size"]').selectOption(choice);await p.locator('[data-testid="new-game"]').click();const states=[];for(let i=0;i<4;i++){if(i)await p.locator('[data-testid="new-game"]').click();const state=await p.evaluate(()=>__game.getState());eq([state.rows,state.cols],[n,n],'selected board dimensions');eq(await p.locator('[data-testid="board"] [data-r][data-c]').count(),n*n,'rendered cell count');const mines=state.cells.map((cell,index)=>cell.mine?index:-1).filter(index=>index>=0);a(mines.length>0&&mines.length<n*n,'valid mine count');states.push(mines.join(','))}a(states.every((value,index)=>index===0||value!==states[index-1]),'consecutive games reused a layout')}});

await tc('VISIBLE_UI',async()=>{
 await p.evaluate(()=>__game.init({rows:3,cols:3,mines:[[0,0],[2,2]]}));
 const cell=(r,c)=>p.locator('[data-testid="board"] [data-r="'+r+'"][data-c="'+c+'"]');
 await cell(1,1).click({button:'right'});
 a((await p.evaluate(()=>__game.getState())).cells[4].flagged,'right click must flag');
 a((await cell(1,1).textContent()).trim().length>0,'flag must be painted');
 await cell(1,1).click();a(!(await p.evaluate(()=>__game.getState())).cells[4].revealed,'flag blocks actual left click');
 await cell(1,1).click({button:'right'});await cell(1,1).click();
 eq((await cell(1,1).innerText()).trim(),'2','visible cell adjacency');
 const copy=await p.evaluate(()=>{const state=__game.getState();state.cells[4].adj=99;return __game.getState().cells[4].adj});eq(copy,2,'state getter copy');
 fs.mkdirSync('/logs/verifier/visual-audit',{recursive:true});
 await p.screenshot({path:'/logs/verifier/visual-audit/flag-and-reveal.png'});
 await cell(0,0).click();eq((await p.locator('[data-testid=status]').innerText()).trim(),'lost','visible loss');
 const lost=await p.evaluate(()=>__game.getState());await cell(0,1).click();eq(await p.evaluate(()=>__game.getState()),lost,'terminal board must not mutate');
 await p.screenshot({path:'/logs/verifier/visual-audit/lost-board.png'});
 const boxes=await p.locator('[data-testid=board] [data-r][data-c]').evaluateAll(nodes=>nodes.map(node=>{const b=node.getBoundingClientRect();return {x:b.x,y:b.y,w:b.width,h:b.height}}));
 for(let i=0;i<boxes.length;i++)for(let j=i+1;j<boxes.length;j++){const a=boxes[i],b=boxes[j];a.w>0&&a.h>0||(()=>{throw Error('invisible cell')})();if(Math.min(a.x+a.w,b.x+b.w)-Math.max(a.x,b.x)>.5&&Math.min(a.y+a.h,b.y+b.h)-Math.max(a.y,b.y)>.5)throw Error('overlapping cells')}
});
await b.close();console.log('ENHANCED_SCORE='+((5-enhancedFailed)/5));})().catch(e=>{console.error(e.stack||e);process.exit(1)});