From 05eeaf28dc02c4cb42e5d96063ee0cd4b64ac122 Mon Sep 17 00:00:00 2001 From: Beehtrix Date: Wed, 2 Dec 2026 00:35:17 +0000 Subject: [PATCH] fix(comparison): use strict score comparisons for insights ties Award category strengths and recommendations only when one developer strictly leads; show equal/tie notes when metrics match. Fixes #268 --- .../comparison/components/insights-list.tsx | 112 +++++++++------ .../comparison/tests/insights-list.test.tsx | 132 ++++++++++++++++++ 2 files changed, 200 insertions(+), 44 deletions(-) create mode 100644 src/features/comparison/tests/insights-list.test.tsx diff --git a/src/features/comparison/components/insights-list.tsx b/src/features/comparison/components/insights-list.tsx index 2be1e3b..93ed67d 100644 --- a/src/features/comparison/components/insights-list.tsx +++ b/src/features/comparison/components/insights-list.tsx @@ -27,6 +27,21 @@ function getDisplayName(user?: UserResult, fallback?: string): string { return normalizedName || user.username; } +function pickScoreLeader( + user1: UserResult, + user2: UserResult, + scoreOf: (user: UserResult) => number, +): UserResult | null { + const score1 = scoreOf(user1); + const score2 = scoreOf(user2); + + if (score1 === score2) { + return null; + } + + return score1 > score2 ? user1 : user2; +} + export function InsightsList({ insights, user1, user2, user1Name, user2Name }: Props) { const { t } = useTranslation(); @@ -37,49 +52,50 @@ export function InsightsList({ insights, user1, user2, user1Name, user2Name }: P const firstName = getDisplayName(user1, user1Name); const secondName = getDisplayName(user2, user2Name); - const finalLeader = user1.finalScore >= user2.finalScore ? user1 : user2; - const finalFollower = finalLeader.username === user1.username ? user2 : user1; - const finalLeaderName = getDisplayName(finalLeader); - const finalFollowerName = getDisplayName(finalFollower); + const finalLeader = pickScoreLeader(user1, user2, (user) => user.finalScore); + const finalFollower = + finalLeader == null ? null : finalLeader.username === user1.username ? user2 : user1; + const finalLeaderName = getDisplayName(finalLeader ?? undefined); + const finalFollowerName = getDisplayName(finalFollower ?? undefined); const finalDiff = Math.abs(user1.finalScore - user2.finalScore); - const repoLeader = user1.repoScore >= user2.repoScore ? user1 : user2; - const prLeader = user1.prScore >= user2.prScore ? user1 : user2; - const contributionLeader = user1.contributionScore >= user2.contributionScore ? user1 : user2; + const repoLeader = pickScoreLeader(user1, user2, (user) => user.repoScore); + const prLeader = pickScoreLeader(user1, user2, (user) => user.prScore); + const contributionLeader = pickScoreLeader(user1, user2, (user) => user.contributionScore); const user1Strengths: string[] = []; const user2Strengths: string[] = []; - if (repoLeader.username === user1.username) { + if (repoLeader?.username === user1.username) { user1Strengths.push(t("insights.strength.repo")); - } else { + } else if (repoLeader?.username === user2.username) { user2Strengths.push(t("insights.strength.repo")); } - if (prLeader.username === user1.username) { + if (prLeader?.username === user1.username) { user1Strengths.push(t("insights.strength.pr")); - } else { + } else if (prLeader?.username === user2.username) { user2Strengths.push(t("insights.strength.pr")); } - if (contributionLeader.username === user1.username) { + if (contributionLeader?.username === user1.username) { user1Strengths.push(t("insights.strength.contribution")); - } else { + } else if (contributionLeader?.username === user2.username) { user2Strengths.push(t("insights.strength.contribution")); } const buildRecommendations = (target: UserResult): string[] => { const recommendations: string[] = []; - if (target.repoScore < repoLeader.repoScore) { + if (repoLeader && target.repoScore < repoLeader.repoScore) { recommendations.push(t("insights.recommendation.repo")); } - if (target.prScore < prLeader.prScore) { + if (prLeader && target.prScore < prLeader.prScore) { recommendations.push(t("insights.recommendation.pr")); } - if (target.contributionScore < contributionLeader.contributionScore) { + if (contributionLeader && target.contributionScore < contributionLeader.contributionScore) { recommendations.push(t("insights.recommendation.contribution")); } @@ -105,11 +121,13 @@ export function InsightsList({ insights, user1, user2, user1Name, user2Name }: P

{t("insights.summary")}

- {t("insights.summaryText", { - winner: finalLeaderName, - diff: Math.round(finalDiff), - other: finalFollowerName, - })} + {finalLeader && finalFollower + ? t("insights.summaryText", { + winner: finalLeaderName, + diff: Math.round(finalDiff), + other: finalFollowerName, + }) + : t("banner.tie")}

@@ -117,33 +135,39 @@ export function InsightsList({ insights, user1, user2, user1Name, user2Name }: P

{t("insights.keyDifferences")}

diff --git a/src/features/comparison/tests/insights-list.test.tsx b/src/features/comparison/tests/insights-list.test.tsx new file mode 100644 index 0000000..283a802 --- /dev/null +++ b/src/features/comparison/tests/insights-list.test.tsx @@ -0,0 +1,132 @@ +// @vitest-environment jsdom + +import { cleanup, render, screen, within } from "@testing-library/react"; +import { afterEach, describe, expect, test, vi } from "vitest"; +import { InsightsList } from "../components/insights-list"; +import type { CompareInsights } from "../types"; +import type { UserResult } from "@/features/developer"; + +vi.mock("@/components/providers/language-provider", () => ({ + useTranslation: () => ({ + t: (key: string) => key, + }), +})); + +function createUser(overrides: Partial & Pick): UserResult { + return { + username: overrides.username, + name: overrides.name ?? overrides.username, + avatarUrl: overrides.avatarUrl ?? "", + repoScore: overrides.repoScore ?? 0, + prScore: overrides.prScore ?? 0, + contributionScore: overrides.contributionScore ?? 0, + finalScore: overrides.finalScore ?? 0, + topRepos: overrides.topRepos ?? [], + topPullRequests: overrides.topPullRequests ?? [], + }; +} + +const insights: CompareInsights = { + summary: "summary", + keyDifferences: [], + user1Strengths: [], + user2Strengths: [], + recommendations: { user1: [], user2: [] }, + confidenceNote: "note", +}; + +function strengthSection(heading: string) { + const title = screen.getByRole("heading", { name: heading }); + const section = title.closest("section"); + if (!section) { + throw new Error(`Missing section for ${heading}`); + } + return within(section); +} + +describe("InsightsList tie handling", () => { + afterEach(() => { + cleanup(); + }); + + test("does not award category strengths or recommendations when scores are tied", () => { + const user1 = createUser({ + username: "alice", + name: "Alice", + repoScore: 10, + prScore: 0, + contributionScore: 5, + finalScore: 15, + }); + const user2 = createUser({ + username: "bob", + name: "Bob", + repoScore: 10, + prScore: 0, + contributionScore: 5, + finalScore: 15, + }); + + render(); + + expect(screen.getByText("banner.tie")).toBeTruthy(); + expect(screen.getByText("insights.equal.repo")).toBeTruthy(); + expect(screen.getByText("insights.equal.pr")).toBeTruthy(); + expect(screen.getByText("insights.equal.contribution")).toBeTruthy(); + + expect(screen.queryByText("insights.strength.repo")).toBeNull(); + expect(screen.queryByText("insights.strength.pr")).toBeNull(); + expect(screen.queryByText("insights.strength.contribution")).toBeNull(); + expect(screen.queryByText("insights.recommendation.repo")).toBeNull(); + expect(screen.queryByText("insights.recommendation.pr")).toBeNull(); + expect(screen.queryByText("insights.recommendation.contribution")).toBeNull(); + + expect(screen.getAllByText("insights.strength.balanced")).toHaveLength(2); + expect(screen.getAllByText("insights.recommendation.maintain")).toHaveLength(2); + }); + + test("awards strengths only when a developer strictly leads a category", () => { + const user1 = createUser({ + username: "alice", + name: "Alice", + repoScore: 20, + prScore: 4, + contributionScore: 8, + finalScore: 32, + }); + const user2 = createUser({ + username: "bob", + name: "Bob", + repoScore: 10, + prScore: 4, + contributionScore: 12, + finalScore: 26, + }); + + render(); + + const aliceStrengths = strengthSection("Alice"); + const bobStrengths = strengthSection("Bob"); + + expect(aliceStrengths.getByText("insights.strength.repo")).toBeTruthy(); + expect(aliceStrengths.queryByText("insights.strength.pr")).toBeNull(); + expect(aliceStrengths.queryByText("insights.strength.contribution")).toBeNull(); + + expect(bobStrengths.getByText("insights.strength.contribution")).toBeTruthy(); + expect(bobStrengths.queryByText("insights.strength.repo")).toBeNull(); + expect(bobStrengths.queryByText("insights.strength.pr")).toBeNull(); + + expect(screen.getByText("insights.repo.leader")).toBeTruthy(); + expect(screen.getByText("insights.equal.pr")).toBeTruthy(); + expect(screen.getByText("insights.contribution.leaderDetailed")).toBeTruthy(); + + expect(screen.getByText("insights.recommendation.repo")).toBeTruthy(); + expect(screen.getByText("insights.recommendation.contribution")).toBeTruthy(); + expect(screen.queryByText("insights.recommendation.pr")).toBeNull(); + }); + + test("returns null when required comparison data is missing", () => { + const { container } = render(); + expect(container.firstChild).toBeNull(); + }); +});