Files
shanhe-puzzle/tests/milestones.test.cjs
codex-bot 4845826deb
Shanhe source and offline package checks / verify (pull_request) Failing after 5s
feat: sync Shanhe 2.2 source and add remote package verification
2026-11-01 17:13:46 +08:00

12 lines
4.2 KiB
JavaScript

const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),vm=require('node:vm');
const {Game,REGIONAL_MILESTONES,VICTORY_MS}=require('../src/game.js');const box={window:{}};vm.runInNewContext(fs.readFileSync('src/maps.js','utf8'),box);const maps=box.window.MAP_DATA,national=maps.levels[0];
function game(level=national){const events=[],g=new Game(level,420,620,4,(type,detail)=>events.push({type,detail}),()=>.5,1);g.status='playing';g.queue=[];return {g,events};}
const ids=(g,codes)=>codes.map(code=>g.level.regions.findIndex(r=>r.id===code));
test('七个玩法地区互不重复覆盖全国34块,台湾港澳都包含在内',()=>{const codes=REGIONAL_MILESTONES.flatMap(m=>m.codes);assert.equal(codes.length,34);assert.equal(new Set(codes).size,34);assert.deepEqual(codes.slice().sort(),Array.from(national.regions,r=>r.id).sort());assert.ok(REGIONAL_MILESTONES.find(m=>m.id==='east').codes.includes('710000'));assert.ok(REGIONAL_MILESTONES.find(m=>m.id==='south').codes.includes('810000'));assert.ok(REGIONAL_MILESTONES.find(m=>m.id==='south').codes.includes('820000'));});
test('只在同一组合包含整个地区时庆祝,独立落下或少一块均不算',()=>{const {g,events}=game(),east=ids(g,REGIONAL_MILESTONES.find(m=>m.id==='east').codes);east.forEach(id=>g.checkMilestones({ids:[id]}));g.checkMilestones({ids:east.slice(1)});assert.equal(g.milestones.length,0);g.checkMilestones({ids:east});assert.equal(g.milestones.length,1);assert.equal(g.milestones[0].name,'华东');assert.equal(events.filter(e=>e.type==='milestone').length,1);assert.equal(g.celebrations[0].count,8);assert.equal(g.score,0,'庆祝不改变得分');g.destroy();});
test('地区可以包含在更大的组合里,每局只提示一次,再开局清空',()=>{const {g}=game(),east=ids(g,REGIONAL_MILESTONES.find(m=>m.id==='east').codes),big=east.concat(ids(g,['410000']));g.checkMilestones({ids:big});const at=g.celebrations[0].started;g.clock+=100;g.checkMilestones({ids:big.concat(ids(g,['420000']))});assert.equal(g.milestones.length,1);assert.equal(g.celebrations.length,1);assert.equal(g.celebrations[0].started,at);g.destroy();const next=game();assert.equal(next.g.milestones.length,0);next.g.destroy();});
test('不同地区的提示依次播放,暂停不推进游戏时间',()=>{const {g}=game();const east=ids(g,REGIONAL_MILESTONES.find(m=>m.id==='east').codes),central=ids(g,REGIONAL_MILESTONES.find(m=>m.id==='central').codes);g.checkMilestones({ids:east});g.clock+=100;g.checkMilestones({ids:central});assert.equal(g.milestones.length,2);assert.equal(g.celebrations[1].started,g.celebrations[0].started+g.celebrations[0].duration);g.status='paused';const clock=g.clock;g.tick(100);assert.equal(g.clock,clock);g.destroy();});
test('细分关卡按最大组合达到25/50/75%提示,跨过多个门槛时只展示最高阶段',()=>{const level=maps.levels.find(l=>l.id==='330000'),{g}=game(level);g.checkMilestones({ids:[0,1]});assert.equal(g.milestones.length,0);g.checkMilestones({ids:[0,1,2]});assert.equal(g.milestones[0].id,'quarter');g.checkMilestones({ids:[0,1,2,3,4,5,6,7,8]});assert.equal(g.milestones.length,3);assert.equal(g.celebrations.length,2);assert.deepEqual(g.celebrations[1].names,['山河渐全']);assert.equal(g.celebrations[1].count,9);g.checkMilestones({ids:[0,1,2,3,4,5,6,7,8]});assert.equal(g.celebrations.length,2);g.destroy();});
test('全拼完建立专属庆祝并覆盖旧提示,冻结得分时间且结束只触发一次',()=>{const {g,events}=game();g.clock=5000;g.score=123;g.checkMilestones({ids:national.regions.map((_,i)=>i)});assert.equal(g.milestones.length,7);g.complete('won');assert.equal(g.status,'won');assert.equal(g.celebrations.length,1);assert.equal(g.celebrations[0].kind,'victory');assert.equal(g.celebrations[0].duration,VICTORY_MS);g.tick(100);assert.equal(g.clock,5000);assert.equal(g.score,123);assert.equal(g.complete('won'),false);assert.equal(events.filter(e=>e.type==='end').length,1);assert.equal(g.history.at(-1).ms,5000);g.destroy();});
test('未完成结束和堆顶判负不产生通关庆祝',()=>{for(const reason of ['stopped','lost']){const {g}=game();g.complete(reason);assert.equal(g.celebrations.some(e=>e.kind==='victory'),false);g.destroy();}});