import test from "node:test";
import assert from "node:assert/strict";

import { communityResetSignature } from "@/features/community/components/communityResetSignature";
import type { CommunityFiltersData } from "@/features/community/components/CommunityFilterModal";

/** What the filter modal and the «مسح» button both hand back when nothing is set. */
const cleared = (): CommunityFiltersData => ({
  name: "",
  startDate: "",
  endDate: "",
  type: "",
  tags: [],
});

test("the reported bug: clearing an unfiltered feed is not a change", () => {
  const before = communityResetSignature({ search: "", filters: cleared() });
  // «مسح» builds a *fresh* object. Under the old reference comparison this
  // emptied the feed: the posts were dropped, the React Query key hashed the
  // same, so the cached response came back as the same object and the
  // accumulator skipped it.
  const after = communityResetSignature({ search: "", filters: cleared() });

  assert.equal(before, after);
  assert.notEqual(cleared(), cleared(), "the objects really are distinct");
});

test("clearing a feed that *was* filtered is a change", () => {
  const filtered = communityResetSignature({
    search: "",
    filters: { ...cleared(), type: "proposing_idea", name: "Sara" },
  });
  const afterClear = communityResetSignature({ search: "", filters: cleared() });

  assert.notEqual(filtered, afterClear);
});

test("typing a search and deleting it again returns to the first signature", () => {
  const empty = communityResetSignature({ search: "", filters: cleared() });
  const typed = communityResetSignature({ search: "abc", filters: cleared() });
  const deleted = communityResetSignature({ search: "", filters: cleared() });

  assert.notEqual(empty, typed);
  assert.equal(empty, deleted);
});

test("every filter field moves the signature", () => {
  const base = communityResetSignature({ search: "", filters: cleared() });
  const fields: Partial<CommunityFiltersData>[] = [
    { name: "Sara" },
    { startDate: "2026-01-01" },
    { endDate: "2026-02-01" },
    { type: "is_funding_required" },
    { tags: ["7"] },
  ];
  for (const patch of fields) {
    assert.notEqual(
      communityResetSignature({ search: "", filters: { ...cleared(), ...patch } }),
      base,
      `changing ${Object.keys(patch)[0]} must invalidate the accumulated posts`
    );
  }
});

test("tag order does not matter — the same two tags are the same filter", () => {
  const a = communityResetSignature({
    search: "",
    filters: { ...cleared(), tags: ["7", "3"] },
  });
  const b = communityResetSignature({
    search: "",
    filters: { ...cleared(), tags: ["3", "7"] },
  });
  assert.equal(a, b);
});

test("key order does not matter — this is why it is not JSON.stringify", () => {
  const written_one_way: CommunityFiltersData = {
    name: "Sara",
    startDate: "",
    endDate: "",
    type: "",
    tags: [],
  };
  const written_the_other: CommunityFiltersData = {
    tags: [],
    type: "",
    endDate: "",
    startDate: "",
    name: "Sara",
  };
  assert.notEqual(
    JSON.stringify(written_one_way),
    JSON.stringify(written_the_other),
    "the two objects really do stringify differently"
  );
  assert.equal(
    communityResetSignature({ search: "", filters: written_one_way }),
    communityResetSignature({ search: "", filters: written_the_other })
  );
});

test("the View All route's tag is part of the signature", () => {
  const noTag = communityResetSignature({ search: "", filters: cleared() });
  const withTag = communityResetSignature({
    search: "",
    filters: cleared(),
    tag: "12",
  });
  assert.notEqual(noTag, withTag);
  // null and undefined are the same "no tag", not two different filters.
  assert.equal(
    communityResetSignature({ search: "", filters: cleared(), tag: null }),
    noTag
  );
});
