The test uses the P2P network to sync blocks, which has no inherent guarantee that the blocks are sent and received in the right order, assuming the headers are received first.
This can mean that the first block file is flushed with block at height 346 and block at height 345 is added to the second file, causing assert_equal(pruneheight_new, 345) to fail.
Fix the issue by using a linear dumb sync.`
Also, after syncing the blocks, we didn't check that the indexes have caught up to the tip before manually pruning.
This could lead to prune blockers lower that the expected height.
This is a backport of core#26417 and core#31468