import { describe, expect, it } from "vitest";
import {
  analyzeFlockHarEntries,
  scoreFlockQuoteCandidate,
  verdictFromCandidates,
  type FlockHarEntry,
} from "@/lib/flock/har-analyze";

describe("flock har-analyze", () => {
  it("高分匹配类似 axel 的 quote JSON", () => {
    const score = scoreFlockQuoteCandidate({
      url: "https://api.flockfreight.com/v1/quote",
      method: "POST",
      status: 200,
      mimeType: "application/json",
      responseText: JSON.stringify({
        rates: [
          { label: "FlockDirect", price: 2300 },
          { label: "Standard", price: 800 },
        ],
      }),
    });
    expect(score).toBeGreaterThanOrEqual(10);
  });

  it("过滤静态与埋点", () => {
    expect(
      scoreFlockQuoteCandidate({
        url: "https://www.googletagmanager.com/gtm.js",
        method: "GET",
        status: 200,
        mimeType: "application/javascript",
        responseText: "gtm",
      }),
    ).toBe(0);
  });

  it("从 HAR entries 选出候选并裁决", () => {
    const entries: FlockHarEntry[] = [
      {
        request: {
          method: "POST",
          url: "https://app.flockfreight.com/api/quotes",
        },
        response: {
          status: 200,
          content: {
            mimeType: "application/json",
            text: JSON.stringify({
              flockDirect: { total: 1999 },
              standard: { total: 777 },
              reference: "FRG-1",
            }),
          },
        },
      },
      {
        request: {
          method: "GET",
          url: "https://cdn.example.com/app.js",
        },
        response: { status: 200, content: { mimeType: "application/javascript" } },
      },
    ];
    const cands = analyzeFlockHarEntries(entries);
    expect(cands.length).toBeGreaterThanOrEqual(1);
    expect(verdictFromCandidates(cands)).toBe("stable_json_quote_api_found");
  });

  it("无候选时裁决为 dom_only", () => {
    expect(verdictFromCandidates([])).toBe("no_quote_json_api_dom_only");
  });
});
