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

import { ReviewWorkbenchListScreen } from "@/features/reviews/review-workbench-list";

function createPendingPayload() {
  return {
    mode: "proxy",
    data: {
      items: [
        {
          reviewTaskId: 301,
          orderId: 101,
          workflowId: "wf-101",
          workflowType: "mid_end",
          currentStep: "review",
          currentStepLabel: "人工审核",
          createdAt: "2026-03-27T00:00:00Z",
          status: "waiting_review",
          statusMeta: {
            label: "待审核",
            tone: "warning",
          },
          hasMockAssets: true,
          failureCount: 2,
        },
      ],
      state: {
        kind: "ready",
      },
    },
  };
}

afterEach(() => {
  vi.unstubAllGlobals();
});

test("renders a compact review queue table with triage columns", async () => {
  const fetchMock = vi.fn().mockResolvedValue(
    new Response(JSON.stringify(createPendingPayload()), {
      status: 200,
      headers: {
        "content-type": "application/json",
      },
    }),
  );

  vi.stubGlobal("fetch", fetchMock);

  render(<ReviewWorkbenchListScreen />);

  expect(await screen.findByRole("columnheader", { name: "订单号" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "修订状态" })).toBeInTheDocument();
  expect(screen.getByRole("button", { name: "刷新队列" })).toBeInTheDocument();
  expect(screen.getByRole("link", { name: "进入详情" })).toHaveAttribute(
    "href",
    "/reviews/workbench/101",
  );
  expect(screen.getByText("Mock 资产")).toBeInTheDocument();
  expect(screen.getByText("失败 2")).toBeInTheDocument();
  expect(screen.queryByText("审核动作面板")).not.toBeInTheDocument();
});
