import { test, expect } from "@playwright/test";
import { fillQuoteForm, mockQuoteApi } from "./helpers";

test.describe("查价页 E2E 核心路径（task-089）", () => {
  test.beforeEach(async ({ page }) => {
    await page.goto("/embed-demo");
    await expect(
      page.getByRole("heading", { name: "卡派查价" }),
    ).toBeVisible({ timeout: 30_000 });
  });

  test("路径1：L2 缓存命中 → 四档报价展示", async ({ page }) => {
    await mockQuoteApi(page, "cache");
    await fillQuoteForm(page);
    await page.getByRole("button", { name: "获取报价" }).click();

    await expect(page.getByRole("button", { name: "标准" })).toBeVisible({
      timeout: 15_000,
    });
    await expect(page.getByRole("button", { name: "保证送达" })).toBeVisible();
  });

  test("路径2：processing → Skeleton → 成功", async ({ page }) => {
    await mockQuoteApi(page, "processing");
    await fillQuoteForm(page);
    await page.getByRole("button", { name: "获取报价" }).click();

    await expect(page.getByText("正在查询报价")).toBeVisible();
    await expect(page.getByRole("button", { name: "标准" })).toBeVisible({
      timeout: 15_000,
    });
  });

  test("路径3：stale 降级 → WarningBanner", async ({ page }) => {
    await mockQuoteApi(page, "stale");
    await fillQuoteForm(page);
    await page.getByRole("button", { name: "获取报价" }).click();

    await expect(
      page.getByText("非实时报价，仅供参考（已使用历史缓存降级）"),
    ).toBeVisible({ timeout: 15_000 });
  });

  test("路径4：API 错误 → ErrorBanner + 重试", async ({ page }) => {
    await mockQuoteApi(page, "error");
    await fillQuoteForm(page);
    await page.getByRole("button", { name: "获取报价" }).click();

    await expect(page.getByRole("button", { name: "重试" })).toBeVisible({
      timeout: 10_000,
    });
  });

  test("路径5：空闲态占位文案", async ({ page }) => {
    await expect(page.getByText("填写信息后获取报价")).toBeVisible();
    await expect(page.getByText("四档运价")).toBeVisible();
  });
});
