import { describe, it, expect, vi } from "vitest";
import { LogRecord } from "@opentelemetry/sdk-logs";
import { EnrichLogProcessor } from "../../lib/processor/enrich-logger-processor.js";

const createMockLogRecord = () => {
  return {
    setAttribute: vi.fn(),
  } as unknown as LogRecord;
};

describe("EnrichLogProcessor", () => {
  it("should enrich log record with static attributes", () => {
    const attributes = { key1: "value1", key2: 42 };
    const processor = new EnrichLogProcessor(attributes);
    const mockLogRecord = createMockLogRecord();

    processor.onEmit(mockLogRecord);

    expect(mockLogRecord.setAttribute).toHaveBeenCalledWith("key1", "value1");
    expect(mockLogRecord.setAttribute).toHaveBeenCalledWith("key2", 42);
  });

  it("should enrich log record with dynamic attributes", () => {
    const attributes = {
      key1: () => "dynamicValue",
      key2: () => 100,
    };
    const processor = new EnrichLogProcessor(attributes);
    const mockLogRecord = createMockLogRecord();

    processor.onEmit(mockLogRecord);

    expect(mockLogRecord.setAttribute).toHaveBeenCalledWith(
      "key1",
      "dynamicValue",
    );
    expect(mockLogRecord.setAttribute).toHaveBeenCalledWith("key2", 100);
  });

  it("should not set attributes if no span attributes are provided", () => {
    const processor = new EnrichLogProcessor();
    const mockLogRecord = createMockLogRecord();

    processor.onEmit(mockLogRecord);

    expect(mockLogRecord.setAttribute).not.toHaveBeenCalled();
  });

  it("should reject forceFlush", async () => {
    await expect(
      new EnrichLogProcessor().forceFlush(),
    ).resolves.toBeUndefined();
  });

  it("should resolve shutdown", async () => {
    await expect(new EnrichLogProcessor().shutdown()).resolves.toBeUndefined();
  });
});
