import { render, screen } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import { FinanceApplicationList } from "./FinanceApplicationList.jsx";

const baseProps = {
  applications: {
    items: [],
    page: 1,
    pageSize: 50,
    total: 0
  },
  error: "",
  filters: {
    appCode: "",
    keyword: "",
    operation: "",
    page: 1,
    status: ""
  },
  loading: false,
  onAudit: vi.fn(),
  onFiltersChange: vi.fn(),
  onReload: vi.fn(),
  operations: [{ label: "增加币商金币", value: "coin_seller_coin_credit" }],
  options: { apps: [] }
};

test("renders finance audit table with explicit applicant and audit time columns", () => {
  render(
    <FinanceApplicationList
      {...baseProps}
      applications={{
        ...baseProps.applications,
        items: [
          {
            appCode: "lalu",
            applicantName: "张三",
            auditedAtMs: 1760003600000,
            auditorName: "王财务",
            auditorUserId: 99,
            coinAmount: 200,
            createdAtMs: 1760000000000,
            id: 9,
            operation: "coin_seller_coin_credit",
            rechargeAmount: 20,
            status: "approved",
            targetUserId: "88"
          }
        ],
        total: 1
      }}
    />
  );

  expect(screen.getByRole("columnheader", { name: "发起申请时间" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "审批人" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "审核时间" })).toBeInTheDocument();
  expect(screen.getByText("王财务")).toBeInTheDocument();
  expect(screen.getByText("99")).toBeInTheDocument();
});

test("keeps empty finance audit table colspan aligned with visible columns", () => {
  const { container } = render(<FinanceApplicationList {...baseProps} />);

  expect(container.querySelector("tbody td")?.colSpan).toBe(12);
  expect(screen.getByText("当前无数据")).toBeInTheDocument();
});
