import { describe, expect, it, vi } from "vitest";
import { RpaError } from "@/modules/rpa/errors";
import {
  isDirectErrorFatalWithoutWidget,
  shouldFallbackToWidgetAfterDirectError,
} from "@/lib/rpa/direct-quote-fallback";

describe("direct-quote-fallback", () => {
  it("CARRIER_NO_CAPACITY 且 Widget fallback 开启时应回退浏览器", () => {
    vi.stubEnv("RPA_DISABLE_WIDGET_QUOTE_FALLBACK", "");
    const err = new RpaError(
      "CARRIER_NO_CAPACITY",
      "MotherShip 该线路暂无可用报价，请调整地址或货物后重试",
      { retryable: false },
    );
    expect(shouldFallbackToWidgetAfterDirectError(err)).toBe(true);
    expect(isDirectErrorFatalWithoutWidget(err)).toBe(false);
  });

  it("RPA_DISABLE_WIDGET_QUOTE_FALLBACK=true 时 direct 无运力仍 fatal", () => {
    vi.stubEnv("RPA_DISABLE_WIDGET_QUOTE_FALLBACK", "true");
    const err = new RpaError("CARRIER_NO_CAPACITY", "无运力", {
      retryable: false,
    });
    expect(shouldFallbackToWidgetAfterDirectError(err)).toBe(false);
    expect(isDirectErrorFatalWithoutWidget(err)).toBe(true);
  });

  it("STRUCT_CHANGE 仍禁止 Widget 回退", () => {
    vi.stubEnv("RPA_DISABLE_WIDGET_QUOTE_FALLBACK", "");
    const err = new RpaError("STRUCT_CHANGE", "结构变更", { retryable: false });
    expect(isDirectErrorFatalWithoutWidget(err)).toBe(true);
  });
});
