Fix divisionRank ordering and add warning for unknown teams
Sort teams by win-loss within each division group before assigning divisionRank so that the leader is always the best-record team, not whichever team happened to appear first in ESPN's flat response. Also warn to console when a team's displayName isn't in the hardcoded MLB_TEAM_DIVISION map, surfacing renames or expansion teams immediately instead of silently storing division="" and recreating the display bug. https://claude.ai/code/session_01288dkXYwKJUfXrEhPd8rmo
This commit is contained in:
parent
a9acfba546
commit
0799177099
2 changed files with 54 additions and 11 deletions
|
|
@ -417,13 +417,50 @@ describe("MlbStandingsAdapter", () => {
|
||||||
expect(rays?.conference).toBe("AL");
|
expect(rays?.conference).toBe("AL");
|
||||||
expect(braves?.conference).toBe("NL");
|
expect(braves?.conference).toBe("NL");
|
||||||
|
|
||||||
// divisionRank should be 1 for each division leader, not just the globally first team
|
// divisionRank is sorted by win-loss within each division, NOT by ESPN response order.
|
||||||
// Rays and Yankees share AL East; Rays appears first → divisionRank 1
|
// Yankees (41-26) has a better record than Rays (40-25) even though Rays appears first
|
||||||
expect(rays?.divisionRank).toBe(1);
|
// in ESPN's flat list → Yankees should be divisionRank 1.
|
||||||
expect(yankees?.divisionRank).toBe(2);
|
expect(yankees?.divisionRank).toBe(1);
|
||||||
|
expect(rays?.divisionRank).toBe(2);
|
||||||
// White Sox is alone in AL Central sample → divisionRank 1
|
// White Sox is alone in AL Central sample → divisionRank 1
|
||||||
expect(whiteSox?.divisionRank).toBe(1);
|
expect(whiteSox?.divisionRank).toBe(1);
|
||||||
// Braves is alone in NL East sample → divisionRank 1
|
// Braves is alone in NL East sample → divisionRank 1
|
||||||
expect(braves?.divisionRank).toBe(1);
|
expect(braves?.divisionRank).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("warns to console when a team display name is not in the hardcoded division map", async () => {
|
||||||
|
const unknownTeamResponse = {
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
name: "American League",
|
||||||
|
standings: {
|
||||||
|
entries: [
|
||||||
|
{
|
||||||
|
team: { id: "99", displayName: "Portland Lumberjacks" },
|
||||||
|
stats: [
|
||||||
|
{ name: "wins", value: 30 },
|
||||||
|
{ name: "losses", value: 30 },
|
||||||
|
{ name: "winPercent", value: 0.5 },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
vi.mocked(fetch).mockResolvedValueOnce({
|
||||||
|
ok: true,
|
||||||
|
json: async () => unknownTeamResponse,
|
||||||
|
} as Response);
|
||||||
|
|
||||||
|
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||||
|
const adapter = new MlbStandingsAdapter();
|
||||||
|
await adapter.fetchStandings();
|
||||||
|
|
||||||
|
expect(warnSpy).toHaveBeenCalledWith(
|
||||||
|
expect.stringContaining("Portland Lumberjacks")
|
||||||
|
);
|
||||||
|
warnSpy.mockRestore();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -83,16 +83,22 @@ export class MlbStandingsAdapter implements StandingsSyncAdapter {
|
||||||
|
|
||||||
// Pre-build statsMap once per entry so the sort doesn't rebuild it on every comparison.
|
// Pre-build statsMap once per entry so the sort doesn't rebuild it on every comparison.
|
||||||
// Resolve division: use ESPN's value when present; fall back to the hardcoded map.
|
// Resolve division: use ESPN's value when present; fall back to the hardcoded map.
|
||||||
const withSm = flattened.map(({ entry, conference, division }) => ({
|
const withSm = flattened.map(({ entry, conference, division }) => {
|
||||||
entry,
|
const resolvedDivision = division || MLB_TEAM_DIVISION[entry.team.displayName] || "";
|
||||||
conference,
|
if (!resolvedDivision) {
|
||||||
division: division || MLB_TEAM_DIVISION[entry.team.displayName] || "",
|
console.warn(
|
||||||
sm: statsMap(entry.stats),
|
`[MLB standings] No division found for team "${entry.team.displayName}" (id=${entry.team.id}). ` +
|
||||||
}));
|
"Update MLB_TEAM_DIVISION if this is a renamed or expansion team."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return { entry, conference, division: resolvedDivision, sm: statsMap(entry.stats) };
|
||||||
|
});
|
||||||
|
|
||||||
// Compute divisionRank: rank each team within its own division by win/loss.
|
// Compute divisionRank: rank each team within its own division by win/loss.
|
||||||
// Key includes conference to prevent AL/NL teams from sharing a group when ESPN
|
// Key includes conference to prevent AL/NL teams from sharing a group when ESPN
|
||||||
// returns short names like "East" for both leagues, or no names at all.
|
// returns short names like "East" for both leagues, or no names at all.
|
||||||
|
// Sort by win/loss within each group so rank 1 is always the best record,
|
||||||
|
// regardless of the order ESPN returns teams in the response.
|
||||||
const divisionRankMap = new Map<string, number>();
|
const divisionRankMap = new Map<string, number>();
|
||||||
const divisionGroups = new Map<string, typeof withSm>();
|
const divisionGroups = new Map<string, typeof withSm>();
|
||||||
for (const item of withSm) {
|
for (const item of withSm) {
|
||||||
|
|
@ -101,7 +107,7 @@ export class MlbStandingsAdapter implements StandingsSyncAdapter {
|
||||||
divisionGroups.get(key)?.push(item);
|
divisionGroups.get(key)?.push(item);
|
||||||
}
|
}
|
||||||
for (const divEntries of divisionGroups.values()) {
|
for (const divEntries of divisionGroups.values()) {
|
||||||
// Use the ESPN entry order within each division — ESPN already applies correct tiebreakers.
|
divEntries.sort(sortByWinLoss);
|
||||||
divEntries.forEach((e, i) => {
|
divEntries.forEach((e, i) => {
|
||||||
divisionRankMap.set(e.entry.team.id, i + 1);
|
divisionRankMap.set(e.entry.team.id, i + 1);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue