|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | +import { readFile } from "node:fs/promises"; |
| 3 | + |
| 4 | +async function download(page, button) { |
| 5 | + const pending = page.waitForEvent("download"); |
| 6 | + await page.getByRole("button", { name: button, exact: true }).click(); |
| 7 | + const file = await pending; |
| 8 | + const output = test.info().outputPath(file.suggestedFilename()); |
| 9 | + await file.saveAs(output); |
| 10 | + return { name: file.suggestedFilename(), text: await readFile(output, "utf8") }; |
| 11 | +} |
| 12 | +async function open(page) { |
| 13 | + await page.goto("/#/models"); |
| 14 | + await expect(page.getByRole("heading", { name: "Your models", exact: true })).toBeVisible(); |
| 15 | +} |
| 16 | + |
| 17 | +test("Python model configuration saves, reloads and exports a runnable evaluation", async ({ page }) => { |
| 18 | + await open(page); |
| 19 | + await page.getByLabel("Snapshot name", { exact: true }).fill("connect4-policy-v1"); |
| 20 | + await page.getByLabel("Python factory", { exact: true }).fill("my_agent:build_policy"); |
| 21 | + await page.getByLabel("Factory options (JSON)", { exact: true }).fill('{"checkpoint":"weights/v1.pt","device":"cpu"}'); |
| 22 | + await page.getByRole("button", { name: "Save configuration", exact: true }).click(); |
| 23 | + await expect(page.getByRole("status").filter({ hasText: "Configuration saved in this browser." })).toBeVisible(); |
| 24 | + const exported = await download(page, "Download agent.json"); |
| 25 | + expect(exported.name).toBe("agent.json"); |
| 26 | + expect(JSON.parse(exported.text)).toEqual({ id: "connect4-policy-v1", type: "python", factory: "my_agent:build_policy", params: { checkpoint: "weights/v1.pt", device: "cpu" } }); |
| 27 | + await page.reload(); |
| 28 | + await page.getByRole("button", { name: "Load connect4-policy-v1", exact: true }).click(); |
| 29 | + await expect(page.getByLabel("Factory options (JSON)", { exact: true })).toHaveValue(/weights\/v1.pt/); |
| 30 | + await page.getByLabel("Evaluation game", { exact: true }).selectOption("tictactoe"); |
| 31 | + await expect(page.getByLabel("Evaluation game", { exact: true }).locator("option")).toHaveCount(3); |
| 32 | + await page.getByLabel("Evaluation opponent", { exact: true }).selectOption("minimax"); |
| 33 | + await page.getByLabel("Opponent level", { exact: true }).selectOption("3"); |
| 34 | + await page.getByLabel("Games per opponent", { exact: true }).fill("20"); |
| 35 | + await page.getByLabel("Save training trajectories", { exact: true }).check(); |
| 36 | + await page.getByLabel("Dataset format", { exact: true }).selectOption("parquet"); |
| 37 | + await expect(page.getByTestId("evaluation-command")).toHaveText("gfa tournament --game tictactoe --agents agent.json --opponents minimax:3 --games 20 --seed 42 --report tournament-report.json --trajectories training-run-001 --format parquet"); |
| 38 | + await page.getByText("Try a single episode first", { exact: true }).click(); |
| 39 | + const script = await download(page, "Download episode script"); |
| 40 | + expect(script.name).toBe("try-model.py"); |
| 41 | + expect(script.text).toContain('env = make("tictactoe", opponent="random")'); |
| 42 | + expect(script.text).toContain('load_document("agent.json")'); |
| 43 | + await page.getByRole("button", { name: "Remove connect4-policy-v1", exact: true }).click(); |
| 44 | + await page.reload(); |
| 45 | + await expect(page.getByRole("article", { name: "connect4-policy-v1", exact: true })).toHaveCount(0); |
| 46 | +}); |
| 47 | + |
| 48 | +test("HTTP model exports environment-based credentials without contacting the endpoint", async ({ page }) => { |
| 49 | + const external = []; |
| 50 | + page.on("request", request => { if (new URL(request.url()).hostname === "model.example") external.push(request.url()); }); |
| 51 | + await open(page); |
| 52 | + await page.getByLabel("Snapshot name", { exact: true }).fill("http-model-v2"); |
| 53 | + await page.getByRole("radio", { name: /HTTP model/ }).check(); |
| 54 | + await page.getByLabel("API root", { exact: true }).fill("https://model.example/v1/"); |
| 55 | + await page.getByLabel("Model identifier", { exact: true }).fill("my-model"); |
| 56 | + await page.getByLabel("API key environment variable (optional)", { exact: true }).fill("MY_MODEL_KEY"); |
| 57 | + await page.getByText("Response settings", { exact: true }).click(); |
| 58 | + await page.getByLabel("Use structured output", { exact: true }).uncheck(); |
| 59 | + await page.getByLabel("Output tokens", { exact: true }).fill("512"); |
| 60 | + await page.getByLabel("Temperature", { exact: true }).fill("0.3"); |
| 61 | + const file = await download(page, "Download agent.json"); |
| 62 | + expect(JSON.parse(file.text)).toEqual({ id: "http-model-v2", type: "chat", base_url: "https://model.example/v1", model: "my-model", api_key_env: "MY_MODEL_KEY", timeout: 60, max_tokens: 512, temperature: 0.3, structured: false }); |
| 63 | + await page.getByRole("button", { name: "Save configuration", exact: true }).click(); |
| 64 | + expect(external).toEqual([]); |
| 65 | + expect(await page.evaluate(() => localStorage.getItem("gamesforai.models.v1"))).not.toContain('"api_key":'); |
| 66 | +}); |
| 67 | + |
| 68 | +test("invalid settings are explained and cannot produce a run configuration", async ({ page }) => { |
| 69 | + await open(page); |
| 70 | + await page.getByRole("button", { name: "Save configuration", exact: true }).click(); |
| 71 | + await expect(page.getByRole("alert")).toContainText("Give this snapshot a name"); |
| 72 | + await page.getByLabel("Snapshot name", { exact: true }).fill("test-policy"); |
| 73 | + await page.getByLabel("Factory options (JSON)", { exact: true }).fill('{"seed":99}'); |
| 74 | + await page.getByRole("button", { name: "Save configuration", exact: true }).click(); |
| 75 | + await expect(page.getByRole("alert")).toContainText("Remove seed and seat"); |
| 76 | + await expect(page.getByRole("button", { name: "Download agent.json", exact: true })).toBeDisabled(); |
| 77 | + await page.getByRole("radio", { name: /HTTP model/ }).check(); |
| 78 | + await page.getByLabel("API root", { exact: true }).fill("https://user:secret@model.example/v1"); |
| 79 | + await page.getByRole("button", { name: "Save configuration", exact: true }).click(); |
| 80 | + await expect(page.getByRole("alert")).toContainText("without credentials"); |
| 81 | + await page.getByLabel("API root", { exact: true }).fill("http://localhost:8000/v1/chat/completions"); |
| 82 | + await expect(page.getByRole("alert")).toContainText("Use the API root ending in /v1"); |
| 83 | + await page.getByLabel("API root", { exact: true }).fill("http://localhost:8000/v1"); |
| 84 | + await page.getByLabel("Model identifier", { exact: true }).fill("local-model"); |
| 85 | + await page.getByLabel("Games per opponent", { exact: true }).fill("3"); |
| 86 | + await expect(page.getByRole("alert")).toContainText("even number of games"); |
| 87 | + await expect(page.getByTestId("evaluation-command")).toHaveCount(0); |
| 88 | +}); |
| 89 | + |
| 90 | +test("mobile model setup supports keyboard, download and unavailable browser storage", async ({ page, context }) => { |
| 91 | + await page.setViewportSize({ width: 360, height: 800 }); |
| 92 | + await context.addInitScript(() => { |
| 93 | + Storage.prototype.getItem = () => { throw new Error("blocked"); }; |
| 94 | + Storage.prototype.setItem = () => { throw new Error("blocked"); }; |
| 95 | + }); |
| 96 | + await open(page); |
| 97 | + await page.getByRole("button", { name: "New", exact: true }).click(); |
| 98 | + await expect(page.getByLabel("Snapshot name", { exact: true })).toBeFocused(); |
| 99 | + await page.getByLabel("Snapshot name", { exact: true }).fill("mobile-policy"); |
| 100 | + await page.getByRole("button", { name: "Save configuration", exact: true }).click(); |
| 101 | + await expect(page.getByRole("alert").filter({ hasText: "Browser storage is unavailable or full." })).toBeVisible(); |
| 102 | + expect(JSON.parse((await download(page, "Download agent.json")).text).id).toBe("mobile-policy"); |
| 103 | + expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)).toBe(true); |
| 104 | +}); |
| 105 | + |
| 106 | +test("the library links to model setup and clipboard failure leaves selectable config", async ({ page }) => { |
| 107 | + await page.goto("/"); |
| 108 | + await page.getByRole("link", { name: "Set up a model", exact: false }).click(); |
| 109 | + await page.getByLabel("Snapshot name", { exact: true }).fill("clipboard-policy"); |
| 110 | + await page.getByText("Configuration preview", { exact: true }).click(); |
| 111 | + await page.evaluate(() => { Object.defineProperty(navigator, "clipboard", { value: undefined, configurable: true }); }); |
| 112 | + await page.getByRole("button", { name: "Copy configuration", exact: true }).click(); |
| 113 | + await expect(page.getByRole("status").filter({ hasText: "Copy unavailable." })).toBeVisible(); |
| 114 | + await expect(page.getByTestId("model-config")).toContainText("clipboard-policy"); |
| 115 | +}); |
0 commit comments