import { test } from "node:test";
import assert from "node:assert/strict";
import { eventCardImage } from "@/features/events/eventCardFields";
import { listingImage } from "@/features/profile/components/profileListingImage";
import type { OpportunityItem } from "@/features/profile/components/commonProfileTypes";

const item = (extra: Partial<OpportunityItem>) => ({ id: 1, ...extra }) as unknown as OpportunityItem;

test("a profile event card is its artwork, never the first documentation photo", () => {
  const photos = [{ image: "https://x/doc1.jpg" }, { image: "https://x/doc2.jpg" }];
  assert.equal(listingImage(item({ cover_image: "https://x/card.jpg", event_images: photos }), true), "https://x/card.jpg");
});

test("a legacy event without artwork falls back to its first photo; none gives null", () => {
  assert.equal(listingImage(item({ cover_image: null, event_images: [{ image: null }, { image: "https://x/doc1.jpg" }] }), true), "https://x/doc1.jpg");
  assert.equal(listingImage(item({ cover_image: null, event_images: [] }), true), null);
});

test("an opportunity card keeps using its first announcement image, whatever an event field says", () => {
  assert.equal(listingImage(item({ opportunity_images: [{ image: "https://x/a.jpg" }], cover_image: "https://x/not-used.jpg" }), false), "https://x/a.jpg");
  assert.equal(listingImage(item({}), false), null);
});

test("eventCardImage prefers the card and tolerates missing fields", () => {
  assert.equal(eventCardImage({ cover_image: "https://x/card.jpg", event_images: [{ image: "https://x/doc.jpg" }] }), "https://x/card.jpg");
  assert.equal(eventCardImage({}), null);
});
