/**
 * Closure review (8 Nov 2026, afternoon), Public area: home, CMS, events,
 * community, notifications, header/footer, config. Regression tests for the
 * frontend defects fixed in this round; see SC/cov2/public.md (C-PUB-n).
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { editableText, textColumnsToWrite } from "@/features/community/localizedText";

const src = (path: string) => readFileSync(new URL(`../src/${path}`, import.meta.url), "utf8");
const root = (path: string) => readFileSync(new URL(`../${path}`, import.meta.url), "utf8");

describe("C-PUB-1 — a deleted event leaves an accumulated list", () => {
  test("the events list and the profile events tab go back to page 1 after a delete", () => {
    assert.match(src("features/events/components/useEventListCard.ts"), /const reload = \(\) => resetPage\(\);/);
    assert.match(src("features/events/components/EventListCard.tsx"), /refetch=\{reload\}/);
    assert.match(src("features/events/components/useProfileEventCard.ts"), /reload: resetPage,/);
    assert.match(src("features/events/components/ProfileEventCard.tsx"), /refetch=\{reload\}/);
  });
});

describe("C-PUB-2 — X2: a team never gets Edit/Repost on an event", () => {
  test("the creator's manage icon is withheld from volunteer teams", () => {
    const button = src("features/events/components/EventDetailsActionButton.tsx");
    assert.match(button, /getPublisherKind\(state\.user\) === "team"/);
    assert.match(button, /if \(isTeam\) return null;/);
  });
});

describe("C-PUB-3 — editing a one-language post or reply", () => {
  const arabicOnly = { idea_text_ar: "فكرة", idea_text_en: null };
  const both = { idea_text_ar: "فكرة", idea_text_en: "Idea" };

  test("the editor opens with the other language's text when the interface side is empty", () => {
    assert.equal(editableText(arabicOnly, "idea_text", "en"), "فكرة");
    assert.equal(editableText(both, "idea_text", "en"), "Idea");
    assert.equal(editableText(undefined, "idea_text", "ar"), "");
  });

  test("the save also writes the column the text came from, so both languages see the edit", () => {
    assert.deepEqual(textColumnsToWrite(arabicOnly, "idea_text", "en"), ["idea_text_en", "idea_text_ar"]);
    assert.deepEqual(textColumnsToWrite(both, "idea_text", "en"), ["idea_text_en"]);
    assert.deepEqual(textColumnsToWrite(undefined, "text", "ar"), ["text_ar"]);
    assert.deepEqual(textColumnsToWrite({ text_ar: "  ", text_en: "Hi" }, "text", "ar"), ["text_ar", "text_en"]);
  });

  test("post and both reply forms use the helpers", () => {
    for (const file of ["useCreatePostForm.ts", "useReplyForm.ts", "useReplyOfReplyForm.ts"]) {
      const body = src(`features/community/components/${file}`);
      assert.match(body, /editableText\(/);
      assert.match(body, /textColumnsToWrite\(/);
    }
  });
});

describe("C-PUB-4 — community lists follow a delete or an edit", () => {
  test("detail delete, home delete and the post editor invalidate every feed", () => {
    assert.match(src("features/community/components/useCommunityDetail.ts"), /onSuccess: \(\) => invalidateCommunityFeeds\(queryClient\)/);
    assert.match(src("features/home/components/CommunityCard.tsx"), /invalidateCommunityFeeds\(queryClient\)/);
    assert.match(src("features/community/components/useCreatePostForm.ts"), /invalidateCommunityFeeds\(queryClient\)/);
    const keys = src("features/community/communityQueryKeys.ts");
    for (const key of ["communityPosts", "communityPostsByTag", "community-posts-homepage"]) assert.ok(keys.includes(`["${key}"]`));
  });

  test("the home carousel delete shows the server's reason and cannot run twice", () => {
    const card = src("features/home/components/CommunityCard.tsx");
    assert.match(card, /toastApiError\(err, selectedLanguage, t\("COMMON\.TOAST\.DELETE_POST_FAILED"\)\)/);
    assert.match(card, /if \(deletingRef\.current \|\|/);
  });

  test("the home carousel shows a one-language post in the other language too (F-CLA-1 class)", () => {
    assert.match(src("features/home/components/CommunityPostCard.tsx"), /pickLocalized\(post\.idea_text_en, post\.idea_text_ar, selectedLanguage\)/);
  });
});

describe("C-PUB-5 — volunteer card location without a text location", () => {
  /* Source check: the module imports a .tsx badge file the node runner cannot load. */
  test("uses the other language, then the map pin label; never «Loading location...»", () => {
    const body = src("features/opportunities/components/opportunityCardLocation.ts");
    assert.doesNotMatch(body, /LOADING_LOCATION/);
    assert.match(body, /\(isArabic \? item\.location_en : item\.location_ar\) \|\|\s*item\.map_desc \|\|/);
    assert.match(src("features/opportunities/components/opportunityCardTypes.ts"), /map_desc\?: string \| null;/);
  });
});

describe("C-PUB-6 — English labels on Arabic pages", () => {
  test("community like/reply icons are named through i18n", () => {
    for (const file of ["ReplyEngagementBar.tsx", "ReplyActionsRow.tsx", "CommunityPostActions.tsx", "InnerPostFooter.tsx"]) {
      const body = src(`features/community/components/${file}`);
      assert.doesNotMatch(body, /alt="(Like|Reply|Thumb|Message)"/);
      assert.match(body, /alt=\{t\("COMMON\.LIKE"\)\}/);
    }
    for (const locale of ["ar", "en"]) assert.match(root(`locales/${locale}/translation.json`), /"COMMON\.LIKE": "/);
  });

  test("the contact page map link is labelled in the page language", () => {
    assert.doesNotMatch(src("features/info/components/ContactUs.tsx"), /View location on Google Maps|alt="Map location"/);
  });
});

describe("C-PUB-7 — CSP frames", () => {
  test("frame-src admits the API origin the certificate preview iframe loads from", () => {
    assert.match(root("next.config.ts"), /`frame-src https:\/\/accounts\.google\.com \$\{API_ORIGIN\}`/);
  });
});
