Skip to content

Commit

Permalink
fix: show only active closures on elevator screens
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalcora committed Jan 16, 2025
1 parent 7f1b144 commit 52d5d54
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 57 deletions.
4 changes: 3 additions & 1 deletion lib/screens/v2/candidate_generator/elevator/closures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ defmodule Screens.V2.CandidateGenerator.Elevator.Closures do
footer_instance
) do
{:ok, alerts} = @alert.fetch_elevator_alerts_with_facilities()
closures = Enum.flat_map(alerts, &elevator_closure/1)

closures =
alerts |> Enum.filter(&Alert.happening_now?/1) |> Enum.flat_map(&elevator_closure/1)

case Enum.find(closures, fn %Closure{id: id} -> id == elevator_id end) do
nil ->
Expand Down
90 changes: 34 additions & 56 deletions test/screens/v2/candidate_generator/elevator/closures_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ defmodule Screens.V2.CandidateGenerator.Elevator.ClosuresTest do
:ok
end

defp build_alert(fields) do
struct!(%Alert{active_period: [{DateTime.utc_now(), nil}], effect: :elevator_closure}, fields)
end

describe "elevator_status_instances/3" do
setup do
config = %ElevatorConfig{
Expand All @@ -61,7 +65,7 @@ defmodule Screens.V2.CandidateGenerator.Elevator.ClosuresTest do
}
end

test "Only returns alerts with effect of :elevator_closure", %{
test "Only returns currently-active alerts with effect of :elevator_closure", %{
config: config,
header_instance: header_instance,
footer_instance: footer_instance
Expand All @@ -75,19 +79,29 @@ defmodule Screens.V2.CandidateGenerator.Elevator.ClosuresTest do
end)

expect(@alert, :fetch_elevator_alerts_with_facilities, fn ->
now = DateTime.utc_now()
active_period = {DateTime.add(now, -1, :day), DateTime.add(now, 1, :day)}
upcoming_period = {DateTime.add(now, 1, :day), DateTime.add(now, 3, :day)}

alerts = [
struct(Alert,
id: "1",
effect: :elevator_closure,
build_alert(
active_period: [active_period],
informed_entities: [
%{stop: "place-test", facility: %{name: "Test", id: "facility-test"}}
]
),
struct(Alert,
effect: :detour,
build_alert(
active_period: [upcoming_period],
informed_entities: [
%{stop: "place-test", facility: %{name: "Test 2", id: "facility-test2"}}
]
),
build_alert(
effect: :detour,
active_period: [active_period],
informed_entities: [
%{stop: "place-test", facility: %{name: "Test 3", id: "facility-test3"}}
]
)
]

Expand Down Expand Up @@ -137,16 +151,12 @@ defmodule Screens.V2.CandidateGenerator.Elevator.ClosuresTest do

expect(@alert, :fetch_elevator_alerts_with_facilities, fn ->
alerts = [
struct(Alert,
id: "1",
effect: :elevator_closure,
build_alert(
informed_entities: [
%{stop: "place-haecl", facility: %{name: "Test 1", id: "facility-test-1"}}
]
),
struct(Alert,
id: "2",
effect: :elevator_closure,
build_alert(
informed_entities: [
%{stop: "place-haecl", facility: %{name: "Test 2", id: "facility-test-2"}}
]
Expand Down Expand Up @@ -195,14 +205,8 @@ defmodule Screens.V2.CandidateGenerator.Elevator.ClosuresTest do

expect(@alert, :fetch_elevator_alerts_with_facilities, fn ->
alerts = [
struct(Alert,
id: "1",
effect: :elevator_closure,
informed_entities: [%{stop: "place-haecl", facility: nil}]
),
struct(Alert,
id: "2",
effect: :elevator_closure,
build_alert(informed_entities: [%{stop: "place-haecl", facility: nil}]),
build_alert(
informed_entities: [
%{stop: "place-haecl", facility: %{name: "Test 2", id: "facility-test-2"}},
%{stop: "place-haecl", facility: %{name: "Test 2", id: "facility-test-3"}}
Expand Down Expand Up @@ -257,26 +261,20 @@ defmodule Screens.V2.CandidateGenerator.Elevator.ClosuresTest do

expect(@alert, :fetch_elevator_alerts_with_facilities, fn ->
alerts = [
struct(Alert,
id: "1",
effect: :elevator_closure,
build_alert(
informed_entities: [
%{stop: "place-test", facility: %{name: "In Station Elevator", id: "112"}}
]
),
struct(Alert,
id: "2",
effect: :elevator_closure,
build_alert(
informed_entities: [
%{
stop: "place-test-redundancy",
facility: %{name: "Other With Redundancy", id: "222"}
}
]
),
struct(Alert,
id: "3",
effect: :elevator_closure,
build_alert(
informed_entities: [
%{
stop: "place-test-no-redundancy",
Expand Down Expand Up @@ -351,32 +349,24 @@ defmodule Screens.V2.CandidateGenerator.Elevator.ClosuresTest do

expect(@alert, :fetch_elevator_alerts_with_facilities, fn ->
alerts = [
struct(Alert,
id: "1",
effect: :elevator_closure,
build_alert(
informed_entities: [
%{stop: "place-1", facility: %{name: "backup in station", id: "1"}}
]
),
struct(Alert,
id: "2",
effect: :elevator_closure,
build_alert(
informed_entities: [
%{stop: "place-2", facility: %{name: "custom backup summary", id: "2"}}
]
),
struct(Alert,
id: "3",
effect: :elevator_closure,
build_alert(
informed_entities: [
# despite having "nearby" redundancy, should not be filtered out, because its
# alternate elevator is also down
%{stop: "place-3", facility: %{name: "alternate elevator down", id: "3"}}
]
),
struct(Alert,
id: "alt",
effect: :elevator_closure,
build_alert(
informed_entities: [
# somewhat unrealistically, place elevator 3's "nearby" alternate at a different
# station, so they aren't combined
Expand Down Expand Up @@ -425,18 +415,8 @@ defmodule Screens.V2.CandidateGenerator.Elevator.ClosuresTest do
} do
expect(@alert, :fetch_elevator_alerts_with_facilities, fn ->
alerts = [
struct(Alert,
id: "1",
effect: :elevator_closure,
informed_entities: [
%{stop: "place-test", facility: %{name: "Test", id: "111"}}
]
),
struct(Alert,
effect: :detour,
informed_entities: [
%{stop: "place-test", facility: %{name: "Test 2", id: "facility-test2"}}
]
build_alert(
informed_entities: [%{stop: "place-test", facility: %{name: "Test", id: "111"}}]
)
]

Expand Down Expand Up @@ -473,9 +453,7 @@ defmodule Screens.V2.CandidateGenerator.Elevator.ClosuresTest do

expect(@alert, :fetch_elevator_alerts_with_facilities, fn ->
alerts = [
struct(Alert,
id: "1",
effect: :elevator_closure,
build_alert(
informed_entities: [
%{stop: "place-test", facility: %{name: "Test", id: "facility-test"}}
]
Expand Down

0 comments on commit 52d5d54

Please sign in to comment.