uci: the engine as a Web Worker in a browser, on the same protocol machine; game-review: the process job is native

On WebAssembly the engine is a Web Worker: a line of UCI is one message each
way, the protocol machine is driven by the worker's messages and by timers set
for its deadlines; the handshake does not block (Engine::start_worker gives a
Starting, polled until readyok). EngineHandle has the same methods as the
process one, minus those that block or name a process. Option and search
checks are shared by both transports.

tests/worker.rs runs the worker transport against a fake engine in JavaScript
in headless Firefox (apps/web/check/uci-worker.mjs: wasm-bindgen-test-runner
serves, a WebDriver BiDi client reads the outcome): handshake, analysis and
stop, coalescing of rapid requests without mixing output, an engine hung on
stop, a crash, and every handshake failure. 5 of 5 pass.

game-review: the job in engine processes (run and its workers) moves to
job/engines.rs, built where there are processes. .cargo/config.toml: the
getrandom backend of WebAssembly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-30 14:29:12 +03:00
parent a66d1016d9
commit 2703e5de38
16 changed files with 1369 additions and 339 deletions

View file

@ -0,0 +1,60 @@
// The tests of uci's worker transport (crates/uci/tests/worker.rs) in headless
// Firefox. wasm-bindgen-test-runner serves them (NO_HEADLESS: it does not look for a
// WebDriver) and this script opens its page and reads the outcome.
//
// node apps/web/check/uci-worker.mjs
//
// Needs wasm-bindgen-test-runner of the wasm-bindgen version in Cargo.lock in PATH
// or in WASM_BINDGEN_TEST_RUNNER.
import { spawn } from 'node:child_process';
import { launch } from './firefox.mjs';
import { sleep, tab } from './bidi.mjs';
const runner = process.env.WASM_BINDGEN_TEST_RUNNER || 'wasm-bindgen-test-runner';
const address = '127.0.0.1:18731';
const cargo = spawn(
'cargo',
['test', '-p', 'uci', '--target', 'wasm32-unknown-unknown', '--test', 'worker'],
{
env: {
...process.env,
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: runner,
NO_HEADLESS: '1',
WASM_BINDGEN_TEST_ADDRESS: address,
},
stdio: ['ignore', 'pipe', 'inherit'],
// Its own process group: the runner it starts is stopped with it.
detached: true,
},
);
const stopCargo = () => {
try {
process.kill(-cargo.pid);
} catch {}
};
let served = false;
cargo.stdout.on('data', (d) => {
if (String(d).includes(address)) served = true;
});
for (let i = 0; i < 1200 && !served; i++) await sleep(100);
if (!served) {
console.error('the test runner did not start');
stopCargo();
process.exit(1);
}
const ff = await launch();
let failed = true;
try {
const t = await tab(ff.browser, 1000, 800, () => {});
await t.navigate(`http://${address}/`);
const text = await t.waitFor(
`(() => { const t = document.body.innerText; return /test result: /.test(t) && t; })()`,
60000,
);
console.log(text.trim());
failed = !/test result: ok\./.test(text);
} finally {
await ff.stop();
stopCargo();
}
process.exit(failed ? 1 : 0);