import { describe, expect, it } from "vitest";
import {
  mapPageTextToErrorCode,
  mapUrlToErrorCode,
} from "@/modules/rpa/error-mapper";

describe("error-mapper", () => {
  it("无运力页 → CARRIER_NO_CAPACITY", () => {
    expect(mapPageTextToErrorCode("No carriers available for this lane")).toBe(
      "CARRIER_NO_CAPACITY",
    );
  });

  it("不支持邮编 → ADDRESS_NOT_SUPPORTED", () => {
    expect(mapPageTextToErrorCode("This zip code is not supported")).toBe(
      "ADDRESS_NOT_SUPPORTED",
    );
  });

  it("验证码 URL → RPA_CAPTCHA", () => {
    expect(mapUrlToErrorCode("https://example.com/captcha")).toBe(
      "RPA_CAPTCHA",
    );
  });

  it("login URL 无凭据 → null（由 adapter 判 STRUCT_CHANGE）", () => {
    delete process.env.MOTHERSHIP_EMAIL;
    delete process.env.MOTHERSHIP_PASSWORD;
    expect(mapUrlToErrorCode("https://example.com/login")).toBe(null);
  });

  it("login URL 有凭据 → PROVIDER_LOGIN_FAILED", () => {
    process.env.MOTHERSHIP_EMAIL = "test@example.com";
    process.env.MOTHERSHIP_PASSWORD = "secret";
    expect(mapUrlToErrorCode("https://example.com/login")).toBe(
      "PROVIDER_LOGIN_FAILED",
    );
  });
});
