import { describe, expect, it, vi } from "vitest";
import type { LogRecord } from "./types.js";
import { ConsoleTransport } from "./console-transport.js";

describe("ConsoleTransport", () => {
  it("pipes debug records to console.debug", () => {
    const spy = vi.spyOn(console, "debug").mockImplementation(() => {});
    const transport = new ConsoleTransport();
    const record: LogRecord = { ts: "2025-01-01T00:00:00Z", level: "debug", msg: "hello", fields: {} };
    transport.write(record);
    expect(spy).toHaveBeenCalledTimes(1);
    expect(spy.mock.calls[0]?.[0]).toContain("hello");
    spy.mockRestore();
  });

  it("pipes info records to console.info", () => {
    const spy = vi.spyOn(console, "info").mockImplementation(() => {});
    const transport = new ConsoleTransport();
    transport.write({ ts: "2025-01-01T00:00:00Z", level: "info", msg: "info msg", fields: { key: "val" } });
    expect(spy).toHaveBeenCalledTimes(1);
    expect(spy.mock.calls[0]?.[0]).toContain("info msg");
    spy.mockRestore();
  });

  it("pipes warn records to console.warn", () => {
    const spy = vi.spyOn(console, "warn").mockImplementation(() => {});
    const transport = new ConsoleTransport();
    transport.write({ ts: "2025-01-01T00:00:00Z", level: "warn", msg: "w", fields: {} });
    expect(spy).toHaveBeenCalledTimes(1);
    spy.mockRestore();
  });

  it("pipes error records to console.error", () => {
    const spy = vi.spyOn(console, "error").mockImplementation(() => {});
    const transport = new ConsoleTransport();
    transport.write({ ts: "2025-01-01T00:00:00Z", level: "error", msg: "e", fields: {} });
    expect(spy).toHaveBeenCalledTimes(1);
    spy.mockRestore();
  });

  it("flush is a no-op that resolves immediately", async () => {
    const transport = new ConsoleTransport();
    await expect(transport.flush()).resolves.toBeUndefined();
  });
});
