-- pgTAP: notes trash purge + pin/archive exclusivity — Fase 3 -- Run with: psql -U postgres -d postgres -f supabase/tests/006_notes_trash_purge.sql -- -- Validates: -- - The CHECK constraint forbidding (is_pinned AND is_archived) -- - The pg_cron purge job (executed inline) hard-deletes notes with -- deleted_at older than 7 days CREATE EXTENSION IF NOT EXISTS pgtap; BEGIN; SELECT plan(4); -- Insert a note that is already 8 days in the trash INSERT INTO public.notes (id, collective_id, content, created_by, updated_by, deleted_at) VALUES ( '99999999-1111-1111-1111-111111111111', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'expired trash note', '11111111-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111111', now() - INTERVAL '8 days' ); -- Insert a note still inside the 7-day window INSERT INTO public.notes (id, collective_id, content, created_by, updated_by, deleted_at) VALUES ( '99999999-2222-2222-2222-222222222222', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'recent trash note', '11111111-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111111', now() - INTERVAL '2 days' ); -- N-PG-01: pre-purge sanity — both notes exist SELECT is( (SELECT count(*)::int FROM public.notes WHERE id IN ('99999999-1111-1111-1111-111111111111', '99999999-2222-2222-2222-222222222222')), 2, 'N-PG-01: both trash fixtures inserted' ); -- Run the purge job's SQL directly (matches what pg_cron schedules) DELETE FROM public.notes WHERE deleted_at < now() - INTERVAL '7 days'; -- N-PG-02: 8-day note is gone SELECT is( (SELECT count(*)::int FROM public.notes WHERE id = '99999999-1111-1111-1111-111111111111'), 0, 'N-PG-02: note older than 7 days is purged' ); -- N-PG-03: 2-day note still exists SELECT is( (SELECT count(*)::int FROM public.notes WHERE id = '99999999-2222-2222-2222-222222222222'), 1, 'N-PG-03: note inside the 7-day window survives' ); -- N-PG-04: pin + archive simultaneously violates CHECK SELECT throws_ok( $$ INSERT INTO public.notes (collective_id, content, created_by, updated_by, is_pinned, is_archived) VALUES ('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'pin+archive', '11111111-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111111', true, true) $$, '23514', NULL, 'N-PG-04: notes with is_pinned AND is_archived are rejected by CHECK' ); SELECT * FROM finish(); ROLLBACK;