mirror of
https://github.com/AmanTahiliani/box-box.git
synced 2026-12-05 14:44:41 -05:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c736f8d353 |
No files matched your search
@@ -58,67 +58,35 @@ function SectionState({
|
||||
return <>{children}</>
|
||||
}
|
||||
|
||||
function circuitIdentity(meeting: Meeting): { name: string; place: string } {
|
||||
const name = meeting.circuit_short_name?.trim() || meeting.meeting_name
|
||||
const location = meeting.location?.trim() ?? ''
|
||||
const country = meeting.country_name?.trim() ?? ''
|
||||
const parts: string[] = []
|
||||
if (location) parts.push(location)
|
||||
if (country && country !== location) parts.push(country)
|
||||
return { name, place: parts.join(' · ') }
|
||||
}
|
||||
|
||||
function TrackOutlineCard({
|
||||
meeting,
|
||||
outline,
|
||||
loading,
|
||||
error,
|
||||
accent,
|
||||
}: {
|
||||
meeting: Meeting
|
||||
outline: TrackOutline | null | undefined
|
||||
loading: boolean
|
||||
error: Error | null
|
||||
accent: string
|
||||
}) {
|
||||
const outlinePath = useMemo(() => buildOutlinePath(outline?.points ?? []), [outline])
|
||||
const identity = circuitIdentity(meeting)
|
||||
|
||||
let body: ReactNode
|
||||
if (loading) {
|
||||
body = <div className="preview-section-state">Loading…</div>
|
||||
} else if (error) {
|
||||
body = (
|
||||
<div className="preview-section-state error">
|
||||
{error instanceof Error ? error.message : 'Failed to load'}
|
||||
</div>
|
||||
)
|
||||
} else if (outlinePath) {
|
||||
body = (
|
||||
<div className="preview-track-stage" style={{ ['--preview-accent' as string]: accent }}>
|
||||
<svg className="preview-track-svg" viewBox="0 0 100 100" role="img" aria-label="Circuit outline">
|
||||
<path className="preview-track-shadow" d={outlinePath} />
|
||||
<path className="preview-track-outline" d={outlinePath} />
|
||||
</svg>
|
||||
</div>
|
||||
)
|
||||
} else {
|
||||
body = (
|
||||
<div className="preview-track-fallback" data-testid="preview-track-fallback">
|
||||
<div className="preview-track-fallback-glyph" aria-hidden />
|
||||
<p className="preview-track-fallback-name">{identity.name}</p>
|
||||
{identity.place && <p className="preview-track-fallback-place">{identity.place}</p>}
|
||||
<p className="preview-track-fallback-hint">
|
||||
A GPS track outline is not available in the local cache.
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<section className="preview-card" data-testid="preview-track-card">
|
||||
<h2 className="preview-card-title">Circuit</h2>
|
||||
{body}
|
||||
<SectionState
|
||||
loading={loading}
|
||||
error={error}
|
||||
empty={!outline || !outlinePath}
|
||||
emptyMessage="Track outline unavailable for this circuit"
|
||||
>
|
||||
<div className="preview-track-stage" style={{ ['--preview-accent' as string]: accent }}>
|
||||
<svg className="preview-track-svg" viewBox="0 0 100 100" role="img" aria-label="Circuit outline">
|
||||
<path className="preview-track-shadow" d={outlinePath} />
|
||||
<path className="preview-track-outline" d={outlinePath} />
|
||||
</svg>
|
||||
</div>
|
||||
</SectionState>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
@@ -440,7 +408,6 @@ export function RacePreviewPage() {
|
||||
|
||||
<div className="preview-grid">
|
||||
<TrackOutlineCard
|
||||
meeting={previewMeeting}
|
||||
outline={trackOutlineQuery.data}
|
||||
loading={trackOutlineQuery.isLoading}
|
||||
error={trackOutlineQuery.isError ? (trackOutlineQuery.error as Error) : null}
|
||||
|
||||
@@ -203,47 +203,6 @@
|
||||
stroke-linejoin: round;
|
||||
}
|
||||
|
||||
.preview-track-fallback {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
gap: var(--s2);
|
||||
min-height: 180px;
|
||||
padding: var(--s5);
|
||||
}
|
||||
|
||||
.preview-track-fallback-glyph {
|
||||
width: 88px;
|
||||
height: 52px;
|
||||
margin-bottom: var(--s2);
|
||||
border: 2px dashed rgba(255, 255, 255, 0.14);
|
||||
border-radius: 42% 50% 46% 54%;
|
||||
}
|
||||
|
||||
.preview-track-fallback-name {
|
||||
font-size: 1.2rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-1);
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.preview-track-fallback-place {
|
||||
font-size: 0.85rem;
|
||||
color: var(--text-2);
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.preview-track-fallback-hint {
|
||||
font-size: 0.8rem;
|
||||
line-height: 1.5;
|
||||
color: var(--text-3);
|
||||
max-width: 22rem;
|
||||
margin: var(--s2) 0 0;
|
||||
}
|
||||
|
||||
.preview-podium {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
@@ -223,8 +223,6 @@ describe('RacePreviewPage', () => {
|
||||
expect(screen.getByTestId('preview-header')).toHaveTextContent('Monaco')
|
||||
expect(screen.getByTestId('preview-schedule')).toHaveTextContent('FP1')
|
||||
expect(screen.getByTestId('preview-track-card')).toBeInTheDocument()
|
||||
expect(screen.getByRole('img', { name: 'Circuit outline' })).toBeInTheDocument()
|
||||
expect(screen.queryByTestId('preview-track-fallback')).not.toBeInTheDocument()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('preview-last-year-card')).toHaveTextContent('VER')
|
||||
@@ -275,75 +273,5 @@ describe('RacePreviewPage', () => {
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('preview-last-year-card')).toHaveTextContent('First time on the calendar')
|
||||
})
|
||||
|
||||
const fallback = screen.getByTestId('preview-track-fallback')
|
||||
expect(screen.getByTestId('preview-track-card')).toContainElement(fallback)
|
||||
expect(fallback).toHaveTextContent('Monaco')
|
||||
expect(fallback).toHaveTextContent('A GPS track outline is not available in the local cache.')
|
||||
expect(screen.queryByRole('img', { name: 'Circuit outline' })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('shows a designed circuit fallback when the track outline is unavailable', async () => {
|
||||
const italianGp: Meeting = {
|
||||
...upcomingMeeting,
|
||||
meeting_name: 'Italian Grand Prix',
|
||||
meeting_official_name: 'Formula 1 Pirelli Gran Premio d\'Italia',
|
||||
location: 'Monza',
|
||||
country_name: 'Italy',
|
||||
country_code: 'ITA',
|
||||
circuit_short_name: 'Monza',
|
||||
}
|
||||
|
||||
mockFetchMeetings.mockImplementation(async (year: number) => {
|
||||
if (year === 2099) return [italianGp]
|
||||
if (year === 2098) return [{ ...italianGp, meeting_key: 90, year: 2098 }]
|
||||
return []
|
||||
})
|
||||
mockFetchSessions.mockImplementation(async (meetingKey: number) => {
|
||||
if (meetingKey === 100) return sessions
|
||||
if (meetingKey === 90) return [priorRaceSession]
|
||||
return []
|
||||
})
|
||||
mockFetchTrackOutline.mockResolvedValue(null)
|
||||
|
||||
renderPage()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('preview-track-fallback')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
const card = screen.getByTestId('preview-track-card')
|
||||
const fallback = screen.getByTestId('preview-track-fallback')
|
||||
expect(card).toContainElement(fallback)
|
||||
expect(card).toHaveTextContent('Circuit')
|
||||
expect(fallback).toHaveTextContent('Monza')
|
||||
expect(fallback).toHaveTextContent('Italy')
|
||||
expect(fallback).toHaveTextContent('A GPS track outline is not available in the local cache.')
|
||||
expect(fallback).not.toHaveTextContent(/unknown/i)
|
||||
expect(screen.queryByRole('img', { name: 'Circuit outline' })).not.toBeInTheDocument()
|
||||
expect(card).not.toHaveTextContent('Track outline unavailable for this circuit')
|
||||
})
|
||||
|
||||
it('keeps a thrown outline request distinct from the designed unavailable state', async () => {
|
||||
mockFetchMeetings.mockImplementation(async (year: number) => {
|
||||
if (year === 2099) return [upcomingMeeting]
|
||||
if (year === 2098) return [priorMeeting]
|
||||
return []
|
||||
})
|
||||
mockFetchSessions.mockImplementation(async (meetingKey: number) => {
|
||||
if (meetingKey === 100) return sessions
|
||||
if (meetingKey === 90) return [priorRaceSession]
|
||||
return []
|
||||
})
|
||||
mockFetchTrackOutline.mockRejectedValue(new Error('outline request failed'))
|
||||
|
||||
renderPage()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('preview-track-card')).toHaveTextContent('outline request failed')
|
||||
})
|
||||
|
||||
expect(screen.queryByTestId('preview-track-fallback')).not.toBeInTheDocument()
|
||||
expect(screen.queryByRole('img', { name: 'Circuit outline' })).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
@@ -72,7 +72,7 @@ func Detect(rc []RaceControl, positions []PositionSample, laps []Lap, totalLaps
|
||||
chapters = append(chapters, detectDecisiveSwings(positions, lapIndex, totalLaps)...)
|
||||
chapters = append(chapters, detectFinish(rc, lapIndex, totalLaps))
|
||||
|
||||
return resolveConflicts(chapters)
|
||||
return resolveConflicts(chapters, lapIndex)
|
||||
}
|
||||
|
||||
func normalizeTotalLaps(totalLaps int, laps []Lap, rc []RaceControl) int {
|
||||
@@ -321,7 +321,7 @@ func detectPitPhases(laps []Lap, idx lapIndex) []Chapter {
|
||||
}
|
||||
ch := Chapter{
|
||||
Kind: KindPitPhase,
|
||||
Title: fmt.Sprintf("Pit phase (L%d-L%d)", start, end),
|
||||
Title: pitPhaseTitle(start, end),
|
||||
StartLap: start,
|
||||
EndLap: end,
|
||||
StartTime: idx.lapStart(start),
|
||||
@@ -339,7 +339,7 @@ func detectPitPhases(laps []Lap, idx lapIndex) []Chapter {
|
||||
drivers[driver] = true
|
||||
}
|
||||
last.DriverNumbers = sortedDriverNumbers(drivers)
|
||||
last.Title = fmt.Sprintf("Pit phase (L%d-L%d)", last.StartLap, last.EndLap)
|
||||
last.Title = pitPhaseTitle(last.StartLap, last.EndLap)
|
||||
continue
|
||||
}
|
||||
windows = append(windows, ch)
|
||||
@@ -479,7 +479,7 @@ func detectFinish(rc []RaceControl, idx lapIndex, totalLaps int) Chapter {
|
||||
}
|
||||
return Chapter{
|
||||
Kind: KindFinish,
|
||||
Title: fmt.Sprintf("Finish (L%d-L%d)", startLap, finishLap),
|
||||
Title: finishTitle(startLap, finishLap),
|
||||
StartLap: startLap,
|
||||
EndLap: finishLap,
|
||||
StartTime: idx.lapStart(startLap),
|
||||
@@ -487,7 +487,31 @@ func detectFinish(rc []RaceControl, idx lapIndex, totalLaps int) Chapter {
|
||||
}
|
||||
}
|
||||
|
||||
func resolveConflicts(chapters []Chapter) []Chapter {
|
||||
func pitPhaseTitle(startLap, endLap int) string {
|
||||
return fmt.Sprintf("Pit phase (L%d-L%d)", startLap, endLap)
|
||||
}
|
||||
|
||||
func finishTitle(startLap, endLap int) string {
|
||||
return fmt.Sprintf("Finish (L%d-L%d)", startLap, endLap)
|
||||
}
|
||||
|
||||
// titleForResolved rebuilds range-bearing titles from the final StartLap/EndLap
|
||||
// after conflict resolution. Flag and pit-phase formatters are reused so stale
|
||||
// pre-trim strings are never parsed.
|
||||
func titleForResolved(ch Chapter) string {
|
||||
switch ch.Kind {
|
||||
case KindSafetyCar, KindVirtualSafetyCar, KindRedFlag:
|
||||
return flagTitle(ch.Kind, ch.StartLap, ch.EndLap)
|
||||
case KindPitPhase:
|
||||
return pitPhaseTitle(ch.StartLap, ch.EndLap)
|
||||
case KindFinish:
|
||||
return finishTitle(ch.StartLap, ch.EndLap)
|
||||
default:
|
||||
return ch.Title
|
||||
}
|
||||
}
|
||||
|
||||
func resolveConflicts(chapters []Chapter, idx lapIndex) []Chapter {
|
||||
normalized := make([]Chapter, 0, len(chapters))
|
||||
for _, ch := range chapters {
|
||||
if ch.StartLap <= 0 {
|
||||
@@ -528,6 +552,7 @@ func resolveConflicts(chapters []Chapter) []Chapter {
|
||||
if priority(ch.Kind) > priority(last.Kind) {
|
||||
if last.StartLap < ch.StartLap {
|
||||
last.EndLap = ch.StartLap - 1
|
||||
last.EndTime = idx.lapEnd(last.EndLap)
|
||||
out = append(out, ch)
|
||||
} else {
|
||||
*last = ch
|
||||
@@ -541,6 +566,9 @@ func resolveConflicts(chapters []Chapter) []Chapter {
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := range out {
|
||||
out[i].Title = titleForResolved(out[i])
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
|
||||
@@ -125,6 +125,42 @@ func TestDetectDecisiveSwingPersistsToFinish(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectSafetyCarTrimmedForFinishRewritesTitle(t *testing.T) {
|
||||
rcs := []RaceControl{
|
||||
rc(3, models.CategorySafetyCar, "", "SAFETY CAR DEPLOYED"),
|
||||
rc(72, models.CategoryFlag, models.FlagChequered, "CHEQUERED FLAG"),
|
||||
}
|
||||
|
||||
got := Detect(rcs, nil, testLaps(72, nil), 72)
|
||||
|
||||
sc := findKind(got, KindSafetyCar)
|
||||
if sc == nil {
|
||||
t.Fatalf("chapters = %+v, want safety car", got)
|
||||
}
|
||||
if sc.StartLap != 3 || sc.EndLap != 70 || sc.Title != "Safety Car (L3-L70)" {
|
||||
t.Fatalf("safety car = %+v, want StartLap=3 EndLap=70 Title=%q", *sc, "Safety Car (L3-L70)")
|
||||
}
|
||||
if wantEnd := lapTime(71); sc.EndTime != wantEnd {
|
||||
t.Fatalf("safety car EndTime = %q, want lap-index end of L70 (%q)", sc.EndTime, wantEnd)
|
||||
}
|
||||
|
||||
fin := findKind(got, KindFinish)
|
||||
if fin == nil || fin.StartLap != 71 || fin.EndLap != 72 {
|
||||
t.Fatalf("finish = %+v, want L71-L72", got)
|
||||
}
|
||||
|
||||
for i := 1; i < len(got); i++ {
|
||||
if got[i].StartLap <= got[i-1].EndLap {
|
||||
t.Fatalf("chapters overlap at %d: %+v then %+v", i, got[i-1], got[i])
|
||||
}
|
||||
}
|
||||
|
||||
headed := ApplyHeadlines(got, nil, rcs, 0)
|
||||
if scH := findKind(headed, KindSafetyCar); scH == nil || scH.Headline != "Safety Car (L3-L70)" {
|
||||
t.Fatalf("headline = %+v, want fallback to resolved title Safety Car (L3-L70)", headed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectFlagPeriodsWinOverConflictingChapters(t *testing.T) {
|
||||
pitOuts := map[int][]int{
|
||||
12: {1, 2},
|
||||
|
||||
Reference in new issue
Block a user