import { describe, expect, it } from "vitest";
import {
  buildAlertPresentation,
  formatCustomerAddressForAdmin,
  formatSelectedAddressForAdmin,
  resolveAlertRootCause,
} from "@/modules/alert/alert-presentation";

describe("alert-presentation", () => {
  it("区分客户填写地址与联想确认地址", () => {
    const addr = {
      street: "123 Main St",
      city: "Denver",
      state: "CO",
      zip: "80202",
      formatted_address: "123 Main St, Denver, CO",
      mothership_display_label: "123 Main St, Denver, CO 80202, USA",
    };
    expect(formatCustomerAddressForAdmin(addr)).toBe(
      "123 Main St, Denver, CO, 80202",
    );
    expect(formatSelectedAddressForAdmin(addr)).toBe(
      "123 Main St, Denver, CO 80202, USA",
    );
  });

  it("将 browserType.launch 转为可读根因", () => {
    const cause = resolveAlertRootCause("RPA_FAILED", {
      reason: "browserType.launch: Executable doesn't exist",
    });
    expect(cause).toContain("无法启动浏览器");
    expect(cause).not.toContain("{");
  });

  it("优先展示 detail.message 而非 STRUCT_CHANGE 默认文案", () => {
    const cause = resolveAlertRootCause("STRUCT_CHANGE", {
      message: "ADDRESS_NOT_CONFIRMED: 派送地址未在 MotherShip 确认",
    });
    expect(cause).toContain("已选地址");
    expect(cause).not.toContain("页面结构变更");
  });

  it("QUOTE_ENTRY_UNAVAILABLE 展示入口不可用根因", () => {
    const cause = resolveAlertRootCause("RPA_FAILED", {
      message: "QUOTE_ENTRY_UNAVAILABLE: 无法打开 MotherShip 报价页",
      entry_error: "QUOTE_ENTRY_UNAVAILABLE",
    });
    expect(cause).toContain("报价页面无法打开");
  });

  it("locator.waitFor 超时译为中文并指出控件", () => {
    const cause = resolveAlertRootCause("RPA_FAILED", {
      message:
        "locator.waitFor: Timeout 8000ms exceeded. Call log: - waiting for getByTestId('address-book-accessorials-option-appointment').first() to be visible",
    });
    expect(cause).toContain("自动化操作超时");
    expect(cause).toContain("预约送货");
    expect(cause).not.toMatch(/locator\.waitFor|getByTestId|Call log/i);
  });

  it("报价单 errorMessage 中的英文 Playwright 日志同样中文化", () => {
    const cause = resolveAlertRootCause(
      "RPA_FAILED",
      { message: "ignored" },
      {
        errorMessage:
          "locator.waitFor: Timeout 8000ms exceeded. waiting for getByTestId('quote-create-pickup-input-search')",
        errorCode: "RPA_DATA_INVALID",
      } as never,
    );
    expect(cause).toContain("提货地址搜索框");
    expect(cause).not.toMatch(/locator\.waitFor/i);
  });

  it("buildAlertPresentation 包含客户与货物字段", () => {
    const presentation = buildAlertPresentation(
      "RPA_FAILED",
      { reason: "test", customer_id: "CUST_001" },
      {
        customerId: "CUST_001",
        pickupJson: {
          street: "A",
          city: "B",
          state: "CO",
          zip: "80000",
          mothership_display_label: "A, B, CO",
        },
        deliveryJson: {
          street: "C",
          city: "D",
          state: "MA",
          zip: "02101",
          mothership_display_label: "C, D, MA",
        },
        weightLb: 500,
        palletCount: 2,
        cargoType: "general_freight",
        dimLIn: 48,
        dimWIn: 40,
        dimHIn: 48,
      } as never,
    );
    expect(presentation.customer_id).toBe("CUST_001");
    expect(presentation.pickup_customer).toContain("A");
    expect(presentation.pickup_selected).toBe("A, B, CO");
    expect(presentation.cargo_summary).toContain("2 托");
  });
});
