-
Notifications
You must be signed in to change notification settings - Fork 0
refactor(schedule): extract shared pure schedule filter #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+395
−93
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,302 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { filterScheduleDays, type ScheduleFilterCriteria } from "./scheduleFilter"; | ||
| import type { ScheduleDay, ScheduleSet } from "@/hooks/useScheduleData"; | ||
|
|
||
| const TIMEZONE = "Europe/Lisbon"; | ||
|
|
||
| describe("filterScheduleDays", () => { | ||
| describe("day predicate", () => { | ||
| it("keeps all days when day is 'all'", () => { | ||
| const days = [ | ||
| makeDay({ date: "2024-07-15" }), | ||
| makeDay({ date: "2024-07-16" }), | ||
| ]; | ||
|
|
||
| const result = filterScheduleDays(days, baseCriteria(), TIMEZONE); | ||
|
|
||
| expect(result.map((day) => day.date)).toEqual([ | ||
| "2024-07-15", | ||
| "2024-07-16", | ||
| ]); | ||
| expect(result[0].stages).toHaveLength(1); | ||
| expect(result[1].stages).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("keeps the matching day's stages and empties non-matching days", () => { | ||
| const days = [ | ||
| makeDay({ date: "2024-07-15" }), | ||
| makeDay({ date: "2024-07-16" }), | ||
| ]; | ||
|
|
||
| const result = filterScheduleDays( | ||
| days, | ||
| baseCriteria({ day: "2024-07-15" }), | ||
| TIMEZONE, | ||
| ); | ||
|
|
||
| expect(result).toHaveLength(2); | ||
| expect(result[0].date).toBe("2024-07-15"); | ||
| expect(result[0].stages).toHaveLength(1); | ||
| expect(result[1].date).toBe("2024-07-16"); | ||
| expect(result[1].stages).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("time-of-day predicate (festival timezone buckets)", () => { | ||
| it("keeps sets in the morning bucket (6-12 festival hour)", () => { | ||
| // 10:00 UTC is 11:00 in Lisbon (WEST, UTC+1 in July) - morning. | ||
| const days = [ | ||
| makeDay({ | ||
| stages: [ | ||
| { | ||
| id: "stage-1", | ||
| name: "Main Stage", | ||
| stage_order: 1, | ||
| sets: [makeSet({ startTime: new Date("2024-07-15T10:00:00Z") })], | ||
| }, | ||
| ], | ||
| }), | ||
| ]; | ||
|
|
||
| const result = filterScheduleDays( | ||
| days, | ||
| baseCriteria({ time: "morning" }), | ||
| TIMEZONE, | ||
| ); | ||
|
|
||
| expect(result[0].stages[0].sets).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("drops sets outside the requested bucket", () => { | ||
| // 22:00 UTC is 23:00 in Lisbon in July - evening, not morning. | ||
| const days = [ | ||
| makeDay({ | ||
| stages: [ | ||
| { | ||
| id: "stage-1", | ||
| name: "Main Stage", | ||
| stage_order: 1, | ||
| sets: [makeSet({ startTime: new Date("2024-07-15T22:00:00Z") })], | ||
| }, | ||
| ], | ||
| }), | ||
| ]; | ||
|
|
||
| const result = filterScheduleDays( | ||
| days, | ||
| baseCriteria({ time: "morning" }), | ||
| TIMEZONE, | ||
| ); | ||
|
|
||
| expect(result[0].stages[0].sets).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("computes buckets in the festival timezone, not UTC", () => { | ||
| // 23:30 UTC on Jul 15 is 00:30 on Jul 16 in Lisbon (WEST, UTC+1) - morning-adjacent | ||
| // "night" hour 0, which is neither morning, afternoon nor evening. | ||
| const days = [ | ||
| makeDay({ | ||
| stages: [ | ||
| { | ||
| id: "stage-1", | ||
| name: "Main Stage", | ||
| stage_order: 1, | ||
| sets: [makeSet({ startTime: new Date("2024-07-15T23:30:00Z") })], | ||
| }, | ||
| ], | ||
| }), | ||
| ]; | ||
|
|
||
| const morning = filterScheduleDays( | ||
| days, | ||
| baseCriteria({ time: "morning" }), | ||
| TIMEZONE, | ||
| ); | ||
| const evening = filterScheduleDays( | ||
| days, | ||
| baseCriteria({ time: "evening" }), | ||
| TIMEZONE, | ||
| ); | ||
|
|
||
| expect(morning[0].stages[0].sets).toHaveLength(0); | ||
| expect(evening[0].stages[0].sets).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("passes sets without a startTime through unfiltered", () => { | ||
| const days = [ | ||
| makeDay({ | ||
| stages: [ | ||
| { | ||
| id: "stage-1", | ||
| name: "Main Stage", | ||
| stage_order: 1, | ||
| sets: [makeSet({ startTime: undefined })], | ||
| }, | ||
| ], | ||
| }), | ||
| ]; | ||
|
|
||
| const result = filterScheduleDays( | ||
| days, | ||
| baseCriteria({ time: "evening" }), | ||
| TIMEZONE, | ||
| ); | ||
|
|
||
| expect(result[0].stages[0].sets).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("keeps all sets when time is 'all'", () => { | ||
| const days = [makeDay()]; | ||
|
|
||
| const result = filterScheduleDays(days, baseCriteria(), TIMEZONE); | ||
|
|
||
| expect(result[0].stages[0].sets).toHaveLength(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe("stage predicate", () => { | ||
| it("keeps all stages when the selection is empty", () => { | ||
| const days = [ | ||
| makeDay({ | ||
| stages: [ | ||
| { id: "stage-1", name: "Main Stage", stage_order: 1, sets: [] }, | ||
| { id: "stage-2", name: "Second Stage", stage_order: 2, sets: [] }, | ||
| ], | ||
| }), | ||
| ]; | ||
|
|
||
| const result = filterScheduleDays(days, baseCriteria(), TIMEZONE); | ||
|
|
||
| expect(result[0].stages.map((s) => s.id)).toEqual([ | ||
| "stage-1", | ||
| "stage-2", | ||
| ]); | ||
| }); | ||
|
|
||
| it("matches stages by id, dropping unselected stages", () => { | ||
| const days = [ | ||
| makeDay({ | ||
| stages: [ | ||
| { id: "stage-1", name: "Main Stage", stage_order: 1, sets: [] }, | ||
| { id: "stage-2", name: "Second Stage", stage_order: 2, sets: [] }, | ||
| ], | ||
| }), | ||
| ]; | ||
|
|
||
| const result = filterScheduleDays( | ||
| days, | ||
| baseCriteria({ stages: ["stage-2"] }), | ||
| TIMEZONE, | ||
| ); | ||
|
|
||
| expect(result[0].stages.map((s) => s.id)).toEqual(["stage-2"]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("combinations", () => { | ||
| it("applies day, time and stage predicates together", () => { | ||
| const days = [ | ||
| makeDay({ | ||
| date: "2024-07-15", | ||
| stages: [ | ||
| { | ||
| id: "stage-1", | ||
| name: "Main Stage", | ||
| stage_order: 1, | ||
| sets: [ | ||
| makeSet({ | ||
| id: "morning-set", | ||
| startTime: new Date("2024-07-15T10:00:00Z"), | ||
| }), | ||
| makeSet({ | ||
| id: "evening-set", | ||
| startTime: new Date("2024-07-15T22:00:00Z"), | ||
| }), | ||
| ], | ||
| }, | ||
| { | ||
| id: "stage-2", | ||
| name: "Second Stage", | ||
| stage_order: 2, | ||
| sets: [ | ||
| makeSet({ | ||
| id: "other-stage-morning-set", | ||
| startTime: new Date("2024-07-15T10:00:00Z"), | ||
| }), | ||
| ], | ||
| }, | ||
| ], | ||
| }), | ||
| makeDay({ date: "2024-07-16" }), | ||
| ]; | ||
|
|
||
| const result = filterScheduleDays( | ||
| days, | ||
| baseCriteria({ | ||
| day: "2024-07-15", | ||
| time: "morning", | ||
| stages: ["stage-1"], | ||
| }), | ||
| TIMEZONE, | ||
| ); | ||
|
|
||
| expect(result).toHaveLength(2); | ||
| expect(result[0].stages).toHaveLength(1); | ||
| expect(result[0].stages[0].id).toBe("stage-1"); | ||
| expect(result[0].stages[0].sets.map((s) => s.id)).toEqual([ | ||
| "morning-set", | ||
| ]); | ||
| expect(result[1].stages).toEqual([]); | ||
| }); | ||
|
|
||
| it("does not mutate the input", () => { | ||
| const days = [makeDay()]; | ||
| const snapshot = JSON.parse(JSON.stringify(days)); | ||
|
|
||
| filterScheduleDays( | ||
| days, | ||
| baseCriteria({ day: "2024-07-16", time: "morning", stages: ["x"] }), | ||
| TIMEZONE, | ||
| ); | ||
|
|
||
| expect(JSON.parse(JSON.stringify(days))).toEqual(snapshot); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| function makeSet(overrides: Partial<ScheduleSet> = {}): ScheduleSet { | ||
| return { | ||
| id: "set-1", | ||
| name: "A set", | ||
| artists: [], | ||
| startTime: new Date("2024-07-15T10:00:00Z"), | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function makeDay(overrides: Partial<ScheduleDay> = {}): ScheduleDay { | ||
| return { | ||
| date: "2024-07-15", | ||
| displayDate: "Monday, Jul 15", | ||
| stages: [ | ||
| { | ||
| id: "stage-1", | ||
| name: "Main Stage", | ||
| stage_order: 1, | ||
| sets: [makeSet()], | ||
| }, | ||
| ], | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function baseCriteria( | ||
| overrides: Partial<ScheduleFilterCriteria> = {}, | ||
| ): ScheduleFilterCriteria { | ||
| return { | ||
| day: "all", | ||
| time: "all", | ||
| stages: [], | ||
| ...overrides, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { getFestivalHour } from "@/lib/timeUtils"; | ||
| import type { TimelineSearch } from "@/lib/searchSchemas"; | ||
| import type { | ||
| ScheduleDay, | ||
| ScheduleSet, | ||
| ScheduleStage, | ||
| } from "@/hooks/useScheduleData"; | ||
|
|
||
| export type ScheduleTimeFilter = TimelineSearch["time"]; | ||
|
|
||
| export interface ScheduleFilterCriteria { | ||
| // "all" or a festival day key (yyyy-MM-dd, see getFestivalDayKey). | ||
| day: string; | ||
| // Time-of-day bucket, computed in the festival timezone via getFestivalHour. | ||
| time: ScheduleTimeFilter; | ||
| // Stage ids to include; an empty array means all stages. | ||
| stages: string[]; | ||
| // Future (PRD #188 - vote-type filtering): an optional field such as | ||
| // `voteTypes?: VoteType[]`, OR-ed against the viewer's own votes on each | ||
| // set, will slot in here without reshaping this interface. | ||
| } | ||
|
|
||
| function matchesTimeOfDay( | ||
| set: ScheduleSet, | ||
| time: ScheduleTimeFilter, | ||
| timezone: string, | ||
| ): boolean { | ||
| if (time === "all" || !set.startTime) return true; | ||
|
|
||
| const hour = getFestivalHour(set.startTime.toISOString(), timezone); | ||
| if (hour === null) return false; | ||
|
|
||
| switch (time) { | ||
| case "morning": | ||
| return hour >= 6 && hour < 12; | ||
| case "afternoon": | ||
| return hour >= 12 && hour < 18; | ||
| case "evening": | ||
| return hour >= 18 && hour < 24; | ||
| default: | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| // Applies the day / time-of-day / stage predicates shared by both Schedule | ||
| // views (Timeline and List). A day that doesn't match `criteria.day` is kept | ||
| // as an entry with empty stages (rather than dropped) purely to mirror the | ||
| // original inline Timeline logic; the strip's axis bounds derive from the | ||
| // remaining sets' times, so an empty day entry contributes nothing and | ||
| // dropping it would render identically. | ||
| export function filterScheduleDays( | ||
| scheduleDays: ScheduleDay[], | ||
| criteria: ScheduleFilterCriteria, | ||
| timezone: string, | ||
| ): ScheduleDay[] { | ||
| return scheduleDays.map((day) => { | ||
| if (criteria.day !== "all" && day.date !== criteria.day) { | ||
| return { ...day, stages: [] }; | ||
| } | ||
|
|
||
| const filteredStages: ScheduleStage[] = day.stages | ||
| .filter( | ||
| (stage) => | ||
| criteria.stages.length === 0 || criteria.stages.includes(stage.id), | ||
| ) | ||
| .map((stage) => ({ | ||
| ...stage, | ||
| sets: stage.sets.filter((set) => | ||
| matchesTimeOfDay(set, criteria.time, timezone), | ||
| ), | ||
| })); | ||
|
|
||
| return { ...day, stages: filteredStages }; | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.