Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 68 additions & 44 deletions src/features/comparison/components/insights-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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"));
}

Expand All @@ -105,45 +121,53 @@ export function InsightsList({ insights, user1, user2, user1Name, user2Name }: P
<section>
<h4 className="font-semibold">{t("insights.summary")}</h4>
<p className="mt-1 text-muted-foreground">
{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")}
</p>
</section>

<section>
<h4 className="font-semibold">{t("insights.keyDifferences")}</h4>
<ul className="mt-2 list-disc space-y-1 ps-5 text-muted-foreground">
<li>
{t("insights.repo.leader", {
username: getDisplayName(repoLeader),
score: Math.round(repoLeader.repoScore),
other: Math.round(
repoLeader.username === user1.username ? user2.repoScore : user1.repoScore,
),
})}
{repoLeader
? t("insights.repo.leader", {
username: getDisplayName(repoLeader),
score: Math.round(repoLeader.repoScore),
other: Math.round(
repoLeader.username === user1.username ? user2.repoScore : user1.repoScore,
),
})
: t("insights.equal.repo")}
</li>
<li>
{t("insights.pr.leader", {
username: getDisplayName(prLeader),
score: Math.round(prLeader.prScore),
other: Math.round(
prLeader.username === user1.username ? user2.prScore : user1.prScore,
),
})}
{prLeader
? t("insights.pr.leader", {
username: getDisplayName(prLeader),
score: Math.round(prLeader.prScore),
other: Math.round(
prLeader.username === user1.username ? user2.prScore : user1.prScore,
),
})
: t("insights.equal.pr")}
</li>
<li>
{t("insights.contribution.leaderDetailed", {
username: getDisplayName(contributionLeader),
score: Math.round(contributionLeader.contributionScore),
other: Math.round(
contributionLeader.username === user1.username
? user2.contributionScore
: user1.contributionScore,
),
})}
{contributionLeader
? t("insights.contribution.leaderDetailed", {
username: getDisplayName(contributionLeader),
score: Math.round(contributionLeader.contributionScore),
other: Math.round(
contributionLeader.username === user1.username
? user2.contributionScore
: user1.contributionScore,
),
})
: t("insights.equal.contribution")}
</li>
</ul>
</section>
Expand Down
132 changes: 132 additions & 0 deletions src/features/comparison/tests/insights-list.test.tsx
Original file line number Diff line number Diff line change
@@ -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<UserResult> & Pick<UserResult, "username">): 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(<InsightsList insights={insights} user1={user1} user2={user2} />);

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(<InsightsList insights={insights} user1={user1} user2={user2} />);

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(<InsightsList />);
expect(container.firstChild).toBeNull();
});
});
Loading