The loser of NBA Play-In Round 1 matches 1 and 3 (East/West 7v8 games) advances to Play-In Round 2 rather than being eliminated, but was being assigned finalPosition=0 and appearing in Discord as knocked out. - Add `loserAdvances` param to `processMatchResult` to skip recording an elimination result when the loser continues to another match - Extract `doesLoserAdvance()` to `playoff-match.ts` as the single source of truth for which play-in matches have advancing losers (replaces duplicated hardcoded checks at both call sites in bracket.server.ts) - Extract `isLoserNotifiable()` as a pure exported helper so Discord notification eligibility is testable; losers only appear when their team's score changed OR they have a finalized (non-partial) result, correctly suppressing advancing losers while still surfacing 0-pt eliminations that produce no score delta - Add 14 new unit tests covering `doesLoserAdvance`, `isLoserNotifiable`, and the advancing-loser/eliminated-loser `processMatchResult` branches Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
400 lines
16 KiB
TypeScript
400 lines
16 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
// Mock external DB-touching dependencies before importing the module under test
|
|
vi.mock("~/services/probability-updater", () => ({
|
|
updateProbabilitiesAfterResult: vi.fn().mockResolvedValue(undefined),
|
|
}));
|
|
|
|
// ── Minimal db mock ────────────────────────────────────────────────────────
|
|
//
|
|
// processMatchResult calls, in order:
|
|
// 1. upsertParticipantResult (x2) → db.query.participantResults.findFirst, db.insert
|
|
// 2. recalculateAffectedLeagues → db.query.seasonSports.findMany (returns [] so loop exits)
|
|
// 3. updateProbabilitiesAfterResult (mocked above)
|
|
|
|
function makeDb(existingResult?: { id: string; isPartialScore: boolean }) {
|
|
// Track all inserted values in order
|
|
const insertedRows: object[] = [];
|
|
const updatedRows: object[] = [];
|
|
|
|
const insertValues = vi.fn().mockImplementation((values: object) => {
|
|
insertedRows.push(values);
|
|
return Promise.resolve();
|
|
});
|
|
|
|
const updateWhere = vi.fn().mockResolvedValue(undefined);
|
|
const updateSet = vi.fn().mockImplementation((values: object) => {
|
|
updatedRows.push(values);
|
|
return { where: updateWhere };
|
|
});
|
|
|
|
// participantResults.findFirst: first call gets existingResult (if any),
|
|
// subsequent calls return undefined (winner/loser don't have existing rows)
|
|
let findFirstCallCount = 0;
|
|
const findFirst = vi.fn().mockImplementation(() => {
|
|
const result = findFirstCallCount === 0 ? existingResult : undefined;
|
|
findFirstCallCount++;
|
|
return Promise.resolve(result);
|
|
});
|
|
|
|
return {
|
|
db: {
|
|
insert: vi.fn().mockReturnValue({ values: insertValues }),
|
|
update: vi.fn().mockReturnValue({ set: updateSet }),
|
|
query: {
|
|
participantResults: { findFirst },
|
|
seasonSports: {
|
|
findMany: vi.fn().mockResolvedValue([]), // empty → standings loop exits immediately
|
|
},
|
|
sportsSeasons: { findFirst: vi.fn().mockResolvedValue(null) },
|
|
scoringEvents: { findFirst: vi.fn().mockResolvedValue(null) },
|
|
},
|
|
} as any,
|
|
insertedRows,
|
|
updatedRows,
|
|
findFirst,
|
|
insertValues,
|
|
updateSet,
|
|
updateWhere,
|
|
};
|
|
}
|
|
|
|
import { processMatchResult, isLoserNotifiable } from "../scoring-calculator";
|
|
import { doesLoserAdvance } from "../playoff-match";
|
|
import { updateProbabilitiesAfterResult } from "~/services/probability-updater";
|
|
|
|
const BASE = {
|
|
winnerId: "winner-1",
|
|
loserId: "loser-1",
|
|
sportsSeasonId: "ss-1",
|
|
bracketTemplateId: null as string | null,
|
|
};
|
|
|
|
describe("processMatchResult", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("Quarterfinals (scoring)", () => {
|
|
it("loser gets position 5 (final), winner gets position 3 (provisional)", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
|
|
await processMatchResult(
|
|
{ ...BASE, round: "Quarterfinals", isScoring: true },
|
|
db
|
|
);
|
|
|
|
expect(insertedRows).toHaveLength(2);
|
|
expect(insertedRows[0]).toMatchObject({ participantId: "loser-1", finalPosition: 5, isPartialScore: false });
|
|
expect(insertedRows[1]).toMatchObject({ participantId: "winner-1", finalPosition: 3, isPartialScore: true });
|
|
});
|
|
|
|
it("also works for Elite Eight / Divisional / Conference Semifinals", async () => {
|
|
for (const round of ["Elite Eight", "Divisional", "Conference Semifinals"]) {
|
|
const { db, insertedRows } = makeDb();
|
|
await processMatchResult({ ...BASE, round, isScoring: true }, db);
|
|
expect(insertedRows[0]).toMatchObject({ finalPosition: 5, isPartialScore: false });
|
|
expect(insertedRows[1]).toMatchObject({ finalPosition: 3, isPartialScore: true });
|
|
}
|
|
});
|
|
|
|
it("recalculates standings (calls db.query.seasonSports.findMany)", async () => {
|
|
const { db } = makeDb();
|
|
await processMatchResult({ ...BASE, round: "Quarterfinals", isScoring: true }, db);
|
|
expect((db.query.seasonSports.findMany as ReturnType<typeof vi.fn>)).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("Semifinals (scoring)", () => {
|
|
it("loser gets position 3 (final), winner gets position 2 (provisional)", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
|
|
await processMatchResult({ ...BASE, round: "Semifinals", isScoring: true }, db);
|
|
|
|
expect(insertedRows[0]).toMatchObject({ participantId: "loser-1", finalPosition: 3, isPartialScore: false });
|
|
expect(insertedRows[1]).toMatchObject({ participantId: "winner-1", finalPosition: 2, isPartialScore: true });
|
|
});
|
|
|
|
it("also works for Final Four / Conference Finals / Conference Championship", async () => {
|
|
for (const round of ["Final Four", "Conference Finals", "Conference Championship"]) {
|
|
const { db, insertedRows } = makeDb();
|
|
await processMatchResult({ ...BASE, round, isScoring: true }, db);
|
|
expect(insertedRows[0]).toMatchObject({ finalPosition: 3, isPartialScore: false });
|
|
expect(insertedRows[1]).toMatchObject({ finalPosition: 2, isPartialScore: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("Finals / Championship (scoring)", () => {
|
|
it("loser=2nd (final), winner=1st (final), no third row", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
|
|
await processMatchResult({ ...BASE, round: "Finals", isScoring: true }, db);
|
|
|
|
expect(insertedRows).toHaveLength(2);
|
|
expect(insertedRows[0]).toMatchObject({ participantId: "loser-1", finalPosition: 2, isPartialScore: false });
|
|
expect(insertedRows[1]).toMatchObject({ participantId: "winner-1", finalPosition: 1, isPartialScore: false });
|
|
});
|
|
|
|
it("handles all Finals aliases the same way", async () => {
|
|
for (const round of ["Championship", "Super Bowl", "NBA Finals", "Grand Final"]) {
|
|
const { db, insertedRows } = makeDb();
|
|
await processMatchResult({ ...BASE, round, isScoring: true }, db);
|
|
expect(insertedRows).toHaveLength(2);
|
|
expect(insertedRows[0]).toMatchObject({ finalPosition: 2, isPartialScore: false });
|
|
expect(insertedRows[1]).toMatchObject({ finalPosition: 1, isPartialScore: false });
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("Non-scoring round (isScoring=false)", () => {
|
|
it("loser eliminated (pos 0 final), winner gets provisional T5 floor (no template = legacy)", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
|
|
await processMatchResult({ ...BASE, round: "Round of 16", isScoring: false }, db);
|
|
|
|
expect(insertedRows[0]).toMatchObject({ participantId: "loser-1", finalPosition: 0, isPartialScore: false });
|
|
expect(insertedRows[1]).toMatchObject({ participantId: "winner-1", finalPosition: 5, isPartialScore: true });
|
|
});
|
|
|
|
describe("NCAA 68 — only Sweet Sixteen winners earn a floor", () => {
|
|
const NCAA = { bracketTemplateId: "ncaa_68" };
|
|
|
|
it("Round of 64: loser=0, winner gets NO score (not yet in scoring bracket)", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
await processMatchResult({ ...BASE, ...NCAA, round: "Round of 64", isScoring: false }, db);
|
|
expect(insertedRows).toHaveLength(1);
|
|
expect(insertedRows[0]).toMatchObject({ participantId: "loser-1", finalPosition: 0, isPartialScore: false });
|
|
});
|
|
|
|
it("Round of 32: loser=0, winner gets NO score (not yet in scoring bracket)", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
await processMatchResult({ ...BASE, ...NCAA, round: "Round of 32", isScoring: false }, db);
|
|
expect(insertedRows).toHaveLength(1);
|
|
expect(insertedRows[0]).toMatchObject({ participantId: "loser-1", finalPosition: 0, isPartialScore: false });
|
|
});
|
|
|
|
it("Sweet Sixteen: loser=0, winner gets provisional T5 floor (entering Elite Eight)", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
await processMatchResult({ ...BASE, ...NCAA, round: "Sweet Sixteen", isScoring: false }, db);
|
|
expect(insertedRows).toHaveLength(2);
|
|
expect(insertedRows[0]).toMatchObject({ participantId: "loser-1", finalPosition: 0, isPartialScore: false });
|
|
expect(insertedRows[1]).toMatchObject({ participantId: "winner-1", finalPosition: 5, isPartialScore: true });
|
|
});
|
|
|
|
it("First Four: loser=0, winner gets NO score (feeds into Round of 64, not scoring)", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
await processMatchResult({ ...BASE, ...NCAA, round: "First Four", isScoring: false }, db);
|
|
expect(insertedRows).toHaveLength(1);
|
|
expect(insertedRows[0]).toMatchObject({ participantId: "loser-1", finalPosition: 0, isPartialScore: false });
|
|
});
|
|
});
|
|
|
|
it("AFL Wildcard Round: loser=0, winner gets T5 floor (feeds into Elimination Finals = scoring)", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
await processMatchResult({ ...BASE, bracketTemplateId: "afl_10", round: "Wildcard Round", isScoring: false }, db);
|
|
expect(insertedRows).toHaveLength(2);
|
|
expect(insertedRows[0]).toMatchObject({ participantId: "loser-1", finalPosition: 0, isPartialScore: false });
|
|
expect(insertedRows[1]).toMatchObject({ participantId: "winner-1", finalPosition: 5, isPartialScore: true });
|
|
});
|
|
|
|
describe("NBA Play-In loserAdvances=true (7v8 game)", () => {
|
|
it("loser is NOT eliminated when loserAdvances=true (advances to Round 2)", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
await processMatchResult(
|
|
{
|
|
...BASE,
|
|
bracketTemplateId: "nba_20",
|
|
round: "Play-In Round 1",
|
|
isScoring: false,
|
|
loserAdvances: true,
|
|
},
|
|
db
|
|
);
|
|
// Neither participant gets a result: loser skipped (still playing), and
|
|
// winner gets nothing because PIR1 doesn't feed directly into a scoring round.
|
|
expect(insertedRows).toHaveLength(0);
|
|
});
|
|
|
|
it("loser IS eliminated when loserAdvances=false (9v10 game)", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
await processMatchResult(
|
|
{
|
|
...BASE,
|
|
bracketTemplateId: "nba_20",
|
|
round: "Play-In Round 1",
|
|
isScoring: false,
|
|
loserAdvances: false,
|
|
},
|
|
db
|
|
);
|
|
expect(insertedRows).toHaveLength(1);
|
|
expect(insertedRows[0]).toMatchObject({ participantId: "loser-1", finalPosition: 0, isPartialScore: false });
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("Unrecognized scoring round", () => {
|
|
it("gives loser 0 pts and still completes without throwing", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
|
|
await processMatchResult({ ...BASE, round: "Mystery Round", isScoring: true }, db);
|
|
|
|
expect(insertedRows[0]).toMatchObject({ finalPosition: 0, isPartialScore: false });
|
|
});
|
|
});
|
|
|
|
describe("AFL rounds", () => {
|
|
it("Elimination Finals: loser exits at position 7 (final)", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
|
|
await processMatchResult(
|
|
{ ...BASE, round: "Elimination Finals", isScoring: true, bracketTemplateId: "afl_10" },
|
|
db
|
|
);
|
|
|
|
expect(insertedRows[0]).toMatchObject({ participantId: "loser-1", finalPosition: 7, isPartialScore: false });
|
|
});
|
|
|
|
it("Qualifying Finals: loser is still alive (provisional floor 5)", async () => {
|
|
const { db, insertedRows } = makeDb();
|
|
|
|
await processMatchResult(
|
|
{ ...BASE, round: "Qualifying Finals", isScoring: true, bracketTemplateId: "afl_10" },
|
|
db
|
|
);
|
|
|
|
// Loser row is provisional (isPartialScore=true)
|
|
expect(insertedRows[0]).toMatchObject({ participantId: "loser-1", finalPosition: 5, isPartialScore: true });
|
|
});
|
|
});
|
|
|
|
describe("Idempotency: already-final row is not un-finalized", () => {
|
|
it("does not overwrite a finalized row with a partial score", async () => {
|
|
// findFirst returns a finalized row for the first participant (loser)
|
|
const existing = { id: "result-1", isPartialScore: false };
|
|
const { db, updatedRows } = makeDb(existing);
|
|
|
|
await processMatchResult(
|
|
{ ...BASE, round: "Quarterfinals", isScoring: true },
|
|
db
|
|
);
|
|
|
|
// The guard in upsertParticipantResult should skip the update when
|
|
// existing.isPartialScore=false and we're trying to write isPartialScore=true.
|
|
// The loser row would try to write isPartialScore=false (final) — that IS
|
|
// allowed (both false). The winner row tries isPartialScore=true — since
|
|
// there's no existing row for the winner (findFirst returns undefined on
|
|
// second call), it inserts.
|
|
//
|
|
// Key assertion: no update/insert with isPartialScore=true was applied to
|
|
// the existing finalized row.
|
|
const finalizedOverwrite = updatedRows.find(
|
|
(r: any) => r.isPartialScore === true
|
|
);
|
|
expect(finalizedOverwrite).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("Probability recalculation", () => {
|
|
it("always calls updateProbabilitiesAfterResult with the sports season id", async () => {
|
|
const { db } = makeDb();
|
|
|
|
await processMatchResult({ ...BASE, round: "Quarterfinals", isScoring: true }, db);
|
|
|
|
expect(updateProbabilitiesAfterResult).toHaveBeenCalledWith("ss-1", true);
|
|
});
|
|
|
|
it("does not throw even if probability update fails", async () => {
|
|
(updateProbabilitiesAfterResult as ReturnType<typeof vi.fn>).mockRejectedValueOnce(
|
|
new Error("network error")
|
|
);
|
|
const { db } = makeDb();
|
|
|
|
// Should not throw — the error is caught and logged
|
|
await expect(
|
|
processMatchResult({ ...BASE, round: "Quarterfinals", isScoring: true }, db)
|
|
).resolves.not.toThrow();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("doesLoserAdvance", () => {
|
|
it("returns true for NBA Play-In Round 1 match 1 (E7v8)", () => {
|
|
expect(doesLoserAdvance("Play-In Round 1", 1, "nba_20")).toBe(true);
|
|
});
|
|
|
|
it("returns true for NBA Play-In Round 1 match 3 (W7v8)", () => {
|
|
expect(doesLoserAdvance("Play-In Round 1", 3, "nba_20")).toBe(true);
|
|
});
|
|
|
|
it("returns false for NBA Play-In Round 1 match 2 (E9v10 — loser eliminated)", () => {
|
|
expect(doesLoserAdvance("Play-In Round 1", 2, "nba_20")).toBe(false);
|
|
});
|
|
|
|
it("returns false for NBA Play-In Round 1 match 4 (W9v10 — loser eliminated)", () => {
|
|
expect(doesLoserAdvance("Play-In Round 1", 4, "nba_20")).toBe(false);
|
|
});
|
|
|
|
it("returns false for NBA Play-In Round 2 (all losers eliminated)", () => {
|
|
expect(doesLoserAdvance("Play-In Round 2", 1, "nba_20")).toBe(false);
|
|
expect(doesLoserAdvance("Play-In Round 2", 2, "nba_20")).toBe(false);
|
|
});
|
|
|
|
it("returns false for other templates", () => {
|
|
expect(doesLoserAdvance("Play-In Round 1", 1, "nfl_14")).toBe(false);
|
|
expect(doesLoserAdvance("Semifinals", 1, "fifa_48")).toBe(false);
|
|
});
|
|
|
|
it("returns false for an empty templateId", () => {
|
|
expect(doesLoserAdvance("Play-In Round 1", 1, "")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("isLoserNotifiable", () => {
|
|
const LOSER = "loser-1";
|
|
const TEAM = "team-1";
|
|
|
|
it("returns true when the loser's team score changed", () => {
|
|
expect(
|
|
isLoserNotifiable(LOSER, TEAM, new Set([TEAM]), new Set())
|
|
).toBe(true);
|
|
});
|
|
|
|
it("returns true when the loser is finalized even with no score change (0-pt elimination)", () => {
|
|
expect(
|
|
isLoserNotifiable(LOSER, TEAM, new Set(), new Set([LOSER]))
|
|
).toBe(true);
|
|
});
|
|
|
|
it("returns false when score did not change and loser is not finalized (advances)", () => {
|
|
expect(
|
|
isLoserNotifiable(LOSER, TEAM, new Set(), new Set())
|
|
).toBe(false);
|
|
});
|
|
|
|
it("returns false when loserId is null", () => {
|
|
expect(
|
|
isLoserNotifiable(null, TEAM, new Set([TEAM]), new Set())
|
|
).toBe(false);
|
|
});
|
|
|
|
it("returns false when loserId is undefined", () => {
|
|
expect(
|
|
isLoserNotifiable(undefined, undefined, new Set(), new Set())
|
|
).toBe(false);
|
|
});
|
|
|
|
it("returns false when loser has no team (undrafted) and is not finalized", () => {
|
|
expect(
|
|
isLoserNotifiable(LOSER, undefined, new Set(), new Set())
|
|
).toBe(false);
|
|
});
|
|
|
|
it("returns true when loser has no team but is finalized (edge case: drafted mid-season?)", () => {
|
|
// finalizedLoserIds wins regardless of team presence
|
|
expect(
|
|
isLoserNotifiable(LOSER, undefined, new Set(), new Set([LOSER]))
|
|
).toBe(true);
|
|
});
|
|
});
|