From 140e2e94353dd1e4e35780fc09053ba65307200c Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Mon, 2 Dec 2024 11:22:30 -0500 Subject: [PATCH] Added tests. --- test/fixtures/config.json | 63 +++++++++++++++ test/screens/v2/screen_data_test.exs | 29 +------ .../v2/screen_api_controller_test.exs | 79 +++++++++++++++++++ 3 files changed, 143 insertions(+), 28 deletions(-) create mode 100644 test/screens_web/controllers/v2/screen_api_controller_test.exs 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