import { describe, expect, it } from "vitest";
import { en } from "./i18n/catalog.en";
import { zhTW } from "./i18n/catalog.zhTW";
import { sanitizeReviewCopy } from "./reviewCopy";

const BANNED = /爬蟲|crawler|\bcrawl(?:ing|ed)?\b/i;

describe("sanitizeReviewCopy", () => {
  it("strips crawler wording in zh-TW", () => {
    expect(sanitizeReviewCopy("Chrome session 已同步到巡樓爬蟲", "zh-TW")).toBe(
      "Chrome session 已同步到測試海巡",
    );
    expect(sanitizeReviewCopy("crawler session required when dev_mode enabled", "zh-TW")).toBe(
      "session required when dev_mode enabled",
    );
  });

  it("strips crawler wording and known backend hints in en", () => {
    expect(sanitizeReviewCopy("Chrome session 已同步到巡樓爬蟲", "en")).toBe(
      "Chrome session 已同步到test patrol",
    );
    expect(sanitizeReviewCopy("Chrome crawler is not configured", "en")).toBe(
      "Chrome is not configured",
    );
    expect(
      sanitizeReviewCopy(
        "今天沒巡到：Chrome 登入已過期。請到設定重新同步已登入的 Threads 分頁，或先關掉開發模式改走 API 搜尋。",
        "en",
      ),
    ).toMatch(/Chrome sign-in expired/);
    expect(sanitizeReviewCopy("crawler session required", "en")).not.toMatch(/crawler/i);
  });
});

describe("i18n review copy", () => {
  it("keeps zh-TW and en catalogs in sync", () => {
    expect(Object.keys(en).sort()).toEqual(Object.keys(zhTW).sort());
  });

  it("has no crawler wording in zh-TW or en catalogs", () => {
    for (const [locale, dict] of [
      ["zh-TW", zhTW],
      ["en", en],
    ] as const) {
      for (const [key, value] of Object.entries(dict)) {
        expect(value, `${locale} ${key}`).not.toMatch(BANNED);
      }
    }
  });
});
