import { describe, it, expect } from "vitest";
import { deriveEmailEngagement } from "./derive-email-engagement";

describe("deriveEmailEngagement", () => {
  it("reports not opened when there are zero OPEN events", () => {
    const result = deriveEmailEngagement({ openCount: 0, clickCount: 0, firstOpenedAt: null });

    expect(result.opened).toBe(false);
    expect(result.openedAt).toBeNull();
    expect(result.openCount).toBe(0);
  });

  it("reports opened with the first-open timestamp when there is at least one OPEN event", () => {
    const firstOpenedAt = new Date("2026-01-01T10:00:00.000Z");

    const result = deriveEmailEngagement({ openCount: 3, clickCount: 0, firstOpenedAt });

    expect(result.opened).toBe(true);
    expect(result.openedAt).toBe(firstOpenedAt.toISOString());
    expect(result.openCount).toBe(3);
  });

  it("does not treat clicks without a recorded open as 'opened'", () => {
    const result = deriveEmailEngagement({ openCount: 0, clickCount: 2, firstOpenedAt: null });

    expect(result.opened).toBe(false);
    expect(result.clickCount).toBe(2);
  });
});
