/**
 * The image keep-set, and the empty case that six forms got wrong.
 *
 * `MediaKeepSet::apply()` soft-deletes every stored image that is not in
 * `existing_image_ids`. An **absent** key means "keep everything" — and
 * `existing_image_ids[]` appended zero times *is* absent, because multipart
 * has no representation for an empty array. So every form that wrote the ids
 * inside `if (ids.length > 0)` could never say "keep none": removing the last
 * image left all of them in place.
 *
 * `MediaKeepSet::normalizeEmptySignal()` accepts a scalar `""` or `"none"` for
 * exactly this. These tests pin that the empty set reaches the wire.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import {
  IMAGE_KEEP_SET_NONE,
  appendImageKeepSet,
} from "@/features/shared/imageKeepSet";

describe("appendImageKeepSet", () => {
  test("an empty set travels as the scalar sentinel, not as nothing", () => {
    const formData = new FormData();
    appendImageKeepSet(formData, []);

    /* The scalar key, which the backend rewrites to `[]`. */
    assert.equal(formData.get("existing_image_ids"), IMAGE_KEEP_SET_NONE);
    // And *not* the array key, which would have been dropped entirely.
    assert.deepEqual(formData.getAll("existing_image_ids[]"), []);
  });

  test("the sentinel is a value the backend recognises", () => {
    /* `normalizeEmptySignal()` matches `''` or `'none'` after `trim()`.
       Asserted as a literal so a rename on either side has to be deliberate. */
    assert.equal(IMAGE_KEEP_SET_NONE, "none");
  });

  test("a non-empty set uses the array key, one append per id", () => {
    const formData = new FormData();
    appendImageKeepSet(formData, [7, 9, 11]);

    assert.deepEqual(formData.getAll("existing_image_ids[]"), ["7", "9", "11"]);
    // The scalar form must not be sent alongside it: `['present', 'array']`
    // would then see a string and 422.
    assert.equal(formData.get("existing_image_ids"), null);
  });

  test("ids are stringified, because that is all FormData holds", () => {
    const formData = new FormData();
    appendImageKeepSet(formData, [42]);
    assert.deepEqual(formData.getAll("existing_image_ids[]"), ["42"]);
  });

  test("order is preserved", () => {
    const formData = new FormData();
    appendImageKeepSet(formData, [3, 1, 2]);
    assert.deepEqual(formData.getAll("existing_image_ids[]"), ["3", "1", "2"]);
  });

  test("the two forms are mutually exclusive, whichever branch runs", () => {
    /*
     * The backend validates the key as `['present', 'array']` *after*
     * normalisation, so exactly one of the two spellings may appear. This is
     * the invariant the helper exists to hold.
     */
    for (const ids of [[], [1], [1, 2]]) {
      const formData = new FormData();
      appendImageKeepSet(formData, ids);
      const scalar = formData.get("existing_image_ids") !== null;
      const array = formData.getAll("existing_image_ids[]").length > 0;
      assert.notEqual(scalar, array, `both or neither for ${JSON.stringify(ids)}`);
    }
  });
});
