Skip to content
Open
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
9 changes: 5 additions & 4 deletions frontend/src/Pages/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import {
import { getAuthToken } from "@/utils/auth";
import { useNavigate } from "react-router-dom";
import { DateRange } from "react-day-picker";
import { formatDebateType } from "@/utils/debateType";
import AvatarModal from "../components/AvatarModal";
import SavedTranscripts from "../components/SavedTranscripts";
import ProfileHover from "../components/ProfileHover";
Expand Down Expand Up @@ -1146,7 +1147,7 @@ const Profile: React.FC = () => {
</div>
<div>
<span className="font-semibold">Type:</span>
<p className="text-muted-foreground capitalize">{selectedDebate.debateType.replace("_", " ")}</p>
<p className="text-muted-foreground">{formatDebateType(selectedDebate.debateType)}</p>
</div>
<div>
<span className="font-semibold">Result:</span>
Expand All @@ -1168,10 +1169,10 @@ const Profile: React.FC = () => {
<div>
<h4 className="font-semibold mb-3">Debate Summary</h4>
<p className="text-sm text-muted-foreground">
This debate was a {selectedDebate.debateType.replace("_", " ")} debate about "{selectedDebate.topic}" against {selectedDebate.opponent}. The result was a {selectedDebate.result}.
{selectedDebate.eloChange && selectedDebate.eloChange !== 0 && (
This debate was a {formatDebateType(selectedDebate.debateType)} debate about "{selectedDebate.topic}" against {selectedDebate.opponent}. The result was a {selectedDebate.result}.
{selectedDebate.eloChange ? (
<span> Your Elo rating changed by {selectedDebate.eloChange > 0 ? "+" : ""}{selectedDebate.eloChange}.</span>
)}
) : null}
</p>
</div>
<Separator />
Expand Down
81 changes: 81 additions & 0 deletions frontend/src/Pages/__tests__/ProfileDebateDetails.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MemoryRouter } from 'react-router-dom';
import Profile from '../Profile';
import { getProfile } from '@/services/profileService';
import { transcriptService } from '@/services/transcriptService';

vi.mock('@/services/profileService', () => ({
getProfile: vi.fn(),
updateProfile: vi.fn(),
checkDisplayNameAvailability: vi.fn(),
}));
vi.mock('@/services/transcriptService', () => ({
transcriptService: { getTranscriptById: vi.fn() },
}));
vi.mock('../../hooks/useUser', () => ({ useUser: () => ({ user: null }) }));
vi.mock('../../components/SavedTranscripts', () => ({ default: () => null }));
vi.mock('../../components/AvatarModal', () => ({ default: () => null }));

const recentDebate = (eloChange: number) => ({
id: 't1',
topic: 'Should social media be regulated?',
result: 'win',
opponent: 'Socrates Bot',
debateType: 'user_vs_bot',
date: '2026-11-24T10:00:00Z',
eloChange,
});

function mockProfile(eloChange: number) {
vi.mocked(getProfile).mockResolvedValue({
profile: { id: 'u1', displayName: 'Demo', email: 'demo@example.com', bio: '', rating: 1500, avatarUrl: '' },
leaderboard: [],
stats: {
wins: 1, losses: 0, draws: 0, winRate: 100, totalDebates: 1,
eloHistory: [], debateHistory: [], recentDebates: [recentDebate(eloChange)],
},
});
vi.mocked(transcriptService.getTranscriptById).mockResolvedValue({
id: 't1', userId: 'u1', email: 'demo@example.com', debateType: 'user_vs_bot',
topic: 'Should social media be regulated?', opponent: 'Socrates Bot', result: 'win',
messages: [], createdAt: '2026-11-24T10:00:00Z', updatedAt: '2026-11-24T10:00:00Z',
});
}

async function openDebateDetails() {
render(
<MemoryRouter>
<Profile />
</MemoryRouter>
);
await userEvent.click(await screen.findByText('Should social media be regulated?'));
return screen.findByRole('dialog');
}

describe('Profile Debate Details dialog', () => {
beforeEach(() => {
localStorage.setItem('token', 'test-token');
});

it('shows a readable debate type and no stray 0 when the Elo change is 0', async () => {
mockProfile(0);
const dialog = await openDebateDetails();

expect(within(dialog).getByText('User vs Bot')).toBeInTheDocument();
expect(within(dialog).queryByText(/vs_bot/i)).not.toBeInTheDocument();

const summary = within(dialog).getByText(/This debate was a/);
expect(summary).toHaveTextContent(
'This debate was a User vs Bot debate about "Should social media be regulated?" against Socrates Bot. The result was a win.'
);
expect(summary.textContent?.trim().endsWith('win.')).toBe(true);
});

it('still shows the Elo sentence when the change is non-zero', async () => {
mockProfile(12);
const dialog = await openDebateDetails();
expect(within(dialog).getByText(/This debate was a/)).toHaveTextContent('Your Elo rating changed by +12.');
});
});
5 changes: 3 additions & 2 deletions frontend/src/components/SavedTranscripts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { format } from 'date-fns';
import CommentTree from './CommentTree';
import { Share2 } from 'lucide-react';
import { useNavigate } from 'react-router-dom';
import { formatDebateType } from '@/utils/debateType';

interface SavedTranscriptsProps {
className?: string;
Expand Down Expand Up @@ -465,8 +466,8 @@ const SavedTranscripts: React.FC<SavedTranscriptsProps> = ({ className }) => {
</div>
<div>
<span className='font-semibold'>Type:</span>
<p className='text-muted-foreground capitalize'>
{selectedTranscript.debateType.replace('_', ' ')}
<p className='text-muted-foreground'>
{formatDebateType(selectedTranscript.debateType)}
</p>
</div>
<div>
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/components/__tests__/SavedTranscripts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ describe('SavedTranscripts', () => {
expect(within(dialog).getByText('Innovation needs freedom')).toBeInTheDocument();
expect(within(dialog).getByText('AI')).toBeInTheDocument();
expect(within(dialog).getByText('comments')).toBeInTheDocument();
expect(within(dialog).getByText('User vs Bot')).toBeInTheDocument();
expect(within(dialog).queryByText(/vs_bot/i)).not.toBeInTheDocument();
});

it('shares a transcript to the community feed', async () => {
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/utils/__tests__/debateType.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, expect, it } from 'vitest';
import { formatDebateType } from '../debateType';

describe('formatDebateType', () => {
it.each([
['user_vs_bot', 'User vs Bot'],
['user_vs_user', 'User vs User'],
])('formats %s as "%s"', (type, label) => {
expect(formatDebateType(type)).toBe(label);
});

it('replaces every underscore for unknown types', () => {
expect(formatDebateType('team_vs_team_v2')).toBe('team vs team v2');
});
});
12 changes: 12 additions & 0 deletions frontend/src/utils/debateType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const DEBATE_TYPE_LABELS: Record<string, string> = {
user_vs_bot: "User vs Bot",
user_vs_user: "User vs User",
};

/**
* Turns a stored debate type such as "user_vs_bot" into a readable label
* ("User vs Bot"). Unknown types fall back to the value with every
* underscore replaced by a space.
*/
export const formatDebateType = (debateType: string): string =>
DEBATE_TYPE_LABELS[debateType] ?? debateType.replace(/_/g, " ");
Loading