import { describe, expect, it } from "vitest";
import {
  extractFlockPendingReference,
  formatFlockRatesPendingMessage,
  isFlockRatesPendingText,
  parseFlockMoney,
  parseFlockTransit,
} from "@/workers/rpa/flock/quote-extract";
import { isFlockNonRetryableFailure } from "@/lib/flock/exclusive-lock";

describe("parseFlockMoney", () => {
  it("解析 Prices from 文案", () => {
    expect(parseFlockMoney("Prices from$1,925Transit(incl. weekends)4 days")).toBe(
      1925,
    );
    expect(parseFlockMoney("Prices from $1,187")).toBe(1187);
  });
});

describe("parseFlockTransit", () => {
  it("解析含周末时效", () => {
    const r = parseFlockTransit(
      "Prices from$1,925Transit(incl. weekends)4 daysGuaranteed",
    );
    expect(r.transitDays).toBe("4");
  });

  it("解析工作日预估", () => {
    const r = parseFlockTransit(
      "Prices from$1,187Transit(business days only)Contact Rep",
    );
    expect(r.transitDescription).toMatch(/business days only|工作日/);
  });
});

describe("Flock rates pending 页", () => {
  const sample =
    "#NCC-ZHZ4 pricing options\nExpires Jul 17, 2026\nHold tight, we are still looking for the best rates!\nWe should have a rate for you in about 15 minutes.";

  it("识别 Hold tight 待价文案", () => {
    expect(isFlockRatesPendingText(sample)).toBe(true);
    expect(isFlockRatesPendingText("Prices from $100")).toBe(false);
  });

  it("从标题抽取参考编号", () => {
    expect(extractFlockPendingReference(sample)).toBe("NCC-ZHZ4");
  });

  it("待价结果禁止 BullMQ 重跑", () => {
    const msg = formatFlockRatesPendingMessage("NCC-ZHZ4");
    expect(msg).toMatch(/FLOCK_RATES_PENDING/);
    expect(msg).toContain("NCC-ZHZ4");
    expect(isFlockNonRetryableFailure(msg)).toBe(true);
  });
});
