It's the latter. Take the test max 1 day (not 24 hours, so you have some more leeway) before the scheduled flight time to return to the US. It needs to be a proctored test - we just used this one and it was easy if you don't want to fuss finding one in the UK (
https://store.optum.com/shop/product...est-kit-2-pack)