-module(api_event_tests). -export([test/0]). -define(BASE_URL, "http://localhost:8080"). test() -> io:format("Testing event API...~n"), OwnerEmail = api_test_runner:unique_email(<<"ev_owner">>), OwnerToken = api_test_runner:register_and_login(OwnerEmail, <<"owner123">>), CalId = api_test_runner:extract_json( api_test_runner:http_post("/v1/calendars", #{title => <<"Test">>}, OwnerToken), <<"id">>), % TEST 1: Create single event io:format(" TEST 1: Create single event... "), EventId = api_test_runner:extract_json( api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events", #{title => <<"Test Event">>, start_time => <<"2026-06-01T10:00:00Z">>, duration => 60}, OwnerToken), <<"id">>), io:format("OK~n"), % TEST 2: Create event in past (should fail) io:format(" TEST 2: Create past event... "), {ok, {{_, 400, _}, _, _}} = api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events", #{title => <<"Past Event">>, start_time => <<"2020-01-01T10:00:00Z">>, duration => 60}, OwnerToken), io:format("OK~n"), % TEST 3: Create recurring event io:format(" TEST 3: Create recurring event... "), RecurringId = api_test_runner:extract_json( api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events", #{title => <<"Weekly Meeting">>, start_time => <<"2026-06-01T10:00:00Z">>, duration => 60, recurrence => #{freq => <<"WEEKLY">>, interval => 1}}, OwnerToken), <<"id">>), io:format("OK~n"), % TEST 4: List events io:format(" TEST 4: List events... "), {ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events", OwnerToken), io:format("OK~n"), % TEST 5: Get event io:format(" TEST 5: Get event... "), {ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/events/" ++ binary_to_list(EventId), OwnerToken), io:format("OK~n"), % TEST 6: Update event io:format(" TEST 6: Update event... "), {ok, {{_, 200, _}, _, _}} = api_test_runner:http_put("/v1/events/" ++ binary_to_list(EventId), #{title => <<"Updated Event">>}, OwnerToken), io:format("OK~n"), % TEST 7: Get occurrences io:format(" TEST 7: Get occurrences... "), {ok, {{_, 200, _}, _, _}} = api_test_runner:http_get( "/v1/events/" ++ binary_to_list(RecurringId) ++ "/occurrences?from=2026-06-01T00:00:00Z&to=2026-06-30T00:00:00Z", OwnerToken), io:format("OK~n"), % TEST 8: Cancel occurrence io:format(" TEST 8: Cancel occurrence... "), {ok, {{_, 200, _}, _, _}} = api_test_runner:http_delete( "/v1/events/" ++ binary_to_list(RecurringId) ++ "/occurrences/2026-06-08T10:00:00Z", OwnerToken), io:format("OK~n"), % TEST 9: Delete event io:format(" TEST 9: Delete event... "), {ok, {{_, 200, _}, _, _}} = api_test_runner:http_delete("/v1/events/" ++ binary_to_list(EventId), OwnerToken), io:format("OK~n"), io:format("~n✅ Event API tests passed!~n"), {?MODULE, ok}.