mirror of
https://github.com/AmanTahiliani/box-box.git
synced 2026-08-07 19:56:18 -04:00
feat(#98): Keep completed analysis as the Race Hub default until the next Grand Prix is imminent
Implemented by opencode via .agents/dev dispatch.
This commit is contained in:
@@ -69,6 +69,9 @@ type WeekendContext struct {
|
||||
ActiveSession *ContextSession `json:"active_session,omitempty"`
|
||||
NextSession *ContextSession `json:"next_session,omitempty"`
|
||||
DefaultAnalysisSession *ContextSession `json:"default_analysis_session,omitempty"`
|
||||
RaceHubDefaultSession *ContextSession `json:"race_hub_default_session,omitempty"`
|
||||
RaceHubPreSession bool `json:"race_hub_pre_session"`
|
||||
RaceHubRefreshAt string `json:"race_hub_refresh_at,omitempty"`
|
||||
ChampionshipRound int `json:"championship_round"`
|
||||
TotalChampionshipRounds int `json:"total_championship_rounds"`
|
||||
}
|
||||
@@ -188,9 +191,34 @@ func (s *Service) ResolveWeekendContext(evidence LiveEvidence) (WeekendContext,
|
||||
if out.FocusMeeting != nil {
|
||||
out.ChampionshipRound = championshipRound(champMeetings, int(out.FocusMeeting.MeetingKey))
|
||||
}
|
||||
applyRaceHubDefault(&out, active, defaultAnalysis, next, now)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// applyRaceHubDefault is deliberately distinct from TemporalPreSession. Other
|
||||
// weekend surfaces begin preparation 48 hours ahead; Race Hub remains an
|
||||
// analysis destination until the one-hour handoff before the next session.
|
||||
func applyRaceHubDefault(out *WeekendContext, active, analysis, next *contextCandidate, now time.Time) {
|
||||
if active != nil {
|
||||
out.RaceHubDefaultSession = out.ActiveSession
|
||||
return
|
||||
}
|
||||
if next != nil {
|
||||
handoff := next.start.Add(-time.Hour)
|
||||
if now.Before(handoff) {
|
||||
out.RaceHubRefreshAt = handoff.Format(time.RFC3339)
|
||||
} else if now.Before(next.start) {
|
||||
out.RaceHubDefaultSession = out.NextSession
|
||||
out.RaceHubPreSession = true
|
||||
out.RaceHubRefreshAt = next.start.Format(time.RFC3339)
|
||||
return
|
||||
}
|
||||
}
|
||||
if analysis != nil {
|
||||
out.RaceHubDefaultSession = out.DefaultAnalysisSession
|
||||
}
|
||||
}
|
||||
|
||||
func currentLocalSeason(years []int, current int) int {
|
||||
for _, year := range years {
|
||||
if year == current {
|
||||
|
||||
@@ -399,3 +399,72 @@ func TestResolveWeekendContextMissingScheduleDoesNotClaimSeasonComplete(t *testi
|
||||
t.Fatalf("total rounds = %d, want scheduled round retained", got.TotalChampionshipRounds)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWeekendContextRaceHubDefault(t *testing.T) {
|
||||
seed := func(t *testing.T, svc *Service) {
|
||||
addContextMeeting(t, svc, 1, "British Grand Prix", "2026-07-03T09:00:00Z", "2026-07-05T16:00:00Z", false)
|
||||
addContextSession(t, svc, 11, 1, "Race", "2026-07-05T14:00:00Z", "2026-07-05T16:00:00Z", false)
|
||||
completeContextSession(t, svc, 11, 1)
|
||||
addContextMeeting(t, svc, 2, "Belgian Grand Prix", "2026-07-17T09:00:00Z", "2026-07-19T16:00:00Z", false)
|
||||
addContextSession(t, svc, 21, 2, "Practice 1", "2026-07-17T09:00:00Z", "2026-07-17T10:00:00Z", false)
|
||||
}
|
||||
|
||||
t.Run("keeps completed analysis before handoff", func(t *testing.T) {
|
||||
now, _ := time.Parse(time.RFC3339, "2026-07-16T07:59:59Z")
|
||||
svc := contextService(t, now)
|
||||
seed(t, svc)
|
||||
got, err := svc.ResolveWeekendContext(LiveEvidence{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.RaceHubDefaultSession == nil || got.RaceHubDefaultSession.Session.SessionKey != 11 || got.RaceHubPreSession {
|
||||
t.Fatalf("race hub default = %+v, pre-session = %t", got.RaceHubDefaultSession, got.RaceHubPreSession)
|
||||
}
|
||||
if got.RaceHubRefreshAt != "2026-07-17T08:00:00Z" {
|
||||
t.Fatalf("refresh = %q", got.RaceHubRefreshAt)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("hands off exactly one hour before first session", func(t *testing.T) {
|
||||
now, _ := time.Parse(time.RFC3339, "2026-07-17T08:00:00Z")
|
||||
svc := contextService(t, now)
|
||||
seed(t, svc)
|
||||
got, err := svc.ResolveWeekendContext(LiveEvidence{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.RaceHubDefaultSession == nil || got.RaceHubDefaultSession.Session.SessionKey != 21 || !got.RaceHubPreSession {
|
||||
t.Fatalf("race hub handoff = %+v, pre-session = %t", got.RaceHubDefaultSession, got.RaceHubPreSession)
|
||||
}
|
||||
if got.RaceHubRefreshAt != "2026-07-17T09:00:00Z" {
|
||||
t.Fatalf("refresh = %q", got.RaceHubRefreshAt)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("active live session wins", func(t *testing.T) {
|
||||
now, _ := time.Parse(time.RFC3339, "2026-07-17T08:30:00Z")
|
||||
svc := contextService(t, now)
|
||||
seed(t, svc)
|
||||
got, err := svc.ResolveWeekendContext(LiveEvidence{Active: true, MeetingName: "Belgian Grand Prix", SessionName: "Practice 1", SessionType: "Practice 1"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.RaceHubDefaultSession == nil || got.RaceHubDefaultSession.Session.SessionKey != 21 || got.RaceHubPreSession {
|
||||
t.Fatalf("live default = %+v, pre-session = %t", got.RaceHubDefaultSession, got.RaceHubPreSession)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("does not select an empty future session before handoff", func(t *testing.T) {
|
||||
now, _ := time.Parse(time.RFC3339, "2026-07-16T12:00:00Z")
|
||||
svc := contextService(t, now)
|
||||
addContextMeeting(t, svc, 2, "Belgian Grand Prix", "2026-07-17T09:00:00Z", "2026-07-19T16:00:00Z", false)
|
||||
addContextSession(t, svc, 21, 2, "Practice 1", "2026-07-17T09:00:00Z", "2026-07-17T10:00:00Z", false)
|
||||
got, err := svc.ResolveWeekendContext(LiveEvidence{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.RaceHubDefaultSession != nil || got.RaceHubPreSession {
|
||||
t.Fatalf("unexpected empty future default: %+v", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -167,3 +167,30 @@ func TestTerminalSessionStatus(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWeekendContextHandlerSerializesRaceHubRefreshDeadline(t *testing.T) {
|
||||
st := openContextStore(t)
|
||||
seedContextHandler(t, st)
|
||||
if err := st.UpsertSessionResult(store.SessionResult{SessionKey: 11, MeetingKey: 1, DriverNumber: 1, Position: 1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := st.UpsertMeeting(store.Meeting{MeetingKey: 2, MeetingName: "Belgian Grand Prix", CircuitShortName: "Spa", Year: 2026, DateStart: "2026-07-17T09:00:00Z", DateEnd: "2026-07-19T16:00:00Z"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := st.UpsertSession(store.Session{SessionKey: 21, MeetingKey: 2, SessionName: "Practice 1", SessionType: "Practice", DateStart: "2026-07-17T09:00:00Z", DateEnd: "2026-07-17T10:00:00Z"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := NewServer(nil, 0, st)
|
||||
s.query = query.NewServiceWithClock(st, func() time.Time {
|
||||
return time.Date(2026, 7, 17, 8, 0, 0, 0, time.UTC)
|
||||
})
|
||||
rr := httptest.NewRecorder()
|
||||
s.handleWeekendContext(rr, httptest.NewRequest(http.MethodGet, "/api/v1/weekend-context", nil))
|
||||
var got query.WeekendContext
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !got.RaceHubPreSession || got.RaceHubRefreshAt != "2026-07-17T09:00:00Z" || got.RaceHubDefaultSession == nil || got.RaceHubDefaultSession.Session.SessionKey != 21 {
|
||||
t.Fatalf("race hub context = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user