import { fireEvent, screen } from "@testing-library/react";
import { describe, expect, test } from "bun:test";
import { createElement } from "react";
import { useLocation } from "react-router";

import { NotFoundPage } from "../../../src/web/features/not-found";
import { renderWithProviders } from "../test-utils";

function LocationProbe() {
  const location = useLocation();
  return <span>当前路径：{location.pathname}</span>;
}

describe("NotFoundPage", () => {
  test("渲染 404 页面", () => {
    renderWithProviders(createElement(NotFoundPage));

    expect(screen.getByText("404")).not.toBeNull();
    expect(screen.getByText("您访问的页面不存在")).not.toBeNull();
    expect(screen.getByRole("button", { name: "返回首页" })).not.toBeNull();
  });

  test("点击返回首页按钮导航到首页", () => {
    renderWithProviders(createElement("div", null, createElement(NotFoundPage), createElement(LocationProbe)), {
      initialRoute: "/missing",
    });

    const button = screen.getByRole("button", { name: "返回首页" });
    fireEvent.click(button);

    expect(screen.getByText("当前路径：/")).not.toBeNull();
  });
});
