/**
 * Which stored images an opportunity edit form may offer.
 *
 * Two filters that were previously spelled inline in two hooks with `(img: any)`
 * annotations:
 *
 *  - completion photos must not appear in the *announcement* picker. The backend
 *    keeps both in one `opportunity_images` array and distinguishes them with
 *    `is_after_completed`, so an edit form that dropped the filter would offer
 *    the organizer their own after-the-fact photos as the opportunity's cover.
 *  - a row with no file must not reach the picker. `opportunityImagesPayload`
 *    emits `image: null` for an empty column; both call sites typed that field
 *    `string`, so the null travelled into an `<img src>` and drew a broken
 *    image.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";

import {
  announcementImages,
  type OpportunityImageRecord,
} from "@/features/shared/uploadedImage";

const image = (
  id: number,
  overrides: Partial<OpportunityImageRecord> = {}
): OpportunityImageRecord => ({
  id,
  image: `https://cdn.example/${id}.jpg`,
  is_after_completed: false,
  ...overrides,
});

describe("announcementImages", () => {
  test("keeps the announcement images", () => {
    assert.deepEqual(announcementImages([image(1), image(2)]), [
      { id: 1, image: "https://cdn.example/1.jpg" },
      { id: 2, image: "https://cdn.example/2.jpg" },
    ]);
  });

  test("drops completion-gallery photos", () => {
    const result = announcementImages([
      image(1),
      image(2, { is_after_completed: true }),
      image(3),
    ]);
    assert.deepEqual(
      result.map((row) => row.id),
      [1, 3]
    );
  });

  test("drops a row whose file is missing", () => {
    const result = announcementImages([image(1), image(2, { image: null })]);
    assert.deepEqual(
      result.map((row) => row.id),
      [1],
      "a null image would render an <img> with no src"
    );
  });

  test("preserves the order the endpoint sent", () => {
    const result = announcementImages([image(9), image(4), image(7)]);
    assert.deepEqual(
      result.map((row) => row.id),
      [9, 4, 7]
    );
  });

  test("an absent or empty list yields an empty list, not undefined", () => {
    assert.deepEqual(announcementImages(undefined), []);
    assert.deepEqual(announcementImages(null), []);
    assert.deepEqual(announcementImages([]), []);
  });

  test("only a literal false counts as 'not a completion photo'", () => {
    // The resource casts with `(bool)`, so the key is always a real boolean.
    // A payload missing it is not this shape, and guessing would be worse than
    // showing nothing.
    const malformed = [{ id: 1, image: "x" }] as OpportunityImageRecord[];
    assert.deepEqual(announcementImages(malformed), []);
  });

  test("every returned row has a non-null image", () => {
    const result = announcementImages([
      image(1),
      image(2, { image: null }),
      image(3, { is_after_completed: true }),
    ]);
    for (const row of result) {
      assert.equal(typeof row.image, "string");
      assert.notEqual(row.image, "");
    }
  });
});
