import { describe, expect, it, vi, afterEach } from "vitest";

describe("createRootLogger", () => {
  afterEach(async () => {
    const mod = await import("./root.js");
    mod.__resetRootLoggerForTests();
    vi.resetModules();
  });

  it("returns a Logger with console transport in development", async () => {
    process.env["NODE_ENV"] = "development";
    const { createRootLogger, __resetRootLoggerForTests } = await import("./root.js");
    __resetRootLoggerForTests();
    const logger = createRootLogger();
    expect(logger).toBeDefined();
    expect(typeof logger.info).toBe("function");
    expect(typeof logger.child).toBe("function");
  });

  it("returns a Logger with JSON-lines transport in production", async () => {
    process.env["NODE_ENV"] = "production";
    process.env["LOGS_ENDPOINT"] = "https://logs.example/ingest";
    const { createRootLogger, __resetRootLoggerForTests } = await import("./root.js");
    __resetRootLoggerForTests();
    const logger = createRootLogger();
    expect(logger).toBeDefined();
    expect(typeof logger.info).toBe("function");
  });

  it("caches the logger instance (returns same object on repeated calls)", async () => {
    process.env["NODE_ENV"] = "development";
    const { createRootLogger, __resetRootLoggerForTests } = await import("./root.js");
    __resetRootLoggerForTests();
    const a = createRootLogger();
    const b = createRootLogger();
    expect(a).toBe(b);
  });

  it("child() produces a Logger with merged context", async () => {
    process.env["NODE_ENV"] = "development";
    const spy = vi.spyOn(console, "info").mockImplementation(() => {});
    const { createRootLogger, __resetRootLoggerForTests } = await import("./root.js");
    __resetRootLoggerForTests();
    const logger = createRootLogger();
    const child = logger.child({ traceId: "test-123" });
    child.info("hello");
    expect(spy).toHaveBeenCalledTimes(1);
    expect(spy.mock.calls[0]?.[0]).toContain("test-123");
    spy.mockRestore();
  });
});
