diff --git a/src/experiments/pathfinding/pathfinding.html b/src/experiments/pathfinding/pathfinding.html
index b1bea8a..ed7399b 100644
--- a/src/experiments/pathfinding/pathfinding.html
+++ b/src/experiments/pathfinding/pathfinding.html
@@ -5,7 +5,7 @@
-
+
算法游乐场 · 寻路竞技场
diff --git a/src/experiments/pathfinding/pathfinding.mjs b/src/experiments/pathfinding/pathfinding.mjs
index 546ff38..f584d58 100644
--- a/src/experiments/pathfinding/pathfinding.mjs
+++ b/src/experiments/pathfinding/pathfinding.mjs
@@ -166,12 +166,16 @@ function render(){
const visited=new Set(frame.visited),frontier=new Set(frame.frontier),path=new Set(frame.path);
cells[name].forEach((cell,index)=>{
const state=index===map.start?'start':index===map.goal?'goal':walls.has(index)?'wall':path.has(index)?'path':visited.has(index)?'visited':frontier.has(index)?'frontier':'';
- cell.className=`cell ${state}${index===frame.current&&!frame.done?' current':''}`;
- cell.textContent=index===map.start?'S':index===map.goal?'G':'';
+ const className=`cell ${state}${index===frame.current&&!frame.done?' current':''}`;
+ if(cell.className!==className)cell.className=className;
+ const text=index===map.start?'S':index===map.goal?'G':'';
+ if(cell.textContent!==text)cell.textContent=text;
const label={start:'起点',goal:'终点',wall:'墙',path:'最终路径',visited:'已探索',frontier:'待探索','':'空地'}[state];
- cell.setAttribute('aria-label',`第 ${Math.floor(index/map.cols)+1} 行,第 ${index%map.cols+1} 列,${label}`);
+ const ariaLabel=`第 ${Math.floor(index/map.cols)+1} 行,第 ${index%map.cols+1} 列,${label}`;
+ if(cell.getAttribute('aria-label')!==ariaLabel)cell.setAttribute('aria-label',ariaLabel);
const candidate=guessing&&name===guessAlgorithm?guessRound?.decision.candidates.find(c=>c.index===index):null;
- cell.setAttribute('aria-pressed',String(!!candidate&&guessRound.choice===index));
+ const pressed=String(!!candidate&&guessRound.choice===index);
+ if(cell.getAttribute('aria-pressed')!==pressed)cell.setAttribute('aria-pressed',pressed);
if(candidate){
cell.classList.toggle('guess-candidate',true);
cell.classList.toggle('guess-picked',guessRound.choice===index);
diff --git a/tests/experiments/pathfinding/pathfinding-ui.test.mjs b/tests/experiments/pathfinding/pathfinding-ui.test.mjs
index c67520b..a613ab3 100644
--- a/tests/experiments/pathfinding/pathfinding-ui.test.mjs
+++ b/tests/experiments/pathfinding/pathfinding-ui.test.mjs
@@ -13,7 +13,9 @@ async function boot(){
}
append(...children){for(const child of children){if(child.parent)child.parent.children=child.parent.children.filter(c=>c!==child);this.children.push(child);child.parent=this;}}
replaceChildren(...children){this.children=[];this.append(...children);}
- setAttribute(k,v){this.attributes.set(k,v);}getAttribute(k){return this.attributes.get(k);}
+ set className(value){this.writes=(this.writes??0)+1;this._className=value;}get className(){return this._className;}
+ set textContent(value){this.writes=(this.writes??0)+1;this._textContent=value;}get textContent(){return this._textContent;}
+ setAttribute(k,v){this.writes=(this.writes??0)+1;this.attributes.set(k,v);}getAttribute(k){return this.attributes.get(k);}
closest(selector){return selector==='.cell'&&this.className.split(' ').includes('cell')?this:null;}
contains(child){return this.children.includes(child);}
getBoundingClientRect(){return {left:0,top:0,right:300,bottom:220,width:300,height:220};}
@@ -44,6 +46,55 @@ async function boot(){
selected(){return presets.find(b=>b.getAttribute('aria-pressed')==='true')?.dataset.map;}};
}
+function gridSnapshot(u,names=['bfs','astar']){
+ return names.flatMap(name=>u.els.get(name+'-grid').children.map(cell=>[
+ cell.className,cell.textContent,cell.getAttribute('aria-label'),cell.getAttribute('aria-pressed')
+ ]));
+}
+function gridWrites(u,names=['bfs','astar']){
+ return names.flatMap(name=>u.els.get(name+'-grid').children).reduce((sum,cell)=>sum+cell.writes,0);
+}
+
+test('playback writes only grid properties that actually change on each search step',async()=>{
+ const u=await boot();try{
+ u.click('play');
+ for(let step=1;step<=5;step++){
+ const before=gridSnapshot(u),writes=gridWrites(u);
+ const [id,tick]=u.timers.entries().next().value;u.timers.delete(id);tick();
+ const after=gridSnapshot(u),changes=after.reduce((sum,cell,i)=>sum+cell.filter((value,j)=>value!==before[i][j]).length,0);
+ assert.equal(u.els.get('bfs-explored').textContent,step);
+ assert.equal(u.els.get('astar-explored').textContent,step);
+ assert.ok(changes>0,'the visible search must advance');
+ assert.equal(gridWrites(u)-writes,changes,'unchanged cells must not receive redundant writes');
+ }
+ }finally{u.restore();}
+});
+
+test('a completed A-star board receives no writes while BFS keeps exploring',async()=>{
+ const u=await boot();try{
+ for(let i=0;i<60;i++)u.click('step');
+ assert.equal(u.els.get('astar-distance').textContent,20);
+ const before=gridSnapshot(u,['astar']),writes=gridWrites(u,['astar']);
+ u.click('step');
+ assert.equal(u.els.get('bfs-explored').textContent,61);
+ assert.deepEqual(gridSnapshot(u,['astar']),before);
+ assert.equal(gridWrites(u,['astar']),writes);
+ }finally{u.restore();}
+});
+
+test('leaving a revealed guess restores every normal grid property at the same step',async()=>{
+ let expected;const normal=await boot();try{
+ normal.click('step');normal.click('step');expected=gridSnapshot(normal);
+ }finally{normal.restore();}
+ const u=await boot();try{
+ u.click('guess-toggle');
+ const decision=decisionAt(search(makeMap(),'astar'),1);
+ u.edit(decision.candidates.find(c=>c.index!==decision.current).index,'astar');
+ u.click('guess-reveal');u.click('guess-toggle');
+ assert.deepEqual(gridSnapshot(u),expected,'candidate numbers, classes, labels and selection must all be cleared');
+ }finally{u.restore();}
+});
+
test('first-run guidance waits for a complete real baseline before inviting a map edit',async()=>{
const u=await boot();try{
assert.match(u.els.get('play-status').textContent,/先.*跑.*对照/);