import { describe, expect, it, vi } from "vitest";
import { assertRpaSelectorsEnv, getSelector } from "@/lib/rpa/selectors";

const REQUIRED = {
  RPA_SELECTOR_PICKUP_STREET: "role=textbox",
  RPA_SELECTOR_DELIVERY_STREET: "role=textbox",
  RPA_SELECTOR_ADDRESS_SUGGESTION: ".pac-item",
  RPA_SELECTOR_SUBMIT: "role=button",
  RPA_SELECTOR_QUOTE_WIDGET: "#react-quoter-landing-plugin",
} as const;

describe("assertRpaSelectorsEnv", () => {
  it("FASTEST 留空时仍可通过启动校验", () => {
    vi.stubEnv("RPA_MOCK_MODE", "false");
    for (const [key, value] of Object.entries(REQUIRED)) {
      vi.stubEnv(key, value);
    }
    vi.stubEnv("RPA_SELECTOR_FASTEST_OPTION", "");
    expect(() => assertRpaSelectorsEnv()).not.toThrow();
    vi.unstubAllEnvs();
  });

  it("env 为空时使用内置双语回退", () => {
    vi.stubEnv("RPA_MOCK_MODE", "false");
    for (const key of Object.keys(REQUIRED)) {
      vi.stubEnv(key, "");
    }
    vi.stubEnv("RPA_SELECTOR_FASTEST_OPTION", "");
    expect(() => assertRpaSelectorsEnv()).not.toThrow();
    vi.unstubAllEnvs();
  });

  it("RPA_SELECTOR_QUOTE_WIDGET 显式空串时 getSelector 走内置回退，禁止空 selector", () => {
    vi.stubEnv("RPA_MOCK_MODE", "false");
    for (const [key, value] of Object.entries(REQUIRED)) {
      vi.stubEnv(key, value);
    }
    vi.stubEnv("RPA_SELECTOR_QUOTE_WIDGET", "");
    expect(getSelector("RPA_SELECTOR_QUOTE_WIDGET")).toBe(
      "#react-quoter-landing-plugin",
    );
    vi.unstubAllEnvs();
  });
});
