-module(admin_handler_stats_tests). -include_lib("eunit/include/eunit.hrl"). -include("records.hrl"). -define(TABLES, [admin, admin_audit, user, event, calendar, review, report, ticket, subscription]). -define(ADMIN_ID, <<"adm_test_1">>). setup() -> eh_test_support:start_mnesia(), eh_test_support:ensure_tables(?TABLES), eh_test_support:ensure_jwt(), eh_test_support:seed_admin(#{id => ?ADMIN_ID, role => admin}), ok. cleanup(_) -> eh_test_support:unload_cowboy(), eh_test_support:delete_tables(?TABLES), eh_test_support:stop_mnesia(), ok. admin_stats_test_() -> {foreach, fun setup/0, fun cleanup/1, [ {"GET stats – success empty", fun test_stats_empty/0}, {"GET stats – with seeded data", fun test_stats_with_data/0}, {"GET – unauthorized", fun test_unauthorized/0}, {"POST – method not allowed", fun test_wrong_method/0} ]}. test_stats_empty() -> {Status, _, Body} = eh_test_support:call(admin_handler_stats, #{ method => <<"GET">>, path => <<"/v1/admin/stats">>, auth => ?ADMIN_ID }), ?assertEqual(200, Status), Result = jsx:decode(Body, [return_maps]), ?assertEqual(0, maps:get(<<"users_total">>, Result)), ?assertEqual(0, maps:get(<<"events_total">>, Result)), ?assertEqual(0, maps:get(<<"tickets_total">>, Result)), ?assert(is_map_key(<<"registrations_by_day">>, Result)). test_stats_with_data() -> ok = mnesia:dirty_write(eh_test_support:make_user(#{ id => <<"u1">>, email => <<"u1@test.local">>, status => active })), {ok, _} = core_report:create(<<"u1">>, calendar, <<"c1">>, <<"spam">>), {ok, _} = core_ticket:create(<<"u1">>, <<"hash1">>, <<"boom">>, <<"stack">>), {Status, _, Body} = eh_test_support:call(admin_handler_stats, #{ method => <<"GET">>, path => <<"/v1/admin/stats">>, auth => ?ADMIN_ID }), ?assertEqual(200, Status), Result = jsx:decode(Body, [return_maps]), ?assertEqual(1, maps:get(<<"users_total">>, Result)), ?assertEqual(1, maps:get(<<"reports_total">>, Result)), ?assertEqual(1, maps:get(<<"tickets_total">>, Result)). test_unauthorized() -> {Status, _, _} = eh_test_support:call(admin_handler_stats, #{ method => <<"GET">>, path => <<"/v1/admin/stats">>, auth => none }), ?assertEqual(401, Status). test_wrong_method() -> {Status, _, _} = eh_test_support:call(admin_handler_stats, #{ method => <<"POST">>, path => <<"/v1/admin/stats">>, auth => ?ADMIN_ID }), ?assertEqual(405, Status).