It's not an idea I've explored seriously in any depth, but it seems like testing for capabilities required for a specific task might be a way to go here over actual drug tests.
Thus if it's determined that people need a certain reaction time to drive safely, that could be one thing tested.
Clearly setting the levels and the particular capabilities might well be contentious, but by aligning the test(s) with what's needed for the task it's fairer and in certain ways more robust: for instance, you could be under the threshold for alcohol/cannabis but if you combine that with legal medicines that cause drowsiness or simply happen to be extremely sleep deprived, you might pass a traditional test but the combined effects would lead one to fail a capability test.
It also helps in other ways too, such as fairly treating older people - you might be a sharp 75 year old and yet come up against some age limit. This let's you continue so long as you maintain the capabilities. Then if things start to change it's clear and takes some of the awkwardness out of the discussion about whether one is still fit for a job.
Of course the risk is that people are pseudoscientific or arbitrary in setting the capabilities to a level that doesn't align. We've all seen the unrealistic hiring prerequisites that managers ask for if left to decide (must have ten years experience of XYZ!) In the wrong hands these could be used unfairly (eg setting totally unrealistic levels precisely to screen out groups they don't want).