import { describe, expect, it } from "vitest";
import type { Attachment } from "../../../types/post";
import {
  videoMetadataPreviewSource,
  videoPreviewSource,
} from "./videoPreviewSource";

const attachment: Attachment = {
  id: "att-1",
  kind: "video",
  url: "/uploads/legacy-preview.mp4",
  desktopUrl: "/uploads/desktop-preview.mp4",
  mobileUrl: "/uploads/mobile-540p-preview.mp4",
  mime: "video/mp4",
  filename: "original.mp4",
  sizeBytes: 1024,
};

describe("videoPreviewSource", () => {
  it("uses the desktop preview by default", () => {
    expect(videoPreviewSource(attachment, false)).toBe(
      "/uploads/desktop-preview.mp4",
    );
  });

  it("uses mobileUrl only when mobile preview is active", () => {
    expect(videoPreviewSource(attachment, true)).toBe(
      "/uploads/mobile-540p-preview.mp4",
    );
  });

  it("falls back to the desktop preview when mobileUrl is absent", () => {
    expect(
      videoPreviewSource({ ...attachment, mobileUrl: undefined }, true),
    ).toBe("/uploads/desktop-preview.mp4");
  });

  it("falls back to url when desktopUrl is absent", () => {
    expect(
      videoPreviewSource(
        { ...attachment, desktopUrl: undefined, mobileUrl: undefined },
        false,
      ),
    ).toBe("/uploads/legacy-preview.mp4");
  });

  it("adds a metadata seek fragment only when the URL has no fragment", () => {
    expect(videoMetadataPreviewSource("/uploads/video.mp4")).toBe(
      "/uploads/video.mp4#t=0.1",
    );
    expect(videoMetadataPreviewSource("/uploads/video.mp4#t=2")).toBe(
      "/uploads/video.mp4#t=2",
    );
  });
});
