import fs from "node:fs";
import path from "node:path";
import { chromium } from "playwright";
import { ensureBrowser, getCdpEndpoint } from "../../agent/browser-launcher.js";
import { startLocalTestServer } from "../local-test-server.js";
import { buildSuiteResult, ok, runTest, skipped } from "../suite-utils.js";
export const browserSuite = {
    id: "browser",
    name: "Browser",
    run: async (context) => {
        const started = Date.now();
        const localServer = await startLocalTestServer();
        const tests = [];
        try {
            tests.push(await runTest("browser", "browser.ensure_browser", "Ensure browser", async () => {
                await ensureBrowser();
                return ok("Browser ensured");
            }));
            const browser = await chromium.connectOverCDP(getCdpEndpoint());
            const ctx = await browser.newContext();
            try {
                tests.push(await runTest("browser", "browser.local_page", "Local page interaction", async () => {
                    const page = await ctx.newPage();
                    await page.goto(`${localServer.baseUrl}/e2e/browser?runId=${context.runId}`);
                    await page.fill("#text-input", `VisionClaw E2E ${context.runId}`);
                    await page.click("#click-button");
                    await page.check("#checkbox");
                    const result = await page.locator("#result").textContent();
                    if (result !== "checked")
                        throw new Error(`Unexpected result: ${result ?? "null"}`);
                    return ok("Local page interactions succeeded");
                }));
                tests.push(await runTest("browser", "browser.screenshot", "Screenshot", async () => {
                    const page = ctx.pages().at(0);
                    if (page === undefined)
                        return skipped("No Playwright page available");
                    const screenshotPath = path.join(context.artifactsDir, "browser-screenshot.png");
                    await page.screenshot({ path: screenshotPath });
                    if (!fs.existsSync(screenshotPath))
                        throw new Error("Screenshot not saved");
                    return ok("Screenshot captured", { screenshotPath });
                }));
                if (context.mode === "smoke" || context.mode === "browser") {
                    tests.push(await runTest("browser", "browser.network", "Network capture", async () => {
                        const resp = await fetch(`${localServer.baseUrl}/api/echo?runId=${context.runId}`);
                        if (!resp.ok)
                            throw new Error(`Echo endpoint failed: ${resp.status}`);
                        return ok("Network request succeeded");
                    }));
                }
            }
            finally {
                await ctx.close();
            }
        }
        finally {
            await localServer.close();
        }
        return buildSuiteResult("browser", "Browser", started, tests);
    },
};
//# sourceMappingURL=browser.js.map