import { describe, expect, it } from "@jest/globals";
import {
  buildTimelineItems,
  formatDayLabel,
  type MessageGroup,
} from "./channelTimeline";

const makeGroup = (key: string, createdAt: string): MessageGroup => ({
  key,
  userId: "user-1",
  isOwn: false,
  messages: [
    {
      id: key,
      userId: "user-1",
      text: "hello",
      createdAt,
      clientId: key,
    },
  ],
});

describe("buildTimelineItems", () => {
  it("inserts one separator for the first group of a day", () => {
    const items = buildTimelineItems(
      [
        makeGroup("a", "2026-04-19T08:00:00.000Z"),
        makeGroup("b", "2026-04-19T10:00:00.000Z"),
      ],
      new Date("2026-04-19T12:00:00.000Z")
    );

    expect(items.filter((item) => item.type === "day-separator")).toHaveLength(1);
  });

  it("inserts a new separator when the day changes", () => {
    const items = buildTimelineItems(
      [
        makeGroup("a", "2026-04-18T12:00:00.000Z"),
        makeGroup("b", "2026-04-19T08:00:00.000Z"),
      ],
      new Date("2026-04-19T12:00:00.000Z")
    );

    expect(items.filter((item) => item.type === "day-separator")).toHaveLength(2);
  });
});

describe("formatDayLabel", () => {
  it("returns Aujourd'hui for the current day", () => {
    expect(
      formatDayLabel(
        new Date("2026-04-19T08:00:00.000Z"),
        new Date("2026-04-19T12:00:00.000Z")
      )
    ).toBe("Aujourd'hui");
  });

  it("returns Hier for the previous day", () => {
    expect(
      formatDayLabel(
        new Date("2026-04-18T08:00:00.000Z"),
        new Date("2026-04-19T12:00:00.000Z")
      )
    ).toBe("Hier");
  });
});
