import { describe, expect, it } from "vitest";
import {
  dedupeAttachmentIds,
  describeBindResult,
  retainAvailableSelections,
  toggleAttachmentSelection,
} from "../bindingState";

describe("F6 binding selection state", () => {
  it("deduplicates IDs while preserving first-selection order", () => {
    expect(dedupeAttachmentIds(["9", "8", "9", "7", "8"]))
      .toEqual(["9", "8", "7"]);
  });

  it("toggles string selections without affecting other pages", () => {
    expect(toggleAttachmentSelection(["9", "8"], "7"))
      .toEqual(["9", "8", "7"]);
    expect(toggleAttachmentSelection(["9", "8"], "9"))
      .toEqual(["8"]);
  });

  it("retains only selections that remain valid after relation refresh", () => {
    expect(retainAvailableSelections(
      ["9", "8", "7"],
      new Set(["9", "7"]),
    )).toEqual(["9", "7"]);
  });

  it("does not claim new success for an all-existing result", () => {
    const message = describeBindResult({
      createdCount: 0,
      existingCount: 2,
      attachmentCount: 2,
      mainPlanRowVersion: 5,
    });
    expect(message).toContain("均已挂载");
    expect(message).not.toContain("新增成功");
  });

  it("reports created and existing counts separately", () => {
    const message = describeBindResult({
      createdCount: 2,
      existingCount: 1,
      attachmentCount: 4,
      mainPlanRowVersion: 6,
    });
    expect(message).toContain("新增挂载 2 项");
    expect(message).toContain("已有挂载 1 项");
    expect(message).toContain("当前共 4 项");
  });
});

