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
14 changes: 12 additions & 2 deletions lib/dotcom_web/components/system_status/subway_status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ defmodule DotcomWeb.Components.SystemStatus.SubwayStatus do

defp combine_rows(
%{route_info: %{branch_ids: branch_ids1}} = row1,
%{route_info: %{branch_ids: branch_ids2}} = _row2
%{route_info: %{branch_ids: branch_ids2}} = row2
) do
combined_branch_ids =
if Enum.empty?(branch_ids1) || Enum.empty?(branch_ids2) do
Expand All @@ -186,10 +186,20 @@ defmodule DotcomWeb.Components.SystemStatus.SubwayStatus do
end

row1
|> Map.put(:status_entry, see_alerts_status())
|> Map.put(:status_entry, combine_status_entries(row1.status_entry, row2.status_entry))
|> Map.put(:alerts, row1.alerts ++ row2.alerts)
|> put_in([:route_info, :branch_ids], combined_branch_ids)
end

defp combine_status_entries(
%{status: status1, prefix: prefix1, future: future1},
%{status: status2, prefix: prefix2, future: future2}
)
when status1 == status2 and prefix1 == prefix2 and future1 == future2,
do: %{status: status1, prefix: prefix1, plural: true, future: future1}

defp combine_status_entries(_status_entry1, _status_entry2), do: see_alerts_status()

defp add_url(row) do
route_id = route_id_from_route_info(row.route_info)
sub_page = if normal?(row.status_entry), do: "line", else: "alerts"
Expand Down
206 changes: 200 additions & 6 deletions test/dotcom_web/components/system_status/subway_status_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ defmodule DotcomWeb.Components.SystemStatus.SubwayStatusTest do
|> Enum.map(fn branch_id ->
Factories.Alerts.Alert.build(:alert_for_route,
route_id: branch_id,
effect: Faker.Util.pick(service_impacting_effects())
effect:
Faker.Util.pick(service_impacting_effects())
|> then(fn {effect, _severity} -> effect end)
)
|> Factories.Alerts.Alert.active_now()
end)
Expand All @@ -154,9 +156,152 @@ defmodule DotcomWeb.Components.SystemStatus.SubwayStatusTest do
rows = status_rows_for_alerts(alerts)

# Verify
assert rows
|> for_route("Green")
|> Enum.map(&status_label_text_for_row/1) == ["See Alerts", "Normal Service"]
assert [affected_row, normal_row] = rows |> for_route("Green")

assert status_label_text_for_row(normal_row) == "Normal Service"
refute status_label_text_for_row(affected_row) == "Normal Service"
end

test "combines collapsed Green line alerts into 'See Alerts' if their effects are different" do
# Setup
affected_branches =
Faker.Util.sample_uniq(2, fn -> Faker.Util.pick(GreenLine.branch_ids()) end)

effects =
Faker.Util.sample_uniq(2, fn ->
Faker.Util.pick(service_impacting_effects())
|> then(fn {effect, _severity} -> effect end)
end)

alerts =
Enum.zip(affected_branches, effects)
|> Enum.map(fn {branch_id, effect} ->
Factories.Alerts.Alert.build(:alert_for_route,
route_id: branch_id,
effect: effect
)
|> Factories.Alerts.Alert.active_now()
end)

# Exercise
rows = status_rows_for_alerts(alerts)

# Verify
assert [affected_row, _normal_row] = rows |> for_route("Green")

assert status_label_text_for_row(affected_row) == "See Alerts"
end

test "shows effect label for collapsed Green line alerts if they all have the same effect" do
# Setup
affected_branches =
Faker.Util.sample_uniq(2, fn -> Faker.Util.pick(GreenLine.branch_ids()) end)

# Excluding `:station_closure` from this test because it follows
# different pluralizing rules than the other effects, we do care
# that other statuses are pluralized properly, and we're testing
# the `:station_closure` case below anyway.
{effect, _severity} =
Faker.Util.pick(@effects_with_simple_descriptions -- [station_closure: 1])

alerts =
affected_branches
|> Enum.map(fn branch_id ->
Factories.Alerts.Alert.build(:alert_for_route,
route_id: branch_id,
effect: effect
)
|> Factories.Alerts.Alert.active_now()
end)

# Exercise
rows = status_rows_for_alerts(alerts)

# Verify
assert [affected_row, _normal_row] = rows |> for_route("Green")

assert status_label_text_for_row(affected_row) ==
status_label_text_for_effect_plural(effect)
end

test "shows 'See Alerts' for collapsed Green line alerts if they all have the same effect, but different prefixes" do
# Setup
now = Dotcom.Utils.DateTime.now()

start_time =
random_time_range_date_time({now, Dotcom.Utils.ServiceDateTime.end_of_service_day(now)})

[branch_id1, branch_id2] =
Faker.Util.sample_uniq(2, fn -> Faker.Util.pick(GreenLine.branch_ids()) end)

{effect, _severity} = Faker.Util.pick(@effects_with_simple_descriptions)

alerts =
[
Factories.Alerts.Alert.build(:alert_for_route,
route_id: branch_id1,
effect: effect
)
|> Factories.Alerts.Alert.active_now(),
Factories.Alerts.Alert.build(:alert_for_route,
route_id: branch_id2,
effect: effect
)
|> Factories.Alerts.Alert.active_starting_at(start_time)
]

# Exercise
rows = status_rows_for_alerts(alerts)

# Verify
assert [affected_row, _normal_row] = rows |> for_route("Green")

assert status_label_text_for_row(affected_row) == "See Alerts"
end

test "shows the prefix and the effect label for collapsed Green line alerts if they all have the same effect, and the same prefix" do
# Setup
now = Dotcom.Utils.DateTime.now()

start_time =
random_time_range_date_time({now, Dotcom.Utils.ServiceDateTime.end_of_service_day(now)})

[branch_id1, branch_id2] =
Faker.Util.sample_uniq(2, fn -> Faker.Util.pick(GreenLine.branch_ids()) end)

# Excluding `:station_closure` from this test because it follows
# different pluralizing rules than the other effects, we do care
# that other statuses are pluralized properly, and we're testing
# the `:station_closure` case below anyway.
{effect, _severity} =
Faker.Util.pick(@effects_with_simple_descriptions -- [station_closure: 1])

alerts =
[
Factories.Alerts.Alert.build(:alert_for_route,
route_id: branch_id1,
effect: effect
)
|> Factories.Alerts.Alert.active_starting_at(start_time),
Factories.Alerts.Alert.build(:alert_for_route,
route_id: branch_id2,
effect: effect
)
|> Factories.Alerts.Alert.active_starting_at(start_time)
]

# Exercise
rows = status_rows_for_alerts(alerts)

# Verify
assert [affected_row, _normal_row] = rows |> for_route("Green")

expected_status_desc = effect |> status_label_text_for_effect_plural()
expected_prefix = Util.narrow_time(start_time)
expected_status_text = "#{expected_prefix}: #{expected_status_desc}"

assert status_label_text_for_row(affected_row) ==
expected_status_text
end

test "groups Green line alerts correctly between normal and affected rows when collapsed" do
Expand Down Expand Up @@ -250,7 +395,12 @@ defmodule DotcomWeb.Components.SystemStatus.SubwayStatusTest do

test "collapses Green line rows if there is a Mattapan alert" do
# Setup
{effect, severity} = Faker.Util.pick(service_impacting_effects())
# Excluding `:station_closure` from this test because it follows
# different pluralizing rules than the other effects, we do care
# that other statuses are pluralized properly, and we're testing
# the `:station_closure` case below anyway.
{effect, severity} =
Faker.Util.pick(@effects_with_simple_descriptions -- [station_closure: 1])

alerts = [
Factories.Alerts.Alert.build(:alert_for_route,
Expand Down Expand Up @@ -280,7 +430,7 @@ defmodule DotcomWeb.Components.SystemStatus.SubwayStatusTest do
assert rows
|> for_route("Green")
|> Enum.map(&status_label_text_for_row/1) == [
"See Alerts"
status_label_text_for_effect_plural(effect)
]
end

Expand Down Expand Up @@ -607,6 +757,45 @@ defmodule DotcomWeb.Components.SystemStatus.SubwayStatusTest do
status_rows_for_alerts([alert])
end

test "shows all affected stops for collapsed green line station closures" do
# Setup
affected_branches =
Faker.Util.sample_uniq(2, fn -> Faker.Util.pick(GreenLine.branch_ids()) end)

alerts =
affected_branches
|> Enum.map(fn branch_id ->
Factories.Alerts.Alert.build(:alert_for_route,
route_id: branch_id,
effect: :station_closure
)
|> Factories.Alerts.Alert.active_now()
end)

stops = Factories.Stops.Stop.build_list(2, :stop)

expect(Dotcom.Alerts.AffectedStops.Mock, :affected_stops, fn
[_], _ ->
stops |> Enum.take(1) |> Enum.map(&%{stop: &1, direction: :all})

[_, _], _ ->
stops |> Enum.map(&%{stop: &1, direction: :all})
end)

# Exercise
rows = status_rows_for_alerts(alerts)

# Verify
assert [affected_row, _normal_row] = rows |> for_route("Green")

[stop1, stop2] = stops

assert status_label_text_for_row(affected_row) == "Stops Skipped"

assert status_subheading_for_row(affected_row) ==
"#{stop1.name} & #{stop2.name}"
end

test "shows 'Stop Skipped' singular if a single station is closed" do
# Setup
affected_line = Faker.Util.pick(@lines_without_branches)
Expand Down Expand Up @@ -1305,6 +1494,11 @@ defmodule DotcomWeb.Components.SystemStatus.SubwayStatusTest do
defp status_label_text_for_effect(:station_closure), do: "Stop Skipped"
defp status_label_text_for_effect(effect), do: effect |> Atom.to_string() |> Recase.to_title()

defp status_label_text_for_effect_plural(:station_closure), do: "Stops Skipped"

defp status_label_text_for_effect_plural(effect),
do: effect |> status_label_text_for_effect() |> Inflex.pluralize()

defp human_delay_severity(severity) do
Alerts.Alert.human_severity(%Alerts.Alert{effect: :delay, severity: severity})
end
Expand Down
Loading