cleanup test code

Though there are a lot of diffs here, it’s mostly just moving things around in an attempt to make this all more readable.