import { describe, expect, it } from "vitest";
import { buildNetworkDiagnostics } from "@/workers/rpa/address-adapter/diagnostics";

describe("address-adapter diagnostics", () => {
  it("buildNetworkDiagnostics 要求 place 200 才算 httpOk", () => {
    const diag = buildNetworkDiagnostics(
      {
        getObservedPlaceCommitIds: () => ["ChIJ_pickup", "ChIJ_delivery"],
        getPlaceNetworkLog: () => [
          {
            placeId: "ChIJ_delivery",
            status: 200,
            url: "https://services.mothership.com/axel/location/place/ChIJ_delivery",
            at: 1,
          },
        ],
        hasPlaceCommit: (id) => id === "ChIJ_delivery",
        hasSuccessfulPlaceNetworkRequest: (id) => id === "ChIJ_delivery",
      },
      "ChIJ_delivery",
      "ChIJ_pickup",
    );

    expect(diag.placeRequestHttpOk).toBe(true);
    expect(diag.pickupLostAfterDelivery).toBe(false);
    expect(diag.placeNetworkEvents).toHaveLength(1);
  });

  it("buildNetworkDiagnostics 不在 bridge 层判定 pickupLost（改由 widget DOM）", () => {
    const diag = buildNetworkDiagnostics(
      {
        getObservedPlaceCommitIds: () => ["ChIJ_delivery"],
        getPlaceNetworkLog: () => [
          {
            placeId: "ChIJ_delivery",
            status: 200,
            url: "",
            at: 1,
          },
        ],
        hasPlaceCommit: () => true,
        hasSuccessfulPlaceNetworkRequest: () => true,
      },
      "ChIJ_delivery",
      "ChIJ_pickup",
    );

    expect(diag.pickupLostAfterDelivery).toBe(false);
  });
});
