vi.mock("../../lib/metrics", () => {
  return {
    getMetric: vi.fn(),
  };
});

import { describe, it, expect, vi, beforeEach } from "vitest";
import { _getPIICounterRedactionMetric } from "../../lib/internals/shared-metrics.js";
import { getMetric } from "../../lib/metrics"; // Get the mocked function

describe("shared metrics", () => {
  const mockMetric = { add: vi.fn() };

  beforeEach(() => {
    vi.resetModules(); // Clear module-level cache
    vi.restoreAllMocks();
    (getMetric as vi.Mock).mockClear(); // clear call history
    (getMetric as vi.Mock).mockReturnValue(mockMetric);
  });

  it("calls getMetric with correct arguments and caches result", () => {
    const metric1 = _getPIICounterRedactionMetric();
    const metric2 = _getPIICounterRedactionMetric();

    expect(getMetric).toHaveBeenCalledOnce();
    expect(getMetric).toHaveBeenCalledWith("counter", {
      meterName: "o11y",
      metricName: "o11y_pii_redaction",
    });

    expect(metric1).toBe(mockMetric);
    expect(metric2).toBe(mockMetric); // should be cached
  });
});
