/**
 * How `RemoteImage` picks a source, and — just as importantly — what that
 * choice does **not** fix.
 *
 * An earlier version of this file asserted that `record.profile_pic || default`
 * "does not catch an empty-string column". That is false: `""` is falsy, so
 * `||` already yields the default. The tests below pin the real boundary —
 * a **whitespace-only** value is truthy and is the one case a bare `||` lets
 * reach `src`, which is why the component trims instead.
 *
 * The component needs React, so the decision it makes is tested here as the
 * same expression it evaluates.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";

/** Exactly the expression `RemoteImage` uses to pick a source. */
const resolve = (src?: string | null) => src?.trim() || "";

/** What a call site writes today, for comparison against `resolve`. */
const plainOr = (src?: string | null) => src || "/assets/profile/male_profile.svg";

describe("RemoteImage source resolution", () => {
  test("an uploaded image wins", () => {
    assert.equal(resolve("https://cdn.example/a.jpg"), "https://cdn.example/a.jpg");
  });

  test("null and undefined resolve to empty, so the component renders nothing", () => {
    // `next/image` throws on a missing `src`; a bare `<img src="">` instead
    // resolves against the document URL and draws a broken graphic. Returning
    // null avoids both. No current call site reaches this — each resolves to a
    // local default first — but `src` is typed `string | null | undefined`.
    assert.equal(resolve(null), "");
    assert.equal(resolve(undefined), "");
  });

  test("an empty string was ALREADY handled by `||` — this is not the bug", () => {
    // The correction: both forms reach the default, so `RemoteImage` buys
    // nothing here. Asserting it keeps the claim from being made again.
    assert.equal(plainOr(""), "/assets/profile/male_profile.svg");
    assert.equal(plainOr(null), "/assets/profile/male_profile.svg");
  });

  test("a whitespace-only value is the one case `||` lets through", () => {
    // Truthy, so `||` keeps it and the browser requests `src="   "`.
    assert.equal(plainOr("   "), "   ");
    // Trimming is what actually closes it.
    assert.equal(resolve("   "), "");
  });

  test("a relative upload path is left alone", () => {
    // `getimg()` returns absolute URLs, but a locally-served default is
    // relative and must not be rewritten.
    assert.equal(resolve("/assets/profile/male_profile.svg"), "/assets/profile/male_profile.svg");
  });
});
