diff --git a/lib/screens_web/controllers/v2/screen_api_controller.ex b/lib/screens_web/controllers/v2/screen_api_controller.ex index c8d39c9c0..080f89d59 100644 --- a/lib/screens_web/controllers/v2/screen_api_controller.ex +++ b/lib/screens_web/controllers/v2/screen_api_controller.ex @@ -84,19 +84,29 @@ defmodule ScreensWeb.V2.ScreenApiController do response = screen_id - |> screen_response(variant, run_all_variants?: true, update_visible_alerts?: true) + |> screen_response(screen, variant, + run_all_variants?: true, + update_visible_alerts?: true + ) |> put_extra_fields(screen_id, screen) json(conn, response) end end - defp screen_response(screen_id, "all", opts) do + defp screen_response(screen_id, _, "all", opts) do {default, variants} = ScreenData.variants(screen_id, opts) Map.put(%{@base_response | data: default}, :variants, variants) end - defp screen_response(screen_id, variant, opts) do + defp screen_response(screen_id, %Screen{vendor: :mercury}, variant, opts) do + %{full_page: data, flex_zone: flex_zone} = + ScreenData.simulation(screen_id, Keyword.put(opts, :generator_variant, variant)) + + Map.merge(%{@base_response | data: data}, %{flex_zone: flex_zone}) + end + + defp screen_response(screen_id, _, variant, opts) do data = ScreenData.get(screen_id, Keyword.put(opts, :generator_variant, variant)) %{@base_response | data: data} end diff --git a/test/fixtures/config.json b/test/fixtures/config.json index 76192bf24..513d14d63 100644 --- a/test/fixtures/config.json +++ b/test/fixtures/config.json @@ -174,6 +174,69 @@ "refresh_if_loaded_before": null, "tags": [], "vendor": "lg_mri" + }, + "EIG-604": { + "disabled": false, + "name": null, + "app_id": "gl_eink_v2", + "app_params": { + "header": { + "direction_id": 0, + "route_id": "Green-E" + }, + "departures": { + "sections": [ + { + "header": { + "title": null, + "arrow": null, + "read_as": null + }, + "filters": { + "max_minutes": null, + "route_directions": null + }, + "query": { + "opts": { + "include_schedules": false + }, + "params": { + "direction_id": 0, + "stop_ids": ["place-nuniv"], + "route_ids": ["Green-E"], + "route_type": null + } + }, + "layout": { + "max": null, + "min": 1, + "base": null, + "include_later": false + }, + "bidirectional": false + } + ] + }, + "footer": { + "stop_id": "place-nuniv" + }, + "alerts": { + "stop_id": "70243" + }, + "evergreen_content": [], + "line_map": { + "stop_id": "70243", + "direction_id": 0, + "route_id": "Green-E", + "station_id": "place-nuniv" + }, + "platform_location": "front" + }, + "device_id": null, + "hidden_from_screenplay": false, + "refresh_if_loaded_before": null, + "tags": [], + "vendor": "mercury" } } } diff --git a/test/screens/v2/screen_data_test.exs b/test/screens/v2/screen_data_test.exs index c4bef8863..ac135abfc 100644 --- a/test/screens/v2/screen_data_test.exs +++ b/test/screens/v2/screen_data_test.exs @@ -1,30 +1,3 @@ -defmodule Screens.V2.ScreenDataTest.Stub do - defmacro candidate_generator(name, instances_fn) do - quote do - defmodule unquote(name) do - alias Screens.V2.CandidateGenerator - alias Screens.V2.Template.Builder - alias Screens.V2.WidgetInstance.Placeholder - - @behaviour CandidateGenerator - - @impl CandidateGenerator - def screen_template(), do: Builder.build_template({:screen, %{normal: [:main]}}) - - @impl CandidateGenerator - def candidate_instances(config) do - unquote(instances_fn).(config) - end - - @impl CandidateGenerator - def audio_only_instances(_widgets, _config), do: [] - - defp placeholder(color), do: %Placeholder{color: color, slot_names: [:main]} - end - end - end -end - defmodule Screens.V2.ScreenDataTest do use ExUnit.Case, async: true @@ -32,7 +5,7 @@ defmodule Screens.V2.ScreenDataTest do alias Screens.V2.ScreenData alias Screens.V2.WidgetInstance.MockWidget alias Screens.V2.WidgetInstance.Placeholder - alias Screens.V2.ScreenDataTest.Stub + alias Screens.TestSupport.CandidateGeneratorStub, as: Stub import ExUnit.CaptureLog import Screens.Inject diff --git a/test/screens_web/controllers/v2/screen_api_controller_test.exs b/test/screens_web/controllers/v2/screen_api_controller_test.exs new file mode 100644 index 000000000..f9fcbc062 --- /dev/null +++ b/test/screens_web/controllers/v2/screen_api_controller_test.exs @@ -0,0 +1,79 @@ +defmodule ScreensWeb.V2.ScreenApiControllerTest do + use ScreensWeb.ConnCase + + alias ScreensConfig.Screen + alias Screens.TestSupport.CandidateGeneratorStub, as: Stub + + import Screens.Inject + import Mox + setup :verify_on_exit! + + @cache injected(Screens.Config.Cache) + @parameters injected(Screens.V2.ScreenData.Parameters) + + require Stub + + Stub.candidate_generator(MercuryGenerator, fn _ -> [placeholder(:green)] end) + Stub.candidate_generator(LgMriGenerator, fn _ -> [placeholder(:red)] end) + + describe "show/2" do + test "only returns flex_zone for Mercury screens", %{conn: conn} do + expect(@cache, :screen, fn + "EIG-604" -> + struct(Screen, app_id: :gl_eink_v2, vendor: :mercury) + end) + + expect(@parameters, :variants, fn _ -> [nil] end) + + stub( + @parameters, + :candidate_generator, + fn %Screen{vendor: :mercury}, nil -> MercuryGenerator end + ) + + stub(@parameters, :refresh_rate, fn _app_id -> 0 end) + + conn = get(conn, "/v2/api/screen/EIG-604?last_refresh=2024-12-02T00:00:00Z") + + assert %{ + "audio_data" => "", + "data" => %{ + "main" => %{"color" => "green", "type" => "placeholder"}, + "type" => "normal" + }, + "disabled" => false, + "flex_zone" => [], + "force_reload" => false, + "last_deploy_timestamp" => nil + } == json_response(conn, 200) + end + + test "omits flex_zone from non-Mercury screens", %{conn: conn} do + expect(@cache, :screen, fn + "1401" -> + struct(Screen, app_id: :bus_shelter_v2, vendor: :lg_mri) + end) + + expect(@parameters, :variants, fn _ -> [nil] end) + + stub( + @parameters, + :candidate_generator, + fn %Screen{vendor: :lg_mri}, nil -> LgMriGenerator end + ) + + stub(@parameters, :refresh_rate, fn _app_id -> 0 end) + + conn = get(conn, "/v2/api/screen/1401?last_refresh=2024-12-02T00:00:00Z") + + assert %{ + "data" => %{ + "main" => %{"color" => "red", "type" => "placeholder"}, + "type" => "normal" + }, + "disabled" => false, + "force_reload" => false + } == json_response(conn, 200) + end + end +end diff --git a/test/support/candidate_generator_stub.ex b/test/support/candidate_generator_stub.ex new file mode 100644 index 000000000..1eb3cdacb --- /dev/null +++ b/test/support/candidate_generator_stub.ex @@ -0,0 +1,26 @@ +defmodule Screens.TestSupport.CandidateGeneratorStub do + defmacro candidate_generator(name, instances_fn) do + quote do + defmodule unquote(name) do + alias Screens.V2.CandidateGenerator + alias Screens.V2.Template.Builder + alias Screens.V2.WidgetInstance.Placeholder + + @behaviour CandidateGenerator + + @impl CandidateGenerator + def screen_template(), do: Builder.build_template({:screen, %{normal: [:main]}}) + + @impl CandidateGenerator + def candidate_instances(config) do + unquote(instances_fn).(config) + end + + @impl CandidateGenerator + def audio_only_instances(_widgets, _config), do: [] + + defp placeholder(color), do: %Placeholder{color: color, slot_names: [:main]} + end + end + end +end