BGS lets the only human of a game against bots take back their last saved move (protocol 0.10.0). The top toolbar shows an undo control while BGS reports it available and the board is live (hidden in replays, analysis, to spectators, once the game is over and while a move is pending); it asks BGS to undo, which then sends the earlier position. That position resets local selections (shown colony, selected amoeba, new amoeba number), also when the moves were taken back elsewhere, and drops replay positions cached beyond it. On the narrowest phones the header tightens to fit the extra control. The chat preview emulates it locally. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
245 lines
8.9 KiB
JavaScript
245 lines
8.9 KiB
JavaScript
// "Undo my move": BGS lets the only human of a game against bots take back
|
|
// their last saved move (protocol 0.10.0 `undo:available` / `undo`).
|
|
import { serveAssets } from "./asset-server.mjs";
|
|
const assetBase = await serveAssets("dist");
|
|
import assert from "node:assert/strict";
|
|
import { chromium } from "playwright";
|
|
import * as E from "../engine/index.js";
|
|
import { prepared } from "../viewer/tutorials.js";
|
|
|
|
// The human's (seat 0) turns, each after their move and the bots' replies.
|
|
function nextTurn(state) {
|
|
let next = E.moveAI(state, 0);
|
|
for (let i = 0; E.currentPlayer(next) !== 0; i++) {
|
|
assert.ok(i < 500);
|
|
next = E.moveAI(next, E.currentPlayer(next));
|
|
}
|
|
return next;
|
|
}
|
|
let first = E.init(4, [], {}, "undo-browser");
|
|
first.players.forEach((p, i) => (p.name = ["You", "Maya", "Leo", "Nina"][i]));
|
|
for (let i = 0; first.round < 2 || E.currentPlayer(first) !== 0; i++) {
|
|
assert.ok(i < 500);
|
|
first = E.moveAI(first, E.currentPlayer(first));
|
|
}
|
|
const second = nextTurn(first);
|
|
const third = nextTurn(second);
|
|
assert.ok(first.history.length < second.history.length);
|
|
assert.ok(second.history.length < third.history.length);
|
|
// A round apart, the same division task; only the move count tells them apart.
|
|
assert.deepEqual(third.tasks[0], second.tasks[0]);
|
|
assert.equal(second.tasks[0].kind, "division");
|
|
// Placing an amoeba stays undoable without a new saved move; the amoeba number
|
|
// chosen next must not survive taking the placement back.
|
|
const dividing = prepared({
|
|
phase: 3,
|
|
bp: 20,
|
|
task: { kind: "division", p: 0 },
|
|
});
|
|
const divided = E.move(
|
|
dividing,
|
|
E.legalMoves(dividing).find((m) => m.type === "divide" && m.n === 3),
|
|
0,
|
|
);
|
|
assert.equal(divided.history.length, dividing.history.length);
|
|
|
|
const browser = await chromium.launch({
|
|
executablePath: process.env.CHROMIUM_EXECUTABLE,
|
|
});
|
|
try {
|
|
for (const width of [320, 1400]) {
|
|
const page = await browser.newPage({ viewport: { width, height: 1000 } });
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
await page.setContent(
|
|
'<!doctype html><meta name="viewport" content="width=device-width,initial-scale=1"><div id="app"></div>',
|
|
);
|
|
await page.addStyleTag({ url: assetBase + "/viewer.css" });
|
|
await page.addScriptTag({ url: assetBase + "/viewer.js" });
|
|
let live;
|
|
await page.exposeFunction("loadReplay", (range) => E.logSlice(live, range));
|
|
await page.evaluate(() => {
|
|
window.e = primordialSoup.launch("#app");
|
|
window.moves = [];
|
|
window.undos = 0;
|
|
window.logRequests = [];
|
|
window.logResponses = 0;
|
|
e.on("move", (m) => moves.push(m));
|
|
e.on("undo", () => undos++);
|
|
e.on("fetchLog", async (range) => {
|
|
logRequests.push(range);
|
|
e.emit("gamelog", { ...range, data: await loadReplay(range) });
|
|
logResponses++;
|
|
});
|
|
e.emit("player", { index: 0 });
|
|
});
|
|
const send = (name, value) =>
|
|
page.evaluate(
|
|
async ([name, value]) => {
|
|
e.emit(name, value);
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
},
|
|
[name, value],
|
|
);
|
|
const show = (state) => {
|
|
live = state;
|
|
return send("state", E.stripSecret(state, 0));
|
|
};
|
|
const undo = page.locator('[data-ui="undo-move"]');
|
|
const shown = (visible) =>
|
|
page.waitForFunction(
|
|
(visible) =>
|
|
!document.querySelector('[data-ui="undo-move"]').hidden === visible,
|
|
visible,
|
|
);
|
|
const replayed = async () => {
|
|
await page.waitForFunction(
|
|
(count) => logResponses === count,
|
|
await page.evaluate(() => logRequests.length),
|
|
);
|
|
await send("replay:end");
|
|
};
|
|
const heading = page.locator(".colony-heading h2");
|
|
const picker = page.locator('.amoeba-picker [aria-label="Amoeba number"]');
|
|
const destination = () =>
|
|
page
|
|
.locator(".water-cell.valid")
|
|
.first()
|
|
.getAttribute("data-destination");
|
|
|
|
await show(second);
|
|
assert.ok(await undo.isHidden(), "hidden until BGS offers undo");
|
|
await send("undo:available", true);
|
|
assert.ok(await undo.isVisible());
|
|
assert.equal(await undo.getAttribute("title"), "Undo my move");
|
|
assert.equal(await undo.getAttribute("aria-label"), "Undo my move");
|
|
const box = await undo.boundingBox();
|
|
assert.ok(box.x >= 0 && box.x + box.width <= width);
|
|
assert.ok(
|
|
await page.evaluate(
|
|
() => document.documentElement.scrollWidth <= innerWidth,
|
|
),
|
|
`no horizontal overflow at ${width}`,
|
|
);
|
|
await page.screenshot({ path: `/tmp/soup-undo-${width}.png` });
|
|
|
|
// Not offered while a move is pending, in replays, analysis, to
|
|
// spectators or once the game is over.
|
|
await page.locator("[data-move]:visible").first().click();
|
|
assert.equal(await page.evaluate(() => moves.length), 1);
|
|
assert.ok(await undo.isHidden(), "hidden while a move is pending");
|
|
await show(second);
|
|
await shown(true);
|
|
await send("replay:start");
|
|
assert.ok(await undo.isHidden(), "hidden while replaying");
|
|
await replayed();
|
|
assert.ok(await undo.isVisible());
|
|
await send("preferences", { analysis: true });
|
|
await shown(false);
|
|
await send("preferences", { analysis: false });
|
|
await shown(true);
|
|
await send("player", {});
|
|
assert.ok(await undo.isHidden(), "hidden from spectators");
|
|
await send("player", { index: 0 });
|
|
assert.ok(await undo.isVisible());
|
|
await send("state", second);
|
|
assert.equal(
|
|
await page.locator(".phase-label").textContent(),
|
|
"Game ended",
|
|
);
|
|
assert.ok(await undo.isHidden(), "hidden once the game is over");
|
|
await show(second);
|
|
assert.ok(await undo.isVisible());
|
|
|
|
// Clicking asks BGS; the earlier position then replaces the local view.
|
|
await page.locator('[data-player="2"]').click();
|
|
assert.equal(await heading.textContent(), "Leo");
|
|
await undo.click();
|
|
assert.equal(await page.evaluate(() => undos), 1);
|
|
assert.equal(await page.evaluate(() => moves.length), 1);
|
|
await show(first);
|
|
assert.equal(
|
|
await page.locator(".colony-heading .eyebrow").textContent(),
|
|
"YOUR COLONY",
|
|
);
|
|
assert.equal(await heading.textContent(), "You");
|
|
assert.equal(
|
|
await page.locator(".round-number").textContent(),
|
|
String(first.round),
|
|
);
|
|
assert.equal(
|
|
await page.locator(".organisms .amoeba-button").count(),
|
|
first.amoebas.length,
|
|
);
|
|
assert.deepEqual(
|
|
await page.locator(".player-bp").allTextContents(),
|
|
first.players.map((p) => String(p.bp)),
|
|
);
|
|
assert.equal(
|
|
(await page.locator(".last-move").textContent()).trim(),
|
|
first.log.at(-1).text,
|
|
);
|
|
assert.ok(await undo.isVisible(), "BGS may offer earlier moves too");
|
|
// Replay must not reuse positions cached before the moves were taken back.
|
|
await send("replay:start");
|
|
assert.deepEqual(await page.evaluate(() => logRequests.at(-1)), {
|
|
start: 0,
|
|
end: first.history.length - 1,
|
|
});
|
|
await replayed();
|
|
|
|
// Moves taken back elsewhere (another tab): fewer moves, same task.
|
|
const specimen = () =>
|
|
page
|
|
.locator('.amoeba-button[aria-pressed="true"]')
|
|
.getAttribute("data-amoeba");
|
|
await show(third);
|
|
await picker.selectOption("6");
|
|
await page.locator('[data-player="2"]').click();
|
|
assert.equal(await heading.textContent(), "Leo");
|
|
assert.match(await specimen(), /^2-/);
|
|
await show(second);
|
|
assert.equal(await picker.inputValue(), "4");
|
|
assert.match(await destination(), /^#4 /);
|
|
assert.equal(await heading.textContent(), "You");
|
|
assert.match(await specimen(), /^0-/);
|
|
|
|
// Same number of saved moves: the undo request alone marks the rewind.
|
|
await show(dividing);
|
|
await show(divided);
|
|
await picker.selectOption("6");
|
|
assert.match(await destination(), /^#6 /);
|
|
await undo.click();
|
|
assert.equal(await page.evaluate(() => undos), 2);
|
|
await show(dividing);
|
|
assert.equal(await picker.inputValue(), "3");
|
|
assert.match(await destination(), /^#3 /);
|
|
assert.equal(
|
|
await page.locator(".bio-balance strong").textContent(),
|
|
String(dividing.players[0].bp),
|
|
);
|
|
|
|
await send("preferences", { locale: "fr" });
|
|
await page.waitForFunction(
|
|
() =>
|
|
document.querySelector('[data-ui="undo-move"]')?.title ===
|
|
"Annuler mon coup",
|
|
);
|
|
assert.equal(await undo.getAttribute("aria-label"), "Annuler mon coup");
|
|
await send("preferences", { locale: "en" });
|
|
await page.waitForFunction(
|
|
() =>
|
|
document.querySelector('[data-ui="undo-move"]')?.title ===
|
|
"Undo my move",
|
|
);
|
|
await send("undo:available", false);
|
|
assert.ok(await undo.isHidden(), "hidden again once unavailable");
|
|
assert.deepEqual(errors, []);
|
|
await page.close();
|
|
console.log(
|
|
`Undo my move ${width}: availability, pending moves, replay, analysis, spectators, ended games, earlier positions, selections and translation passed`,
|
|
);
|
|
}
|
|
} finally {
|
|
await browser.close();
|
|
}
|