Mamdani has cast the contests as a referendum on what it means to be a Democrat in New York City and across the country.