import { describe, expect, it } from "vitest";
import {
  buildPriority1CanonicalTestCases,
  generateRandomExpeditedCases,
  generateRandomFtlCases,
  generateRandomPriority1Cases,
  parsePriority1DemoFromEnv,
  PRIORITY1_CANONICAL_PATHS,
} from "@/workers/rpa/priority1/demo-input";

describe("priority1 demo-input", () => {
  it("canonical 全矩阵 24 条", () => {
    expect(PRIORITY1_CANONICAL_PATHS).toHaveLength(24);
    const ltl = PRIORITY1_CANONICAL_PATHS.filter((p) => p.id.startsWith("P"));
    const ftl = PRIORITY1_CANONICAL_PATHS.filter((p) => p.id.startsWith("F"));
    const exp = PRIORITY1_CANONICAL_PATHS.filter((p) => p.id.startsWith("E"));
    expect(ltl).toHaveLength(9);
    expect(ftl).toHaveLength(11);
    expect(exp).toHaveLength(4);
  });

  it("导出系统联调用例含 API 载荷", () => {
    const cases = buildPriority1CanonicalTestCases({
      customerId: "CUST_X",
      requestIdPrefix: "req",
    });
    expect(cases).toHaveLength(24);
    expect(cases[0]?.apiPayload.request_id).toBe("req-P01-ltl-residential");
    expect(cases[0]?.apiPayload.customer_id).toBe("CUST_X");
    expect(cases.find((c) => c.id === "E03-expedited-large")?.apiPayload.priority1_input.weightLb).toBe(
      "10000",
    );
  });
  it("相同 seed 生成相同用例", () => {
    const a = generateRandomPriority1Cases(10, 42);
    const b = generateRandomPriority1Cases(10, 42);
    expect(a).toEqual(b);
    expect(a).toHaveLength(10);
    expect(a[0]?.id).toBe("P01");
  });

  it("起运与目的 ZIP 不相同", () => {
    const cases = generateRandomPriority1Cases(10, 99);
    for (const c of cases) {
      expect(c.input.originZip).not.toBe(c.input.destinationZip);
      expect(c.input.originZip).toMatch(/^\d{5}$/);
    }
  });

  it("生成 FTL / Expedited 用例", () => {
    const ftl = generateRandomFtlCases(3, 42);
    const exp = generateRandomExpeditedCases(3, 42);
    expect(ftl[0]?.input.shipmentType).toBe("ftl");
    expect(exp[0]?.input.shipmentType).toBe("expedited");
    expect(ftl[0]?.id).toBe("F01");
    expect(exp[0]?.id).toBe("E01");
  });

  it("解析环境 JSON 覆盖默认字段", () => {
    const demo = parsePriority1DemoFromEnv(
      JSON.stringify({ originZip: "30303", weightLb: "999" }),
    );
    expect(demo.originZip).toBe("30303");
    expect(demo.weightLb).toBe("999");
    expect(demo.destinationZip).toBe("90017");
  });
});
