\set ON_ERROR_STOP on BEGIN; CREATE OR REPLACE FUNCTION pg_temp.assert_true(condition boolean, message text) RETURNS void LANGUAGE plpgsql AS $$ BEGIN IF condition IS NOT TRUE THEN RAISE EXCEPTION 'assertion_failed: %', message; END IF; END; $$; INSERT INTO auth.users ( id, aud, role, email, encrypted_password, email_confirmed_at, raw_app_meta_data, raw_user_meta_data, created_at, updated_at ) VALUES ( '26000000-0000-4000-8000-000000000001', 'authenticated', 'authenticated', 'stt-quota-free@example.invalid', crypt('fixture-password', gen_salt('bf')), now(), '{"provider":"email","providers":["email"]}'::jsonb, '{}'::jsonb, now(), now() ), ( '26000000-0000-4000-8000-000000000002', 'authenticated', 'authenticated', 'stt-quota-pro@example.invalid', crypt('fixture-password', gen_salt('bf')), now(), '{"provider":"email","providers":["email"]}'::jsonb, '{}'::jsonb, now(), now() ); UPDATE public.subscriptions SET tier = 'free', status = 'active', overage_credits = 0 WHERE user_id = '26000000-0000-4000-8000-000000000001'; UPDATE public.subscriptions SET tier = 'pro', status = 'active', overage_credits = 0 WHERE user_id = '26000000-0000-4000-8000-000000000002'; SELECT pg_temp.assert_true( NOT has_function_privilege('authenticated', 'public.reserve_stt_quota(uuid,uuid)', 'EXECUTE'), 'authenticated users cannot reserve their own quota directly' ); SELECT pg_temp.assert_true( NOT has_function_privilege('authenticated', 'public.finalize_stt_quota(uuid,boolean)', 'EXECUTE'), 'authenticated users cannot refund their own quota directly' ); SELECT pg_temp.assert_true( has_function_privilege('service_role', 'public.reserve_stt_quota(uuid,uuid)', 'EXECUTE'), 'service role can reserve quota' ); SELECT pg_temp.assert_true( NOT has_table_privilege('authenticated', 'public.stt_quota_reservations', 'SELECT'), 'authenticated users cannot read the reservation ledger' ); INSERT INTO public.daily_usage(user_id, date, feature, count) VALUES ('26000000-0000-4000-8000-000000000001', CURRENT_DATE, 'stt_transcribe', 249); DO $$ DECLARE first jsonb; replay jsonb; denied jsonb; overage_reservation jsonb; finalized jsonb; usage_count integer; credits integer; BEGIN first := public.reserve_stt_quota( '26000000-0000-4000-8000-000000000001', '26100000-0000-4000-8000-000000000001' ); PERFORM pg_temp.assert_true((first->>'allowed')::boolean, 'last free base unit is reserved'); PERFORM pg_temp.assert_true(first->>'consumed_from' = 'base', 'base unit is identified'); PERFORM pg_temp.assert_true((first->>'current')::integer = 250, 'weekly count reaches the exact limit'); replay := public.reserve_stt_quota( '26000000-0000-4000-8000-000000000001', '26100000-0000-4000-8000-000000000001' ); SELECT count INTO usage_count FROM public.daily_usage WHERE user_id = '26000000-0000-4000-8000-000000000001' AND date = CURRENT_DATE AND feature = 'stt_transcribe'; PERFORM pg_temp.assert_true((replay->>'allowed')::boolean AND usage_count = 250, 'reservation replay is idempotent'); denied := public.reserve_stt_quota( '26000000-0000-4000-8000-000000000001', '26100000-0000-4000-8000-000000000002' ); PERFORM pg_temp.assert_true(NOT (denied->>'allowed')::boolean, 'quota denies before provider work'); UPDATE public.subscriptions SET overage_credits = 1 WHERE user_id = '26000000-0000-4000-8000-000000000001'; overage_reservation := public.reserve_stt_quota( '26000000-0000-4000-8000-000000000001', '26100000-0000-4000-8000-000000000003' ); PERFORM pg_temp.assert_true(overage_reservation->>'consumed_from' = 'overage', 'overage is reserved only after base exhaustion'); finalized := public.finalize_stt_quota('26100000-0000-4000-8000-000000000003', false); SELECT count INTO usage_count FROM public.daily_usage WHERE user_id = '26000000-0000-4000-8000-000000000001' AND date = CURRENT_DATE AND feature = 'stt_transcribe'; SELECT overage_credits INTO credits FROM public.subscriptions WHERE user_id = '26000000-0000-4000-8000-000000000001'; PERFORM pg_temp.assert_true(finalized->>'status' = 'released' AND usage_count = 250 AND credits = 1, 'provider failure refunds exactly one usage and overage unit'); finalized := public.finalize_stt_quota('26100000-0000-4000-8000-000000000003', false); SELECT overage_credits INTO credits FROM public.subscriptions WHERE user_id = '26000000-0000-4000-8000-000000000001'; PERFORM pg_temp.assert_true(finalized->>'status' = 'released' AND credits = 1, 'failure finalization replay does not double-refund'); END; $$; -- Expired provider work is reclaimed before a replacement reservation. UPDATE public.stt_quota_reservations SET lease_expires_at = now() - interval '1 second' WHERE id = '26100000-0000-4000-8000-000000000001'; DO $$ DECLARE replacement jsonb; old_status text; usage_count integer; BEGIN replacement := public.reserve_stt_quota( '26000000-0000-4000-8000-000000000001', '26100000-0000-4000-8000-000000000004' ); SELECT status INTO old_status FROM public.stt_quota_reservations WHERE id = '26100000-0000-4000-8000-000000000001'; SELECT count INTO usage_count FROM public.daily_usage WHERE user_id = '26000000-0000-4000-8000-000000000001' AND date = CURRENT_DATE AND feature = 'stt_transcribe'; PERFORM pg_temp.assert_true((replacement->>'allowed')::boolean, 'expired unit is reusable'); PERFORM pg_temp.assert_true(old_status = 'released' AND usage_count = 250, 'reclaim plus replacement preserves the exact total'); PERFORM public.finalize_stt_quota('26100000-0000-4000-8000-000000000004', true); END; $$; DO $$ DECLARE unlimited jsonb; finalized jsonb; BEGIN unlimited := public.reserve_stt_quota( '26000000-0000-4000-8000-000000000002', '26200000-0000-4000-8000-000000000001' ); finalized := public.finalize_stt_quota('26200000-0000-4000-8000-000000000001', true); PERFORM pg_temp.assert_true( unlimited->>'consumed_from' = 'unlimited' AND (unlimited->>'limit')::integer = -1 AND finalized->>'status' = 'completed', 'paid unlimited usage is still measured and finalized' ); END; $$; ROLLBACK;