Files
wehub-resource-sync b3a7f98e5a
CI / E2E Cloudflare (4/8) (push) Failing after 0s
CI / E2E Cloudflare (8/8) (push) Failing after 1s
CI / Lint (push) Failing after 1s
Auto Extract / Extract (push) Failing after 4s
CI / Version Check (push) Failing after 9s
CI / Integration Tests (push) Failing after 1s
CI / E2E tests (1/8) (push) Failing after 1s
CI / E2E tests (2/8) (push) Failing after 2s
CI / E2E tests (3/8) (push) Failing after 2s
CI / Browser Tests (push) Failing after 1s
CI / E2E tests (5/8) (push) Failing after 1s
CI / E2E Cloudflare (2/8) (push) Failing after 2s
CI / Typecheck (push) Failing after 1s
CI / Changeset Validation (push) Failing after 2s
CI / E2E Cloudflare (5/8) (push) Failing after 1s
CI / E2E Cloudflare (6/8) (push) Failing after 1s
CI / E2E Cloudflare (7/8) (push) Failing after 1s
CodeQL / Analyze (javascript-typescript) (push) Failing after 1s
Format / Format (push) Failing after 0s
CodeQL / Analyze (actions) (push) Failing after 4s
CI / E2E tests (4/8) (push) Failing after 1s
CI / E2E tests (6/8) (push) Failing after 1s
CI / E2E tests (7/8) (push) Failing after 2s
CI / E2E tests (8/8) (push) Failing after 1s
CI / E2E Cloudflare (1/8) (push) Failing after 1s
CI / E2E Cloudflare (3/8) (push) Failing after 2s
Preview Releases / Publish Preview (push) Failing after 0s
zizmor / Run zizmor (push) Failing after 1s
Release / Release (push) Failing after 2s
CI / Smoke Tests (push) Failing after 5m36s
CI / Tests (push) Failing after 6m36s
Release / Sync Templates (push) Has been skipped
CI / E2E Tests (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:23:53 +08:00

99 lines
3.4 KiB
TypeScript

import { describe, it, expect } from "vitest";
import {
foldForMatch,
termExactMatches,
termMatches,
type MatchableTerm,
} from "../../src/lib/taxonomy-match.js";
/**
* Tests for the admin picker matcher. The matcher is the sole gate between
* an editor's typed input and the "Create new term" escape hatch, so every
* branch has real user impact:
*
* - accent-fold branch: `"Mexico"` must find `"México"` or editors create
* a duplicate term and fragment the taxonomy.
* - no-match branch: genuinely new terms must still offer Create.
* - exact-match branch: governs whether Create is suppressed, so an
* accent-insensitive exact match must count.
*/
const mexico: MatchableTerm = { label: "México" };
const hongKong: MatchableTerm = { label: "Hong Kong" };
describe("foldForMatch", () => {
it("folds diacritics and case to the same key", () => {
expect(foldForMatch("México")).toBe("mexico");
expect(foldForMatch("MEXICO")).toBe("mexico");
expect(foldForMatch("méxico")).toBe("mexico");
});
it("handles empty input", () => {
expect(foldForMatch("")).toBe("");
});
it("leaves non-accented characters unchanged", () => {
expect(foldForMatch("USA")).toBe("usa");
expect(foldForMatch("Hong Kong")).toBe("hong kong");
});
it("handles precomposed and decomposed forms identically", () => {
// Precomposed U+00E9 vs decomposed U+0065 U+0301 both fold to "e".
expect(foldForMatch("caf\u00e9")).toBe("cafe");
expect(foldForMatch("cafe\u0301")).toBe("cafe");
});
});
describe("termMatches", () => {
it("matches across the diacritic boundary (regression: Mexico/México)", () => {
expect(termMatches(mexico, "Mexico")).toBe(true);
expect(termMatches(mexico, "mexico")).toBe(true);
expect(termMatches(mexico, "MEX")).toBe(true);
});
it("matches an accented term against an accented query too", () => {
expect(termMatches(mexico, "México")).toBe(true);
expect(termMatches(mexico, "méx")).toBe(true);
});
it("does not match genuinely unrelated input (Create button must still appear)", () => {
expect(termMatches(mexico, "Japan")).toBe(false);
expect(termMatches(hongKong, "Canada")).toBe(false);
});
it("returns false for empty input so the dropdown stays closed", () => {
expect(termMatches(mexico, "")).toBe(false);
});
it("rejects whitespace-only input even when term labels contain spaces", () => {
// Regression guard: the label `"Hong Kong"` contains a space,
// so a naive `includes(needle)` without a whitespace guard would
// match a needle of `" "` and surface every multi-word term.
expect(termMatches(hongKong, " ")).toBe(false);
expect(termMatches(hongKong, " ")).toBe(false);
});
it("tolerates terms carrying unknown keys without crashing", () => {
const term = { label: "Canadá", extra: 42 } as unknown as MatchableTerm;
expect(termMatches(term, "Canada")).toBe(true);
});
});
describe("termExactMatches", () => {
it("treats diacritic-only differences as equal (so Create stays hidden)", () => {
expect(termExactMatches(mexico, "Mexico")).toBe(true);
expect(termExactMatches(mexico, "México")).toBe(true);
});
it("is stricter than termMatches — substrings do not count as exact", () => {
expect(termExactMatches(mexico, "Mex")).toBe(false);
expect(termExactMatches(hongKong, "Hong")).toBe(false);
});
it("returns false for empty or whitespace-only input", () => {
expect(termExactMatches(mexico, "")).toBe(false);
expect(termExactMatches(mexico, " ")).toBe(false);
});
});