import { describe, expect, it } from "vitest";
import { AxelSelectionBridge, formatPlaceCommitFailure, normalizeAxelPlaceId } from "@/workers/rpa/axel-selection-bridge";

describe("AxelSelectionBridge", () => {
  it("resolveSelection 从 googlePlacesResults 绑定 placeId", () => {
    const bridge = new AxelSelectionBridge();
    bridge.ingestSearchResults({
      googlePlacesResults: [
        {
          description: "1234 Warehouse Street, Los Angeles, CA 90001, USA",
          mainText: "1234 Warehouse Street",
          placeId: "ChIJ_test_la",
          secondaryText: "Los Angeles, CA 90001, USA",
        },
      ],
    });

    const binding = bridge.resolveSelection(
      "1234 Warehouse StreetLos Angeles, CA",
      {
        street: "1234 Warehouse Street",
        city: "Los Angeles",
        state: "CA",
        zip: "90001",
      },
    );

    expect(binding).toEqual({
      label: "1234 Warehouse StreetLos Angeles, CA",
      placeId: "ChIJ_test_la",
      description: "1234 Warehouse Street, Los Angeles, CA 90001, USA",
    });
  });

  it("记录 place/{id} 200 为 commit", () => {
    const bridge = new AxelSelectionBridge();
    bridge.recordPlaceCommit("ChIJ_commit_ok");

    expect(bridge.hasPlaceCommit("ChIJ_commit_ok")).toBe(true);
    expect(bridge.getObservedPlaceCommitIds()).toContain("ChIJ_commit_ok");
  });

  it("normalizeAxelPlaceId 从 base64 复合串提取 ChIJ", () => {
    const raw =
      "EisxMjM0IFdhcmVob3VzZSBTdHJlZXQsIExvcyBBbmdlbGVzLCBDQSwgVVNBIjESLwoUChIJywDT6CXGwoARuoz5HIJe20gQ0gkqFAoSCSEaJHUmxsKAEY2YXFohyZol";
    expect(normalizeAxelPlaceId(raw)).toBe("ChIJywDT6CXGwoARuoz5HIJe20g");
  });

  it("normalizeAxelPlaceId 截断 ChIJ 后粘连 base64 尾巴", () => {
    const composite =
      "ChIJywDT6CXGwoARuoz5HIJe20gQ0gkqFAoSCSEaJHUmxsKAEY2YXFohyZol";
    expect(normalizeAxelPlaceId(composite)).toBe("ChIJywDT6CXGwoARuoz5HIJe20g");
  });

  it("resolveSelection 有门牌 query 时优先带门牌的 placeId", () => {
    const bridge = new AxelSelectionBridge();
    bridge.ingestSearchResults({
      googlePlacesResults: [
        {
          description: "Distribution Way, Farmers Branch, TX, USA",
          mainText: "Distribution Way",
          placeId: "ChIJ_generic_way",
          secondaryText: "Farmers Branch, TX, USA",
        },
        {
          description: "5678 Distribution Way, Farmers Branch, TX, USA",
          mainText: "5678 Distribution Way",
          placeId: "ChIJ_numbered_way",
          secondaryText: "Farmers Branch, TX, USA",
        },
      ],
    });

    const binding = bridge.resolveSelection("Distribution WayFarmers Branch, TX", {
      street: "5678 Distribution Way",
      city: "Farmers Branch",
      state: "TX",
      zip: "",
    });

    expect(binding?.placeId).toBe("ChIJ_numbered_way");
  });

  it("无 search 结果时 resolveSelection 返回 null", () => {
    const bridge = new AxelSelectionBridge();
    expect(
      bridge.resolveSelection("1234 Warehouse StreetLos Angeles, CA"),
    ).toBeNull();
  });

  it("formatPlaceCommitFailure 区分无请求与 HTTP 错误", () => {
    expect(
      formatPlaceCommitFailure("ChIJ_expected", ["keyboard-bound-0"], {
        committedPlaceIds: [],
        placeNetworkLog: [],
      }),
    ).toContain("未发起 GET place 请求");

    expect(
      formatPlaceCommitFailure("ChIJ_expected", ["pac-item-0"], {
        committedPlaceIds: [],
        placeNetworkLog: [{ placeId: "ChIJ_other", status: 404, url: "", at: 0 }],
      }),
    ).toContain("placeId 不匹配");
  });
});
