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 medium-density list rows that link into independent review detail pages", 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.findByText("审核目标 #101")).toBeInTheDocument();
  expect(screen.getByText(/工作流 wf-101/)).toBeInTheDocument();
  expect(screen.getByText("Mock 资产")).toBeInTheDocument();
  expect(screen.getByText("失败 2")).toBeInTheDocument();
  expect(screen.queryByText("审核动作面板")).not.toBeInTheDocument();
  expect(screen.getByRole("link", { name: /审核目标 #101/ })).toHaveAttribute(
    "href",
    "/reviews/workbench/101",
  );
});
