\set ON_ERROR_STOP on begin; create function pg_temp.assert_summary(condition boolean, label text) returns void language plpgsql as $$ begin if condition is distinct from true then raise exception 'FAIL: %', label; end if; raise notice 'PASS: %', label; end; $$; create temp table ai_fixture as select gen_random_uuid() event_id, 'ai-admin-' || gen_random_uuid() || '@example.invalid' admin_email, 'ai-attendee-' || gen_random_uuid() || '@example.invalid' attendee_email; insert into public.events(id, name, start_date, end_date, is_active) select event_id, 'AI test event', '2026-10-23', '2026-10-23', false from ai_fixture; insert into public.contacts(email, full_name, is_admin) select admin_email, 'AI test admin', true from ai_fixture union all select attendee_email, 'AI test attendee', false from ai_fixture; select pg_temp.assert_summary( (select bool_and(relrowsecurity) from pg_class where oid in ('public.llm_settings'::regclass, 'public.feedback_summaries'::regclass)), 'RLS enabled on both AI tables'); select pg_temp.assert_summary( not has_table_privilege('anon', 'public.llm_settings', 'select') and not has_table_privilege('authenticated', 'public.llm_settings', 'select') and not has_table_privilege('authenticated', 'public.llm_settings', 'insert,update,delete'), 'client roles cannot access LLM settings or credentials'); select pg_temp.assert_summary( not has_table_privilege('anon', 'public.feedback_summaries', 'select') and not has_table_privilege('authenticated', 'public.feedback_summaries', 'insert,update,delete'), 'anonymous reads and all client summary writes are denied'); select pg_temp.assert_summary( not has_function_privilege('authenticated', 'public.evaluate_session_feedback(uuid)', 'execute') and not has_function_privilege('anon', 'public.evaluate_session_feedback(uuid)', 'execute'), 'obsolete AI stub cannot be called by clients'); select event_id::text as ai_event_id, admin_email as ai_admin_email, attendee_email as ai_attendee_email from ai_fixture \gset set local role service_role; insert into public.llm_settings(id, endpoint, model, api_key) values (true, 'https://llm.example/v1/chat/completions', 'test', 'fixture-only-key') on conflict (id) do update set model = excluded.model; insert into public.feedback_summaries(scope, target_id, event_id, summary, response_count, source_fingerprint, model) values ('event', :'ai_event_id', :'ai_event_id', 'Fixture summary', 1, 'fixture-hash', 'test'); reset role; select pg_temp.assert_summary( exists(select 1 from public.feedback_summaries where event_id = :'ai_event_id'), 'service role can persist summaries'); set local role authenticated; select set_config('request.jwt.claims', json_build_object('email', :'ai_attendee_email')::text, true); select pg_temp.assert_summary( not exists(select 1 from public.feedback_summaries where event_id = :'ai_event_id'), 'non-admin cannot read a summary'); select set_config('request.jwt.claims', json_build_object('email', :'ai_admin_email')::text, true); select pg_temp.assert_summary( (select summary = 'Fixture summary' from public.feedback_summaries where event_id = :'ai_event_id'), 'admin can read the saved summary'); reset role; delete from public.events where id = :'ai_event_id'; select pg_temp.assert_summary( not exists(select 1 from public.feedback_summaries where event_id = :'ai_event_id'), 'deleting the event cascades its summaries'); rollback;