Compare predicted states independently of nested map field order
This commit is contained in:
parent
3a3e1b87a4
commit
42f6b8d77c
6 changed files with 108 additions and 4 deletions
|
|
@ -175,3 +175,8 @@ Recruitment logs attribute Draft, Sanitation, Sparta and Mercenaries only when t
|
|||
# Viewer 0.1.64 / engine unchanged (0.4.61)
|
||||
|
||||
Trade Routes reward amounts now swap directly between engine-approved combinations. Raising mood, gold or another reward reduces food first, preserves other choices when possible, and always keeps a complete legal reward. Taxes and other reward allocations retain their existing limits. Desktop/mobile smoke coverage exercises India's food/gold/mood choices and verifies the submitted reward.
|
||||
|
||||
|
||||
# Viewer 0.1.65 / engine unchanged (0.4.61)
|
||||
|
||||
Prediction and server-acknowledgement fingerprints now canonicalize nested object fields, preserving array order and all visible values. Rust map serialization order could previously make equivalent states compare unequal, intermittently forcing deterministic actions such as Theaters to wait for the backend. Regression coverage checks semantic differences and completing Theaters before either server acknowledgement. The browser smoke test can also accept a local pre-Theaters state via THEATERS_STATE; no live game moves are submitted.
|
||||
|
|
|
|||
4
web-client/package-lock.json
generated
4
web-client/package-lock.json
generated
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "@boardgamers/clash-web-client",
|
||||
"version": "0.1.64",
|
||||
"version": "0.1.65",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@boardgamers/clash-web-client",
|
||||
"version": "0.1.64",
|
||||
"version": "0.1.65",
|
||||
"dependencies": {
|
||||
"@boardgamers/protocol": "0.9.0",
|
||||
"lucide-svelte": "^0.577.0",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@boardgamers/clash-web-client",
|
||||
"version": "0.1.64",
|
||||
"version": "0.1.65",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
|
|
|||
52
web-client/scripts/theaters-smoke.mjs
Normal file
52
web-client/scripts/theaters-smoke.mjs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import assert from 'node:assert/strict';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { createRequire } from 'node:module';
|
||||
import { chromium } from 'playwright';
|
||||
const engine = createRequire(import.meta.url)('../.engine/server.js');
|
||||
const npcs = JSON.parse(await engine.init(2, [], {}, 'abilities-ui', {})).players.slice(2);
|
||||
async function fixture(name) {
|
||||
const game = JSON.parse(
|
||||
await readFile(new URL(`../../server/tests/test_games/${name}.json`, import.meta.url), 'utf8'),
|
||||
);
|
||||
for (const npc of npcs)
|
||||
if (!game.players.some((p) => p.civilization === npc.civilization))
|
||||
game.players.push({ ...npc, id: game.players.length });
|
||||
return game;
|
||||
}
|
||||
const browser = await chromium.launch({
|
||||
headless: true,
|
||||
args: ['--use-gl=angle', '--use-angle=swiftshader'],
|
||||
});
|
||||
try {
|
||||
for (const [width,height] of [[1400,900],[390,740]]) {
|
||||
const state=process.env.THEATERS_STATE ? JSON.parse(await readFile(process.env.THEATERS_STATE,'utf8')) : await fixture('advances/sanitation_and_draft');
|
||||
const seat=process.env.THEATERS_STATE ? 1 : 0;
|
||||
if(!process.env.THEATERS_STATE) {state.players[0].advances.push('Arts','Theaters');state.players[0].resources.mood_tokens=2;state.players[0].resources.culture_tokens=2;}
|
||||
const page=await browser.newPage({viewport:{width,height}}),errors=[];
|
||||
page.on('pageerror',e=>errors.push(e.message));
|
||||
await page.route('**/*',async route=>{
|
||||
const path=new URL(route.request().url()).pathname;
|
||||
if(path==='/') return route.fulfill({contentType:'text/html',body:'<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><div id="app"></div><script src="/viewer.js"></script>'});
|
||||
if(!['/viewer.js','/server_bg.wasm'].includes(path)) return route.abort();
|
||||
return route.fulfill({contentType:path.endsWith('.wasm')?'application/wasm':'text/javascript',body:await readFile(new URL('../dist'+path,import.meta.url))});
|
||||
});
|
||||
await page.goto('http://clash.test');
|
||||
await page.evaluate(({state,seat})=>{window.host=clash3d.launch('#app');window.sent=[];host.on('move',a=>sent.push(a));host.emit('player',{index:seat});host.emit('theme',{dark:true});host.emit('preferences',{sound:false});host.emit('state',state)},{state:engine.stripSecret(JSON.stringify(state),seat),seat});
|
||||
await page.getByRole('button',{name:'Abilities and cultural influence',exact:true}).click();
|
||||
const start=performance.now();
|
||||
await page.getByRole('button',{name:/Use Theaters/}).click();
|
||||
const pay=page.getByRole('button',{name:/^Pay 1 Mood/i});
|
||||
await pay.waitFor();
|
||||
assert.equal(await pay.isEnabled(),true);
|
||||
const elapsed=performance.now()-start;
|
||||
assert.equal(await page.evaluate(()=>sent.length),1);
|
||||
await page.screenshot({path:`/tmp/clash-theaters-${width}.png`});
|
||||
await pay.click();
|
||||
await page.waitForFunction(()=>!document.querySelector('.decision-panel'));
|
||||
assert.equal(await page.evaluate(()=>sent.length),1,'Conversion is queued and shown without a backend response');
|
||||
assert.deepEqual(errors,[]);
|
||||
console.log({width,choiceReadyMs:Math.round(elapsed),serverAcknowledgements:0});
|
||||
await page.close();
|
||||
}
|
||||
console.log('Theaters desktop/mobile smoke passed');
|
||||
} finally {await browser.close();}
|
||||
|
|
@ -504,3 +504,43 @@ test('Spy waits before looking at a masked hand without trapping the local engin
|
|||
const next = fixture('advances/collect_free_economy', ore);
|
||||
assert.ok('raw' in predictMove(engine, strip(next), ore, 0));
|
||||
});
|
||||
|
||||
|
||||
test('prediction fingerprints ignore nested map field order but preserve values and array order', () => {
|
||||
const a = { players: [{ id: 0, resources: { food: 2, gold: 1 }, event_info: { first: 'a', second: 'b' } }], events: [{ details: { target: 'A1', amount: 1 } }] };
|
||||
const b = { events: [{ details: { amount: 1, target: 'A1' } }], players: [{ event_info: { second: 'b', first: 'a' }, resources: { gold: 1, food: 2 }, id: 0 }] };
|
||||
assert.equal(fingerprint(JSON.stringify(a)), fingerprint(JSON.stringify(b)));
|
||||
b.players[0].resources.food = 3;
|
||||
assert.notEqual(fingerprint(JSON.stringify(a)), fingerprint(JSON.stringify(b)));
|
||||
assert.notEqual(fingerprint('{"choices":[1,2]}'), fingerprint('{"choices":[2,1]}'));
|
||||
});
|
||||
|
||||
test('Theaters opens its direction choice and completes locally before any server acknowledgement', async () => {
|
||||
const g = JSON.parse(fixture('advances/sanitation_and_draft', { Playing: 'EndTurn' }));
|
||||
g.players[0].advances.push('Arts', 'Theaters');
|
||||
g.players[0].resources.mood_tokens = 2;
|
||||
g.players[0].resources.culture_tokens = 2;
|
||||
// Multiple nested map entries exercise hash-map iteration in engine serialization.
|
||||
g.players[0].event_info = { first: 'a', second: 'b', third: 'c' };
|
||||
const raw = JSON.stringify(g), app = harness();
|
||||
try {
|
||||
app.controller.setPlayer(0);
|
||||
await app.controller.load(strip(raw));
|
||||
const theater = app.session().view!.specialActions!.find(a => a.name === 'Theaters')!;
|
||||
app.controller.submit(theater.action);
|
||||
assert.equal(app.sent.length, 1);
|
||||
assert.equal(app.session().pending, false);
|
||||
assert.equal(app.session().view!.decision?.name, 'Theaters');
|
||||
const response = app.controller.query<{action: any}>({kind: 'decision', values: [], payments: [{mood_tokens: 1}]}).action;
|
||||
app.controller.submit(response);
|
||||
assert.equal(app.sent.length, 1, 'Second move is queued without waiting to update the UI');
|
||||
assert.equal(app.session().pending, false);
|
||||
assert.equal(app.session().game!.players[0].resources!.culture_tokens, 3);
|
||||
const first = move(raw, theater.action);
|
||||
await app.controller.load(strip(first));
|
||||
assert.equal(app.sent.length, 2);
|
||||
await app.controller.load(strip(move(first, response)));
|
||||
assert.equal(app.session().pending, false);
|
||||
assert.equal(app.session().game!.players[0].resources!.culture_tokens, 3);
|
||||
} finally { app.close(); }
|
||||
});
|
||||
|
|
|
|||
|
|
@ -28,7 +28,14 @@ export function fingerprint(raw: string): string {
|
|||
delete game.rng;
|
||||
delete game.seed;
|
||||
delete game.messages;
|
||||
return JSON.stringify(Object.fromEntries(Object.entries(game).sort(([a], [b]) => (a < b ? -1 : 1))));
|
||||
// Rust maps can serialize in different insertion orders between executions.
|
||||
// Canonicalize every object, including nested player/event data; array order
|
||||
// still matters for actions, choices and history.
|
||||
return JSON.stringify(game, (_key, value) =>
|
||||
value && typeof value === 'object' && !Array.isArray(value)
|
||||
? Object.fromEntries(Object.entries(value).sort(([a], [b]) => (a < b ? -1 : 1)))
|
||||
: value,
|
||||
);
|
||||
}
|
||||
|
||||
/** The same public state with every kind of masked information resampled. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue