// SPEC: PJ2026-010405 云端控制台.
// Implementation reference: draft-2026-07-13-p0-cloud-console.
// Responsibility: 浏览器验证 OpenCode iframe load/retry 状态与 MDTODO 分栏键盘调整。

import { expect, test } from "../fixtures/test";

test("OpenCode waits for the iframe and recovers through the retry action", async ({ page }) => {
  await page.addInitScript(() => {
    window.HWLAB_CLOUD_WEB_CONFIG = {
      ...(window.HWLAB_CLOUD_WEB_CONFIG ?? {}),
      opencode: { url: "/opencode" }
    };
  });

  let frameUrlRequests = 0;
  await page.route("**/opencode/frame-url", async (route) => {
    frameUrlRequests += 1;
    if (frameUrlRequests === 1) {
      await route.fulfill({ status: 503, contentType: "application/json", body: JSON.stringify({ error: "temporary_unavailable" }) });
      return;
    }
    await route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify({ url: "/__e2e/opencode-frame?ticket=redacted" }) });
  });
  await page.route("**/__e2e/opencode-frame**", async (route) => {
    await route.fulfill({ status: 200, contentType: "text/html", body: "<!doctype html><title>OpenCode fixture</title><main>OpenCode ready</main>" });
  });

  await page.goto("/opencode");
  await expect(page.getByTestId("opencode-load-state")).toHaveText("error");
  await page.getByRole("button", { name: "重试" }).click();
  await expect(page.locator('iframe[title="OpenCode"]')).toBeVisible();
  await expect(page.getByTestId("opencode-load-state")).toHaveText("ready");
  expect(frameUrlRequests).toBe(2);
});

test("MDTODO split separator is focusable and adjusts its bounded value", async ({ page }) => {
  await page.goto("/projects/mdtodo");
  const separator = page.getByRole("separator", { name: "调整左侧面板宽度" });
  await expect(separator).toBeVisible();
  await expect(separator).toHaveAttribute("aria-valuemin", "22");
  await expect(separator).toHaveAttribute("aria-valuemax", "30");

  const initialValue = Number(await separator.getAttribute("aria-valuenow"));
  await separator.focus();
  await page.keyboard.press("ArrowRight");
  await expect(separator).toBeFocused();
  await expect(separator).toHaveAttribute("aria-valuenow", String(Math.min(30, initialValue + 2)));
});
