import { describe, expect, it, vi } from "vitest";
import type { ApplicationItem } from "@/data/mockApplications";
import { resolveApplicationDraftCaseId } from "./resolveApplicationDraftCaseId";

vi.mock("@/lib/applicantSubmissionRecord", () => ({
  loadApplicantSubmissionRecord: () => null,
}));

function baseItem(over: Partial<ApplicationItem>): ApplicationItem {
  return {
    id: "sub-abc",
    submittedDate: "2026-01-01T00:00:00Z",
    name: "T",
    author: { id: "CASE-REAL-1", name: "A" },
    status: "pending",
    ...over,
  };
}

describe("resolveApplicationDraftCaseId", () => {
  it("prefers draftCaseId from API", () => {
    expect(resolveApplicationDraftCaseId(baseItem({ draftCaseId: "CASE-X" }))).toBe("CASE-X");
  });

  it("treats SUB- prefix like sub- when matching author id", () => {
    expect(
      resolveApplicationDraftCaseId(
        baseItem({
          id: "SUB-55ba706b9b39",
          author: { id: "CASE-789", name: "A" },
        }),
      ),
    ).toBe("CASE-789");
  });

  it("returns submission id when it is the only key (server resolves case_code)", () => {
    expect(resolveApplicationDraftCaseId(baseItem({ id: "SUB-xyz", author: { id: "SUB-xyz", name: "A" } }))).toBe(
      "SUB-xyz",
    );
  });
});
