// @vitest-environment jsdom
import { renderHook } from "@testing-library/react";
import type { ReactNode } from "react";
import { LoggerProvider, useLogger } from "./provider.js";
import type { Logger } from "./types.js";

const stubLogger: Logger = {
  debug: vi.fn(),
  info: vi.fn(),
  warn: vi.fn(),
  error: vi.fn(),
  child: vi.fn(() => stubLogger),
};

function wrapper({ children }: { children: ReactNode }) {
  return <LoggerProvider logger={stubLogger}>{children}</LoggerProvider>;
}

describe("LoggerProvider / useLogger", () => {
  it("provides Logger to descendants", () => {
    const { result } = renderHook(() => useLogger(), { wrapper });
    expect(result.current).toBe(stubLogger);
  });

  it("throws when used outside LoggerProvider", () => {
    expect(() => renderHook(() => useLogger())).toThrow(
      "useLogger() must be used within a <LoggerProvider>",
    );
  });
});
