|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | +const site = "http://127.0.0.1:18081"; |
| 3 | +const examples = [ |
| 4 | + ["tictactoe", {}, ["r1c1", "r2c1", "r1c2", "r2c2", "r1c3"]], |
| 5 | + ["connect4", {}, ["1", "2", "1", "2", "1", "2", "1"]], |
| 6 | + ["chess", {}, ["e2e4", "e7e5", "g1f3"]], |
| 7 | + ["sudoku", { config: { size: 4 }, start: { position: "123434122143432." } }, ["r4c4+1", "r4c4=1"]], |
| 8 | +]; |
| 9 | +async function fixture(request, game, extra, moves) { |
| 10 | + const response = await request.post("/v1/matches", { data: { game_id: game, include_info: false, ...extra } }); |
| 11 | + expect(response.ok()).toBe(true); |
| 12 | + let state = await response.json(); |
| 13 | + const id = state.match_id; |
| 14 | + for (const action of moves) { |
| 15 | + const result = await request.post(`/v1/matches/${id}/actions`, { data: { seat: state.to_act[0], turn: state.turn, action, reasoning: "Recorded browser strategy at turn " + state.turn } }); |
| 16 | + expect(result.ok()).toBe(true); |
| 17 | + state = (await result.json()).state; |
| 18 | + } |
| 19 | + return id; |
| 20 | +} |
| 21 | +for (const [game, extra, moves] of examples) test(`${game} replays the selected turn and branches without changing its parent`, async ({ page, request }) => { |
| 22 | + if (game === "sudoku") await page.setViewportSize({ width: 360, height: 800 }); |
| 23 | + const id = await fixture(request, game, extra, moves); |
| 24 | + const original = await (await request.get(`/v1/matches/${id}/replay`)).json(); |
| 25 | + await page.goto(site + `/#/matches/${id}/replay?turn=1`); |
| 26 | + await expect(page.getByTestId("replay-turn")).toHaveText(`Turn 1 of ${moves.length}`); |
| 27 | + await page.getByText("Text board", { exact: true }).click(); |
| 28 | + await expect(page.locator(".spectator-text pre")).toHaveText(original.states[1].observation.text); |
| 29 | + await page.getByRole("button", { name: "Branch from here", exact: true }).click(); |
| 30 | + expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)).toBe(true); |
| 31 | + await page.getByRole("button", { name: "Branch and play", exact: true }).click(); |
| 32 | + await expect(page.getByLabel("Legal move", { exact: true })).toBeEnabled(); |
| 33 | + const child = /#\/matches\/([^/?]+)/.exec(page.url())[1]; |
| 34 | + expect(child).not.toBe(id); |
| 35 | + await expect(page.locator(".turn-counter")).toHaveText("Turn 0"); |
| 36 | + const metadata = await (await request.get(`/v1/matches/${child}`)).json(); |
| 37 | + expect(metadata.forked_from).toEqual({ match_id: id, turn: 1 }); |
| 38 | + await page.getByLabel("Legal move", { exact: true }).selectOption({ index: 1 }); |
| 39 | + await expect(page.locator(".turn-counter")).toHaveText("Turn 1"); |
| 40 | + expect(await (await request.get(`/v1/matches/${id}/replay`)).json()).toEqual(original); |
| 41 | + await page.getByRole("link", { name: "Replay and branch", exact: true }).click(); |
| 42 | + await expect(page.getByRole("region", { name: "Variations", exact: true })).toContainText(id); |
| 43 | + await page.getByRole("link", { name: "Parent at turn 1", exact: true }).click(); |
| 44 | + await expect(page.getByTestId("replay-turn")).toHaveText(`Turn 1 of ${moves.length}`); |
| 45 | + await expect(page.getByRole("link", { name: "Variation from turn 1", exact: true })).toBeVisible(); |
| 46 | +}); |
| 47 | + |
| 48 | +test("replay timeline, perspective, autoplay, reasoning and turn links stay in sync", async ({ page, request, context }) => { |
| 49 | + const id = await fixture(request, ...examples[0]); |
| 50 | + await context.grantPermissions(["clipboard-read", "clipboard-write"]); |
| 51 | + await page.goto(site + `/#/matches/${id}/replay?turn=2`); |
| 52 | + await expect(page.getByTestId("replay-turn")).toHaveText("Turn 2 of 5"); |
| 53 | + await page.getByRole("button", { name: "Next", exact: true }).click(); |
| 54 | + await expect(page.getByTestId("replay-turn")).toHaveText("Turn 3 of 5"); |
| 55 | + await page.getByLabel("Replay turn").press("ArrowLeft"); |
| 56 | + await expect(page.getByTestId("replay-turn")).toHaveText("Turn 2 of 5"); |
| 57 | + await page.getByLabel("Replay perspective").selectOption("1"); |
| 58 | + await expect(page.getByTestId("replay-turn")).toHaveText("Turn 2 of 5"); |
| 59 | + await page.getByRole("button", { name: "Copy turn link", exact: true }).click(); |
| 60 | + await expect(page.getByRole("status").filter({ hasText: "Turn link copied." })).toBeVisible(); |
| 61 | + expect(await page.evaluate(() => navigator.clipboard.readText())).toBe(site + `/#/matches/${id}/replay?turn=2&seat=1`); |
| 62 | + await page.getByText("Reasoning", { exact: true }).first().click(); |
| 63 | + await expect(page.getByText("Recorded browser strategy at turn 0", { exact: true })).toBeVisible(); |
| 64 | + await page.getByRole("button", { name: "First", exact: true }).click(); |
| 65 | + await page.getByLabel("Playback speed").selectOption("4"); |
| 66 | + await page.getByRole("button", { name: "Play", exact: true }).click(); |
| 67 | + await expect(page.getByTestId("replay-turn")).toHaveText("Turn 5 of 5"); |
| 68 | + await expect(page.getByRole("button", { name: "Play", exact: true })).toBeVisible(); |
| 69 | + await page.reload(); |
| 70 | + await expect(page.getByTestId("replay-turn")).toHaveText("Turn 5 of 5"); |
| 71 | +}); |
| 72 | + |
| 73 | +test("a replay can branch against an installed opponent and show recorded estimates", async ({ page, request }) => { |
| 74 | + const id = await fixture(request, ...examples[0]); |
| 75 | + await page.goto(site + `/#/matches/${id}/replay?turn=1`); |
| 76 | + await page.getByRole("button", { name: "Branch from here", exact: true }).click(); |
| 77 | + await page.getByLabel("Variation players").selectOption("opponent"); |
| 78 | + await page.getByLabel("Variation opponent").selectOption("minimax"); |
| 79 | + await page.getByRole("button", { name: "Branch and play", exact: true }).click(); |
| 80 | + await expect(page.locator(".turn-counter")).toHaveText("Turn 1"); |
| 81 | + const child = /#\/matches\/([^/?]+)/.exec(page.url())[1]; |
| 82 | + const state = await (await request.get(`/v1/matches/${child}/state?seat=0`)).json(); |
| 83 | + await request.post(`/v1/matches/${child}/resign`, { data: { seat: 0, turn: state.turn } }); |
| 84 | + await page.goto(site + `/#/matches/${child}/replay`); |
| 85 | + await expect(page.getByRole("img", { name: "Recorded engine estimates", exact: true })).toBeVisible(); |
| 86 | + await page.getByText("Agent details", { exact: true }).click(); |
| 87 | + await expect(page.locator(".agent-record pre")).toContainText("evaluation"); |
| 88 | +}); |
0 commit comments