/* eslint-disable @typescript-eslint/require-await */
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { render, screen } from "@testing-library/react";
import { describe, expect, test } from "bun:test";
import { createElement, StrictMode } from "react";

import { App } from "../../src/web/app";
import { ErrorBoundary } from "../../src/web/components/ErrorBoundary";

function createTestQueryClient() {
  return new QueryClient({
    defaultOptions: {
      queries: {
        retry: false,
        staleTime: 0,
      },
    },
  });
}

function renderApp() {
  const queryClient = createTestQueryClient();

  return render(
    createElement(
      StrictMode,
      null,
      createElement(
        ErrorBoundary,
        null,
        createElement(QueryClientProvider, { client: queryClient }, createElement(App)),
      ),
    ),
  );
}

describe("App", () => {
  test("渲染 Layout 骨架和品牌名", () => {
    // mock /health fetch 避免网络错误
    window.fetch = (async () => {
      return new Response(JSON.stringify({ ok: true, service: "test-app", timestamp: new Date().toISOString() }), {
        headers: { "Content-Type": "application/json" },
        status: 200,
      });
    }) as unknown as typeof fetch;

    renderApp();

    expect(screen.getByText("{{app-name}}")).not.toBeNull();
    expect(screen.getByText("系统")).not.toBeNull();
    expect(screen.getByText("明亮")).not.toBeNull();
    expect(screen.getByText("黑暗")).not.toBeNull();
  });
});
