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

test("renders dead-letter retry budget and allows an authorized manual retry", () => {
    const onRetryJob = vi.fn();
    render(
        <ActivityTemplateRewardsPanel
            abilities={{ canRetry: true }}
            boardType="total"
            data={{
                items: [
                    {
                        attemptCount: 8,
                        boardType: "total",
                        createdAtMs: Date.parse("2026-07-14T03:00:00Z"),
                        failureReason: "wallet unavailable",
                        grantedAtMs: 0,
                        maxAttempts: 8,
                        nextRetryAtMs: 0,
                        periodKey: "total",
                        rankNo: 1,
                        resourceGroupId: 81,
                        rewardItems: [{ name: "Avatar frame" }],
                        rewardJobId: "reward-101",
                        rewardName: "Champion reward",
                        rewardSnapshotHash: "snapshot-hash",
                        rewardSnapshotId: "rgs-81-v3",
                        score: 19999,
                        status: "dead",
                        updatedAtMs: Date.parse("2026-07-14T03:02:00Z"),
                        user: { displayUserId: "HY503", username: "User 503" },
                        userId: 503,
                        versionNo: 3,
                    },
                ],
                pageSize: 20,
                total: 1,
            }}
            error=""
            loading={false}
            loadingAction=""
            page={1}
            periodKey=""
            periodOptions={[]}
            status="dead"
            onBoardTypeChange={vi.fn()}
            onPageChange={vi.fn()}
            onPeriodChange={vi.fn()}
            onRetry={vi.fn()}
            onRetryJob={onRetryJob}
            onStatusChange={vi.fn()}
        />,
    );

    expect(screen.getAllByText("人工处理")).toHaveLength(2);
    expect(screen.getByText("8/8 次")).toBeInTheDocument();
    expect(screen.getByText("Champion reward")).toBeInTheDocument();

    fireEvent.click(screen.getByRole("button", { name: "重试发放" }));
    expect(onRetryJob).toHaveBeenCalledWith(expect.objectContaining({ rewardJobId: "reward-101" }));
});
