brackt/app/lib/__tests__/normalize-team-name.test.ts
Chris Parsons 81d063faa1
All checks were successful
🚀 Deploy / 🧪 Test (pull_request) Successful in 3m13s
🚀 Deploy / ʦ🔍 Typecheck & Lint (pull_request) Successful in 1m23s
🚀 Deploy / 🐳 Build (pull_request) Has been skipped
🚀 Deploy / 🚀 Deploy (pull_request) Has been skipped
Auto-populate & auto-score tennis Grand Slam brackets from Wikipedia
Add a per-event "Sync Draw" that pulls a tennis major's full 128-player
draw from its Wikipedia article, auto-creates/links participants (and
propagates them to every linked sibling season), builds the bracket, and
runs the qualifying-points scorer. Re-running advances the bracket and
scoring as matches complete.

Core
- match-sync: WikipediaTennisAdapter + wikitext bracket parser, DrawSync
  DTOs, syncTennisDraw orchestrator, pure draw->rows mapping
- playoff-match: populateBracketFromDraw (idempotent upsert on externalMatchId)
- scoring_events.externalSourceKey column (Wikipedia article; migration 0123)
- admin bracket "Sync Draw" card (accepts URL or title) + cron pass

Scoring fix
- deriveBracketQualifyingStates only floors players who have reached the
  scoring stage; for deep brackets (tennis_128) early-round losers earn 0 QP
  instead of a phantom 9th-place floor. CS2/simple_8 behavior preserved
  (gated on rounds[0].isScoring). Re-sync reconciles stale QP rows in a
  transaction.

Matching & parsing
- accent-folding in normalizeTeamName; strip Wikipedia "(tennis)"
  disambiguators; treat Bye/TBD/Qualifier as TBD; extract wikilink before
  template-stripping so {{nowrap}}-wrapped players parse

Admin UX
- dry-run preview (matched / will-create / possible duplicates / unfilled
  slots) with inline "rename existing" / "create as new" resolution via
  fetcher (no full reload)

Tests: parser vs real 2025 Wimbledon fixture, draw mapping, tennis_128
scoring, accent/disambiguator/nowrap parsing, URL parsing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 21:48:00 -07:00

59 lines
2.1 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { normalizeTeamName, findMatchingTeamName } from "../normalize-team-name";
describe("normalizeTeamName", () => {
it("lowercases and trims", () => {
expect(normalizeTeamName(" Boston Celtics ")).toBe("boston celtics");
});
it("collapses multiple spaces", () => {
expect(normalizeTeamName("Los Angeles Lakers")).toBe("los angeles lakers");
});
it("is idempotent", () => {
const name = "oklahoma city thunder";
expect(normalizeTeamName(name)).toBe(name);
});
it("folds accents so diacritics don't block a match", () => {
expect(normalizeTeamName("Stéfanos Tsitsipás")).toBe("stefanos tsitsipas");
expect(normalizeTeamName("Félix Auger-Aliassime")).toBe("felix auger-aliassime");
});
});
describe("findMatchingTeamName", () => {
const participants = [
"Boston Celtics",
"Los Angeles Lakers",
"Oklahoma City Thunder",
"Golden State Warriors",
"Toronto Raptors",
];
it("returns exact case-insensitive match", () => {
expect(findMatchingTeamName("Boston Celtics", participants)).toBe("Boston Celtics");
expect(findMatchingTeamName("boston celtics", participants)).toBe("Boston Celtics");
expect(findMatchingTeamName("TORONTO RAPTORS", participants)).toBe("Toronto Raptors");
});
it("matches partial city+team via substring (e.g. API returns city only)", () => {
// "Golden State" is a substring of "Golden State Warriors"
expect(findMatchingTeamName("Golden State", participants)).toBe("Golden State Warriors");
// "Oklahoma City" is a substring of "Oklahoma City Thunder"
expect(findMatchingTeamName("Oklahoma City", participants)).toBe("Oklahoma City Thunder");
});
it("matches across accents (API has them, our list doesn't)", () => {
expect(findMatchingTeamName("Stéfanos Tsitsipás", ["Stefanos Tsitsipas"])).toBe(
"Stefanos Tsitsipas",
);
});
it("returns null for unmatched name", () => {
expect(findMatchingTeamName("Phoenix Suns", participants)).toBeNull();
});
it("returns null for empty participants list", () => {
expect(findMatchingTeamName("Boston Celtics", [])).toBeNull();
});
});