Files
Dune/tests/sapho-aggressor.test.ts
T
Quintine 9b67a780eb Connect early Juice of Sapho battle aggressor
During the existing public pre-plan opportunity, the holder can discard canonical unreserved Sapho to become aggressor before their readiness or any binding plan/Voice/inspection/Truthtrance commitment. Unchosen preparation prompts remain available. No new confirmation-only or holder-dependent response window is introduced. Later intervention and its allocation/privacy protocol remain unfinished.

A battle/turn/revision-bound history and independent event list preserve accepted priority while keeping attacker, defender and chooser stable. Legitimately recovered cards can change priority again at a fresh event. Reads, actions and normalization reject corrupt histories/custody. Shared tie priority respects Habbanya in either physical slot and reaches pre-commit Stone/Voice checks, public views, bot calculations and actual ordinary/Stone resolution. The final quote previously assumed original attacker priority and now consumes the validated aggressor. Harkonnen capture still follows actual winner/loser.

Owner controls, public aggressor/tie text, automatic notices and internal guidance are connected. Focused tests exercise actual tied results in Basic/Advanced, both Habbanya sides, mixed-force Stone finishability before opposing commitments, a real allied Prescience boundary, stale/late/cross-owner rejection, private rival noninterference, receipt corruption, saved recovered-card revision, actual policies at all four AI levels and authenticated SQLite races/seat restoration. Independent review found no material defect. A prior scheduling test now explicitly readies its human holder to save the card instead of allowing the new legal AI play.

Types, lint and 4866 offline tests, production build and 43 HTTP/session tests pass on the unchanged source fingerprint below. Two genuine CHOAM/Richese samples completed with 1279 accepted actions, 34 periodic JSON restores and no rejected candidates. One movement Sapho play occurred; neither sample used aggressor. Targeted tests and browser evidence, not those samples, establish the new effect. Full expansion and module acceptance remain incomplete.

In a new isolated browser room, genuine Advanced faction setup was followed by conserved battle staging. The human Guild used the actual aggressor button, restored the spent card and priority after refresh, readied, selected Master Bewt with zero dial/support, and declined a Traitor call. The staged Emperor opponent used real engine ready/plan/vote actions with Caid and zero dial/support. Guild won the 3-3 tie, retained four forces and 30 spice, and Emperor lost four forces. Final refresh and private JSON views restored the same outcome; the original seat row is unchanged. Screenshot capture timed out, so visual screenshot acceptance is not claimed.

All 1072 original games and original seat/recovery/entry/handover records are unchanged; 1106 rooms remain including isolated QA and HTTP fixtures. SQLite quick-check and foreign keys pass. The healthy server was reused without reset/restart or automation.

The Mirror audit now records why every genuine CHOAM/Richese setup includes unresolved copy candidates: an ordinary-copy-only gate would need hidden eligibility or altered inventory. Mirror stays gated pending the existing disposal ruling and copied-choice work. No mode/publication gate is opened.

Verified source tree: 2fb3160efd713a3f7068aedb474868ba6863bccc420848a56e69e1df1d84441b
2026-09-20 11:45:47 +10:00

93 lines
8.3 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {applyAction,viewGame,normalizeAutomaticGame,type Game,type Action} from '../game/engine';
import {botActions} from '../game/bots';
import {DIFFICULTIES} from '../game/bot-profiles';
import {createStrongholdCards} from '../game/stronghold-cards';
import {territory} from '../game/board';
import {saphoBattleCustody,takeSaphoBattleCard,SAPHO_BATTLE_CARD} from './fixture-sapho-battle-order';
import {saphoAggressorGame,aggressorAction,prepareAggressorPlans,revealEqualAggressorPlans,finishAggressorBattle} from './fixture-sapho-aggressor';
function reject(g:Game,id:string,action:Action){const before=JSON.stringify(g);assert.throws(()=>applyAction(g,id,action));assert.equal(JSON.stringify(g),before);}
const restore=(g:Game)=>{const next=JSON.parse(JSON.stringify(g)) as Game; for(const p of g.players) assert.deepEqual(viewGame(next,p.id),viewGame(g,p.id));saphoBattleCustody(next);return next;};
void test('Sapho changes actual ordinary and Stone ties in both modes without swapping physical slots or chooser',()=>{
for(const advanced of [false,true]) for(const stone of [false,true]) {
let g=saphoAggressorGame({advanced}); const original=structuredClone(g.battle!); const order=[...g.order];
if(stone) takeSaphoBattleCard(g,'a','richese-stone-burner');
const action=aggressorAction(g); const before=JSON.stringify(g);
g=applyAction(g,'b',action); assert.notEqual(JSON.stringify(g),before);
assert.equal(g.battle!.attacker,original.attacker);assert.equal(g.battle!.defender,original.defender);assert.equal(g.battle!.chooser,original.chooser);assert.deepEqual(g.order,order);
for(const p of g.players){const view=viewGame(g,p.id);assert.equal(view.battle!.aggressor,'b');assert.equal(view.battle!.tieWinner,'b');assert.equal(view.players.find(q=>q.id!=='r'&&q.id!==p.id)!.hand,undefined);}
assert.equal(g.discard.filter(c=>c.id===SAPHO_BATTLE_CARD).length,1);reject(g,'b',action);g=restore(g);
g=revealEqualAggressorPlans(g,stone?'a':undefined); g=restore(g);g=finishAggressorBattle(g);
assert.equal(g.lastBattleContext!.winner,'b');assert.equal(g.lastBattleContext!.event,original.event);saphoBattleCustody(g);
}
});
void test('Habbanya takes precedence in either stable slot despite an opposing Sapho aggressor',()=>{
for(const owner of ['a','b']) {
let g=saphoAggressorGame({advanced:true});g.battle!.territory='habbanya_ridge_sietch';
for(const p of g.players.slice(0,2)){p.forces={[`habbanya_ridge_sietch:${territory('habbanya_ridge_sietch').sectors[0]}`]:4};}
g.strongholdCards=createStrongholdCards();g.strongholdCards.claimedTurn=1;g.strongholdCards.owners.habbanya_ridge_sietch=owner;
g=applyAction(g,'b',aggressorAction(g));assert.equal(viewGame(g,'a').battle!.aggressor,'b');assert.equal(viewGame(g,'a').battle!.tieWinner,owner);
g=finishAggressorBattle(revealEqualAggressorPlans(g));assert.equal(g.lastBattleContext!.winner,owner);
}
});
void test('early scope excludes noncombatants, readiness, later commitments and stale actions without writes',()=>{
const opening=saphoAggressorGame();const action=aggressorAction(opening);
reject(opening,'r',action);reject(opening,'a',action);reject(opening,'b',{...action,event:'old'});reject(opening,'b',{...action,mode:'last'});reject(opening,'b',{...action,extra:true});
const ready=applyAction(opening,'b',{type:'battlePreparationReady',event:opening.battle!.event});assert.deepEqual(viewGame(ready,'b').saphoOptions,[]);reject(ready,'b',action);
const planned=prepareAggressorPlans(opening);assert.deepEqual(viewGame(planned,'b').saphoOptions,[]);reject(planned,'b',action);
const revealed=revealEqualAggressorPlans(opening);reject(revealed,'b',action);
for(const field of ['voice','prescience','nexusInspection','fullPlan','truthPromises'] as const){const g=structuredClone(opening);Object.assign(g.battle!,{[field]:field==='truthPromises'?[{}]:{}});assert.throws(()=>applyAction(g,'b',action));}
const earlier=applyAction(opening,'a',{type:'battlePreparationReady',event:opening.battle!.event});assert.ok(aggressorAction(earlier));
});
void test('saved receipts fail closed and legitimate recovered-card reuse has a fresh opportunity',()=>{
let g=saphoAggressorGame();const first=aggressorAction(g);g=applyAction(g,'b',first);
for(const mutate of [(x:Game)=>{delete x.battle!.saphoAggressor;},(x:Game)=>{delete x.battle!.saphoAggressorEvents;},(x:Game)=>{x.battle!.saphoAggressor!.turn++;},(x:Game)=>{x.battle!.saphoAggressor!.uses[0].player='a';},(x:Game)=>{x.battle!.saphoAggressorEvents![0]='old';},(x:Game)=>{x.discard=x.discard.filter(c=>c.id!==SAPHO_BATTLE_CARD);}] ) {
const bad=structuredClone(g);mutate(bad);const before=JSON.stringify(bad);assert.throws(()=>viewGame(bad,'b'));assert.throws(()=>normalizeAutomaticGame(bad));assert.throws(()=>applyAction(bad,'b',{type:'ready'}));assert.equal(JSON.stringify(bad),before);
}
// A conserved relocation models later legal recovery without claiming the recovery action itself.
takeSaphoBattleCard(g,'a');const second=aggressorAction(g,'a');assert.notEqual(second.event,first.event);reject(g,'a',{...second,event:first.event});
g=applyAction(g,'a',second);assert.equal(viewGame(restore(g),'b').battle!.aggressor,'a');assert.equal(g.battle!.saphoAggressor!.uses.length,2);saphoBattleCustody(g);
});
void test('an unchosen allied Prescience prompt allows early Sapho but an actual inspection closes that scope',()=>{
const g=saphoAggressorGame({},g=>{g.players[1].ally='c';g.players[2].ally='b';});
const action=aggressorAction(g);assert.equal(g.battle!.preparation?.kind,'prescience');
const inspecting=normalizeAutomaticGame(applyAction(g,'c',{type:'prescience',field:'weapon'}));
assert.equal(inspecting.battle!.preparation?.kind,'prescienceAnswer');
assert.equal(inspecting.battle!.preLeader!.closed,false);
assert.deepEqual(viewGame(inspecting,'b').saphoOptions,[]);reject(inspecting,'b',action);
});
void test('early priority feeds Stone pre-commit finishability before any hidden opposing plan exists',()=>{
const initial=saphoAggressorGame({advanced:true,holder:'a'},g=>{
g.order=['b','a','c','r'];g.active='b';
const a=g.players[0],b=g.players[1];a.forces={'carthag:11':2};a.reserves=18;
a.elites!.forces={'carthag:11':1};a.elites!.reserves=4;
b.forces={'carthag:11':1};b.reserves=19;takeSaphoBattleCard(g,'b','richese-stone-burner');
});
assert.equal(initial.battle!.attacker,'b');
const plan={type:'battlePlan',dial:0,support:0,leader:initial.players[1].leaders[0].id,weapon:'richese-stone-burner'};
// Original Guild tie priority makes every opposing allocation finishable.
assert.doesNotThrow(()=>applyAction(prepareAggressorPlans(initial),'b',plan));
let changed=applyAction(initial,'a',aggressorAction(initial,'a'));
changed=prepareAggressorPlans(changed);assert.equal(viewGame(changed,'b').battle!.tieWinner,'a');
// The Emperor's possible mixed allocation now spans both sides of the tie.
// Reject the Stone commitment before seeing or accepting an opposing plan.
reject(changed,'b',plan);assert.equal(Object.keys(changed.battle!.plans).length,0);
});
void test('rival private cards and spice never affect the early option or AI, and every profile completes the battle',()=>{
for(const advanced of [false,true]) for(const level of DIFFICULTIES){
let g=saphoAggressorGame({advanced});const original=viewGame(g,'b');const expected=original.saphoOptions;
const changed=structuredClone(g);changed.players[0].spice=0;takeSaphoBattleCard(changed,'a','richese-stone-burner');assert.deepEqual(viewGame(changed,'b').saphoOptions,expected);
const view=viewGame(g,'b');view.players.find(p=>p.id==='b')!.bot=level;
const actions=botActions(view);assert.equal(actions[0].scope,'battleAggressor');
for(const p of view.players.filter(p=>p.id!=='b'))for(const key of ['hand','spice','traitors','faceDancers'])Object.defineProperty(p,key,{get(){throw new Error('Unauthorized rival field');}});
assert.deepEqual(botActions(view),actions);g=applyAction(g,'b',actions[0]);const event=g.battle!.event;
for(const p of g.players)p.bot=level;
for(let step=0;g.battle&&step<100;step++){
const next=g.players.map(p=>({player:p.id,action:botActions(viewGame(g,p.id))[0]})).find(next=>next.action);
assert.ok(next);g=applyAction(g,next.player,next.action);g=restore(g);
}
assert.equal(g.battle,null);assert.equal(g.lastBattleContext!.event,event);saphoBattleCustody(g);
}
});