import { describe, expect, it } from "vitest";
import type { Post } from "../types/post";
import { postToResource } from "./postResourceAdapter";

const basePost: Post = {
  id: "post-1",
  postType: "link",
  categoryId: 1,
  categorySlug: "official-announcement",
  language: "zh",
  title: "Link post",
  text: "https://example.com/article",
  attachments: [],
  isRecommended: true,
  publishedAt: "2026-05-30T00:00:00Z",
};

describe("postToResource", () => {
  it("uses link preview images as the resource cover when there is no attachment", () => {
    const resource = postToResource(
      {
        ...basePost,
        linkPreview: {
          url: "https://example.com/article",
          canonicalUrl: "https://example.com/article",
          siteName: "Example",
          title: "Example article",
          description: "Example description",
          imageUrl: "https://example.com/preview.jpg",
        },
      },
      "zh-TW",
    );

    expect(resource.coverImage).toBe("https://example.com/preview.jpg");
    expect(resource.previewUrl).toBe("https://example.com/preview.jpg");
  });

  it("uses document thumbnails before poster URLs", () => {
    const resource = postToResource(
      {
        ...basePost,
        postType: "pdf",
        attachments: [
          {
            id: "att-1",
            kind: "document",
            url: "/uploads/file.pdf",
            mime: "application/pdf",
            filename: "file.pdf",
            sizeBytes: 123,
            posterUrl: "/uploads/file.pdf",
            thumbnailUrl: "/uploads/thumb.jpg",
          },
        ],
      },
      "zh-TW",
    );

    expect(resource.coverImage).toBe("/uploads/thumb.jpg");
    expect(resource.previewUrl).toBe("/uploads/thumb.jpg");
  });

  it("keeps an attachment thumbnail ahead of a link preview image", () => {
    const resource = postToResource(
      {
        ...basePost,
        attachments: [
          {
            id: "att-1",
            kind: "image",
            url: "/uploads/full.jpg",
            mime: "image/jpeg",
            filename: "full.jpg",
            sizeBytes: 123,
            thumbnailUrl: "/uploads/thumb.jpg",
          },
        ],
        linkPreview: {
          url: "https://example.com/article",
          canonicalUrl: "https://example.com/article",
          siteName: "Example",
          title: "Example article",
          description: "Example description",
          imageUrl: "https://example.com/preview.jpg",
        },
      },
      "zh-TW",
    );

    expect(resource.coverImage).toBe("/uploads/thumb.jpg");
  });
});
