Cover full host preferences and home orientation regression

This commit is contained in:
coyotte508 2026-11-15 23:40:33 +00:00
commit 7af8257c42
5 changed files with 346 additions and 0 deletions

View file

@ -0,0 +1,38 @@
# Home-at-bottom host preferences regression
The reported production failure was not camera math: platform badge SVGs were
inlined as `data:` URLs. The protocol requires absolute HTTP(S) badge URLs, so a
single invalid badge rejected the **entire** preferences event, including
`homeAtBottom`. The platform fix emits SVG assets with HTTP(S) URLs; Clash must
not weaken URL validation to accept unsafe or relative URLs.
`src/host-preferences.test.ts` covers rejection of the full snapshot at the
reported badge paths, successful delivery of the complete synthetic host
preferences, and continued rejection of unsafe URLs. It runs in `npm test`.
For rendered-camera coverage, install Playwright Chromium, prepare the engine
bridge (`npm run build:bridge`), and run:
```sh
npm run test:home-orientation
```
This builds the viewer and checks every seat in 2-, 3-, and 4-player games in
both 3D and strategy modes with the full synthetic host preferences. It checks
callback ordering, state refresh, toggles, resize, fullscreen, side panels,
and manual camera orbit/reset. The rejected inline-badge snapshot is reproduced
through the real viewer emitter before sending valid preferences.
An optional anonymous public gameplay response can be exercised locally:
```sh
node scripts/home-orientation-smoke.mjs --snapshot=/tmp/public-game.json --seat=1
```
The script requires `secrets_stripped: true` in the game state. Keep downloaded
snapshots outside the repository. This is an offline viewer harness: it does not
submit live game moves. Synthetic preferences avoid capturing account metadata.
These are regression tests only: no engine/viewer version bump or BGS publication
is required. Passing them does not establish that the separate platform fix has
been deployed.

View file

@ -8,6 +8,7 @@
"build": "vite build",
"check": "svelte-check --tsconfig ./tsconfig.json",
"test": "node --experimental-strip-types --test src/*.test.ts && node --test scripts/publish-gallery.test.mjs && npm run test:achievement-assets && npm run test:locales",
"test:home-orientation": "npm run build && node scripts/home-orientation-smoke.mjs",
"build:achievement-assets": "node scripts/build-achievement-assets.mjs",
"test:achievement-assets": "node --test scripts/achievement-assets.test.mjs scripts/publish-achievement-assets.test.mjs scripts/publish-achievement-backups.test.mjs",
"build:bridge": "bash scripts/build-bridge.sh",

View file

@ -0,0 +1,212 @@
// Run after npm run build. Exercise the real BGS viewer callbacks and rendered camera,
// not a second implementation of the camera math.
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import { chromium } from 'playwright';
import { hostPreferences } from './host-preferences-fixture.mjs';
const engine = createRequire(import.meta.url)('../.engine/server.js');
// Optional anonymous /api/gameplay response, kept outside the repository:
// node scripts/home-orientation-smoke.mjs --snapshot=/tmp/public-game.json --seat=1
// Never strip it again: retain public setup log and board history for home resolution.
const snapshotPath = process.argv.find((arg) => arg.startsWith('--snapshot='))?.slice(11);
const seatArg = process.argv.find((arg) => arg.startsWith('--seat='))?.slice(7);
const selectedSeat = seatArg === undefined ? undefined : Number(seatArg);
if (selectedSeat !== undefined) assert(Number.isInteger(selectedSeat) && selectedSeat >= 0);
const snapshot = snapshotPath ? JSON.parse(await readFile(snapshotPath, 'utf8')) : undefined;
const snapshotRaw = snapshot
? typeof snapshot.data === 'string'
? snapshot.data
: JSON.stringify(snapshot)
: undefined;
const snapshotGame = snapshotRaw ? JSON.parse(snapshotRaw) : undefined;
if (snapshotGame)
assert.equal(snapshotGame.secrets_stripped, true, 'Only use a public, secrets-stripped snapshot');
const counts = snapshotGame
? [snapshotGame.players.filter((p) => !['Barbarians', 'Pirates'].includes(p.civilization)).length]
: [2, 3, 4];
const browser = await chromium.launch({
headless: true,
args: ['--use-gl=angle', '--use-angle=swiftshader'],
});
let checks = 0;
try {
for (const count of counts) {
if (selectedSeat !== undefined) assert(selectedSeat < count, 'Seat must be a human player index');
const raw = snapshotRaw ?? (await engine.init(count, [], {}, 'home-orientation', {}));
const game = JSON.parse(raw);
const homes = game.players.slice(0, count).map((p) => p.cities[0].position);
for (const mapView of ['3d', 'strategy']) {
const page = await browser.newPage({ viewport: { width: 1400, height: 900 } });
const errors = [];
page.on('pageerror', (e) => errors.push(e.message));
page.on('console', (message) => {
if (
message.type() === 'warning' &&
/event: preferences|invalid_format|Invalid payload/.test(message.text())
)
errors.push(message.text());
});
await page.route('**/*', async (route) => {
const p = new URL(route.request().url()).pathname;
if (p === '/')
return route.fulfill({
contentType: 'text/html',
body: '<!doctype html><meta charset="utf-8"><div id="app"></div><script src="/viewer.js"></script>',
});
if (!['/viewer.js', '/server_bg.wasm'].includes(p)) return route.abort();
return route.fulfill({
contentType: p.endsWith('.wasm') ? 'application/wasm' : 'text/javascript',
body: await readFile(new URL('../dist' + p, import.meta.url)),
});
});
const settle = () =>
page.evaluate(
() => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve))),
);
const positions = async () => {
await settle();
await page.waitForTimeout(350); // Let strategy-label CSS transforms finish.
return page.evaluate(
({ homes, mapView }) =>
homes.map((home) => {
const node =
mapView === 'strategy'
? document.querySelector(`.strategy-map-tile[data-position="${home}"]`)
: [...document.querySelectorAll('.city-map-label')].find((n) =>
n.getAttribute('aria-label').includes(`city ${home} ·`),
);
if (!node) throw Error(`Missing home ${home}`);
const box = node.getBoundingClientRect();
return { x: box.x + box.width / 2, y: box.y + box.height / 2 };
}),
{ homes, mapView },
);
};
const bottom = async (seat, stage) => {
const points = await positions();
assert(
points.every((p, i) => i === seat || points[seat].y > p.y + 5),
`${count}p ${mapView} seat ${seat} ${stage}: ${JSON.stringify(points)}`,
);
assert.deepEqual(errors, []);
checks++;
};
for (let seat = 0; seat < count; seat++) {
if (selectedSeat !== undefined && seat !== selectedSeat) continue;
for (const order of ['player-first', 'state-first', 'preferences-last']) {
await page.goto('http://clash.test/');
const publicState = snapshotRaw ?? engine.stripSecret(raw, seat);
await page.evaluate(
({ publicState, seat, preferences, order }) => {
window.host = clash3d.launch('#app');
// Reproduce the production failure through the real registered emitter,
// then deliver the fixed platform snapshot with HTTP(S) badge assets.
const broken = structuredClone(preferences);
broken.bgs.players[0].badges[0].url = 'data:image/svg+xml,%3Csvg%3E%3C/svg%3E';
const warn = console.warn;
const diagnostics = [];
console.warn = (diagnostic) => diagnostics.push(diagnostic);
let accepted;
try {
accepted = host.emit('preferences', broken);
} finally {
console.warn = warn;
}
if (accepted || diagnostics.length !== 1 || diagnostics[0].event !== 'preferences')
throw Error('Expected inlined badge to reject the full preferences event');
const emit = host.emit.bind(host);
host.emit = (event, payload) => {
const result = emit(event, payload);
if (event === 'preferences' && !result) throw Error('Valid host preferences rejected');
return result;
};
window.prefs = preferences;
if (order !== 'preferences-last') host.emit('preferences', prefs);
if (order === 'player-first') host.emit('player', { index: seat });
host.emit('state', publicState);
},
{ publicState, seat, preferences: hostPreferences(mapView, count), order },
);
await page
.locator(
order === 'preferences-last'
? '.city-map-label'
: mapView === 'strategy'
? '.strategy-map-tile'
: '.city-map-label',
)
.first()
.waitFor();
await settle();
if (order !== 'player-first')
await page.evaluate((seat) => host.emit('player', { index: seat }), seat);
if (order === 'preferences-last') await page.evaluate(() => host.emit('preferences', prefs));
await page
.locator(mapView === 'strategy' ? '.strategy-map-tile' : '.city-map-label')
.first()
.waitFor();
await bottom(seat, order);
await page.evaluate((publicState) => host.emit('state', publicState + ' '), publicState);
await bottom(seat, 'state refresh');
await page.evaluate(() => {
prefs.homeAtBottom = false;
host.emit('preferences', prefs);
});
const normal = await positions();
await page.evaluate(() => {
prefs.homeAtBottom = true;
host.emit('preferences', prefs);
});
await bottom(seat, 'enable after disable');
await page.evaluate(() => {
prefs.homeAtBottom = false;
host.emit('preferences', prefs);
});
assert.deepEqual(await positions(), normal, 'disabling restores the standard camera');
await page.evaluate(() => {
prefs.homeAtBottom = true;
host.emit('preferences', prefs);
});
}
await page.setViewportSize({ width: 1000, height: 700 });
await bottom(seat, 'resize');
await page.getByRole('button', { name: 'Enter fullscreen', exact: true }).click();
await page.waitForFunction(() => !!document.fullscreenElement);
await bottom(seat, 'fullscreen');
await page.evaluate(() => document.exitFullscreen());
await page.setViewportSize({ width: 1400, height: 900 });
await bottom(seat, 'exit fullscreen');
const homeLabel =
mapView === 'strategy'
? page.locator(`.strategy-map-tile[data-position="${homes[seat]}"]`)
: page.locator(`.city-map-label[aria-label*="city ${homes[seat]} ·"]`);
await homeLabel.click();
await bottom(seat, 'city side panel');
const closePanel = page.getByRole('button', { name: 'Close tile actions', exact: true });
if (await closePanel.isVisible()) await closePanel.click();
if (mapView === '3d') {
const canvas = page.locator('canvas[aria-label*="civilization map"]');
const box = await canvas.boundingBox();
await page.mouse.move(box.x + box.width / 2, box.y + 40);
await page.mouse.down();
await page.mouse.move(box.x + box.width / 2 + 160, box.y + 40, { steps: 8 });
await page.mouse.up();
const rotated = await positions();
await page.evaluate(() => host.emit('preferences', prefs));
assert.deepEqual(await positions(), rotated, 'preference snapshot must not lock the user camera');
await canvas.focus();
await page.keyboard.press('Home');
await bottom(seat, 'explicit camera reset after orbit');
}
}
await page.close();
console.log(
`${count}p ${mapView}: ${selectedSeat === undefined ? 'every seat' : `seat ${selectedSeat}`} and event order passed`,
);
}
}
console.log(`${checks} rendered home-bottom assertions passed`);
} finally {
await browser.close();
}

View file

@ -0,0 +1,32 @@
// Synthetic public metadata, not a captured player/account payload. Badge images
// use HTTP(S), matching the platform fix: emit assets, not inlined SVG data URLs.
export function hostPreferences(mapView = '3d', count = 3) {
const badge = (id, url) => ({
id,
label: `Badge ${id}`,
shortLabel: id,
description: 'Public achievement',
url,
});
const safe = badge('supporter', 'https://assets.example.test/supporter.svg');
return {
locale: 'en',
sound: false,
replayAutoplay: false,
mapView,
homeAtBottom: true,
platformChat: true,
preferredPlayerColors: Array(count).fill(null),
bgs: {
players: Array.from({ length: count }, (_, seat) => ({
pro: seat === 1,
badges:
seat === 1
? [safe, badge('achievement', 'https://assets.example.test/achievement.svg')]
: [badge('achievement', 'https://assets.example.test/achievement.svg')],
})),
playerColors: ['#ff0000', '#00ff00', '#0000ff', '#ffff00'].slice(0, count),
supporterBadge: { label: 'Supporter', url: safe.url },
},
};
}

View file

@ -0,0 +1,63 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { ViewerEmitter } from '@boardgamers/protocol/viewer';
import { viewerPreferencesSchema } from '@boardgamers/protocol';
import { hostPreferences } from '../scripts/host-preferences-fixture.mjs';
test('inlined platform badges reject the entire preferences snapshot at the reported paths', () => {
const input = hostPreferences();
for (const [seat, badge] of [
[0, 0],
[1, 1],
[2, 0],
]) {
input.bgs.players[seat].badges[badge].url = 'data:image/svg+xml,%3Csvg%3E%3C/svg%3E';
}
const parsed = viewerPreferencesSchema.safeParse(input);
assert.equal(parsed.success, false);
if (!parsed.success)
assert.deepEqual(
parsed.error.issues.map((issue) => issue.path),
[
['bgs', 'players', 0, 'badges', 0, 'url'],
['bgs', 'players', 1, 'badges', 1, 'url'],
['bgs', 'players', 2, 'badges', 0, 'url'],
],
);
const diagnostics: unknown[] = [];
const emitter = new ViewerEmitter({ onInvalid: (d) => diagnostics.push(d) });
let delivered = false;
emitter.on('preferences', () => {
delivered = true;
});
assert.equal(emitter.emit('preferences', input), false);
assert.equal(delivered, false, 'homeAtBottom never reaches the viewer');
assert.equal(diagnostics.length, 1);
assert.match(JSON.stringify(diagnostics), /https/);
});
test('HTTP asset badges deliver the complete preferences snapshot, including homeAtBottom', () => {
const input = hostPreferences();
const diagnostics: unknown[] = [];
const emitter = new ViewerEmitter({ onInvalid: (d) => diagnostics.push(d) });
let delivered: unknown;
emitter.on('preferences', (value) => {
delivered = value;
});
assert.equal(emitter.emit('preferences', input), true);
assert.deepEqual(delivered, input);
assert.deepEqual(diagnostics, []);
});
test('badge URL validation remains strict; unsafe and relative URLs are not accepted', () => {
for (const url of [
'/badge.svg',
'//example.test/badge.svg',
'data:image/svg+xml,<svg/>',
'javascript:alert(1)',
]) {
const input = hostPreferences();
input.bgs.players[0].badges[0].url = url;
assert.equal(viewerPreferencesSchema.safeParse(input).success, false);
}
});