diff --git a/packages/supabase/tests/sandbox/animals.sandbox.ts b/packages/supabase/tests/sandbox/animals.sandbox.ts index 8431695f..62fe78f6 100644 --- a/packages/supabase/tests/sandbox/animals.sandbox.ts +++ b/packages/supabase/tests/sandbox/animals.sandbox.ts @@ -425,7 +425,11 @@ describe('animals and relations through PostgREST, per role', () => { }) it('a caller outside the workspace never waits on its locks', async () => { + const bAnimal = idOf( + await createAnimal(b, await workspaceOf(b.id), { name: 'Lock probe' }), + ) const holder = sql(`begin; + select 1 from public.clients where id = '${marie}' for update; select 1 from public.animals where id = '${eclair}' for update; select 1 from public.animal_client_relations where id = '${relation}' for update; select pg_sleep(2); commit;`) @@ -439,13 +443,14 @@ describe('animals and relations through PostgREST, per role', () => { b.token, ), link(b, eclair, marie, 'contact'), + link(b, bAnimal, marie, 'contact'), ]) const elapsed = Date.now() - started await holder - expect(results.map(shape)).toEqual([NOT_FOUND, NOT_FOUND, NOT_FOUND]) + expect(results.map(shape)).toEqual(Array(4).fill(NOT_FOUND)) expect(elapsed).toBeLessThan(1000) console.log( - `[measure] A's animal and relation locked 2 s: B's three refusals answered in ${elapsed} ms`, + `[measure] A's client, animal and relation locked 2 s: B's four refusals answered in ${elapsed} ms`, ) }) @@ -621,6 +626,29 @@ describe('concurrent writes', () => { ) }) + it('a relation create racing the archive of its animal: created before, or refused archived after', async () => { + for (let round = 0; round < 10; round++) { + const animal = idOf( + await createAnimal(a, wA, { name: `Animal archive race ${round}` }), + ) + const [linked, archived] = await Promise.all([ + link(a, animal, clients[3], 'keeper'), + rpc( + 'set_animal_archived', + { animal_id: animal, archived: true }, + a.token, + ), + ]) + expect(messageOf(archived)).toBe('ok') + expect(['ok', 'archived']).toContain(messageOf(linked)) + expect( + await sql( + `select count(*) from public.animal_client_relations where animal_id = '${animal}'`, + ), + ).toBe(messageOf(linked) === 'ok' ? '1' : '0') + } + }) + it('the budget is one counter for clients, animals and relations: at 990, 30 mixed writes, exactly 10 pass', async () => { const animals: string[] = [] for (let i = 0; i < 10; i++) diff --git a/supabase/tests/014_animals_relations.sql b/supabase/tests/014_animals_relations.sql index 7501c0bb..ffde347e 100644 --- a/supabase/tests/014_animals_relations.sql +++ b/supabase/tests/014_animals_relations.sql @@ -9,7 +9,7 @@ -- creating an animal; S a platform owner; E (per-animal caps, animal cap), -- F (budget), H (workspace relation cap) and G (D9a) confirmed. begin; -select plan(254); +select plan(258); insert into auth.users (id, email, email_confirmed_at) values ('70000000-0000-0000-0000-00000000000a', 'alice-an@test.local', now()), @@ -306,6 +306,9 @@ select is( public.create_animal(pg_temp.wa(), '71000000-0000-0000-0000-000000000002', 'Tonnerre', 'equine', 'male', client_id => pg_temp.id('marie'), relation_role => 'owner', started_on => '2020-01-01') ->> 'relation_id', pg_temp.id('r_tonnerre')::text, 'a replay returns the same relation'); +select is(pg_temp.err($$ select public.create_animal(pg_temp.wa(), '71000000-0000-0000-0000-000000000002', 'Tonnerre', 'equine', 'male', + client_id => pg_temp.id('marie'), relation_role => 'keeper', started_on => '2020-01-01') $$), + 'P0001|request_conflict||', 'a replay with another relation role is refused'); select is(pg_temp.err($$ select public.create_animal(pg_temp.wa(), gen_random_uuid(), 'X', 'equine', 'male', client_id => pg_temp.id('marie')) $$), 'P0001|invalid_input||', 'a client without a role is refused'); select is(pg_temp.err($$ select public.create_animal(pg_temp.wa(), gen_random_uuid(), 'X', 'equine', 'male', @@ -376,6 +379,8 @@ select is(pg_temp.err($$ select public.update_animal_client_relation(pg_temp.id( 'P0001|invalid_input||', 'a voided relation cannot be corrected'); select lives_ok($$ select pg_temp.new_relation('r_rex_marie', 'rex', 'marie', 'owner') $$, 'a voided relation no longer holds the current-relation slot'); +select lives_ok($$ select pg_temp.new_relation('r_sans_ana_wrong', 'sans', 'ana', 'keeper') $$, 'Ana entered as keeper by mistake'); +select is(public.void_animal_client_relation(pg_temp.id('r_sans_ana_wrong'), 1) ->> 'revision', '2', 'voided while current'); select lives_ok($$ select pg_temp.new_relation('r_rex_lac_contact', 'rex', 'lac', 'contact') $$, 'a contact'); select is(public.end_animal_client_relation(pg_temp.id('r_rex_lac_contact'), 1, '2024-01-01') ->> 'revision', '2', 'the contact ends'); select is(public.void_animal_client_relation(pg_temp.id('r_rex_lac_contact'), 2) ->> 'revision', '3', @@ -494,6 +499,7 @@ select is(pg_temp.client_count('marie'), 4, 'Marie: Éclair (two roles, counted select is(pg_temp.client_count('paul'), 2, 'Paul: Éclair and Quatuor; Bijou archived is not counted'); select is(pg_temp.client_count('lac'), 3, 'the stable: Éclair, Luna, Quatuor; ended and voided relations left out'); select is(pg_temp.client_count('zoe'), 1, 'archived Zoé: Luna; her ended keeper relation left out'); +select is(pg_temp.client_count('ana'), 1, 'Ana: Quatuor; her voided keeper relation to Sans Maître left out'); select is(pg_temp.client_animals('lac'), 'Éclair:keeper:current|Luna:owner:current|Quatuor:owner:current|Bijou:owner:former', 'a client''s animals: current first, then former, each by name; voided left out');