Stage 10 final

This commit is contained in:
2026-04-22 23:15:20 +03:00
parent e3a08cfa04
commit 081dcf9588
85 changed files with 2116 additions and 160 deletions

View File

@@ -0,0 +1,61 @@
-module(ws_handler_tests).
-include_lib("eunit/include/eunit.hrl").
-record(state, {
user_id :: binary() | undefined,
subscriptions = [] :: [binary()]
}).
setup() ->
pg:start_link(),
ok.
cleanup(_) ->
ok.
ws_handler_test_() ->
{foreach,
fun setup/0,
fun cleanup/1,
[
{"WebSocket info notification - booking", fun test_websocket_info_booking/0},
{"WebSocket info notification - calendar", fun test_websocket_info_calendar/0},
{"WebSocket info notification - event", fun test_websocket_info_event/0}
]}.
test_websocket_info_booking() ->
State = #state{user_id = <<"user123">>, subscriptions = []},
Data = #{booking_id => <<"book123">>, event_id => <<"ev123">>, status => confirmed},
Msg = {notification, booking_update, Data},
case ws_handler:websocket_info(Msg, State) of
{reply, {text, Reply}, _} ->
Decoded = jsx:decode(Reply, [return_maps]),
?assertEqual(<<"booking_update">>, maps:get(<<"type">>, Decoded)),
?assertEqual(<<"book123">>, maps:get(<<"booking_id">>, maps:get(<<"data">>, Decoded)));
_ -> ?assert(false, "Expected reply")
end.
test_websocket_info_calendar() ->
State = #state{user_id = <<"user123">>, subscriptions = [<<"cal123">>]},
Data = #{calendar_id => <<"cal123">>, title => <<"Updated">>},
Msg = {notification, calendar_update, Data},
case ws_handler:websocket_info(Msg, State) of
{reply, {text, Reply}, _} ->
Decoded = jsx:decode(Reply, [return_maps]),
?assertEqual(<<"calendar_update">>, maps:get(<<"type">>, Decoded));
_ -> ?assert(false, "Expected reply")
end.
test_websocket_info_event() ->
State = #state{user_id = <<"user123">>, subscriptions = []},
Data = #{event_id => <<"ev123">>, title => <<"Updated Event">>},
Msg = {notification, event_update, Data},
case ws_handler:websocket_info(Msg, State) of
{reply, {text, Reply}, _} ->
Decoded = jsx:decode(Reply, [return_maps]),
?assertEqual(<<"event_update">>, maps:get(<<"type">>, Decoded));
_ -> ?assert(false, "Expected reply")
end.