import { describe, expect, it } from "vitest";
import {
  formatSelectionTraceEvent,
  isMothershipSuggestionClickTarget,
  pickSelectionGoldInteractions,
  type SelectionTraceSnapshot,
} from "@/workers/rpa/selection-trace";

function baseSnapshot(
  overrides: Partial<SelectionTraceSnapshot> = {},
): SelectionTraceSnapshot {
  return {
    label: "test",
    at: 1,
    activeElement: {
      tagName: "INPUT",
      role: "combobox",
      className: "pac-target-input",
      id: "",
      ariaExpanded: "true",
      ariaAutocomplete: "list",
      ariaActivedescendant: null,
      placeholder: "Search",
      valuePreview: "Duval Street",
      isPacTarget: true,
    },
    activeDescendant: null,
    pac: { visible: true, itemCount: 1, highlightedIndex: -1, items: [] },
    listboxOptions: [],
    widgetInputs: [],
    comboboxAlive: false,
    ...overrides,
  };
}

describe("selection-trace", () => {
  it("CAPTURE_SELECTION_TRACE 拼接后可被解析执行", () => {
    const label = "manual-pickup-done";
    const expr = `(function(label) {
      return { label: label, at: 1, ok: true };
    })(${JSON.stringify(label)})`;
    const result = new Function(`return ${expr}`)() as { label: string; ok: boolean };
    expect(result.ok).toBe(true);
    expect(result.label).toBe(label);
  });

  it("isMothershipSuggestionClickTarget 识别 99-ff22eee0 金样本 sc-bBXraC", () => {
    expect(
      isMothershipSuggestionClickTarget({
        tagName: "DIV",
        role: null,
        className: "sc-bBXraC jcUlJN",
        id: "",
        textPreview:
          "La Concha Key West, Autograph CollectionDuval Street, Key West, Florida, USA",
        closestOption: null,
        closestPacItem: null,
        dataOptionId: null,
        closestMothershipSuggestion: null,
      }),
    ).toBe(true);
    expect(
      isMothershipSuggestionClickTarget({
        tagName: "DIV",
        role: null,
        className: "sc-fubEtJ jSdUjy",
        id: "",
        textPreview: "Pick up from",
        closestOption: null,
        closestPacItem: null,
        dataOptionId: null,
        closestMothershipSuggestion: null,
      }),
    ).toBe(false);
  });

  it("pickSelectionGoldInteractions 识别 option/pac 点击", () => {
    const gold = pickSelectionGoldInteractions({
      clicks: [
        {
          kind: "click",
          at: 1,
          target: {
            tagName: "DIV",
            role: null,
            className: "sc-foo",
            id: "",
            textPreview: "La Concha",
            closestOption: "LI#opt.role-option",
            closestPacItem: null,
            dataOptionId: "2",
            closestMothershipSuggestion: null,
          },
        },
      ],
      keys: [
        {
          kind: "keydown",
          at: 2,
          key: "Enter",
          target: {
            tagName: "BUTTON",
            role: null,
            className: "",
            id: "",
            textPreview: "",
            closestOption: null,
            closestPacItem: null,
            dataOptionId: null,
          },
        },
        {
          kind: "keydown",
          at: 3,
          key: "Enter",
          target: {
            tagName: "INPUT",
            role: null,
            className: "sc-input",
            id: "",
            textPreview: "",
            closestOption: null,
            closestPacItem: null,
            dataOptionId: null,
          },
        },
      ],
    });
    expect(gold.selectionClicks).toHaveLength(1);
    expect(gold.selectionKeys).toHaveLength(1);
    expect(gold.selectionKeys[0]?.key).toBe("Enter");
  });

  it("formatSelectionTraceEvent：aria-activedescendant=null 标记 combobox-dead", () => {
    const event = formatSelectionTraceEvent(baseSnapshot());
    expect(event).toContain("aria-activedescendant=null");
    expect(event).toContain("combobox-dead");
    expect(event).toContain("pac-highlight=none");
  });

  it("formatSelectionTraceEvent：有 activeDescendant 时 combobox-alive", () => {
    const event = formatSelectionTraceEvent(
      baseSnapshot({
        activeDescendant: {
          id: "option-0",
          tagName: "DIV",
          role: "option",
          className: "",
          ariaSelected: "true",
          textPreview: "La Concha",
        },
        comboboxAlive: true,
        pac: { visible: true, itemCount: 2, highlightedIndex: 0, items: [] },
      }),
    );
    expect(event).toContain("aria-activedescendant=option-0");
    expect(event).toContain("combobox-alive");
    expect(event).toContain("pac-highlight=0");
  });
});
