This adds a few targeted tests to make sure that when refactoring
this function later I don't break these properties.
Some are tested in passing elsewhere but this makes it more
obvious what went wrong when it fails.
This doesn't cover everything the function does, I couldn't
find any examples that would exercise some of the code.