-module(sprocket@test_helpers). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([connect/1, render_html/1, render_el_html/1, mouse_move/2, key_down/2, assert_element/1, assert_regex/2, wait_while/2, wait_until/2, find_element/2, has_element/2, render_event/3]). -export_type([event/0, find_element_by/0]). -type event() :: click_event | {input_event, binary()} | {mouse_move_event, sprocket@html@events:mouse_event()} | {form_change_event, gleam@dict:dict(binary(), binary())} | {form_submit_event, gleam@dict:dict(binary(), binary())} | blur_event | focus_event | {key_down_event, sprocket@html@events:key_event()}. -type find_element_by() :: {by_id, binary()} | {by_class, binary()} | {by_tag, binary()} | {by_predicate, fun((sprocket@internal@reconcile:reconciled_element()) -> boolean())}. -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 19). -spec connect(sprocket@context:element()) -> gleam@erlang@process:subject(sprocket@runtime:message()). connect(View) -> _assert_subject = sprocket@runtime:start( View, {updater, fun(_) -> {ok, nil} end}, none ), {ok, Spkt} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"sprocket/test_helpers"/utf8>>, function => <<"connect"/utf8>>, line => 20}) end, Spkt. -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 25). -spec render_html(gleam@erlang@process:subject(sprocket@runtime:message())) -> {gleam@erlang@process:subject(sprocket@runtime:message()), binary()}. render_html(Spkt) -> sprocket@render:renderer( sprocket@renderers@html:html_renderer(), fun(Render_html) -> Html = begin _pipe = sprocket@runtime:reconcile_immediate(Spkt), Render_html(_pipe) end, {Spkt, Html} end ). -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 35). -spec render_el_html(sprocket@internal@reconcile:reconciled_element()) -> binary(). render_el_html(El) -> sprocket@render:renderer( sprocket@renderers@html:html_renderer(), fun(Render_html) -> Render_html(El) end ). -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 41). -spec mouse_move(integer(), integer()) -> event(). mouse_move(X, Y) -> {mouse_move_event, {mouse_event, X, Y, false, false, false, false}}. -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 45). -spec key_down(binary(), binary()) -> event(). key_down(Key, Code) -> {key_down_event, {key_event, Key, Code, false, false, false, false}}. -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 192). -spec assert_element( {ok, sprocket@internal@reconcile:reconciled_element()} | {error, nil} ) -> sprocket@internal@reconcile:reconciled_element(). assert_element(Maybe_el) -> case Maybe_el of {ok, El} -> El; _ -> erlang:error(#{gleam_error => panic, message => <<"Element not found"/utf8>>, module => <<"sprocket/test_helpers"/utf8>>, function => <<"assert_element"/utf8>>, line => 195}) end. -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 199). -spec assert_regex( {ok, sprocket@internal@reconcile:reconciled_element()} | {error, nil}, binary() ) -> boolean(). assert_regex(Maybe_el, Regex) -> case Maybe_el of {ok, El} -> Html = render_el_html(El), _assert_subject = gleam@regexp:from_string(Regex), {ok, Re} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"sprocket/test_helpers"/utf8>>, function => <<"assert_regex"/utf8>>, line => 204}) end, gleam@regexp:check(Re, Html); _ -> erlang:error(#{gleam_error => panic, message => <<"Element not found"/utf8>>, module => <<"sprocket/test_helpers"/utf8>>, function => <<"assert_regex"/utf8>>, line => 207}) end. -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 253). -spec wait_helper(fun(() -> boolean()), integer(), integer()) -> boolean(). wait_helper(Predicate, Timeout, Started_at) -> gleam@bool:guard( (os:system_time(millisecond) - Started_at) > Timeout, false, fun() -> gleam@bool:guard( Predicate(), true, fun() -> wait_helper(Predicate, Timeout, Started_at) end ) end ). -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 233). -spec wait_while(fun(() -> boolean()), integer()) -> boolean(). wait_while(Predicate, Timeout) -> case wait_helper( fun() -> not Predicate() end, Timeout, os:system_time(millisecond) ) of true -> true; false -> erlang:error(#{gleam_error => panic, message => <<"Timeout waiting for condition"/utf8>>, module => <<"sprocket/test_helpers"/utf8>>, function => <<"wait_while"/utf8>>, line => 242}) end. -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 246). -spec wait_until(fun(() -> boolean()), integer()) -> boolean(). wait_until(Predicate, Timeout) -> case wait_helper(Predicate, Timeout, os:system_time(millisecond)) of true -> true; false -> erlang:error(#{gleam_error => panic, message => <<"Timeout waiting for condition"/utf8>>, module => <<"sprocket/test_helpers"/utf8>>, function => <<"wait_until"/utf8>>, line => 249}) end. -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 263). -spec find_by_matching_attr( sprocket@internal@reconcile:reconciled_element(), binary(), binary() ) -> boolean(). find_by_matching_attr(El, Key, Expected_value) -> case El of {reconciled_element, _, _, Attrs, _} -> Matching_attr = begin _pipe = Attrs, gleam@list:find(_pipe, fun(Attr) -> case Attr of {reconciled_attribute, Attr_key, Attr_value} when (Attr_key =:= Key) andalso (Attr_value =:= Expected_value) -> true; _ -> false end end) end, case Matching_attr of {ok, _} -> true; _ -> false end; _ -> false end. -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 218). -spec check_predicate( sprocket@internal@reconcile:reconciled_element(), find_element_by() ) -> boolean(). check_predicate(El, Is_desired) -> case Is_desired of {by_id, Id} -> find_by_matching_attr(El, <<"id"/utf8>>, Id); {by_class, Class_name} -> find_by_matching_attr(El, <<"class"/utf8>>, Class_name); {by_tag, Tag_name} -> case El of {reconciled_element, Tag, _, _, _} when Tag =:= Tag_name -> true; _ -> false end; {by_predicate, Func} -> Func(El) end. -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 286). -spec panic_no_reconciled() -> any(). panic_no_reconciled() -> erlang:error(#{gleam_error => panic, message => <<"Nothing reconciled! This is likely because the view has not been rendered yet."/utf8>>, module => <<"sprocket/test_helpers"/utf8>>, function => <<"panic_no_reconciled"/utf8>>, line => 287}). -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 178). -spec find_element( gleam@erlang@process:subject(sprocket@runtime:message()), find_element_by() ) -> {ok, sprocket@internal@reconcile:reconciled_element()} | {error, nil}. find_element(Spkt, Is_desired) -> case sprocket@runtime:get_reconciled(Spkt) of {some, Reconciled} -> sprocket@internal@reconcilers@recursive:find( Reconciled, fun(_capture) -> check_predicate(_capture, Is_desired) end ); none -> panic_no_reconciled() end. -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 211). -spec has_element( gleam@erlang@process:subject(sprocket@runtime:message()), find_element_by() ) -> boolean(). has_element(Spkt, Is_desired) -> case find_element(Spkt, Is_desired) of {ok, _} -> true; _ -> false end. -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 290). -spec panic_no_element_with_id() -> any(). panic_no_element_with_id() -> erlang:error(#{gleam_error => panic, message => <<"No element found with matching id"/utf8>>, module => <<"sprocket/test_helpers"/utf8>>, function => <<"panic_no_element_with_id"/utf8>>, line => 291}). -file("/home/runner/work/sprocket/sprocket/src/sprocket/test_helpers.gleam", 60). -spec render_event( gleam@erlang@process:subject(sprocket@runtime:message()), event(), binary() ) -> gleam@erlang@process:subject(sprocket@runtime:message()). render_event(Spkt, Event, Html_id) -> case sprocket@runtime:get_reconciled(Spkt) of {some, Reconciled} -> Found = sprocket@internal@reconcilers@recursive:find( Reconciled, fun(El) -> case El of {reconciled_element, _, _, Attrs, _} -> Matching_id_attr = begin _pipe = Attrs, gleam@list:find(_pipe, fun(Attr) -> case Attr of {reconciled_attribute, <<"id"/utf8>>, Id} when Id =:= Html_id -> true; _ -> false end end) end, case Matching_id_attr of {ok, _} -> true; _ -> false end; _ -> false end end ), case Found of {ok, {reconciled_element, _, _, Attrs@1, _}} -> {Event_kind, Event_payload} = case Event of click_event -> {<<"click"/utf8>>, gleam_stdlib:identity(nil)}; {input_event, Value} -> {<<"input"/utf8>>, gleam_stdlib:identity( maps:from_list( [{<<"target"/utf8>>, maps:from_list( [{<<"value"/utf8>>, Value}] )}] ) )}; {mouse_move_event, E} -> {<<"mousemove"/utf8>>, gleam_stdlib:identity( begin _pipe@1 = maps:new(), _pipe@2 = gleam@dict:insert( _pipe@1, <<"clientX"/utf8>>, gleam_stdlib:identity( erlang:element(2, E) ) ), _pipe@3 = gleam@dict:insert( _pipe@2, <<"clientY"/utf8>>, gleam_stdlib:identity( erlang:element(3, E) ) ), _pipe@4 = gleam@dict:insert( _pipe@3, <<"ctrlKey"/utf8>>, gleam_stdlib:identity( erlang:element(4, E) ) ), _pipe@5 = gleam@dict:insert( _pipe@4, <<"shiftKey"/utf8>>, gleam_stdlib:identity( erlang:element(5, E) ) ), _pipe@6 = gleam@dict:insert( _pipe@5, <<"altKey"/utf8>>, gleam_stdlib:identity( erlang:element(6, E) ) ), gleam@dict:insert( _pipe@6, <<"metaKey"/utf8>>, gleam_stdlib:identity( erlang:element(7, E) ) ) end )}; {form_change_event, Data} -> {<<"change"/utf8>>, gleam_stdlib:identity( maps:from_list( [{<<"formData"/utf8>>, Data}] ) )}; {form_submit_event, Data@1} -> {<<"submit"/utf8>>, gleam_stdlib:identity( maps:from_list( [{<<"formData"/utf8>>, Data@1}] ) )}; blur_event -> {<<"blur"/utf8>>, gleam_stdlib:identity(nil)}; focus_event -> {<<"focus"/utf8>>, gleam_stdlib:identity(nil)}; {key_down_event, E@1} -> {<<"keydown"/utf8>>, gleam_stdlib:identity( begin _pipe@7 = maps:new(), _pipe@8 = gleam@dict:insert( _pipe@7, <<"key"/utf8>>, gleam_stdlib:identity( erlang:element(2, E@1) ) ), _pipe@9 = gleam@dict:insert( _pipe@8, <<"code"/utf8>>, gleam_stdlib:identity( erlang:element(3, E@1) ) ), _pipe@10 = gleam@dict:insert( _pipe@9, <<"ctrlKey"/utf8>>, gleam_stdlib:identity(false) ), _pipe@11 = gleam@dict:insert( _pipe@10, <<"shiftKey"/utf8>>, gleam_stdlib:identity(false) ), _pipe@12 = gleam@dict:insert( _pipe@11, <<"altKey"/utf8>>, gleam_stdlib:identity(false) ), gleam@dict:insert( _pipe@12, <<"metaKey"/utf8>>, gleam_stdlib:identity(false) ) end )} end, Rendered_event_handler = begin _pipe@13 = Attrs@1, gleam@list:find(_pipe@13, fun(Attr@1) -> case Attr@1 of {reconciled_event_handler, Kind, _} when Kind =:= Event_kind -> true; _ -> false end end) end, case Rendered_event_handler of {ok, {reconciled_event_handler, _, Event_id}} -> case sprocket@runtime:process_event_immediate( Spkt, Event_id, Event_payload ) of {ok, _} -> Spkt; _ -> erlang:error(#{gleam_error => panic, message => <<"`panic` expression evaluated."/utf8>>, module => <<"sprocket/test_helpers"/utf8>>, function => <<"render_event"/utf8>>, line => 150}) end; _ -> erlang:error(#{gleam_error => panic, message => <<"No handler found for event"/utf8>>, module => <<"sprocket/test_helpers"/utf8>>, function => <<"render_event"/utf8>>, line => 154}) end; _ -> panic_no_element_with_id() end; none -> panic_no_reconciled() end, Spkt.