SQL - Set a Flag Based on a Conditions Based on Multiple IDs
Something like this may be

select t.*, 
  case sum(case when test in ('a','b') then 1 else 0 end) -- count occurrences of tests 'a' and 'b'
       over (partition by primary_id) -- for each primary_id
  when 2 then 1 else 0 end flag  -- if there are two, we're OK
from test t


fiddle