import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { act, render, screen } from "@testing-library/react";
import { TasksWidget } from "../TasksWidget";

async function finishWidgetLoad(): Promise<void> {
  await act(async () => {
    await vi.advanceTimersByTimeAsync(500);
  });
}

describe("TasksWidget", (): void => {
  beforeEach((): void => {
    vi.useFakeTimers();
  });

  afterEach((): void => {
    vi.useRealTimers();
  });

  it("renders loading state initially", (): void => {
    render(<TasksWidget id="tasks-1" />);

    expect(screen.getByText("Loading tasks...")).toBeInTheDocument();
  });

  it("renders default summary stats", async (): Promise<void> => {
    render(<TasksWidget id="tasks-1" />);

    await finishWidgetLoad();

    expect(screen.getByText("Total")).toBeInTheDocument();
    expect(screen.getByText("In Progress")).toBeInTheDocument();
    expect(screen.getByText("Done")).toBeInTheDocument();
    expect(screen.getByText("3")).toBeInTheDocument();
  });

  it("renders default task rows", async (): Promise<void> => {
    render(<TasksWidget id="tasks-1" />);

    await finishWidgetLoad();

    expect(screen.getByText("Complete project documentation")).toBeInTheDocument();
    expect(screen.getByText("Review pull requests")).toBeInTheDocument();
    expect(screen.getByText("Update dependencies")).toBeInTheDocument();
  });

  it("shows due date labels for each task", async (): Promise<void> => {
    render(<TasksWidget id="tasks-1" />);

    await finishWidgetLoad();

    expect(screen.getAllByText(/Due:/).length).toBe(3);
  });
});
